@Override public ServerTemplateList listServerTemplates() { return new ServerTemplateList(templateStorage.load()); }
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 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; }
private KieServerTemplateStorage createMockStorageWithOneTemplate(ServerTemplate serverTemplate) { KieServerTemplateStorage templateStorageMock = mock(KieServerTemplateStorage.class); when(templateStorageMock.load(eq(serverTemplate.getId()))) .thenReturn(serverTemplate); return templateStorageMock; } }
@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()); }
@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); }
@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 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); } } }
@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); }
@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); }
@Test public void getContainersByServerInstance_returnsEmptyList_whenInstanceWithIdDoesntExistInTemplate() { final String templateId = "templateId"; final String serverInstanceId = "serverInstanceId"; KieServerTemplateStorage templateStorageMock = mock(KieServerTemplateStorage.class); when(templateStorageMock.load(eq(templateId))) .thenReturn(new ServerTemplate(null, null, Collections.emptyList(), Collections.emptyMap(), Collections.emptyList(), Collections.emptyList())); // Setup tested service runtimeManagementService.setTemplateStorage(templateStorageMock); specManagementService.setTemplateStorage(templateStorageMock); // Tested method Collection<Container> containers = runtimeManagementServiceCDI.getContainersByServerInstance(templateId, serverInstanceId); assertThat(containers) .as("List of containers should be empty, when Server template doesn't contain server instance id") .isEmpty(); }
@Override public void afterResponseSent() { ServerTemplate serverTemplate = controller.getTemplateStorage().load(serverInfo.getServerId()); ServerInstanceKey serverInstanceKey = serverTemplate.getServerInstanceKeys().stream() .filter(server -> server.getUrl().equals(serverInfo.getLocation())) .findFirst() .get(); ServerInstance serverInstance = new ServerInstance(); serverInstance.setServerName(serverInstanceKey.getServerName()); serverInstance.setServerTemplateId(serverInstanceKey.getServerTemplateId()); serverInstance.setServerInstanceId(serverInstanceKey.getServerInstanceId()); serverInstance.setUrl(serverInstanceKey.getUrl()); controller.getNotificationService().notify(new ServerInstanceUpdated(serverInstance)); controller.getNotificationService().notify(new ServerInstanceConnected(serverInstance)); }
@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 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); }
@Override public synchronized void saveContainerSpec(String serverTemplateId, ContainerSpec containerSpec) { ServerTemplate serverTemplate = templateStorage.load(serverTemplateId); if (serverTemplate == null) { throw new KieServerControllerIllegalArgumentException("No server template found for id " + serverTemplateId); } if (serverTemplate.hasContainerSpec(containerSpec.getId())) { throw new KieServerControllerException("Server template with id " + serverTemplateId + " associated already with container " + containerSpec.getId()); } // make sure correct server template is set containerSpec.setServerTemplateKey(new ServerTemplateKey(serverTemplate.getId(), serverTemplate.getName())); serverTemplate.addContainerSpec(containerSpec); templateStorage.update(serverTemplate); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); if (containerSpec.getStatus().equals(KieContainerStatus.STARTED)) { List<Container> containers = kieServerInstanceManager.startContainer(serverTemplate, containerSpec); notificationService.notify(serverTemplate, containerSpec, containers); } }
@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); }