diff options
Diffstat (limited to 'framework/src/onos/incubator/rpc')
6 files changed, 503 insertions, 0 deletions
diff --git a/framework/src/onos/incubator/rpc/pom.xml b/framework/src/onos/incubator/rpc/pom.xml new file mode 100644 index 00000000..8619f92e --- /dev/null +++ b/framework/src/onos/incubator/rpc/pom.xml @@ -0,0 +1,135 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ Copyright 2015 Open Networking Laboratory + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <artifactId>onos-incubator</artifactId> + <groupId>org.onosproject</groupId> + <version>1.4.0-SNAPSHOT</version> + </parent> + + <artifactId>onos-incubator-rpc</artifactId> + <packaging>bundle</packaging> + + <description>ONOS inter-cluster RPC service</description> + <url>http://onosproject.org</url> + + <properties> + <onos.app.name>org.onosproject.incubator.rpc</onos.app.name> + </properties> + + <dependencies> + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-api</artifactId> + </dependency> + + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onlab-osgi</artifactId> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-api</artifactId> + <scope>test</scope> + <classifier>tests</classifier> + </dependency> + + <dependency> + <groupId>org.onosproject</groupId> + <artifactId>onos-incubator-api</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + <scope>provided</scope> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <executions> + <execution> + <id>generate-scr-srcdescriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + <configuration> + <supportedProjectTypes> + <supportedProjectType>bundle</supportedProjectType> + <supportedProjectType>war</supportedProjectType> + </supportedProjectTypes> + </configuration> + </plugin> + <plugin> + <groupId>org.onosproject</groupId> + <artifactId>onos-maven-plugin</artifactId> + <executions> + <execution> + <id>cfg</id> + <phase>generate-resources</phase> + <goals> + <goal>cfg</goal> + </goals> + </execution> + <execution> + <id>swagger</id> + <phase>generate-sources</phase> + <goals> + <goal>swagger</goal> + </goals> + </execution> + <execution> + <id>app</id> + <phase>package</phase> + <goals> + <goal>app</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> diff --git a/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/AbstractProviderRegistry.java b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/AbstractProviderRegistry.java new file mode 100644 index 00000000..2a8dda32 --- /dev/null +++ b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/AbstractProviderRegistry.java @@ -0,0 +1,73 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.incubator.rpc.impl; + +import java.util.Map; +import java.util.Set; + +import org.onosproject.incubator.rpc.RemoteServiceContextProvider; +import org.onosproject.incubator.rpc.RemoteServiceContextProviderService; +import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry; +import org.onosproject.net.provider.ProviderId; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; + +// Probably should change name or add missing feature (provider from scheme) to +// org.onosproject.net.provider.AbstractProviderRegistry<P, S> +@Beta +abstract class AbstractProviderRegistry + implements RemoteServiceProviderRegistry { + + + private final Map<ProviderId, RemoteServiceContextProvider> pidToProvider = Maps.newConcurrentMap(); + private final Map<String, RemoteServiceContextProvider> schemeToProvider = Maps.newConcurrentMap(); + + public AbstractProviderRegistry() { + super(); + } + + protected abstract RemoteServiceContextProviderService createProviderService(RemoteServiceContextProvider provider); + + @Override + public synchronized RemoteServiceContextProviderService register(RemoteServiceContextProvider provider) { + // TODO check if it already exists + pidToProvider.put(provider.id(), provider); + schemeToProvider.put(provider.id().scheme(), provider); + return createProviderService(provider); + } + + @Override + public synchronized void unregister(RemoteServiceContextProvider provider) { + pidToProvider.remove(provider.id(), provider); + schemeToProvider.remove(provider.id().scheme(), provider); + } + + @Override + public Set<ProviderId> getProviders() { + return ImmutableSet.copyOf(pidToProvider.keySet()); + } + + protected RemoteServiceContextProvider getProvider(ProviderId pid) { + return pidToProvider.get(pid); + } + + protected RemoteServiceContextProvider getProvider(String scheme) { + return schemeToProvider.get(scheme); + } + +} diff --git a/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/LocalRemoteServiceProvider.java b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/LocalRemoteServiceProvider.java new file mode 100644 index 00000000..d38248ab --- /dev/null +++ b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/LocalRemoteServiceProvider.java @@ -0,0 +1,124 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.incubator.rpc.impl; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.net.URI; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.ReferenceCardinality; +import org.onlab.osgi.DefaultServiceDirectory; +import org.onlab.osgi.ServiceDirectory; +import org.onosproject.incubator.rpc.RemoteServiceContext; +import org.onosproject.incubator.rpc.RemoteServiceContextProvider; +import org.onosproject.incubator.rpc.RemoteServiceContextProviderService; +import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry; +import org.onosproject.net.provider.ProviderId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.Beta; + +/** + * Sample implementation of RemoteServiceContextProvider. + * + * Scheme: "local", calling corresponding local service on request. + * Only expected for testing until real RPC implementation is ready. + * + * Note: This is expected to be removed or separated out as separate bundle + * once other RPC implementaion became available. + */ +@Beta +@Component(immediate = true) +public class LocalRemoteServiceProvider implements RemoteServiceContextProvider { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + private RemoteServiceContext theOne = new LocalServiceContext(); + + private static final ProviderId PID = new ProviderId("local", "org.onosproject.rpc.provider.local"); + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected RemoteServiceProviderRegistry rpcRegistry; + + private final Map<Class<? extends Object>, Object> services = new ConcurrentHashMap<>(); + + private RemoteServiceContextProviderService providerService; + + @Activate + protected void activate() { + + services.put(SomeOtherService.class, new SomeOtherServiceImpl()); + + providerService = rpcRegistry.register(this); + log.info("Started"); + } + + @Deactivate + protected void deactivate() { + rpcRegistry.unregister(this); + log.info("Stopped"); + } + + @Override + public ProviderId id() { + return PID; + } + + @Override + public RemoteServiceContext get(URI uri) { + checkArgument(Objects.equals(uri.getScheme(), "local")); + return theOne; + } + + private final class LocalServiceContext implements RemoteServiceContext { + + private final ServiceDirectory directory = new DefaultServiceDirectory(); + + @Override + public <T> T get(Class<T> serviceClass) { + @SuppressWarnings("unchecked") + T service = (T) services.get(serviceClass); + if (service != null) { + return service; + } + // look up OSGi services on this host. + // provided to unblock development depending on RPC. + return directory.get(serviceClass); + } + } + + // Service provided by RPC can be non-OSGi Service + public static interface SomeOtherService { + String hello(); + } + + public static class SomeOtherServiceImpl implements SomeOtherService { + + @Override + public String hello() { + return "Goodbye"; + } + } + +} diff --git a/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/RemoteServiceManager.java b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/RemoteServiceManager.java new file mode 100644 index 00000000..3b851739 --- /dev/null +++ b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/RemoteServiceManager.java @@ -0,0 +1,80 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.incubator.rpc.impl; + +import java.net.URI; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Service; +import org.onosproject.incubator.rpc.RemoteServiceContext; +import org.onosproject.incubator.rpc.RemoteServiceDirectory; +import org.onosproject.incubator.rpc.RemoteServiceContextProvider; +import org.onosproject.incubator.rpc.RemoteServiceContextProviderService; +import org.onosproject.incubator.rpc.RemoteServiceProviderRegistry; +import org.onosproject.net.provider.AbstractProviderService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.Beta; + +/** + * Provides RemoteService related APIs. + */ +@Beta +@Component(immediate = true) +@Service +public class RemoteServiceManager extends AbstractProviderRegistry + implements RemoteServiceDirectory, RemoteServiceProviderRegistry { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + @Activate + protected void activate() { + log.info("Started"); + } + + @Deactivate + protected void deactivate() { + log.info("Stopped"); + } + + @Override + public RemoteServiceContext get(URI uri) { + RemoteServiceContextProvider factory = getProvider(uri.getScheme()); + if (factory != null) { + return factory.get(uri); + } + throw new UnsupportedOperationException(uri.getScheme() + " not supported"); + } + + private final class InternalRemoteServiceContextProviderService + extends AbstractProviderService<RemoteServiceContextProvider> + implements RemoteServiceContextProviderService { + + public InternalRemoteServiceContextProviderService(RemoteServiceContextProvider provider) { + super(provider); + } + } + + // make this abstract method if slicing out + @Override + protected RemoteServiceContextProviderService createProviderService(RemoteServiceContextProvider provider) { + return new InternalRemoteServiceContextProviderService(provider); + } + +} diff --git a/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/package-info.java b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/package-info.java new file mode 100644 index 00000000..c77b4bec --- /dev/null +++ b/framework/src/onos/incubator/rpc/src/main/java/org/onosproject/incubator/rpc/impl/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Implementation of the inter-cluster RPC service. + */ +package org.onosproject.incubator.rpc.impl; diff --git a/framework/src/onos/incubator/rpc/src/test/java/org/onosproject/incubator/rpc/impl/RemoteServiceManagerTest.java b/framework/src/onos/incubator/rpc/src/test/java/org/onosproject/incubator/rpc/impl/RemoteServiceManagerTest.java new file mode 100644 index 00000000..1bfa1e42 --- /dev/null +++ b/framework/src/onos/incubator/rpc/src/test/java/org/onosproject/incubator/rpc/impl/RemoteServiceManagerTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.incubator.rpc.impl; + +import static org.junit.Assert.*; + +import java.net.URI; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onosproject.incubator.rpc.RemoteServiceContext; +import org.onosproject.incubator.rpc.RemoteServiceDirectory; +import org.onosproject.incubator.rpc.impl.LocalRemoteServiceProvider.SomeOtherService; + +/** + * Set of tests of the RemoteServiceManager component. + */ +public class RemoteServiceManagerTest { + + private static final URI LOCAL_URI = URI.create("local://whateverIgnored"); + + private RemoteServiceManager rpcManager; + private RemoteServiceDirectory rpcDirectory; + + private LocalRemoteServiceProvider rpcProvider; + + @Before + public void setUp() { + rpcManager = new RemoteServiceManager(); + rpcManager.activate(); + rpcDirectory = rpcManager; + + rpcProvider = new LocalRemoteServiceProvider(); + rpcProvider.rpcRegistry = rpcManager; + rpcProvider.activate(); + + } + + @After + public void tearDown() { + rpcProvider.deactivate(); + + rpcManager.deactivate(); + } + + @Test + public void basics() { + RemoteServiceContext remoteServiceContext = rpcDirectory.get(LOCAL_URI); + assertNotNull("Expecting valid RPC context", remoteServiceContext); + + SomeOtherService someService = remoteServiceContext.get(SomeOtherService.class); + assertNotNull("Expecting reference to sample service", someService); + + assertEquals("Goodbye", someService.hello()); + } + +} |