Search in sources :

Example 46 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCache method treeCacheEvent.

protected void treeCacheEvent(PathChildrenCacheEvent event) {
    ChildData childData = event.getData();
    if (childData == null) {
        return;
    }
    String path = childData.getPath();
    LOG.trace("Event {} on path {}", event.getType().toString(), path);
    PathChildrenCacheEvent.Type type = event.getType();
    byte[] data = childData.getData();
    if (data == null || data.length == 0 || path == null) {
        return;
    }
    if (path.startsWith(zkPath)) {
        path = path.substring(zkPath.length());
    }
    List<String> split = Strings.splitAndTrimAsList(path, "/");
    // Lets just use the group name as the service path.
    path = split.get(0);
    String zNode = split.get(1);
    boolean remove = false;
    switch(type) {
        case CHILD_ADDED:
        case CHILD_UPDATED:
            break;
        case CHILD_REMOVED:
            remove = true;
            break;
        default:
            return;
    }
    ServiceDTO dto = null;
    try {
        dto = mapper.readValue(data, ServiceDTO.class);
        expandPropertyResolvers(dto);
        dto.setContainer(dto.getContainer() + "_zNode_" + zNode);
        if (remove) {
            LOG.info("Removed gateway service: " + path + ": " + dto);
            serviceMap.serviceRemoved(path, dto);
        } else {
            LOG.info("Updated gateway service: " + path + ": " + dto);
            serviceMap.serviceUpdated(path, dto);
        }
    } catch (IOException e) {
        LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
    } catch (URISyntaxException e) {
        LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
    }
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 47 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class HttpMappingRuleConfiguration method updateConfiguration.

private void updateConfiguration(Map<String, ?> configuration) throws Exception {
    LOG.info("activating http mapping rule " + configuration);
    configurer.configure(configuration, this);
    LOG.info("activating http mapping rule " + zooKeeperPath + " on " + gateway.get().getPort());
    String zkPath = getZooKeeperPath();
    Objects.notNull(zkPath, "zooKeeperPath");
    Objects.notNull(getUriTemplate(), "uriTemplate");
    LoadBalancer loadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    LOG.info("activating http mapping ZooKeeper path: " + zkPath + " with URI template: " + uriTemplate + " enabledVersion: " + enabledVersion + " with load balancer: " + loadBalancer);
    if (httpMappingRuleBase != null) {
        gateway.get().removeMappingRuleConfiguration(httpMappingRuleBase);
    }
    httpMappingRuleBase = new HttpMappingRuleBase(new SimplePathTemplate(uriTemplate), gateway.get().getGatewayVersion(), enabledVersion, loadBalancer, reverseHeaders);
    mappingTree = new HttpMappingZooKeeperTreeCache(curator.get(), httpMappingRuleBase, zooKeeperPath);
    mappingTree.init();
    gateway.get().addMappingRuleConfiguration(httpMappingRuleBase);
}
Also used : HttpMappingZooKeeperTreeCache(io.fabric8.gateway.fabric.support.http.HttpMappingZooKeeperTreeCache) HttpMappingRuleBase(io.fabric8.gateway.fabric.support.http.HttpMappingRuleBase) SimplePathTemplate(io.fabric8.zookeeper.internal.SimplePathTemplate) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer)

Example 48 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class FabricMQGateway method createListener.

