Search in sources :

Example 1 with DefaultTopologyService

use of org.apache.knox.gateway.services.topology.impl.DefaultTopologyService in project knox by apache.

the class DefaultTopologyServiceTest method testGetTopologies.

@Test
public void testGetTopologies() throws Exception {
    File dir = createDir();
    File topologyDir = new File(dir, "topologies");
    File descriptorsDir = new File(dir, "descriptors");
    descriptorsDir.mkdirs();
    File sharedProvidersDir = new File(dir, "shared-providers");
    sharedProvidersDir.mkdirs();
    long time = topologyDir.lastModified();
    try {
        createFile(topologyDir, "one.xml", "org/apache/knox/gateway/topology/file/topology-one.xml", time);
        TestTopologyListener topoListener = new TestTopologyListener();
        FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
        TopologyService provider = new DefaultTopologyService();
        Map<String, String> c = new HashMap<>();
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayProvidersConfigDir()).andReturn(sharedProvidersDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayDescriptorsDir()).andReturn(descriptorsDir.getAbsolutePath()).anyTimes();
        EasyMock.replay(config);
        provider.init(config, c);
        provider.addTopologyChangeListener(topoListener);
        provider.reloadTopologies();
        Collection<Topology> topologies = provider.getTopologies();
        assertThat(topologies, notNullValue());
        assertThat(topologies.size(), is(1));
        Topology topology = topologies.iterator().next();
        assertThat(topology.getName(), is("one"));
        assertThat(topology.getTimestamp(), is(time));
        assertThat(topoListener.events.size(), is(1));
        topoListener.events.clear();
        // Add a file to the directory.
        File two = createFile(topologyDir, "two.xml", "org/apache/knox/gateway/topology/file/topology-two.xml", 1L);
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(2));
        Set<String> names = new HashSet<>(Arrays.asList("one", "two"));
        Iterator<Topology> iterator = topologies.iterator();
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        assertThat(names.size(), is(0));
        assertThat(topoListener.events.size(), is(1));
        List<TopologyEvent> events = topoListener.events.get(0);
        assertThat(events.size(), is(1));
        TopologyEvent event = events.get(0);
        assertThat(event.getType(), is(TopologyEvent.Type.CREATED));
        assertThat(event.getTopology(), notNullValue());
        // Update a file in the directory.
        two = createFile(topologyDir, "two.xml", "org/apache/knox/gateway/topology/file/topology-three.xml", 2L);
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(2));
        names = new HashSet<>(Arrays.asList("one", "two"));
        iterator = topologies.iterator();
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        assertThat(names.size(), is(0));
        // Remove a file from the directory.
        two.delete();
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(1));
        topology = topologies.iterator().next();
        assertThat(topology.getName(), is("one"));
        assertThat(topology.getTimestamp(), is(time));
    } finally {
        FileUtils.deleteQuietly(dir);
    }
}
Also used : FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) HashMap(java.util.HashMap) TopologyEvent(org.apache.knox.gateway.topology.TopologyEvent) Topology(org.apache.knox.gateway.topology.Topology) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with DefaultTopologyService

use of org.apache.knox.gateway.services.topology.impl.DefaultTopologyService in project knox by apache.

the class DefaultTopologyServiceTest method testSimpleDescriptorsTopologyGeneration.

/**
 * KNOX-1014
 *
 * Test the lifecycle relationship between simple descriptors and topology files.
 *
 * N.B. This test depends on the DummyServiceDiscovery extension being configured:
 *        org.apache.knox.gateway.topology.discovery.test.extension.DummyServiceDiscovery
 */
