Search in sources :

Example 16 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class TopologyRulesModuleTest method testParseSimpleTopologyXmlInKnoxFormat.

@Test
public void testParseSimpleTopologyXmlInKnoxFormat() throws IOException, SAXException, URISyntaxException {
    Digester digester = loader.newDigester();
    String name = "org/apache/knox/gateway/topology/xml/simple-topology-knox-format.xml";
    URL url = ClassLoader.getSystemResource(name);
    assertThat("Failed to find URL for resource " + name, url, notNullValue());
    File file = new File(url.getFile());
    TopologyBuilder topologyBuilder = digester.parse(url);
    Topology topology = topologyBuilder.build();
    assertThat("Failed to parse resource " + name, topology, notNullValue());
    topology.setTimestamp(file.lastModified());
    assertThat(topology.getName(), is("topology"));
    assertThat(topology.getTimestamp(), is(file.lastModified()));
    assertThat(topology.getServices().size(), is(3));
    Service comp = topology.getServices().iterator().next();
    assertThat(comp, notNullValue());
    assertThat(comp.getRole(), is("WEBHDFS"));
    assertThat(comp.getVersion().toString(), is("2.4.0"));
    assertThat(comp.getUrls().size(), is(2));
    assertThat(comp.getUrls(), hasItem("http://host1:80/webhdfs"));
    assertThat(comp.getUrls(), hasItem("http://host2:80/webhdfs"));
    Provider provider = topology.getProviders().iterator().next();
    assertThat(provider, notNullValue());
    assertThat(provider.isEnabled(), is(true));
    assertThat(provider.getRole(), is("authentication"));
    assertThat(provider.getParams().size(), is(5));
    Service service = topology.getService("WEBHDFS", "webhdfs", new Version(2, 4, 0));
    assertEquals(comp, service);
    comp = topology.getService("RESOURCEMANAGER", null, new Version("2.5.0"));
    assertThat(comp, notNullValue());
    assertThat(comp.getRole(), is("RESOURCEMANAGER"));
    assertThat(comp.getVersion().toString(), is("2.5.0"));
    assertThat(comp.getUrl(), is("http://host1:8088/ws"));
    comp = topology.getService("HIVE", "hive", null);
    assertThat(comp, notNullValue());
    assertThat(comp.getRole(), is("HIVE"));
    assertThat(comp.getName(), is("hive"));
    assertThat(comp.getUrl(), is("http://host2:10001/cliservice"));
}
Also used : TopologyBuilder(org.apache.knox.gateway.topology.builder.TopologyBuilder) Version(org.apache.knox.gateway.topology.Version) Digester(org.apache.commons.digester3.Digester) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) File(java.io.File) URL(java.net.URL) Provider(org.apache.knox.gateway.topology.Provider) Test(org.junit.Test)

Example 17 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class DeploymentFactory method selectContextServices.