protected GatewayServiceTreeCache createListener() {
    String zkPath = getZooKeeperPath();
    // TODO we should discover the broker group configuration here using the same
    // mq-create / mq-client profiles so that we only listen to a subset of the available brokers here?
    ServiceMap serviceMap = new ServiceMap();
    VertxService vertxService = getVertxService();
    Vertx vertx = vertxService.getVertx();
    CuratorFramework curator = getCurator();
    LoadBalancer pathLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    LoadBalancer serviceLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    LOG.info("activating MQ mapping ZooKeeper path: " + zkPath + " host: " + host + " with load balancer: " + pathLoadBalancer);
    List<TcpGateway> gateways = new ArrayList<TcpGateway>();
    addGateway(gateways, vertx, serviceMap, "tcp", isOpenWireEnabled(), getOpenWirePort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "stomp", isStompEnabled(), getStompPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "amqp", isAmqpEnabled(), getAmqpPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "mqtt", isMqttEnabled(), getMqttPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "ws", isWebsocketEnabled(), getWebsocketPort(), pathLoadBalancer, serviceLoadBalancer);
    if (gateways.isEmpty()) {
        return null;
    }
    return new GatewayServiceTreeCache(curator, zkPath, serviceMap, gateways);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceMap(io.fabric8.gateway.ServiceMap) TcpGateway(io.fabric8.gateway.handlers.tcp.TcpGateway) VertxService(io.fabric8.gateway.fabric.support.vertx.VertxService) ArrayList(java.util.ArrayList) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) Vertx(org.vertx.java.core.Vertx)

Example 49 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCache method treeCacheEvent.

protected void treeCacheEvent(PathChildrenCacheEvent event) {
    ChildData childData = event.getData();
    if (childData == null) {
        return;
    }
    String path = childData.getPath();
    PathChildrenCacheEvent.Type type = event.getType();
    byte[] data = childData.getData();
    if (data == null || data.length == 0 || path == null) {
        return;
    }
    if (path.startsWith(zkPath)) {
        path = path.substring(zkPath.length());
    }
    boolean remove = false;
    switch(type) {
        case CHILD_ADDED:
        case CHILD_UPDATED:
            break;
        case CHILD_REMOVED:
            remove = true;
            break;
        default:
            return;
    }
    ServiceDTO dto = null;
    try {
        dto = mapper.readValue(data, ServiceDTO.class);
        expandPropertyResolvers(dto);
        if (remove) {
            serviceMap.serviceRemoved(path, dto);
        } else {
            serviceMap.serviceUpdated(path, dto);
        }
    } catch (IOException e) {
        LOG.warn("Failed to parse the JSON: " + new String(data) + ". Reason: " + e, e);
    } catch (URISyntaxException e) {
        LOG.warn("Failed to update URI for dto: " + dto + ", .Reason: " + e, e);
    }
}
Also used : PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ChildData(org.apache.curator.framework.recipes.cache.ChildData) ServiceDTO(io.fabric8.gateway.ServiceDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 50 with Path

use of io.fabric8.annotations.Path in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCacheTest method treeCacheEvent.

@Test
public void treeCacheEvent() throws Exception {
    String zkPath = "/fabric/registry/clusters/test";
    ServiceMap serviceMap = new ServiceMap();
    CuratorFramework curator = mock(CuratorFramework.class);
    GatewayServiceTreeCache cache = new GatewayServiceTreeCache(curator, zkPath, serviceMap);
    String path = "/fabric/registry/clusters/test/default/0000001";
    // Add container1 - master
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container1
    // Update container2 - master
    // Add container1 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container2", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container2
    // Update container1 - master
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container2
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceMap(io.fabric8.gateway.ServiceMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)45 File (java.io.File)41 IOException (java.io.IOException)34 ArrayList (java.util.ArrayList)18 PathTestUtil.createTmpFile (io.fabric8.maven.docker.util.PathTestUtil.createTmpFile)17 HashMap (java.util.HashMap)12 ResourceConfig (io.fabric8.maven.core.config.ResourceConfig)11 VolumeConfig (io.fabric8.maven.core.config.VolumeConfig)11 Path (java.nio.file.Path)11 FabricService (io.fabric8.api.FabricService)10 RuntimeProperties (io.fabric8.api.RuntimeProperties)9 Properties (java.util.Properties)9 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)8 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)8 InputStream (java.io.InputStream)8 Path (javax.ws.rs.Path)8 HttpProxyRule (io.fabric8.gateway.model.HttpProxyRule)7 NodeState (io.fabric8.groups.NodeState)7 URISyntaxException (java.net.URISyntaxException)7 List (java.util.List)6