Reorganized SADIS to separate out api and app
Change-Id: Id30e9bc2880282930f97ab947dd95f19f853b3e8
diff --git a/app/app.xml b/app/app.xml
new file mode 100755
index 0000000..ee1f3c8
--- /dev/null
+++ b/app/app.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present 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.
+ -->
+<app name="org.opencord.sadis" origin="ON.Lab" version="${project.version}"
+ category="Traffic Steering" url="http://onosproject.org" title="Subscriber And Device Information App"
+ featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features"
+ features="${project.artifactId}" apps="org.opencord.config">
+ <description>${project.description}</description>
+ <artifact>mvn:${project.groupId}/${project.artifactId}/${project.version}</artifact>
+ <artifact>mvn:${project.groupId}/sadis-api/${project.version}</artifact>
+</app>
diff --git a/app/features.xml b/app/features.xml
new file mode 100755
index 0000000..2e1e300
--- /dev/null
+++ b/app/features.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+ ~ Copyright 2017-present 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.
+ -->
+<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
+ <feature name="${project.artifactId}" version="${project.version}"
+ description="${project.description}">
+ <feature>onos-api</feature>
+ <bundle>mvn:${project.groupId}/sadis-api/${project.version}</bundle>
+ <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
+ </feature>
+</features>
diff --git a/app/pom.xml b/app/pom.xml
new file mode 100755
index 0000000..c9d29ac
--- /dev/null
+++ b/app/pom.xml
@@ -0,0 +1,193 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present 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">
+ <parent>
+ <groupId>org.opencord</groupId>
+ <artifactId>sadis</artifactId>
+ <version>1.0.0</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>sadis-app</artifactId>
+
+ <packaging>bundle</packaging>
+ <description>Subscriber and Device Information application for CORD</description>
+
+ <properties>
+ <web.context>/onos/sadis</web.context>
+ <api.version>1.0.0</api.version>
+ <api.title>Subscriber And Device Information REST API</api.title>
+ <api.description>Subscriber And Device Information REST API</api.description>
+ <api.package>org.onosproject.sadis.rest</api.package>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-api</artifactId>
+ <version>${onos.version}</version>
+ <scope>test</scope>
+ <classifier>tests</classifier>
+ </dependency>
+
+ <dependency>
+ <groupId>org.opencord</groupId>
+ <artifactId>sadis-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onlab-osgi</artifactId>
+ <version>${onos.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onlab-junit</artifactId>
+ <version>${onos.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.12</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ <version>1.9.12</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.ws.rs</groupId>
+ <artifactId>javax.ws.rs-api</artifactId>
+ <version>2.0.1</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-annotations</artifactId>
+ <version>2.8.6</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>2.8.6</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>5.0.0</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-rest</artifactId>
+ <version>${onos.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.karaf.shell</groupId>
+ <artifactId>org.apache.karaf.shell.console</artifactId>
+ <version>3.0.8</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-cli</artifactId>
+ <version>${onos.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.glassfish.jersey.containers</groupId>
+ <artifactId>jersey-container-servlet</artifactId>
+ <version>2.25.1</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onlab-rest</artifactId>
+ <version>${onos.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-maven-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <_wab>src/main/webapp/</_wab>
+ <Include-Resource>
+ WEB-INF/classes/apidoc/swagger.json=target/swagger.json,
+ {maven-resources}
+ </Include-Resource>
+ <Bundle-SymbolicName>
+ ${project.groupId}.${project.artifactId}
+ </Bundle-SymbolicName>
+ <Import-Package>
+ *,org.glassfish.jersey.servlet
+ </Import-Package>
+ <Web-ContextPath>${web.context}</Web-ContextPath>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ <version>1.21.0</version>
+ <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>
+ </plugins>
+ </build>
+</project>
diff --git a/app/src/main/java/org/opencord/sadis/cli/SubscriberGetCommand.java b/app/src/main/java/org/opencord/sadis/cli/SubscriberGetCommand.java
new file mode 100644
index 0000000..bc5e812
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/cli/SubscriberGetCommand.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2017-present 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.opencord.sadis.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+
+/**
+ * Subscriber And Device Information Service CLI.
+ */
+@Command(scope = "onos", name = "sadis", description = "Subscriber And Device Information Service CLI command")
+public class SubscriberGetCommand extends AbstractShellCommand {
+
+ @Argument(index = 0, name = "ID", description = "subscriber ID", required = true, multiValued = false)
+ String id;
+
+ @Override
+ protected void execute() {
+ print("Hello %s", "World");
+ }
+
+}
diff --git a/app/src/main/java/org/opencord/sadis/cli/package-info.java b/app/src/main/java/org/opencord/sadis/cli/package-info.java
new file mode 100644
index 0000000..04738d2
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present 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.
+ */
+
+/**
+ * SADIS application for handling Subscriber and Device Information.
+ */
+package org.opencord.sadis.cli;
diff --git a/app/src/main/java/org/opencord/sadis/impl/SadisConfig.java b/app/src/main/java/org/opencord/sadis/impl/SadisConfig.java
new file mode 100644
index 0000000..26aaa87
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/impl/SadisConfig.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright 2017-present 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.opencord.sadis.impl;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.VlanId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.Config;
+import org.opencord.sadis.SubscriberAndDeviceInformation;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.ObjectCodec;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+/**
+ * Configuration options for the Subscriber And Device Information Service.
+ *
+ * <pre>
+ * "sadis" : {
+ * "integration" : {
+ * "url" : "http://{hostname}{:port}",
+ * "cache" : {
+ * "enable" : true or false,
+ * "maxsize" : number of entries,
+ * "entryttl" : duration, i.e. 10s or 1m
+ * }
+ * },
+ * "entries" : [
+ * {
+ * "id" : "uniqueid",
+ * "ctag" : int,
+ * "stag" : int,
+ * "nasportid" : string,
+ * "port" : int,
+ * "slot" : int,
+ * "hardwareidentifier" : string,
+ * "ipAddress" : string,
+ * "nasId" : string
+ * }, ...
+ * ]
+ * }
+ * </pre>
+ */
+public class SadisConfig extends Config<ApplicationId> {
+
+ private final Logger log = LoggerFactory.getLogger(this.getClass());
+
+ private static final String SADIS_INTEGRATION = "integration";
+ private static final String SADIS_CACHE = "cache";
+ private static final String SADIS_CACHE_ENABLED = "enabled";
+ private static final String SADIS_CACHE_SIZE = "maxsize";
+ private static final String SADIS_CACHE_TTL = "ttl";
+ private static final String SADIS_URL = "url";
+ private static final String SADIS_ENTRIES = "entries";
+ private static final String DEFAULT_CACHE_TTL = "PT0S";
+
+ /**
+ * Returns SADIS integration URL.
+ *
+ * @return configured URL or null
+ * @throws MalformedURLException
+ * specified URL not valid
+ */
+ public URL getUrl() throws MalformedURLException {
+ final JsonNode integration = this.object.path(SADIS_INTEGRATION);
+ if (integration.isMissingNode()) {
+ return null;
+ }
+
+ final JsonNode url = integration.path(SADIS_URL);
+ if (url.isMissingNode()) {
+ return null;
+ }
+
+ return new URL(url.asText());
+ }
+
+ /**
+ * Returns configuration if cache should be used or not. Only used if
+ * integration URL is set.
+ *
+ * @return if cache should be used
+ */
+ public Boolean getCacheEnabled() {
+ final JsonNode integration = this.object.path(SADIS_INTEGRATION);
+ if (integration.isMissingNode()) {
+ return false;
+ }
+
+ final JsonNode cache = integration.path(SADIS_CACHE);
+ if (cache.isMissingNode()) {
+ return false;
+ }
+
+ return cache.path(SADIS_CACHE_ENABLED).asBoolean(false);
+ }
+
+ public int getCacheMaxSize() {
+ final JsonNode integration = this.object.path(SADIS_INTEGRATION);
+ if (integration.isMissingNode()) {
+ return -1;
+ }
+
+ final JsonNode cache = integration.path(SADIS_CACHE);
+ if (cache.isMissingNode()) {
+ return -1;
+ }
+
+ return cache.path(SADIS_CACHE_SIZE).asInt(-1);
+ }
+
+ public Duration getCacheTtl() {
+ final JsonNode integration = this.object.path(SADIS_INTEGRATION);
+ if (integration.isMissingNode()) {
+ return Duration.parse(DEFAULT_CACHE_TTL);
+ }
+
+ final JsonNode cache = integration.path(SADIS_CACHE);
+ if (cache.isMissingNode()) {
+ return Duration.parse(DEFAULT_CACHE_TTL);
+ }
+
+ return Duration.parse(cache.path(SADIS_CACHE_TTL).asText(DEFAULT_CACHE_TTL));
+ }
+
+ public List<SubscriberAndDeviceInformation> getEntries() {
+ List<SubscriberAndDeviceInformation> result = new ArrayList<SubscriberAndDeviceInformation>();
+ ObjectMapper mapper = new ObjectMapper();
+ SimpleModule module = new SimpleModule();
+ module.addDeserializer(VlanId.class, new VlanIdDeserializer());
+ module.addDeserializer(Ip4Address.class, new Ip4AddressDeserializer());
+ mapper.registerModule(module);
+ final JsonNode entries = this.object.path(SADIS_ENTRIES);
+ entries.forEach(entry -> {
+ try {
+ result.add(mapper.readValue(entry.toString(), SubscriberAndDeviceInformation.class));
+ } catch (IOException e) {
+ log.warn("Unable to parse configuration entry, '{}', error: {}", entry.toString(), e.getMessage());
+ }
+ });
+
+ return result;
+ }
+
+ public class VlanIdDeserializer extends JsonDeserializer<VlanId> {
+ @Override
+ public VlanId deserialize(JsonParser jp, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ ObjectCodec oc = jp.getCodec();
+ JsonNode node = oc.readTree(jp);
+ return VlanId.vlanId((short) node.asInt());
+ }
+ }
+
+ public class Ip4AddressDeserializer extends JsonDeserializer<Ip4Address> {
+ @Override
+ public Ip4Address deserialize(JsonParser jp, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ ObjectCodec oc = jp.getCodec();
+ JsonNode node = oc.readTree(jp);
+ return Ip4Address.valueOf((String) node.asText());
+ }
+ }
+}
diff --git a/app/src/main/java/org/opencord/sadis/impl/SadisManager.java b/app/src/main/java/org/opencord/sadis/impl/SadisManager.java
new file mode 100644
index 0000000..713b8b0
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/impl/SadisManager.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2017-present 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.opencord.sadis.impl;
+
+import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
+
+import java.util.Set;
+
+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.apache.felix.scr.annotations.Service;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * Subscriber And Device Information Service application component. Component
+ * that manages the integration of ONOS into a deployment providing a bridge
+ * between ONOS and deployment specific information about subscribers and access
+ * devices.
+ */
+@Service
+@Component(immediate = true)
+public class SadisManager extends SubscriberAndDeviceInformationAdapter {
+ private final Logger log = LoggerFactory.getLogger(this.getClass());
+
+ private static final String SADIS_APP = "org.opencord.sadis";
+ private ApplicationId appId;
+ private final InternalConfigListener cfgListener = new InternalConfigListener();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigRegistry cfgService;
+
+ @SuppressWarnings("rawtypes")
+ private final Set<ConfigFactory> factories = ImmutableSet
+ .of(new ConfigFactory<ApplicationId, SadisConfig>(APP_SUBJECT_FACTORY, SadisConfig.class, "sadis") {
+ @Override
+ public SadisConfig createConfig() {
+ return new SadisConfig();
+ }
+ });
+
+ /**
+ * Initialize the SADIS ONOS application.
+ */
+ @Activate
+ protected void activate() {
+
+ this.appId = this.coreService.registerApplication(SADIS_APP);
+ this.cfgService.addListener(this.cfgListener);
+ this.factories.forEach(this.cfgService::registerConfigFactory);
+ this.updateConfig();
+
+ this.log.info("Started");
+ }
+
+ /**
+ * Cleans up resources utilized by the SADIS ONOS application.
+ */
+ @Deactivate
+ protected void deactivate() {
+ this.log.info("Stopped");
+ }
+
+ /**
+ * Validates the configuration and updates any operational settings that are
+ * affected by configuration changes.
+ */
+ private void updateConfig() {
+ final SadisConfig cfg = this.cfgService.getConfig(this.appId, SadisConfig.class);
+ if (cfg == null) {
+ this.log.warn("Subscriber And Device Information Service (SADIS) configuration not available");
+ return;
+ }
+ this.log.info("Cache Enabled: {}", cfg.getCacheEnabled());
+ this.log.info("Cache Mac Size: {}", cfg.getCacheMaxSize());
+ this.log.info("Cache TTL: {}", cfg.getCacheTtl().getSeconds());
+ this.log.info("Entries: {}", cfg.getEntries());
+
+ configure(cfg);
+ }
+
+ /**
+ * Listener for SADIS configuration events.
+ */
+ private class InternalConfigListener implements NetworkConfigListener {
+
+ @Override
+ public void event(final NetworkConfigEvent event) {
+
+ if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED
+ || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)
+ && event.configClass().equals(SadisConfig.class)) {
+ SadisManager.this.updateConfig();
+ SadisManager.this.log.info("Reconfigured");
+ }
+ }
+ }
+}
diff --git a/app/src/main/java/org/opencord/sadis/impl/SubscriberAndDeviceInformationAdapter.java b/app/src/main/java/org/opencord/sadis/impl/SubscriberAndDeviceInformationAdapter.java
new file mode 100644
index 0000000..7e063f7
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/impl/SubscriberAndDeviceInformationAdapter.java
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2017-present 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.opencord.sadis.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import org.opencord.sadis.SubscriberAndDeviceInformation;
+import org.opencord.sadis.SubscriberAndDeviceInformationService;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.collect.Maps;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService {
+
+ private final Logger log = LoggerFactory.getLogger(this.getClass());
+
+ private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0;
+ private static final long DEFAULT_TTL = 0;
+
+ private String url;
+ private Cache<String, SubscriberAndDeviceInformation> cache;
+ private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
+ private long cacheEntryTtl = DEFAULT_TTL;
+
+ private Map<String, SubscriberAndDeviceInformation> localCfgData = null;
+
+ public SubscriberAndDeviceInformationAdapter() {
+ cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize)
+ .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
+ }
+
+ /**
+ * Configures the Adapter for data source and cache parameters.
+ *
+ * @param cfg Configuration data.
+ */
+ public void configure(SadisConfig cfg) {
+ String url = null;
+ try {
+ // if the url is not present then assume data is in netcfg
+ if (cfg.getUrl() != null) {
+ url = cfg.getUrl().toString();
+ } else {
+ localCfgData = Maps.newConcurrentMap();
+
+ cfg.getEntries().forEach(entry -> {
+ localCfgData.put(entry.id(), entry);
+ });
+ log.info("url is null, data source is local netcfg data");
+ }
+ } catch (MalformedURLException mUrlEx) {
+ log.error("Invalid URL specified: {}", mUrlEx);
+ }
+
+ int maximumCacheSeize = cfg.getCacheMaxSize();
+ long cacheEntryTtl = cfg.getCacheTtl().getSeconds();
+
+ // Rebuild cache if needed
+ if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize ||
+ cacheEntryTtl != this.cacheEntryTtl) {
+ this.maxiumCacheSize = maximumCacheSeize;
+ this.cacheEntryTtl = cacheEntryTtl;
+ this.url = url;
+
+ Cache<String, SubscriberAndDeviceInformation> newCache = CacheBuilder.newBuilder()
+ .maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
+ Cache<String, SubscriberAndDeviceInformation> oldCache = cache;
+
+ synchronized (this) {
+ cache = newCache;
+ }
+
+ oldCache.invalidateAll();
+ oldCache.cleanUp();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.opencord.sadis.SubscriberAndDeviceInformationService#clearCache()
+ */
+ @Override
+ public void invalidateAll() {
+ cache.invalidateAll();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.opencord.sadis.SubscriberAndDeviceInformationService#get(java.lang.
+ * String)
+ */
+ @Override
+ public SubscriberAndDeviceInformation get(String id) {
+ Cache<String, SubscriberAndDeviceInformation> local;
+ synchronized (this) {
+ local = cache;
+ }
+
+ SubscriberAndDeviceInformation info = local.getIfPresent(id);
+ if (info != null) {
+ return info;
+ }
+
+ /*
+ * Not in cache, if we have a URL configured we can attempt to get it
+ * from there, else check for it in the locally configured data
+ */
+ if (this.url == null) {
+ info = localCfgData.get(id);
+
+ if (info != null) {
+ local.put(id, info);
+ return info;
+ }
+ } else {
+ // Augment URL with query parameters
+ StringBuilder buf = new StringBuilder(this.url);
+ if (buf.charAt(buf.length() - 1) != '/') {
+ buf.append('/');
+ }
+
+ buf.append(id);
+
+ try (InputStream io = new URL(buf.toString()).openStream()) {
+ ObjectMapper mapper = new ObjectMapper();
+ info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
+ local.put(id, info);
+ return info;
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ log.error("Data not found for id {}", id);
+ return null;
+ }
+}
diff --git a/app/src/main/java/org/opencord/sadis/impl/package-info.java b/app/src/main/java/org/opencord/sadis/impl/package-info.java
new file mode 100644
index 0000000..cef1f21
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/impl/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2017-present 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.
+ */
+/**
+ * The purpose of this service is to define an optional service that provides a bridge
+ * to the customer's infrastructure to query / cache subsciber/access information
+ * and make it available to other services / applications within the ONOS/CORD
+ * infrastructure.
+ */
+package org.opencord.sadis;
diff --git a/app/src/main/java/org/opencord/sadis/rest/AppWebApplication.java b/app/src/main/java/org/opencord/sadis/rest/AppWebApplication.java
new file mode 100644
index 0000000..25c3683
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/rest/AppWebApplication.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017-present 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.opencord.sadis.rest;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * Subscriber And Device Infomration Service REST API web application.
+ */
+public class AppWebApplication extends AbstractWebApplication {
+ @Override
+ public Set<Class<?>> getClasses() {
+ return getClasses(AppWebResource.class);
+ }
+}
diff --git a/app/src/main/java/org/opencord/sadis/rest/AppWebResource.java b/app/src/main/java/org/opencord/sadis/rest/AppWebResource.java
new file mode 100644
index 0000000..41c2819
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/rest/AppWebResource.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2017-present 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.opencord.sadis.rest;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.rest.AbstractWebResource;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+/**
+ * Subscriber And Device Information Service web resource.
+ */
+@Path("sadis")
+public class AppWebResource extends AbstractWebResource {
+
+ /**
+ * Get subscriber object.
+ *
+ * @param id
+ * ID of the subscriber
+ *
+ * @return 200 OK
+ */
+ @GET
+ @Path("/subscriber/{id}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getSubscriber(@PathParam("id") String id) {
+ ObjectNode node = mapper().createObjectNode().put("hello", "world");
+ return ok(node).build();
+ }
+
+ /**
+ * Update subscriber object.
+ *
+ * @param id
+ * ID of the subscriber
+ *
+ * @return 200 OK
+ */
+ @PUT
+ @Path("/subscriber/{id}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response putSubscriber(@PathParam("id") String id) {
+ return Response.ok().build();
+ }
+
+ /**
+ * Create subscriber object.
+ *
+ * @return 201 Created
+ */
+ @POST
+ @Path("/subscriber")
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response postSubscriber() {
+ try {
+ return Response.created(new URI("/subsciber/123")).build();
+ } catch (URISyntaxException e) {
+ return Response.serverError().build();
+ }
+ }
+
+ /**
+ * Delete subscriber object.
+ *
+ * @param id
+ * ID of the subscriber
+ * @return 204 NoContent
+ */
+ @DELETE
+ @Path("/subscriber/{id}")
+ public Response deleteSubscriber(@PathParam("id") String id) {
+ return Response.noContent().build();
+ }
+}
diff --git a/app/src/main/java/org/opencord/sadis/rest/package-info.java b/app/src/main/java/org/opencord/sadis/rest/package-info.java
new file mode 100755
index 0000000..4a0c20e
--- /dev/null
+++ b/app/src/main/java/org/opencord/sadis/rest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present 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.
+ */
+
+/**
+ * REST APIs for the SADIS application.
+ */
+package org.opencord.sadis.rest;
diff --git a/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..4f3126c
--- /dev/null
+++ b/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,24 @@
+<!--
+ ~ Copyright 2017-present 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.
+ -->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+ <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+ <command>
+ <action class="org.opencord.sadis.cli.SubscriberGetCommand"/>
+ </command>
+ </command-bundle>
+
+</blueprint>
diff --git a/app/src/main/resources/config.json b/app/src/main/resources/config.json
new file mode 100644
index 0000000..eff6202
--- /dev/null
+++ b/app/src/main/resources/config.json
@@ -0,0 +1,46 @@
+{
+ "integration":
+ {
+ "url": "http://localhost:8090",
+ "cache":
+ {
+ "enabled": true,
+ "maxsize": 50,
+ "ttl": "PT1m"
+ }
+ },
+
+ "entries":
+ [
+ {
+ "id": "1",
+ "cTag": 2,
+ "sTag": 2,
+ "nasPortId": "1/1/2",
+ "port": 125,
+ "slot": 3,
+ "hardwareIdentifier": "aa:bb:cc:dd:ee:ff",
+ "ipAddress":"10.10.10.10",
+ "nasId":"XXX-NASID"
+ },
+
+ {
+ "id": "2",
+ "cTag": 4,
+ "sTag": 4,
+ "nasPortId": "1/1/2",
+ "port": 129,
+ "slot": 4,
+ "hardwareIdentifier": "aa:bb:cc:dd:ee:ff",
+ "ipAddress":"1.1.1.1",
+ "nasId":"YYY-NASID"
+ },
+
+ {
+ "id": "cc:dd:ee:ff:aa:bb",
+ "hardwareIdentifier": "cc:dd:ee:ff:aa:bb",
+ "ipAddress":"12.12.12.12",
+ "nasId":"CCC-NASID"
+ }
+ ]
+}
diff --git a/app/src/main/webapp/WEB-INF/web.xml b/app/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..1319998
--- /dev/null
+++ b/app/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present 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.
+ -->
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ id="ONOS" version="2.5">
+ <display-name>Sample App REST API v1.0</display-name>
+
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>Secured</web-resource-name>
+ <url-pattern>/*</url-pattern>
+ </web-resource-collection>
+ <auth-constraint>
+ <role-name>admin</role-name>
+ </auth-constraint>
+ </security-constraint>
+
+ <security-role>
+ <role-name>admin</role-name>
+ </security-role>
+
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ <realm-name>karaf</realm-name>
+ </login-config>
+
+ <servlet>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
+ <init-param>
+ <param-name>javax.ws.rs.Application</param-name>
+ <param-value>org.opencord.sadis.rest.AppWebApplication</param-value>
+ </init-param>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+</web-app>
diff --git a/app/src/test/java/org/opencord/sadis/impl/SadisManagerTest.java b/app/src/test/java/org/opencord/sadis/impl/SadisManagerTest.java
new file mode 100644
index 0000000..4b89b40
--- /dev/null
+++ b/app/src/test/java/org/opencord/sadis/impl/SadisManagerTest.java
@@ -0,0 +1,293 @@
+/*
+ * Copyright 2017-present 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.opencord.sadis.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.InputStream;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigApplyDelegate;
+import org.onosproject.net.config.NetworkConfigRegistryAdapter;
+
+import org.opencord.sadis.SubscriberAndDeviceInformation;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * Set of tests of the SADIS ONOS application component.
+ */
+public class SadisManagerTest {
+
+ private SadisManager sadis;
+
+ @Before
+ public void setUp() throws Exception {
+ this.sadis = new SadisManager();
+ this.sadis.coreService = new MockCoreService();
+
+ final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream("/config.json");
+
+ final ObjectMapper mapper = new ObjectMapper();
+ final JsonNode testConfig = mapper.readTree(jsonStream);
+ final ConfigApplyDelegate delegate = new MockConfigDelegate();
+
+ final SadisConfig config = new SadisConfig();
+ final ApplicationId subject = this.sadis.coreService.registerApplication("org.opencord.sadis");
+
+ config.init(subject, "sadis-test", testConfig, mapper, delegate);
+
+ this.sadis.cfgService = new MockNetworkConfigRegistry(config);
+ this.sadis.activate();
+ }
+
+ @After
+ public void tearDown() {
+ this.sadis.deactivate();
+ }
+
+ @Test
+ public void testConfiguration() {
+ SadisConfig config = sadis.cfgService.getConfig(null, SadisConfig.class);
+ assertEquals(true, config.getCacheEnabled());
+ assertEquals(50, config.getCacheMaxSize());
+ assertEquals(Duration.parse("PT1m"), config.getCacheTtl());
+ List<SubscriberAndDeviceInformation> entries = config.getEntries();
+ assertEquals(3, entries.size());
+ assertTrue(SubscriberAndDeviceInformationBuilder.build("1", (short) 2, (short) 2, "1/1/2", (short) 125,
+ (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10").checkEquals(entries.get(0)));
+ assertTrue(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129,
+ (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1").checkEquals(entries.get(1)));
+ assertTrue(SubscriberAndDeviceInformationBuilder.build("cc:dd:ee:ff:aa:bb", (short) -1, (short) -1, null,
+ (short) -1,
+ (short) -1, "cc:dd:ee:ff:aa:bb", "CCC-NASID", "12.12.12.12").checkEquals(entries.get(2)));
+
+ }
+
+ // Mocks live here
+
+ private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
+
+ public static SubscriberAndDeviceInformationBuilder build(String id, short cTag, short sTag, String nasPortId,
+ short port, short slot, String mac, String nasId, String ipAddress) {
+ //SubscriberAndDeviceInformation info = new SubscriberAndDeviceInformation();
+ SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder();
+ info.setId(id);
+ if (cTag != -1) {
+ info.setCTag(VlanId.vlanId(cTag));
+ }
+ if (sTag != -1) {
+ info.setSTag(VlanId.vlanId(sTag));
+ }
+ info.setNasPortId(nasPortId);
+ if (port != -1) {
+ info.setPort(port);
+ }
+ if (slot != -1) {
+ info.setSlot(slot);
+ }
+ info.setHardwareIdentifier(MacAddress.valueOf(mac));
+ info.setIPAddress(Ip4Address.valueOf(ipAddress));
+ info.setNasId(nasId);
+ return info;
+ }
+
+ public boolean checkEquals(SubscriberAndDeviceInformation other) {
+ if (other == null) {
+ return false;
+ }
+ if (this.cTag() == null) {
+ if (other.cTag() != null) {
+ return false;
+ }
+ } else if (!this.cTag().equals(other.cTag())) {
+ return false;
+ }
+ if (this.hardwareIdentifier() == null) {
+ if (other.hardwareIdentifier() != null) {
+ return false;
+ }
+ } else if (!this.hardwareIdentifier().equals(other.hardwareIdentifier())) {
+ return false;
+ }
+ if (this.id() == null) {
+ if (other.id() != null) {
+ return false;
+ }
+ } else if (!this.id().equals(other.id())) {
+ return false;
+ }
+ if (this.nasPortId() == null) {
+ if (other.nasPortId() != null) {
+ return false;
+ }
+ } else if (!this.nasPortId().equals(other.nasPortId())) {
+ return false;
+ }
+ if (this.nasId() == null) {
+ if (other.nasId() != null) {
+ return false;
+ }
+ } else if (!this.nasId().equals(other.nasId())) {
+ return false;
+ }
+ if (this.ipAddress() == null) {
+ if (other.ipAddress() != null) {
+ return false;
+ }
+ } else if (!this.ipAddress().equals(other.ipAddress())) {
+ return false;
+ }
+ if (this.port() != other.port()) {
+ return false;
+ }
+ if (this.sTag() == null) {
+ if (other.sTag() != null) {
+ return false;
+ }
+ } else if (!this.sTag().equals(other.sTag())) {
+ return false;
+ }
+ if (this.slot() != other.slot()) {
+ return false;
+ }
+ return true;
+ }
+ }
+
+ /**
+ * Mocks an ONOS configuration delegate to allow JSON based configuration to
+ * be tested.
+ */
+ private static final class MockConfigDelegate implements ConfigApplyDelegate {
+ @Override
+ public void onApply(@SuppressWarnings("rawtypes") Config config) {
+ config.apply();
+ }
+ }
+
+ /**
+ * Mocks an instance of {@link ApplicationId} so that the application
+ * component under test can query and use its application ID.
+ */
+ private static final class MockApplicationId implements ApplicationId {
+
+ private final short id;
+ private final String name;
+
+ public MockApplicationId(short id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ @Override
+ public short id() {
+ return id;
+ }
+
+ @Override
+ public String name() {
+ return name;
+ }
+ }
+
+ /**
+ * Mocks the core services of ONOS so that the application under test can
+ * register and query application IDs.
+ */
+ private static final class MockCoreService extends CoreServiceAdapter {
+
+ private List<ApplicationId> idList = new ArrayList<ApplicationId>();
+ private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
+ */
+ @Override
+ public ApplicationId getAppId(Short id) {
+ if (id >= idList.size()) {
+ return null;
+ }
+ return idList.get(id);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
+ */
+ @Override
+ public ApplicationId getAppId(String name) {
+ return idMap.get(name);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
+ * .String)
+ */
+ @Override
+ public ApplicationId registerApplication(String name) {
+ ApplicationId appId = idMap.get(name);
+ if (appId == null) {
+ appId = new MockApplicationId((short) idList.size(), name);
+ idList.add(appId);
+ idMap.put(name, appId);
+ }
+ return appId;
+ }
+
+ }
+
+ /**
+ * Mocks the ONOS network configuration registry so that the application
+ * under test can access a JSON defined configuration.
+ */
+ static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
+ private final SadisConfig config;
+
+ public MockNetworkConfigRegistry(final SadisConfig config) {
+ this.config = config;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
+ return (C) this.config;
+ }
+ }
+}