// Scan through the services in the topology.
// For each that we find add it to the list of service roles included in the topology.
private static Map<String, List<ServiceDeploymentContributor>> selectContextServices(Topology topology) {
    Map<String, List<ServiceDeploymentContributor>> defaults = new HashMap<>();
    for (Service service : topology.getServices()) {
        String role = service.getRole();
        ServiceDeploymentContributor contributor = getServiceContributor(role, service.getName(), service.getVersion());
        if (contributor != null) {
            List<ServiceDeploymentContributor> list = defaults.get(role);
            if (list == null) {
                list = new ArrayList<ServiceDeploymentContributor>(1);
                defaults.put(role, list);
            }
            if (!list.contains(contributor)) {
                list.add(contributor);
            }
        }
    }
    return defaults;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Service(org.apache.knox.gateway.topology.Service) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class DeploymentFactory method contributeServices.

private static void contributeServices(DeploymentContext context, Topology topology, Map<String, List<ServiceDeploymentContributor>> services) {
    if (services != null) {
        for (Service service : topology.getServices()) {
            ServiceDeploymentContributor contributor = getServiceContributor(service.getRole(), service.getName(), service.getVersion());
            if (contributor != null) {
                try {
                    log.contributeService(service.getName(), service.getRole());
                    contributor.contributeService(context, service);
                    if (gatewayServices != null) {
                        ServiceRegistry sr = gatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
                        if (sr != null) {
                            String regCode = sr.getRegistrationCode(topology.getName());
                            sr.registerService(regCode, topology.getName(), service.getRole(), service.getUrls());
                        }
                    }
                } catch (Exception e) {
                    // Maybe it makes sense to throw exception
                    log.failedToContributeService(service.getName(), service.getRole(), e);
                    throw new DeploymentException("Failed to contribute service.", e);
                }
            }
        }
    }
}
Also used : Service(org.apache.knox.gateway.topology.Service) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 19 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_MultipleMixed.

@Test
public void testServiceLevelParamOverrides_MultipleMixed() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify a subset of the possible HaProvider-level params for TestRoleOne
    providerParams.put("TestRoleOne", "enabled=true;maxRetryAttempts=1;retrySleep=10;maxFailoverAttempts=2;failoverSleep=20");
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleTwo", "enabled=false;" + "maxRetryAttempts=3;" + "retrySleep=30;" + "maxFailoverAttempts=4;" + "failoverSleep=40;" + "zookeeperNamespace=testRoleTwo;" + "zookeeperEnsemble=http://host1:2181,http://host2:2181");
    Provider testHaProvider = createHaProvider(providerParams);
    // Define the topology content (e.g., services)
    Collection<Service> topologyServices = new HashSet<>();
    // A service with no param overrides
    Service testRoleOneService = EasyMock.createNiceMock(Service.class);
    EasyMock.expect(testRoleOneService.getRole()).andReturn("TestRoleOne").anyTimes();
    EasyMock.expect(testRoleOneService.getName()).andReturn("TestRoleOneService").anyTimes();
    EasyMock.expect(testRoleOneService.getParams()).andReturn(Collections.emptyMap()).anyTimes();
    EasyMock.replay(testRoleOneService);
    topologyServices.add(testRoleOneService);
    // Override all the possible params in the TestRoleTwo service level
    Map<String, String> testRoleTwoParams = new HashMap<>();
    testRoleTwoParams.put("enabled", "true");
    testRoleTwoParams.put("maxRetryAttempts", "6");
    testRoleTwoParams.put("retrySleep", "60");
    testRoleTwoParams.put("maxFailoverAttempts", "8");
    testRoleTwoParams.put("failoverSleep", "80");
    testRoleTwoParams.put("zookeeperNamespace", "testRoleTwoOverride");
    testRoleTwoParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
    Service testRoleTwoService = EasyMock.createNiceMock(Service.class);
    EasyMock.expect(testRoleTwoService.getRole()).andReturn("TestRoleTwo").anyTimes();
    EasyMock.expect(testRoleTwoService.getName()).andReturn("TestRoleTwoService").anyTimes();
    EasyMock.expect(testRoleTwoService.getParams()).andReturn(testRoleTwoParams).anyTimes();
    EasyMock.replay(testRoleTwoService);
    topologyServices.add(testRoleTwoService);
    Topology topology = EasyMock.createNiceMock(Topology.class);
    EasyMock.expect(topology.getServices()).andReturn(topologyServices).anyTimes();
    EasyMock.replay(topology);
    WebArchive war = EasyMock.createNiceMock(WebArchive.class);
    EasyMock.replay(war);
    DeploymentContext context = new DescriptorCaptureDeploymentContext(topology, war);
    // Invoke the contributor
    HaProviderDeploymentContributor haPDC = new HaProviderDeploymentContributor();
    haPDC.contributeProvider(context, testHaProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(2, descriptor.getServiceConfigs().size());
    // Validate the service with no-overrides, checking that the provider-level defaults are applied
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 20, 2, 10, 1, null, null);
    // Validate the service with all-overrides, checking that the service-level defaults are applied
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleTwo"), true, 80, 8, 60, 6, "testRoleTwoOverride", "http://host3:2181,http://host4:2181");
}
Also used : HashMap(java.util.HashMap) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Provider(org.apache.knox.gateway.topology.Provider) DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class BeanPropertyTopologyBuilder method build.

public Topology build() {
    Topology topology = new Topology();
    topology.setName(name);
    topology.setDefaultServicePath(defaultService);
    topology.setGenerated(isGenerated);
    for (Provider provider : providers) {
        topology.addProvider(provider);
    }
    for (Service service : services) {
        topology.addService(service);
    }
    for (Application application : applications) {
        topology.addApplication(application);
    }
    return topology;
}
Also used : Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Application(org.apache.knox.gateway.topology.Application) Provider(org.apache.knox.gateway.topology.Provider)

Aggregations

Service (org.apache.knox.gateway.topology.Service)25 Topology (org.apache.knox.gateway.topology.Topology)22 Test (org.junit.Test)18 Provider (org.apache.knox.gateway.topology.Provider)17 HashMap (java.util.HashMap)13 File (java.io.File)11 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)9 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)7 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)7 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)7 Param (org.apache.knox.gateway.topology.Param)7 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)6 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)6 URL (java.net.URL)5 HaDescriptor (org.apache.knox.gateway.ha.provider.HaDescriptor)5 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)5 Document (org.w3c.dom.Document)5 HashSet (java.util.HashSet)4 Digester (org.apache.commons.digester3.Digester)4 TopologyBuilder (org.apache.knox.gateway.topology.builder.TopologyBuilder)4