@Override public ContainerSpec getContainerInfo(String serverTemplateId, String containerId) { final ServerTemplate serverTemplate = getServerTemplate(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerId); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("Server template " + serverTemplateId + " does not have container with id " + containerId); } return containerSpec; }
@Test public void testValidContainerIdWhenContainerIdIsValidInTheThirdAttempt() { final SpecManagementServiceCDI service = spy(new SpecManagementServiceCDI()); final ServerTemplate template = mock(ServerTemplate.class); final ContainerSpec containerSpec = mock(ContainerSpec.class); when(template.getContainerSpec("org.jbpm:Evaluation:1.0")).thenReturn(containerSpec); when(template.getContainerSpec("org.jbpm:Evaluation:1.0-2")).thenReturn(containerSpec); when(template.getContainerSpec("org.jbpm:Evaluation:1.0-3")).thenReturn(null); doReturn(template).when(service).getServerTemplate("templateId"); final String containerId = service.validContainerId("templateId", "org.jbpm:Evaluation:1.0"); assertEquals(containerId, "org.jbpm:Evaluation:1.0-3"); } }
@Test public void testValidContainerIdWhenContainerIdIsValidInTheSecondAttempt() { final SpecManagementServiceCDI service = spy(new SpecManagementServiceCDI()); final ServerTemplate template = mock(ServerTemplate.class); final ContainerSpec containerSpec = mock(ContainerSpec.class); when(template.getContainerSpec("org.jbpm:Evaluation:1.0")).thenReturn(containerSpec); when(template.getContainerSpec("org.jbpm:Evaluation:1.0-2")).thenReturn(null); doReturn(template).when(service).getServerTemplate("templateId"); final String containerId = service.validContainerId("templateId", "org.jbpm:Evaluation:1.0"); assertEquals(containerId, "org.jbpm:Evaluation:1.0-2"); }
@Test public void testValidContainerIdWhenContainerIdIsValidInTheFirstAttempt() { final SpecManagementServiceCDI service = spy(new SpecManagementServiceCDI()); final ServerTemplate template = mock(ServerTemplate.class); when(template.getContainerSpec("org.jbpm:Evaluation:1.0")).thenReturn(null); doReturn(template).when(service).getServerTemplate("templateId"); final String containerId = service.validContainerId("templateId", "org.jbpm:Evaluation:1.0"); assertEquals(containerId, "org.jbpm:Evaluation:1.0"); }
public ContainerList getServerTemplateContainers(final String serverTemplateId, final String containerSpecId) { final ServerTemplate serverTemplate = getServerTemplate(serverTemplateId); if (serverTemplate.hasContainerSpec(containerSpecId) == false) { throw new KieServerControllerIllegalArgumentException("Server template with id " + serverTemplateId + " has no container with id " + containerSpecId); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecId); Collection<Container> containers = kieServerInstanceManager.getContainers(serverTemplate, containerSpec); return new ContainerList(containers); }
@Test public void isNewServerTemplateIdValidTest() { final SpecManagementServiceCDI specManagementService = spy(new SpecManagementServiceCDI()); final ServerTemplate serverTemplate = mock(ServerTemplate.class); when(serverTemplate.getContainerSpec(any())).thenReturn(null); doThrow(KieServerControllerIllegalArgumentException.class).when(specManagementService).getServerTemplate("noDoraId"); doReturn(serverTemplate).when(specManagementService).getServerTemplate("doraId"); assertTrue(specManagementService.isNewServerTemplateIdValid("noDoraId")); assertFalse(specManagementService.isNewServerTemplateIdValid("doraId")); }
@Test public void testUpdateContainerConfigWhenAffectedContainersIsEmpty() { final SpecManagementServiceImpl specManagementService = spy((SpecManagementServiceImpl) this.specManagementService); final String serverTemplateId = "serverTemplateId"; final String containerSpecId = "containerSpecId"; final Capability capability = Capability.PROCESS; final ContainerConfig containerConfig = mock(ContainerConfig.class); final ServerTemplate serverTemplate = mock(ServerTemplate.class); final ContainerSpec containerSpec = mock(ContainerSpec.class); final Map<Capability, ContainerConfig> configs = spy(new HashMap<>()); final List<?> expectedContainers = new ArrayList<>(); specManagementService.setTemplateStorage(templateStorage); specManagementService.setNotificationService(notificationService); doReturn(serverTemplate).when(templateStorage).load(serverTemplateId); doReturn(containerSpec).when(serverTemplate).getContainerSpec(containerSpecId); doReturn(expectedContainers).when(specManagementService).updateContainerConfig(capability, containerConfig, serverTemplate, containerSpec); doReturn(configs).when(containerSpec).getConfigs(); specManagementService.updateContainerConfig(serverTemplateId, containerSpecId, capability, containerConfig); verify(specManagementService).logInfo("Update of container configuration resulted in no changes to containers running on kie-servers"); verify(specManagementService, never()).logDebug(any(), any()); verify(configs).put(capability, containerConfig); verify(templateStorage).update(serverTemplate); verify(notificationService).notify(any(ServerTemplateUpdated.class)); }
@Test public void testStartContainer() { createServerTemplateWithContainer(); List<Container> fakeResult = new ArrayList<Container>(); fakeResult.add(container); when(kieServerInstanceManager.startContainer(any(ServerTemplate.class), any(ContainerSpec.class))).thenReturn(fakeResult); specManagementService.startContainer(containerSpec); verify(kieServerInstanceManager, times(1)).startContainer(any(ServerTemplate.class), any(ContainerSpec.class)); ServerTemplate updated = specManagementService.getServerTemplate(serverTemplate.getId()); assertNotNull(updated); ContainerSpec updatedContainer = updated.getContainerSpec(containerSpec.getId()); assertNotNull(updatedContainer); assertEquals(KieContainerStatus.STARTED, updatedContainer.getStatus()); }
@Test public void testStopContainer() { createServerTemplateWithContainer(); List<Container> fakeResult = new ArrayList<Container>(); fakeResult.add(container); when(kieServerInstanceManager.stopContainer(any(ServerTemplate.class), any(ContainerSpec.class))).thenReturn(fakeResult); specManagementService.stopContainer(containerSpec); verify(kieServerInstanceManager, times(1)).stopContainer(any(ServerTemplate.class), any(ContainerSpec.class)); ServerTemplate updated = specManagementService.getServerTemplate(serverTemplate.getId()); assertNotNull(updated); ContainerSpec updatedContainer = updated.getContainerSpec(containerSpec.getId()); assertNotNull(updatedContainer); assertEquals(KieContainerStatus.STOPPED, updatedContainer.getStatus()); }
@Override public synchronized void updateContainerConfig(final String serverTemplateId, final String containerSpecId, final Capability capability, final ContainerConfig containerConfig) { final ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecId); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecId + " within server template with id " + serverTemplateId); } final List<Container> affectedContainers = updateContainerConfig(capability, containerConfig, serverTemplate, containerSpec); if (affectedContainers.isEmpty()) { logInfo("Update of container configuration resulted in no changes to containers running on kie-servers"); } affectedContainers.forEach(ac -> { logDebug("Container {} on server {} was affected by a change in the scanner", ac.getContainerSpecId(), ac.getServerInstanceKey()); }); containerSpec.getConfigs().put(capability, containerConfig); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); }
@Override public synchronized void deleteContainerSpec(String serverTemplateId, String containerSpecId) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (serverTemplate.hasContainerSpec(containerSpecId)) { ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecId); kieServerInstanceManager.stopContainer(serverTemplate, containerSpec); serverTemplate.deleteContainerSpec(containerSpecId); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); } else { throw new KieServerControllerIllegalArgumentException("Container " + containerSpecId + " not found"); } }
@Override public void scanNow(final ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId()); } List<Container> containers = kieServerInstanceManager.scanNow(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public void startScanner(final ContainerSpecKey containerSpecKey, final Long interval) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId()); } ContainerConfig containerConfig = containerSpec.getConfigs().get(Capability.RULE); if (containerConfig == null) { containerConfig = new RuleConfig(); containerSpec.getConfigs().put(Capability.RULE, containerConfig); } ((RuleConfig) containerConfig).setPollInterval(interval); ((RuleConfig) containerConfig).setScannerStatus(KieScannerStatus.STARTED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.startScanner(serverTemplate, containerSpec, interval); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public void stopScanner(final ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId()); } ContainerConfig containerConfig = containerSpec.getConfigs().get(Capability.RULE); if (containerConfig == null) { containerConfig = new RuleConfig(); containerSpec.getConfigs().put(Capability.RULE, containerConfig); } if(((RuleConfig) containerConfig).getScannerStatus() == KieScannerStatus.STOPPED){ return; } ((RuleConfig) containerConfig).setPollInterval(null); ((RuleConfig) containerConfig).setScannerStatus(KieScannerStatus.STOPPED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.stopScanner(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public synchronized void stopContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } containerSpec.setStatus(KieContainerStatus.STOPPED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.stopContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public synchronized void startContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } containerSpec.setStatus(KieContainerStatus.STARTED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
when(serverTemplate.getContainerSpec(any())).thenReturn(null); doReturn(serverTemplate).when(specManagementService).getServerTemplate("templateId");
@Override public synchronized void deactivateContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } if (!containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { throw new KieServerControllerIllegalArgumentException("Container " + containerSpecKey.getId() + " cannot be deactivated because it's not in started state, actual state " + containerSpec.getStatus()); } containerSpec.setStatus(KieContainerStatus.DEACTIVATED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.deactivateContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public synchronized void activateContainer(ContainerSpecKey containerSpecKey) { ServerTemplate serverTemplate = templateStorage.load(containerSpecKey.getServerTemplateKey().getId()); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + containerSpecKey.getServerTemplateKey().getId()); } final ContainerSpec containerSpec = serverTemplate.getContainerSpec(containerSpecKey.getId()); if (containerSpec == null) { throw new KieServerControllerIllegalArgumentException("No container spec found for id " + containerSpecKey.getId() + " within server template with id " + serverTemplate.getId()); } if (!containerSpec.getStatus().equals(KieContainerStatus.DEACTIVATED)) { throw new KieServerControllerIllegalArgumentException("Container " + containerSpecKey.getId() + " cannot be activated because it's not in deactivated state, actual state " + containerSpec.getStatus()); } containerSpec.setStatus(KieContainerStatus.STARTED); templateStorage.update(serverTemplate); List<Container> containers = kieServerInstanceManager.activateContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); }
@Override public synchronized void updateContainerSpec(final String serverTemplateId, final String containerId, final ContainerSpec containerSpec) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (!containerSpec.getId().equals(containerId)) { throw new KieServerControllerException("Cannot update container " + containerSpec.getId() + " on container " + containerId); } if (!serverTemplate.hasContainerSpec(containerSpec.getId())) { throw new KieServerControllerIllegalArgumentException("Server template with id " + serverTemplateId + " has no container with id " + containerSpec.getId()); } if (!serverTemplate.hasMatchingId(containerSpec.getServerTemplateKey())) { throw new KieServerControllerException("Cannot change container template key during update."); } // make sure correct server template is set containerSpec.setServerTemplateKey(new ServerTemplateKey(serverTemplate.getId(), serverTemplate.getName())); ContainerSpec currentVersion = serverTemplate.getContainerSpec(containerSpec.getId()); serverTemplate.deleteContainerSpec(currentVersion.getId()); serverTemplate.addContainerSpec(containerSpec); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); // in case container was started before it was update or update comes with status started update container in running servers if (currentVersion.getStatus().equals(KieContainerStatus.STARTED) || containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.upgradeAndStartContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } }