private KieServerMgmtCommandServiceImpl() { ServiceLoader<PersistingServerTemplateStorageService> storageServices = ServiceLoader.load(PersistingServerTemplateStorageService.class); if (storageServices != null && storageServices.iterator().hasNext()) { PersistingServerTemplateStorageService storageService = storageServices.iterator().next(); setTemplateStorage(storageService.getTemplateStorage()); } else { LOGGER.debug("No server template storage defined. Default storage: InMemoryKieServerTemplateStorage will be used"); } ServiceLoader<NotificationServiceFactory> notificationServiceLoader = ServiceLoader.load(NotificationServiceFactory.class); if (notificationServiceLoader != null && notificationServiceLoader.iterator().hasNext()) { final NotificationService notificationService = notificationServiceLoader.iterator().next().getNotificationService(); setNotificationService(notificationService); } else { LOGGER.warn("Notification service not defined. Default notification: LoggingNotificationService will be used"); } }
@Test public void testScanNow() { List<Container> fakeResult = new ArrayList<Container>(); fakeResult.add(container); when(kieServerInstanceManager.scanNow(any(ServerTemplate.class), any(ContainerSpec.class))).thenReturn(fakeResult); ruleCapabilitiesService.scanNow(containerSpec); verify(kieServerInstanceManager, times(1)).scanNow(any(ServerTemplate.class), any(ContainerSpec.class)); }
@Test public void testListServerTemplates() { int limit = getRandomInt(5, 10); for (int x = 0; x < limit; x++) { ServerTemplate serverTemplate = new ServerTemplate(); serverTemplate.setName("test server " + x); serverTemplate.setId(UUID.randomUUID().toString()); specManagementService.saveServerTemplate(serverTemplate); } ServerTemplateKeyList existing = specManagementService.listServerTemplateKeys(); assertNotNull(existing); assertEquals(limit, existing.getServerTemplates().length); ServerTemplateList allTemplates = specManagementService.listServerTemplates(); assertNotNull(allTemplates); assertEquals(limit, allTemplates.getServerTemplates().length); }
@Test public void testCreateAndDeleteServerTemplate() { ServerTemplate serverTemplate = new ServerTemplate(); serverTemplate.setName("test server"); serverTemplate.setId(UUID.randomUUID().toString()); specManagementService.saveServerTemplate(serverTemplate); ServerTemplateKeyList existing = specManagementService.listServerTemplateKeys(); assertNotNull(existing); assertEquals(1, existing.getServerTemplates().length); org.kie.server.controller.api.model.spec.ServerTemplateKey saved = existing.getServerTemplates()[0]; assertEquals(serverTemplate.getName(), saved.getName()); assertEquals(serverTemplate.getId(), saved.getId()); specManagementService.deleteServerTemplate(serverTemplate.getId()); existing = specManagementService.listServerTemplateKeys(); assertNotNull(existing); assertEquals(0, existing.getServerTemplates().length); }
@Test public void testGetContainersByTemplate() { when(kieServerInstanceManager.getContainers(any(), any())).thenReturn(singletonList(container)); org.kie.server.controller.api.model.runtime.ServerInstanceKey instanceKey = new ServerInstanceKey("instanceId", "test server", serverTemplate.getId(), "http://fake.url.org"); serverTemplate.addServerInstance(instanceKey); specManagementService.saveServerTemplate(serverTemplate); ContainerList containers = runtimeManagementService.getContainers(serverTemplate, containerSpec); assertNotNull(containers); assertEquals(1, containers.getContainers().length); assertEquals(container, containers.getContainers()[0]); verify(kieServerInstanceManager).getContainers(any(), any()); }
final ReleaseId upgradeTo = new ReleaseId("org.kie", "kie-server-kjar", "2.0"); ruleCapabilitiesService.upgradeContainer(containerSpec, upgradeTo); final ServerTemplate updated = specManagementService.getServerTemplate(serverTemplate.getId());
@Test public void testCreateServerTemplate() { ServerTemplate serverTemplate = new ServerTemplate(); serverTemplate.setName("test server"); serverTemplate.setId(UUID.randomUUID().toString()); specManagementService.saveServerTemplate(serverTemplate); ServerTemplateKeyList existing = specManagementService.listServerTemplateKeys(); assertNotNull(existing); assertEquals(1, existing.getServerTemplates().length); org.kie.server.controller.api.model.spec.ServerTemplateKey saved = existing.getServerTemplates()[0]; assertEquals(serverTemplate.getName(), saved.getName()); assertEquals(serverTemplate.getId(), saved.getId()); }
@Test public void testGetServerInstances() { ServerInstanceKeyList found = runtimeManagementService.getServerInstances(serverTemplate.getId()); assertNotNull(found); assertEquals(0, found.getServerInstanceKeys().length); serverTemplate.addServerInstance(new ServerInstanceKey(serverTemplate.getId(), "test server","instanceId" , "http://fake.url.org")); specManagementService.saveServerTemplate(serverTemplate); found = runtimeManagementService.getServerInstances(serverTemplate.getId()); assertNotNull(found); assertEquals(1, found.getServerInstanceKeys().length); org.kie.server.controller.api.model.runtime.ServerInstanceKey server = found.getServerInstanceKeys()[0]; assertNotNull(server); assertEquals(serverTemplate.getId(), server.getServerTemplateId()); assertEquals("instanceId", server.getServerInstanceId()); assertEquals("test server", server.getServerName()); assertEquals("http://fake.url.org", server.getUrl()); }
protected void notifyOnDisconnect(ServerInstanceKey serverInstanceKey, ServerTemplate serverTemplate) { notificationService.notify(new ServerInstanceDeleted(serverInstanceKey.getServerInstanceId())); notificationService.notify(new ServerTemplateUpdated(serverTemplate)); notificationService.notify(new ServerInstanceDisconnected(serverInstanceKey.getServerInstanceId())); }
public WebSocketKieServerControllerImpl() { ServiceLoader<PersistingServerTemplateStorageService> storageServices = ServiceLoader.load(PersistingServerTemplateStorageService.class); if (storageServices != null && storageServices.iterator().hasNext()) { PersistingServerTemplateStorageService storageService = storageServices.iterator().next(); setTemplateStorage(storageService.getTemplateStorage()); logger.debug("Server template storage for standalone kie server controller is {}", storageService.getTemplateStorage().toString()); } else { logger.debug("No server template storage defined. Default storage: InMemoryKieServerTemplateStorage will be used"); } ServiceLoader<NotificationServiceFactory> notificationServiceLoader = ServiceLoader.load(NotificationServiceFactory.class); if (notificationServiceLoader != null && notificationServiceLoader.iterator().hasNext()) { final NotificationService notificationService = notificationServiceLoader.iterator().next().getNotificationService(); this.setNotificationService(notificationService); logger.debug("Notification service for standalone kie server controller is {}", notificationService.toString()); } else { logger.warn("Notification service not defined. Default notification: LoggingNotificationService will be used"); } }
@Test public void testGetContainersByInstance() { when(kieServerInstanceManager.getContainers(any(ServerInstanceKey.class))).thenReturn(singletonList(container)); org.kie.server.controller.api.model.runtime.ServerInstanceKey instanceKey = new ServerInstanceKey(serverTemplate.getId(), "test server", "instanceId", "http://fake.url.org"); serverTemplate.addServerInstance(instanceKey); specManagementService.saveServerTemplate(serverTemplate); ContainerList containers = runtimeManagementService.getContainers(instanceKey); assertNotNull(containers); assertEquals(1, containers.getContainers().length); assertEquals(container, containers.getContainers()[0]); verify(kieServerInstanceManager).getContainers(any()); }
@Test public void testUpgradeContainerWhenContainerSpecStatusIsStarted() { final List<Container> fakeResult = new ArrayList<Container>() {{ add(container); }}; doReturn(fakeResult).when(kieServerInstanceManager).upgradeContainer(any(ServerTemplate.class), any(ContainerSpec.class)); containerSpec.setStatus(KieContainerStatus.STARTED); final ReleaseId initial = containerSpec.getReleasedId(); final ReleaseId upgradeTo = new ReleaseId("org.kie", "kie-server-kjar", "2.0"); ruleCapabilitiesService.upgradeContainer(containerSpec, upgradeTo); verify(kieServerInstanceManager).upgradeContainer(any(ServerTemplate.class), any(ContainerSpec.class)); final ServerTemplate updated = specManagementService.getServerTemplate(serverTemplate.getId()); final Collection<ContainerSpec> containerSpecs = updated.getContainersSpec(); assertNotNull(containerSpecs); assertEquals(1, containerSpecs.size()); ContainerSpec updatedContainer = containerSpecs.iterator().next(); assertNotNull(updatedContainer); assertNotNull(updatedContainer.getReleasedId()); assertNotEquals(initial, updatedContainer.getReleasedId()); assertEquals(upgradeTo, updatedContainer.getReleasedId()); assertEquals(updatedContainer.getStatus(), KieContainerStatus.STARTED); }
@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 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 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 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 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 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); }