Search in sources :

Example 16 with Topology

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

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_NoProviderParams.

/**
 * Simple test verifying that HaProvider service params specified ONLY at the service level works.
 */
@Test
public void testServiceLevelParamOverrides_NoProviderParams() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleOne", "");
    Provider haProvider = createHaProvider(providerParams);
    // Define the topology content (e.g., services)
    Collection<Service> topologyServices = new HashSet<>();
    // Specify all the possible params in the TestRoleOne service level
    Map<String, String> testRoleOneParams = new HashMap<>();
    testRoleOneParams.put("enabled", "true");
    testRoleOneParams.put("maxRetryAttempts", "6");
    testRoleOneParams.put("retrySleep", "60");
    testRoleOneParams.put("maxFailoverAttempts", "8");
    testRoleOneParams.put("failoverSleep", "80");
    testRoleOneParams.put("zookeeperNamespace", "testRoleOneOverride");
    testRoleOneParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
    // A service with all the params overriden
    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(testRoleOneParams).anyTimes();
    EasyMock.replay(testRoleOneService);
    topologyServices.add(testRoleOneService);
    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, haProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(1, descriptor.getServiceConfigs().size());
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 80, 8, 60, 6, "testRoleOneOverride", "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 17 with Topology

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

the class JerseyDeploymentContributorTest method testDeploymentContributors.

@Test
public void testDeploymentContributors() throws Exception {
    JerseyDispatchDeploymentContributor providerContributor = new JerseyDispatchDeploymentContributor();
    assertThat(providerContributor.getRole(), is("pivot"));
    assertThat(providerContributor.getName(), is("jersey"));
    MockJerseyService serviceContributor = new MockJerseyService();
    WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-archive");
    Topology topology = new Topology();
    topology.setName("test-topology");
    Provider provider = new Provider();
    provider.setRole("pivot");
    provider.setName("jersey");
    provider.setEnabled(true);
    topology.addProvider(provider);
    GatewayDescriptor descriptor = GatewayDescriptorFactory.create();
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getWebArchive()).andReturn(webArchive).anyTimes();
    EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
    EasyMock.expect(context.getGatewayDescriptor()).andReturn(descriptor).anyTimes();
    context.contributeFilter(EasyMock.<Service>isA(Service.class), EasyMock.<ResourceDescriptor>isA(ResourceDescriptor.class), EasyMock.<String>isA(String.class), EasyMock.<String>isA(String.class), EasyMock.<List>isA(List.class));
    EasyMock.expectLastCall().andDelegateTo(new MockDeploymentContext(context, providerContributor, provider)).anyTimes();
    EasyMock.replay(context);
    // Just make sure they don't blow up.
    providerContributor.initializeContribution(context);
    serviceContributor.initializeContribution(context);
    Service service = new Service();
    service.setRole("test-service-role");
    service.setName("test-service-name");
    service.addUrl("http://test-service-host:777/test-service-path");
    // This should end up calling providerContributor.contributeFilter
    serviceContributor.contributeService(context, service);
    ResourceDescriptor resource = context.getGatewayDescriptor().resources().get(0);
    // Just make sure they don't blow up.
    serviceContributor.finalizeContribution(context);
    providerContributor.finalizeContribution(context);
    /*
    GatewayDescriptorFactory.store( descriptor, "xml", new PrintWriter( System.out ) );
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <gateway>
      <resource>
        <role>test-service-role</role>
        <pattern>test-service/?**</pattern>
        <filter>
          <role>dispatch</role>
          <name>jersey</name>
          <class>org.glassfish.jersey.servlet.ServletContainer</class>
          <param>
            <name>jersey.config.server.provider.packages</name>
            <value>test-package-1;test-package-2</value>
          </param>
        </filter>
      </resource>
      <resource>
        <role>test-service-role</role>
        <pattern>test-service/**?**</pattern>
        <filter>
          <role>dispatch</role>
          <name>jersey</name>
          <class>org.glassfish.jersey.servlet.ServletContainer</class>
          <param>
            <name>jersey.config.server.provider.packages</name>
            <value>test-package-1;test-package-2</value>
          </param>
        </filter>
      </resource>
    </gateway>
    */
    List<ResourceDescriptor> resources = context.getGatewayDescriptor().resources();
    assertThat(resources.size(), is(2));
    resource = resources.get(0);
    assertThat(resource.role(), is("test-service-role"));
    assertThat(resource.pattern(), is("test-service/?**"));
    List<FilterDescriptor> filters = resource.filters();
    assertThat(filters.size(), is(1));
    FilterDescriptor filter = filters.get(0);
    assertThat(filter.role(), is("pivot"));
    assertThat(filter.name(), is("jersey"));
    assertThat(filter.impl(), is("org.glassfish.jersey.servlet.ServletContainer"));
    List<FilterParamDescriptor> params = filter.params();
    assertThat(params.size(), is(1));
    FilterParamDescriptor param = params.get(0);
    assertThat(param.name(), is("jersey.config.server.provider.packages"));
    assertThat(param.value(), is("test-package-1;test-package-2"));
    resource = resources.get(1);
    assertThat(resource.role(), is("test-service-role"));
    assertThat(resource.pattern(), is("test-service/**?**"));
    filters = resource.filters();
    assertThat(filters.size(), is(1));
    filter = filters.get(0);
    assertThat(filter.role(), is("pivot"));
    assertThat(filter.name(), is("jersey"));
    assertThat(filter.impl(), is("org.glassfish.jersey.servlet.ServletContainer"));
    params = filter.params();
    assertThat(params.size(), is(1));
    param = params.get(0);
    assertThat(param.name(), is("jersey.config.server.provider.packages"));
    assertThat(param.value(), is("test-package-1;test-package-2"));
}
Also used : GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) FilterParamDescriptor(org.apache.knox.gateway.descriptor.FilterParamDescriptor) 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) FilterDescriptor(org.apache.knox.gateway.descriptor.FilterDescriptor) List(java.util.List) ResourceDescriptor(org.apache.knox.gateway.descriptor.ResourceDescriptor) Test(org.junit.Test)

