@Override public synchronized void updateServerTemplateConfig(String serverTemplateId, Capability capability, ServerConfig serverTemplateConfig) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } Map<Capability, ServerConfig> configs = serverTemplate.getConfigs(); configs.put(capability, serverTemplateConfig); serverTemplate.setConfigs(configs); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); }
@Override public synchronized void deleteServerTemplate(String serverTemplateId) { if (!templateStorage.exists(serverTemplateId)) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } templateStorage.delete(serverTemplateId); notificationService.notify(new ServerTemplateDeleted(serverTemplateId)); }
@Override public ServerTemplateKeyList listServerTemplateKeys() { return new ServerTemplateKeyList(templateStorage.loadKeys()); }
@Override public synchronized void copyServerTemplate(String serverTemplateId, String newServerTemplateId, String newServerTemplateName) { final ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } final Map<Capability, ServerConfig> configMap = new HashMap<Capability, ServerConfig>(serverTemplate.getConfigs().size()); for (final Map.Entry<Capability, ServerConfig> entry : serverTemplate.getConfigs().entrySet()) { configMap.put(entry.getKey(), copy(entry.getValue())); } final Collection<ContainerSpec> containerSpecs = new ArrayList<ContainerSpec>(serverTemplate.getContainersSpec().size()); for (final ContainerSpec entry : serverTemplate.getContainersSpec()) { containerSpecs.add(copy(entry, newServerTemplateId, newServerTemplateName)); } final ServerTemplate copy = new ServerTemplate(newServerTemplateId, newServerTemplateName, serverTemplate.getCapabilities(), configMap, containerSpecs); templateStorage.store(copy); }
protected ServerTemplate getServerTemplate(final String serverTemplateId){ final ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } return serverTemplate; }
@Override public synchronized KieServerSetup connect(KieServerInfo serverInfo) { ServerTemplate serverTemplate = templateStorage.load(serverInfo.getServerId()); KieServerSetup serverSetup = new KieServerSetup(); ServerInstanceKey serverInstanceKey = ModelFactory.newServerInstanceKey(serverInfo.getServerId(), serverInfo.getLocation()); serverTemplate.addServerInstance(serverInstanceKey); templateStorage.update(serverTemplate); logger.debug("KieServerInstance updated after connect from server {}", serverInfo.getLocation()); templateStorage.store(serverTemplate);
@Override public synchronized void saveServerTemplate(ServerTemplate serverTemplate) { if (templateStorage.exists(serverTemplate.getId())) { templateStorage.update(serverTemplate); } else { templateStorage.store(serverTemplate); } notificationService.notify(new ServerTemplateUpdated(serverTemplate)); Collection<ContainerSpec> containerSpecs = serverTemplate.getContainersSpec(); if (containerSpecs != null && !containerSpecs.isEmpty()) { for (ContainerSpec containerSpec : containerSpecs) { if (containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } } } }
@Override public void deleteServerInstance(final ServerInstanceKey serverInstanceKey) { checkNotNull("serverInstanceKey", serverInstanceKey); if (getKieServerInstanceManager().isAlive(serverInstanceKey)) { throw new RuntimeException("Can't delete live instance."); } else { final String serverInstanceId = serverInstanceKey.getServerInstanceId(); final ServerTemplate serverTemplate = getServerTemplate(serverInstanceKey.getServerTemplateId()); if (serverTemplate != null) { serverTemplate.deleteServerInstance(serverInstanceId); getTemplateStorage().update(serverTemplate); getNotificationService().notify(new ServerInstanceDeleted(serverInstanceId)); } } }
@Override public ServerTemplate getServerTemplate(String serverTemplateId) { final ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } return serverTemplate; }
@Override public synchronized void disconnect(KieServerInfo serverInfo) { ServerTemplate serverTemplate = templateStorage.load(serverInfo.getServerId()); if (serverTemplate != null) { logger.debug("Server id {} known to the controller, checking if given server exists", serverInfo.getServerId()); if (serverTemplate != null) { logger.info("Server {} disconnected from controller", serverInfo.getLocation()); ServerInstanceKey serverInstanceKey = ModelFactory.newServerInstanceKey(serverInfo.getServerId(), serverInfo.getLocation()); serverTemplate.deleteServerInstance(serverInstanceKey.getServerInstanceId()); templateStorage.update(serverTemplate); notifyOnDisconnect(serverInstanceKey, serverTemplate); } } }
@Override public ServerTemplateList listServerTemplates() { return new ServerTemplateList(templateStorage.load()); }
@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"); } }
private KieServerTemplateStorage createMockStorageWithOneTemplate(ServerTemplate serverTemplate) { KieServerTemplateStorage templateStorageMock = mock(KieServerTemplateStorage.class); when(templateStorageMock.load(eq(serverTemplate.getId()))) .thenReturn(serverTemplate); return templateStorageMock; } }
@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 ContainerSpecList listContainerSpec(String serverTemplateId) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } return new ContainerSpecList(serverTemplate.getContainersSpec()); }
@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 testUpdateContainerConfigWhenContainerSpecIsNull() { final SpecManagementServiceImpl specManagementService = (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); specManagementService.setTemplateStorage(templateStorage); doReturn(serverTemplate).when(templateStorage).load(serverTemplateId); doReturn(null).when(serverTemplate).getContainersSpec(); expectedException.expect(KieServerControllerIllegalArgumentException.class); expectedException.expectMessage("No container spec found for id containerSpecId within server template with id serverTemplateId"); specManagementService.updateContainerConfig(serverTemplateId, containerSpecId, capability, containerConfig); }
@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); }
@Test public void testUpdateContainerConfigWhenServerTemplateIsNull() { final SpecManagementServiceImpl specManagementService = (SpecManagementServiceImpl) this.specManagementService; final String serverTemplateId = "serverTemplateId"; final String containerSpecId = "containerSpecId"; final Capability capability = Capability.PROCESS; final ContainerConfig containerConfig = mock(ContainerConfig.class); specManagementService.setTemplateStorage(templateStorage); doReturn(null).when(templateStorage).load(serverTemplateId); expectedException.expect(KieServerControllerIllegalArgumentException.class); expectedException.expectMessage("No server template found for id serverTemplateId"); specManagementService.updateContainerConfig(serverTemplateId, containerSpecId, capability, containerConfig); }
@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); }