@Test
public void testSimpleDescriptorsTopologyGeneration() throws Exception {
    File dir = createDir();
    File topologyDir = new File(dir, "topologies");
    topologyDir.mkdirs();
    File descriptorsDir = new File(dir, "descriptors");
    descriptorsDir.mkdirs();
    File sharedProvidersDir = new File(dir, "shared-providers");
    sharedProvidersDir.mkdirs();
    try {
        TestTopologyListener topoListener = new TestTopologyListener();
        FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
        TopologyService provider = new DefaultTopologyService();
        Map<String, String> c = new HashMap<>();
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
        EasyMock.replay(config);
        provider.init(config, c);
        provider.addTopologyChangeListener(topoListener);
        provider.reloadTopologies();
        // Add a simple descriptor to the descriptors dir to verify topology generation and loading (KNOX-1006)
        AliasService aliasService = EasyMock.createNiceMock(AliasService.class);
        EasyMock.expect(aliasService.getPasswordFromAliasForGateway(anyObject(String.class))).andReturn(null).anyTimes();
        EasyMock.replay(aliasService);
        DefaultTopologyService.DescriptorsMonitor dm = new DefaultTopologyService.DescriptorsMonitor(config, topologyDir, aliasService);
        // Listener to simulate the topologies directory monitor, to notice when a topology has been deleted
        provider.addTopologyChangeListener(new TestTopologyDeleteListener((DefaultTopologyService) provider));
        // Write out the referenced provider config first
        File provCfgFile = createFile(sharedProvidersDir, "ambari-cluster-policy.xml", "org/apache/knox/gateway/topology/file/ambari-cluster-policy.xml", System.currentTimeMillis());
        try {
            // Create the simple descriptor in the descriptors dir
            File simpleDesc = createFile(descriptorsDir, "four.json", "org/apache/knox/gateway/topology/file/simple-topology-four.json", System.currentTimeMillis());
            // Trigger the topology generation by noticing the simple descriptor
            dm.onFileChange(simpleDesc);
            // Load the generated topology
            provider.reloadTopologies();
            Collection<Topology> topologies = provider.getTopologies();
            assertThat(topologies.size(), is(1));
            Iterator<Topology> iterator = topologies.iterator();
            Topology topology = iterator.next();
            assertThat("four", is(topology.getName()));
            int serviceCount = topology.getServices().size();
            assertEquals("Expected the same number of services as are declared in the simple dscriptor.", 10, serviceCount);
            // Overwrite the simple descriptor with a different set of services, and check that the changes are
            // propagated to the associated topology
            simpleDesc = createFile(descriptorsDir, "four.json", "org/apache/knox/gateway/topology/file/simple-descriptor-five.json", System.currentTimeMillis());
            dm.onFileChange(simpleDesc);
            provider.reloadTopologies();
            topologies = provider.getTopologies();
            topology = topologies.iterator().next();
            assertNotEquals(serviceCount, topology.getServices().size());
            assertEquals(6, topology.getServices().size());
            // Delete the simple descriptor, and make sure that the associated topology file is deleted
            simpleDesc.delete();
            dm.onFileDelete(simpleDesc);
            provider.reloadTopologies();
            topologies = provider.getTopologies();
            assertTrue(topologies.isEmpty());
            // Delete a topology file, and make sure that the associated simple descriptor is deleted
            // Overwrite the simple descriptor with a different set of services, and check that the changes are
            // propagated to the associated topology
            simpleDesc = createFile(descriptorsDir, "deleteme.json", "org/apache/knox/gateway/topology/file/simple-descriptor-five.json", System.currentTimeMillis());
            dm.onFileChange(simpleDesc);
            provider.reloadTopologies();
            topologies = provider.getTopologies();
            assertFalse(topologies.isEmpty());
            topology = topologies.iterator().next();
            assertEquals("deleteme", topology.getName());
            File topologyFile = new File(topologyDir, topology.getName() + ".xml");
            assertTrue(topologyFile.exists());
            topologyFile.delete();
            provider.reloadTopologies();
            assertFalse("Simple descriptor should have been deleted because the associated topology was.", simpleDesc.exists());
        } finally {
            provCfgFile.delete();
        }
    } finally {
        FileUtils.deleteQuietly(dir);
    }
}
Also used : AliasService(org.apache.knox.gateway.services.security.AliasService) FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) HashMap(java.util.HashMap) Topology(org.apache.knox.gateway.topology.Topology) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 3 with DefaultTopologyService

use of org.apache.knox.gateway.services.topology.impl.DefaultTopologyService in project knox by apache.

the class CLIGatewayServices method init.

public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
    ms = new CLIMasterService();
    ms.init(config, options);
    services.put("MasterService", ms);
    ks = new DefaultKeystoreService();
    ks.setMasterService(ms);
    ks.init(config, options);
    services.put(KEYSTORE_SERVICE, ks);
    DefaultAliasService alias = new DefaultAliasService();
    alias.setKeystoreService(ks);
    alias.init(config, options);
    services.put(ALIAS_SERVICE, alias);
    DefaultCryptoService crypto = new DefaultCryptoService();
    crypto.setKeystoreService(ks);
    crypto.setAliasService(alias);
    crypto.init(config, options);
    services.put(CRYPTO_SERVICE, crypto);
    DefaultTopologyService tops = new DefaultTopologyService();
    tops.init(config, options);
    services.put(TOPOLOGY_SERVICE, tops);
    RemoteConfigurationRegistryClientService registryClientService = RemoteConfigurationRegistryClientServiceFactory.newInstance(config);
    registryClientService.setAliasService(alias);
    registryClientService.init(config, options);
    services.put(REMOTE_REGISTRY_CLIENT_SERVICE, registryClientService);
}
Also used : DefaultKeystoreService(org.apache.knox.gateway.services.security.impl.DefaultKeystoreService) DefaultAliasService(org.apache.knox.gateway.services.security.impl.DefaultAliasService) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) RemoteConfigurationRegistryClientService(org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClientService) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) CLIMasterService(org.apache.knox.gateway.services.security.impl.CLIMasterService)

