Search in sources :

Example 21 with Endpoint

use of io.fabric8.annotations.Endpoint in project carbon-apimgt by wso2.

the class ServiceDiscovererKubernetesTestCase method testListServices.

@Test(description = "Test .listServices() method")
public void testListServices() throws Exception {
    OpenShiftClient openShiftClient = Mockito.mock(OpenShiftClient.class, Mockito.RETURNS_DEEP_STUBS);
    ServiceDiscovererKubernetes sdKubernetes = new ServiceDiscovererKubernetes();
    sdKubernetes.setClient(openShiftClient);
    // Include ClusterIPs
    sdKubernetes.setIncludeClusterIP(true);
    // Include ExternalNames
    sdKubernetes.setIncludeExternalNameTypeServices(true);
    NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
    Mockito.when(openShiftClient.services().inNamespace(null)).thenReturn(nonNamespaceOperation);
    Mockito.when(nonNamespaceOperation.list()).thenReturn(createServiceList());
    Mockito.when(openShiftClient.getMasterUrl()).thenReturn(new URL(MASTER_URL));
    List<Endpoint> endpoints = sdKubernetes.listServices();
    Assert.assertEquals(endpoints.size(), 10);
    // 2 NodePort URL endpoints
    Mockito.verify(openShiftClient, Mockito.times(2)).getMasterUrl();
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) URL(java.net.URL) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 22 with Endpoint

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

the class CamelAutoTestService method onConfigured.