Example 18 with Topology

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

the class HaProviderDeploymentContributor method contributeProvider.

@Override
public void contributeProvider(DeploymentContext context, Provider provider) {
    Topology topology = context.getTopology();
    Map<String, String> params = provider.getParams();
    HaDescriptor descriptor = HaDescriptorFactory.createDescriptor();
    for (Entry<String, String> entry : params.entrySet()) {
        String role = entry.getKey();
        String roleParams = entry.getValue();
        // Create the config based on whatever is specified at the provider level
        HaServiceConfig config = HaDescriptorFactory.createServiceConfig(role, roleParams);
        // Check for service-level param overrides
        Map<String, String> serviceLevelParams = null;
        for (Service s : topology.getServices()) {
            if (s.getRole().equals(role)) {
                serviceLevelParams = s.getParams();
                break;
            }
        }
        // Apply any service-level param overrides
        applyParamOverrides(config, serviceLevelParams);
        // Add the reconciled HA service config to the descriptor
        descriptor.addServiceConfig(config);
    }
    StringWriter writer = new StringWriter();
    try {
        HaDescriptorManager.store(descriptor, writer);
    } catch (IOException e) {
        LOG.failedToWriteHaDescriptor(e);
    }
    String asset = writer.toString();
    context.getWebArchive().addAsWebInfResource(new StringAsset(asset), HaServletContextListener.DESCRIPTOR_DEFAULT_FILE_NAME);
    context.addDescriptor(HA_DESCRIPTOR_NAME, descriptor);
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) StringWriter(java.io.StringWriter) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) IOException(java.io.IOException) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) HaServiceConfig(org.apache.knox.gateway.ha.provider.HaServiceConfig)

Example 19 with Topology

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

the class DefaultTopologyService method loadTopology.

private Topology loadTopology(File file) throws IOException, SAXException, URISyntaxException, InterruptedException {
    // ms
    final long TIMEOUT = 250;
    // ms
    final long DELAY = 50;
    log.loadingTopologyFile(file.getAbsolutePath());
    Topology topology;
    long start = System.currentTimeMillis();
    while (true) {
        try {
            topology = loadTopologyAttempt(file);
            break;
        } catch (IOException e) {
            if (System.currentTimeMillis() - start < TIMEOUT) {
                log.failedToLoadTopologyRetrying(file.getAbsolutePath(), Long.toString(DELAY), e);
                Thread.sleep(DELAY);
            } else {
                throw e;
            }
        } catch (SAXException e) {
            if (System.currentTimeMillis() - start < TIMEOUT) {
                log.failedToLoadTopologyRetrying(file.getAbsolutePath(), Long.toString(DELAY), e);
                Thread.sleep(DELAY);
            } else {
                throw e;
            }
        }
    }
    return topology;
}
Also used : Topology(org.apache.knox.gateway.topology.Topology) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 20 with Topology

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

the class PropertyTopologyBuilder method build.

public Topology build() {
    Topology topology = new Topology();
    PropertyInterpreter propertyInterpreter = new PropertyInterpreter(topology);
    for (Property property : properties) {
        try {
            propertyInterpreter.interpret(property.getName(), property.getValue());
        } catch (InterpretException ie) {
            log.failedToInterpretProperty(property.getName(), ie);
            throw new IllegalArgumentException(gatewayResources.wrongTopologyDataFormatError());
        }
    }
    return topology;
}
Also used : InterpretException(org.apache.knox.gateway.topology.builder.property.interpreter.InterpretException) PropertyInterpreter(org.apache.knox.gateway.topology.builder.property.interpreter.PropertyInterpreter) Topology(org.apache.knox.gateway.topology.Topology) Property(org.apache.knox.gateway.topology.builder.property.Property)

Aggregations

Topology (org.apache.knox.gateway.topology.Topology)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)23 Service (org.apache.knox.gateway.topology.Service)22 File (java.io.File)20 Provider (org.apache.knox.gateway.topology.Provider)20 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)17 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)10 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)9 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)9 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)9 IOException (java.io.IOException)8 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 URL (java.net.URL)7 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)7 Application (org.apache.knox.gateway.topology.Application)7 Param (org.apache.knox.gateway.topology.Param)7 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)7 Document (org.w3c.dom.Document)7 Digester (org.apache.commons.digester3.Digester)6