Example 4 with DefaultTopologyService

use of org.apache.knox.gateway.services.topology.impl.DefaultTopologyService in project knox by apache.

the class DefaultGatewayServices method start.

public void start() throws ServiceLifecycleException {
    ms.start();
    ks.start();
    DefaultAliasService alias = (DefaultAliasService) services.get(ALIAS_SERVICE);
    alias.start();
    SSLService ssl = (SSLService) services.get(SSL_SERVICE);
    ssl.start();
    ServerInfoService sis = (ServerInfoService) services.get(SERVER_INFO_SERVICE);
    sis.start();
    RemoteConfigurationRegistryClientService clientService = (RemoteConfigurationRegistryClientService) services.get(REMOTE_REGISTRY_CLIENT_SERVICE);
    clientService.start();
    (services.get(CLUSTER_CONFIGURATION_MONITOR_SERVICE)).start();
    DefaultTopologyService tops = (DefaultTopologyService) services.get(TOPOLOGY_SERVICE);
    tops.start();
    DefaultMetricsService metricsService = (DefaultMetricsService) services.get(METRICS_SERVICE);
    metricsService.start();
}
Also used : DefaultAliasService(org.apache.knox.gateway.services.security.impl.DefaultAliasService) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) DefaultMetricsService(org.apache.knox.gateway.services.metrics.impl.DefaultMetricsService) RemoteConfigurationRegistryClientService(org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClientService) JettySSLService(org.apache.knox.gateway.services.security.impl.JettySSLService) SSLService(org.apache.knox.gateway.services.security.SSLService)

Example 5 with DefaultTopologyService

use of org.apache.knox.gateway.services.topology.impl.DefaultTopologyService in project knox by apache.

the class DefaultTopologyServiceTest method testConfigurationCRUDAPI.

/**
 * KNOX-1039
 */