@Override
protected void onConfigured() throws Exception {
    LOG.debug("onConfigured. mockOutputs: " + mockOutputs + " mockInputs: " + mockInputs + " messageFolder: " + messageFolder);
    FabricService fabricService = this.fabricService.getOptional();
    // lets find the camel contexts to test in this container
    MBeanServer mbeanServerValue = mbeanServer;
    if (mbeanServerValue != null && fabricService != null) {
        Profile overlayProfile = fabricService.getCurrentContainer().getOverlayProfile();
        Profile effectiveProfile = Profiles.getEffectiveProfile(fabricService, overlayProfile);
        Set<String> configurationFileNames = effectiveProfile.getConfigurationFileNames();
        for (CamelContext camelContext : camelContexts.values()) {
            String camelContextID = camelContext.getName();
            // check we only add testing stuff to each context once
            if (camelContext instanceof ModelCamelContext) {
                final ModelCamelContext modelCamelContext = (ModelCamelContext) camelContext;
                List<RouteDefinition> routeDefinitions = modelCamelContext.getRouteDefinitions();
                if (camelContextsConfigured.add(camelContextID)) {
                    NodeIdFactory nodeIdFactory = camelContext.getNodeIdFactory();
                    if (mockInputs || mockOutputs) {
                        for (RouteDefinition routeDefinition : routeDefinitions) {
                            String routeId = routeDefinition.idOrCreate(nodeIdFactory);
                            modelCamelContext.stopRoute(routeId);
                            final String routeKey = camelContextID + "." + routeId;
                            LOG.info("Mocking Camel route: " + routeKey);
                            routeDefinition.adviceWith(modelCamelContext, new AdviceWithRouteBuilder() {

                                @Override
                                public void configure() throws Exception {
                                    if (mockOutputs) {
                                        modelCamelContext.addRegisterEndpointCallback(strategy);
                                    }
                                }
                            });
                        // the advised route is automatic restarted
                        }
                    }
                    String path = messageFolder;
                    if (Strings.isNotBlank(path)) {
                        path += "/";
                    }
                    path += camelContextID;
                    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
                    try {
                        for (RouteDefinition routeDefinition : routeDefinitions) {
                            String routeId = routeDefinition.idOrCreate(nodeIdFactory);
                            String routePath = path + "/" + routeId + "/";
                            List<FromDefinition> inputs = routeDefinition.getInputs();
                            for (FromDefinition input : inputs) {
                                Endpoint endpoint = input.getEndpoint();
                                if (endpoint == null) {
                                    String uri = input.getUri();
                                    if (Strings.isNullOrBlank(uri)) {
                                        String ref = input.getRef();
                                        if (Strings.isNotBlank(ref)) {
                                            uri = "ref:" + ref;
                                        }
                                    }
                                    if (Strings.isNotBlank(uri)) {
                                        endpoint = camelContext.getEndpoint(uri);
                                    }
                                }
                                if (endpoint == null) {
                                    LOG.warn("Cannot find endpoint, uri or ref of input " + input + " on route " + routeId + " camelContext: " + camelContextID);
                                } else {
                                    for (String configFile : configurationFileNames) {
                                        if (configFile.startsWith(routePath)) {
                                            LOG.info("Sending file: " + configFile + " to " + endpoint);
                                            byte[] data = effectiveProfile.getFileConfiguration(configFile);
                                            if (data != null) {
                                                // lest send this message to this endpoint
                                                producerTemplate.sendBody(endpoint, data);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        producerTemplate.stop();
                    }
                }
            }
        }
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) FromDefinition(org.apache.camel.model.FromDefinition) Profile(io.fabric8.api.Profile) ModelCamelContext(org.apache.camel.model.ModelCamelContext) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder) NodeIdFactory(org.apache.camel.spi.NodeIdFactory) RouteDefinition(org.apache.camel.model.RouteDefinition) Endpoint(org.apache.camel.Endpoint) FabricService(io.fabric8.api.FabricService) MBeanServer(javax.management.MBeanServer)

Example 23 with Endpoint

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

the class FabricWebRegistrationHandler method servletEvent.

@Override
public void servletEvent(ServletEvent servletEvent) {
    if (isValid()) {
        WebEvent webEvent = webEvents.get(servletEvent.getBundle());
        if (webEvent != null || servletEvent.getAlias() == null) {
            // this servlet is part of a web application, ignore it
            return;
        }
        Map<String, ServletEvent> events;
        synchronized (servletEvents) {
            events = servletEvents.get(servletEvent.getBundle());
            if (events == null) {
                events = new ConcurrentHashMap<String, ServletEvent>();
                servletEvents.put(servletEvent.getBundle(), events);
            }
        }
        events.put(servletEvent.getAlias(), servletEvent);
        if (curator.get().getZookeeperClient().isConnected()) {
            switch(servletEvent.getType()) {
                case ServletEvent.DEPLOYING:
                    break;
                case ServletEvent.DEPLOYED:
                    try {
                        registerServlet(fabricService.get().getCurrentContainer(), servletEvent);
                    } catch (IllegalStateException e) {
                        if ("Client is not started".equals(e.getMessage())) {
                            LOGGER.warn("Zookeeper connection not available. Servlet endpoint might have not been registered correctly in zookeeper registry: {}", servletEvent);
                        }
                    }
                    break;
                default:
                    try {
                        unregisterServlet(fabricService.get().getCurrentContainer(), servletEvent);
                    } catch (IllegalStateException e) {
                        if ("Client is not started".equals(e.getMessage())) {
                            LOGGER.warn("Zookeeper connection not available. Servlet endpoint might have not been removed correctly from zookeeper registry: {}", servletEvent);
                        }
                    }
                    break;
            }
        }
    }
}
Also used : ServletEvent(org.ops4j.pax.web.service.spi.ServletEvent) JsonHelper.jsonEncodeString(io.fabric8.internal.JsonHelper.jsonEncodeString) WebEvent(org.ops4j.pax.web.service.spi.WebEvent)

Example 24 with Endpoint

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

the class RestJsonSchemaJMXTest method setUp.

@Before
public void setUp() {
    SpringBusFactory bf = new SpringBusFactory();
    Bus bus = bf.createBus("/io/fabric8/cxf/endpoint/jaxrs/jmx-enable.xml");
    BusFactory.setDefaultBus(bus);
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(BookStore.class, BookStoreSpring.class);
    sf.setResourceProvider(BookStore.class, new SingletonResourceProvider(new BookStore(), true));
    sf.setResourceProvider(BookStoreSpring.class, new SingletonResourceProvider(new BookStoreSpring(), true));
    sf.setTransportId(LocalTransportFactory.TRANSPORT_ID);
    sf.setAddress("local://books");
    sf.getFeatures().add(new ManagedApiFeature());
    localServer = sf.create();
}
Also used : Bus(org.apache.cxf.Bus) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) ManagedApiFeature(io.fabric8.cxf.endpoint.ManagedApiFeature) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) Before(org.junit.Before)

Example 25 with Endpoint

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

the class DetectingGateway method route.

public void route(final SocketWrapper socket, ConnectionParameters params, final Buffer received) {
    NetClient client = null;
    if (params.protocolVirtualHost == null) {
        params.protocolVirtualHost = defaultVirtualHost;
    }
    HashSet<String> schemes = new HashSet<String>(Arrays.asList(params.protocolSchemes));
    if (params.protocolVirtualHost != null) {
        List<ServiceDetails> services = serviceMap.getServices(params.protocolVirtualHost);
        // Lets try again with the defaultVirtualHost
        if (services.isEmpty() && !params.protocolVirtualHost.equals(defaultVirtualHost)) {
            params.protocolVirtualHost = defaultVirtualHost;
            services = serviceMap.getServices(params.protocolVirtualHost);
        }
        LOG.debug(String.format("%d services match the virtual host", services.size()));
        if (!services.isEmpty()) {
            ClientRequestFacade clientRequestFacade = clientRequestFacadeFactory.create(socket, params);
            ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, clientRequestFacade);
            if (serviceDetails != null) {
                List<String> urlStrings = serviceDetails.getServices();
                LOG.debug("Selected service exposes the following URLS: {}", urlStrings);
                for (String urlString : urlStrings) {
                    if (Strings.notEmpty(urlString)) {
                        // lets create a client for this request...
                        try {
                            URI uri = new URI(urlString);
                            // URL url = new URL(urlString);
                            String urlProtocol = uri.getScheme();
                            if (schemes.contains(urlProtocol)) {
                                if (!socket.remoteAddress().toString().equals(clientRequestFacade.getClientRequestKey())) {
                                    LOG.info(String.format("Connecting client from '%s' (with key '%s') requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), clientRequestFacade.getClientRequestKey(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                } else {
                                    LOG.info(String.format("Connecting client from '%s' requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                }
                                client = createClient(params, socket, uri, received);
                                break;
                            }
                        } catch (URISyntaxException e) {
                            LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
                        }
                    }
                }
            }
        }
    }
    if (client == null) {
        // failed to route
        handleConnectFailure(socket, String.format("No endpoint available for virtual host '%s' and protocol %s", params.protocolVirtualHost, params.protocol));
    }
}
Also used : NetClient(org.vertx.java.core.net.NetClient) ServiceDetails(io.fabric8.gateway.ServiceDetails) ClientRequestFacade(io.fabric8.gateway.loadbalancer.ClientRequestFacade) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)12 Service (io.fabric8.kubernetes.api.model.Service)9 ArrayList (java.util.ArrayList)9 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)7 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)7 URL (java.net.URL)7 BeforeTest (org.testng.annotations.BeforeTest)7 Test (org.testng.annotations.Test)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 IOException (java.io.IOException)5 MalformedURLException (java.net.MalformedURLException)5 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)4 ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 EndpointsList (io.fabric8.kubernetes.api.model.EndpointsList)3 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)3 File (java.io.File)3 Collection (java.util.Collection)3 Configuration (io.fabric8.annotations.Configuration)2 Endpoint (io.fabric8.annotations.Endpoint)2