@Test
public void testConfigurationCRUDAPI() throws Exception {
    File dir = createDir();
    File topologyDir = new File(dir, "topologies");
    topologyDir.mkdirs();
    File descriptorsDir = new File(dir, "descriptors");
    descriptorsDir.mkdirs();
    File sharedProvidersDir = new File(dir, "shared-providers");
    sharedProvidersDir.mkdirs();
    try {
        TestTopologyListener topoListener = new TestTopologyListener();
        FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
        TopologyService ts = new DefaultTopologyService();
        Map<String, String> c = new HashMap<>();
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
        EasyMock.replay(config);
        ts.init(config, c);
        ts.addTopologyChangeListener(topoListener);
        ts.reloadTopologies();
        java.lang.reflect.Field dmField = ts.getClass().getDeclaredField("descriptorsMonitor");
        dmField.setAccessible(true);
        DefaultTopologyService.DescriptorsMonitor dm = (DefaultTopologyService.DescriptorsMonitor) dmField.get(ts);
        final String simpleDescName = "six.json";
        final String provConfOne = "provider-config-one.xml";
        final String provConfTwo = "ambari-cluster-policy.xml";
        // "Deploy" the referenced provider configs first
        boolean isDeployed = ts.deployProviderConfiguration(provConfOne, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/provider-config-one.xml").toURI())));
        assertTrue(isDeployed);
        File provConfOneFile = new File(sharedProvidersDir, provConfOne);
        assertTrue(provConfOneFile.exists());
        isDeployed = ts.deployProviderConfiguration(provConfTwo, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/ambari-cluster-policy.xml").toURI())));
        assertTrue(isDeployed);
        File provConfTwoFile = new File(sharedProvidersDir, provConfTwo);
        assertTrue(provConfTwoFile.exists());
        // Validate the provider configurations known by the topology service
        Collection<File> providerConfigurations = ts.getProviderConfigurations();
        assertNotNull(providerConfigurations);
        assertEquals(2, providerConfigurations.size());
        assertTrue(providerConfigurations.contains(provConfOneFile));
        assertTrue(providerConfigurations.contains(provConfTwoFile));
        // "Deploy" the simple descriptor, which depends on provConfOne
        isDeployed = ts.deployDescriptor(simpleDescName, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/simple-descriptor-six.json").toURI())));
        assertTrue(isDeployed);
        File simpleDesc = new File(descriptorsDir, simpleDescName);
        assertTrue(simpleDesc.exists());
        // Validate the simple descriptors known by the topology service
        Collection<File> descriptors = ts.getDescriptors();
        assertNotNull(descriptors);
        assertEquals(1, descriptors.size());
        assertTrue(descriptors.contains(simpleDesc));
        // "Notice" the simple descriptor, so the provider configuration dependency relationship is recorded
        dm.onFileChange(simpleDesc);
        // Attempt to delete the referenced provConfOne
        assertFalse("Should not be able to delete a provider configuration that is referenced by one or more descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfOne)));
        // Overwrite the simple descriptor with content that changes the provider config reference to provConfTwo
        isDeployed = ts.deployDescriptor(simpleDescName, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/simple-descriptor-five.json").toURI())));
        assertTrue(isDeployed);
        assertTrue(simpleDesc.exists());
        ts.getProviderConfigurations();
        // "Notice" the simple descriptor, so the provider configuration dependency relationship is updated
        dm.onFileChange(simpleDesc);
        // Attempt to delete the referenced provConfOne
        assertTrue("Should be able to delete the provider configuration, now that it's not referenced by any descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfOne)));
        // Re-validate the provider configurations known by the topology service
        providerConfigurations = ts.getProviderConfigurations();
        assertNotNull(providerConfigurations);
        assertEquals(1, providerConfigurations.size());
        assertFalse(providerConfigurations.contains(provConfOneFile));
        assertTrue(providerConfigurations.contains(provConfTwoFile));
        // Attempt to delete the referenced provConfTwo
        assertFalse("Should not be able to delete a provider configuration that is referenced by one or more descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfTwo)));
        // Delete the referencing simple descriptor
        assertTrue(ts.deleteDescriptor(FilenameUtils.getBaseName(simpleDescName)));
        assertFalse(simpleDesc.exists());
        // Re-validate the simple descriptors known by the topology service
        descriptors = ts.getDescriptors();
        assertNotNull(descriptors);
        assertTrue(descriptors.isEmpty());
        // "Notice" the simple descriptor, so the provider configuration dependency relationship is updated
        dm.onFileDelete(simpleDesc);
        // Attempt to delete the referenced provConfTwo
        assertTrue("Should be able to delete the provider configuration, now that it's not referenced by any descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfTwo)));
        // Re-validate the provider configurations known by the topology service
        providerConfigurations = ts.getProviderConfigurations();
        assertNotNull(providerConfigurations);
        assertTrue(providerConfigurations.isEmpty());
    } finally {
        FileUtils.deleteQuietly(dir);
    }
}
Also used : FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) HashMap(java.util.HashMap) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Aggregations

DefaultTopologyService (org.apache.knox.gateway.services.topology.impl.DefaultTopologyService)10 DefaultAliasService (org.apache.knox.gateway.services.security.impl.DefaultAliasService)6 File (java.io.File)4 HashMap (java.util.HashMap)4 FileAlterationMonitor (org.apache.commons.io.monitor.FileAlterationMonitor)4 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)4 Test (org.junit.Test)4 RemoteConfigurationRegistryClientService (org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClientService)3 DefaultMetricsService (org.apache.knox.gateway.services.metrics.impl.DefaultMetricsService)3 JettySSLService (org.apache.knox.gateway.services.security.impl.JettySSLService)3 Topology (org.apache.knox.gateway.topology.Topology)3 SSLService (org.apache.knox.gateway.services.security.SSLService)2 DefaultCryptoService (org.apache.knox.gateway.services.security.impl.DefaultCryptoService)2 DefaultKeystoreService (org.apache.knox.gateway.services.security.impl.DefaultKeystoreService)2 HashSet (java.util.HashSet)1 DefaultHostMapperService (org.apache.knox.gateway.services.hostmap.impl.DefaultHostMapperService)1 DefaultServiceDefinitionRegistry (org.apache.knox.gateway.services.registry.impl.DefaultServiceDefinitionRegistry)1 DefaultServiceRegistryService (org.apache.knox.gateway.services.registry.impl.DefaultServiceRegistryService)1 AliasService (org.apache.knox.gateway.services.security.AliasService)1 CLIMasterService (org.apache.knox.gateway.services.security.impl.CLIMasterService)1