ONOS-2488 Finished moving network config stuff out of the incubator area.
Change-Id: I62c511938fdf8f33def99ce43f0d4417b4ba1918
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/AllowedEntityConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/AllowedEntityConfig.java
new file mode 100644
index 0000000..6e6663c
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/AllowedEntityConfig.java
@@ -0,0 +1,49 @@
+/*
+ * 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.net.config.basics;
+
+import org.onosproject.net.config.Config;
+
+/**
+ * Base abstraction for network entities for which admission into control
+ * domain can be selectively configured, e.g. devices, end-stations, links
+ */
+public abstract class AllowedEntityConfig<S> extends Config<S> {
+
+ private static final String ALLOWED = "allowed";
+
+ /**
+ * Indicates whether the element is allowed for admission into the control
+ * domain.
+ *
+ * @return true if element is allowed
+ */
+ public boolean isAllowed() {
+ return get(ALLOWED, true);
+ }
+
+ /**
+ * Specifies whether the element is to be allowed for admission into the
+ * control domain.
+ *
+ * @param isAllowed true to allow; false to forbid; null to clear
+ * @return self
+ */
+ public AllowedEntityConfig isAllowed(Boolean isAllowed) {
+ return (AllowedEntityConfig) setOrClear(ALLOWED, isAllowed);
+ }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
new file mode 100644
index 0000000..fd8bfa3
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
@@ -0,0 +1,70 @@
+/*
+ * 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.net.config.basics;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+
+/**
+ * Basic configuration for network infrastructure devices.
+ */
+public class BasicDeviceConfig extends BasicElementConfig<DeviceId> {
+
+ public static final String TYPE = "type";
+ public static final String DRIVER = "driver";
+
+ /**
+ * Returns the device type.
+ *
+ * @return device type override
+ */
+ public Device.Type type() {
+ return get(TYPE, Device.Type.SWITCH, Device.Type.class);
+ }
+
+ /**
+ * Sets the device type.
+ *
+ * @param type device type override
+ * @return self
+ */
+ public BasicDeviceConfig type(Device.Type type) {
+ return (BasicDeviceConfig) setOrClear(TYPE, type);
+ }
+
+ /**
+ * Returns the device driver name.
+ *
+ * @return driver name of null if not set
+ */
+ public String driver() {
+ return get(DRIVER, subject.toString());
+ }
+
+ /**
+ * Sets the driver name.
+ *
+ * @param driverName new driver name; null to clear
+ * @return self
+ */
+ public BasicElementConfig driver(String driverName) {
+ return (BasicElementConfig) setOrClear(DRIVER, driverName);
+ }
+
+ // TODO: device port meta-data to be configured via BasicPortsConfig
+ // TODO: device credentials/keys
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
new file mode 100644
index 0000000..7b3248c
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
@@ -0,0 +1,130 @@
+/*
+ * 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.net.config.basics;
+
+/**
+ * Basic configuration for network elements, e.g. devices, hosts. Such elements
+ * can have a friendly name, geo-coordinates, logical rack coordinates and
+ * an owner entity.
+ */
+public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> {
+
+ public static final String NAME = "name";
+
+ public static final String LATITUDE = "latitude";
+ public static final String LONGITUDE = "longitude";
+
+ public static final String RACK_ADDRESS = "rackAddress";
+ public static final String OWNER = "owner";
+
+ protected static final double DEFAULT_COORD = -1.0;
+
+ /**
+ * Returns friendly label for the element.
+ *
+ * @return friendly label or element id itself if not set
+ */
+ public String name() {
+ return get(NAME, subject.toString());
+ }
+
+ /**
+ * Sets friendly label for the element.
+ *
+ * @param name new friendly label; null to clear
+ * @return self
+ */
+ public BasicElementConfig name(String name) {
+ return (BasicElementConfig) setOrClear(NAME, name);
+ }
+
+ /**
+ * Returns element latitude.
+ *
+ * @return element latitude; -1 if not set
+ */
+ public double latitude() {
+ return get(LATITUDE, DEFAULT_COORD);
+ }
+
+ /**
+ * Sets the element latitude.
+ *
+ * @param latitude new latitude; null to clear
+ * @return self
+ */
+ public BasicElementConfig latitude(Double latitude) {
+ return (BasicElementConfig) setOrClear(LATITUDE, latitude);
+ }
+
+ /**
+ * Returns element latitude.
+ *
+ * @return element latitude; -1 if not set
+ */
+ public double longitude() {
+ return get(LONGITUDE, DEFAULT_COORD);
+ }
+
+ /**
+ * Sets the element longitude.
+ *
+ * @param longitude new longitude; null to clear
+ * @return self
+ */
+ public BasicElementConfig longitude(Double longitude) {
+ return (BasicElementConfig) setOrClear(LONGITUDE, longitude);
+ }
+
+ /**
+ * Returns the element rack address.
+ *
+ * @return rack address; null if not set
+ */
+ public String rackAddress() {
+ return get(RACK_ADDRESS, null);
+ }
+
+ /**
+ * Sets element rack address.
+ *
+ * @param address new rack address; null to clear
+ * @return self
+ */
+ public BasicElementConfig rackAddress(String address) {
+ return (BasicElementConfig) setOrClear(RACK_ADDRESS, address);
+ }
+
+ /**
+ * Returns owner of the element.
+ *
+ * @return owner or null if not set
+ */
+ public String owner() {
+ return get(OWNER, null);
+ }
+
+ /**
+ * Sets the owner of the element.
+ *
+ * @param owner new owner; null to clear
+ * @return self
+ */
+ public BasicElementConfig owner(String owner) {
+ return (BasicElementConfig) setOrClear(OWNER, owner);
+ }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
new file mode 100644
index 0000000..2fe2b2c
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
@@ -0,0 +1,27 @@
+/*
+ * 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.net.config.basics;
+
+import org.onosproject.net.HostId;
+
+/**
+ * Basic configuration for network end-station hosts.
+ */
+public class BasicHostConfig extends BasicElementConfig<HostId> {
+
+ // TODO: determine what aspects of configuration to add for hosts
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicLinkConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicLinkConfig.java
new file mode 100644
index 0000000..b6068ee
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicLinkConfig.java
@@ -0,0 +1,90 @@
+/*
+ * 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.net.config.basics;
+
+import org.onosproject.net.Link;
+import org.onosproject.net.LinkKey;
+
+import java.time.Duration;
+
+/**
+ * Basic configuration for network infrastructure link.
+ */
+public class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
+
+ public static final String TYPE = "type";
+ public static final String LATENCY = "latency";
+ public static final String BANDWIDTH = "bandwidth";
+
+ /**
+ * Returns the link type.
+ *
+ * @return link type override
+ */
+ public Link.Type type() {
+ return get(TYPE, Link.Type.DIRECT, Link.Type.class);
+ }
+
+ /**
+ * Sets the link type.
+ *
+ * @param type link type override
+ * @return self
+ */
+ public BasicLinkConfig type(Link.Type type) {
+ return (BasicLinkConfig) setOrClear(TYPE, type);
+ }
+
+ /**
+ * Returns link latency in terms of nanos.
+ *
+ * @return link latency; -1 if not set
+ */
+ public Duration latency() {
+ return Duration.ofNanos(get(LATENCY, -1));
+ }
+
+ /**
+ * Sets the link latency.
+ *
+ * @param latency new latency; null to clear
+ * @return self
+ */
+ public BasicLinkConfig latency(Duration latency) {
+ Long nanos = latency == null ? null : latency.toNanos();
+ return (BasicLinkConfig) setOrClear(LATENCY, nanos);
+ }
+
+ /**
+ * Returns link bandwidth in terms of Mbps.
+ *
+ * @return link bandwidth; -1 if not set
+ */
+ public long bandwidth() {
+ return get(BANDWIDTH, -1);
+ }
+
+ /**
+ * Sets the link bandwidth.
+ *
+ * @param bandwidth new bandwidth; null to clear
+ * @return self
+ */
+ public BasicLinkConfig bandwidth(Long bandwidth) {
+ return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
+ }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/OpticalPortConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/OpticalPortConfig.java
new file mode 100644
index 0000000..4478ead
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/OpticalPortConfig.java
@@ -0,0 +1,147 @@
+package org.onosproject.net.config.basics;
+
+import java.util.Optional;
+
+import org.onosproject.net.config.Config;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Port;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+
+/**
+ * Configurations for an optical port on a device.
+ */
+public class OpticalPortConfig extends Config<ConnectPoint> {
+ // optical type {OMS, OCH, ODUClt, fiber}
+ public static final String TYPE = "type";
+
+ // port name. "name" is the alphanumeric name of the port, but "port" refers
+ // to the port number used as a name string (i.e., for ports without
+ // alphanumeric names).
+ public static final String NAME = "name";
+ public static final String PORT = "port";
+ public static final String STATIC_PORT = "staticPort";
+ public static final String STATIC_LAMBDA = "staticLambda";
+
+ /**
+ * Returns the Enum value representing the type of port.
+ *
+ * @return the port type, or null if invalid or unset
+ */
+ public Port.Type type() {
+ JsonNode type = node.path(TYPE);
+ if (type.isMissingNode()) {
+ return null;
+ }
+ return Port.Type.valueOf(type.asText());
+ }
+
+ /**
+ * Returns the port name associated with this port configuration. The Name
+ * is an alphanumeric string.
+ *
+ * @return the name of this port, else, an empty string
+ */
+ public String name() {
+ return getStringValue(NAME);
+ }
+
+ /**
+ * Returns a stringified representation of the port number, configured in
+ * some port types without an alphanumeric name as the port name.
+ *
+ * @return A string representation of the port number
+ */
+ public String numberName() {
+ return getStringValue(PORT);
+ }
+
+ /**
+ * Returns the string-representation of name of the output port. This is
+ * usually an OMS port for an OCH input ports, or an OCH port for ODU input
+ * ports.
+ *
+ * @return the name of this port, else, an empty string
+ */
+ public String staticPort() {
+ return getStringValue(STATIC_PORT);
+ }
+
+ private String getStringValue(String field) {
+ JsonNode name = node.path(field);
+ return name.isMissingNode() ? "" : name.asText();
+ }
+
+ /**
+ * Returns the output lambda configured for this port. The lambda value is
+ * expressed as a frequency value.
+ *
+ * @return an Optional that may contain a frequency value.
+ */
+ public Optional<Long> staticLambda() {
+ JsonNode sl = node.path(STATIC_LAMBDA);
+ if (sl.isMissingNode()) {
+ return Optional.empty();
+ }
+ return Optional.of(sl.asLong());
+ }
+
+ /**
+ * Sets the port type, or updates it if it's already set. A null argument removes
+ * this field.
+ *
+ * @param type the port type
+ * @return this OpticalPortConfig instance
+ */
+ public OpticalPortConfig portType(Port.Type type) {
+ // if unspecified, ideally fall back on FIBER or PACKET.
+ String pt = (type == null) ? null : type.toString();
+ return (OpticalPortConfig) setOrClear(TYPE, pt);
+ }
+
+ /**
+ * Sets the port name, or updates it if already set. A null argument removes
+ * this field.
+ *
+ * @param name the port's name
+ * @return this OpticalPortConfig instance
+ */
+ public OpticalPortConfig portName(String name) {
+ return (OpticalPortConfig) setOrClear(NAME, name);
+ }
+
+ /**
+ * Sets the port name from port number, or updates it if already set. A null
+ * argument removes this field.
+ *
+ * @param name the port number, to be used as name
+ * @return this OpticalPortConfig instance
+ */
+ public OpticalPortConfig portNumberName(Long name) {
+ return (OpticalPortConfig) setOrClear(PORT, name);
+ }
+
+ /**
+ * Sets the output port name, or updates it if already set. A null argument
+ * removes this field.
+ *
+ * @param name the output port's name
+ * @return this OpticalPortConfig instance
+ */
+ public OpticalPortConfig staticPort(String name) {
+ return (OpticalPortConfig) setOrClear(STATIC_PORT, name);
+ }
+
+ /**
+ * Sets the output lambda index, or updates it if already set. A null argument
+ * removes this field.
+ *
+ * @param index the output lambda
+ * @return this OpticalPortConfig instance
+ */
+ public OpticalPortConfig staticLambda(Long index) {
+ return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index);
+ }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java b/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
new file mode 100644
index 0000000..884f2e2
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/SubjectFactories.java
@@ -0,0 +1,93 @@
+/*
+ * 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.net.config.basics;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.LinkKey;
+import org.onosproject.net.config.SubjectFactory;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Set of subject factories for potential configuration subjects.
+ */
+public final class SubjectFactories {
+
+ // Construction forbidden
+ private SubjectFactories() {
+ }
+
+ // Required for resolving application identifiers
+ private static CoreService coreService;
+
+ public static final SubjectFactory<ApplicationId> APP_SUBJECT_FACTORY =
+ new SubjectFactory<ApplicationId>(ApplicationId.class, "apps") {
+ @Override
+ public ApplicationId createSubject(String key) {
+ return coreService.registerApplication(key);
+ }
+ };
+
+ public static final SubjectFactory<DeviceId> DEVICE_SUBJECT_FACTORY =
+ new SubjectFactory<DeviceId>(DeviceId.class, "devices") {
+ @Override
+ public DeviceId createSubject(String key) {
+ return DeviceId.deviceId(key);
+ }
+ };
+
+ public static final SubjectFactory<ConnectPoint> CONNECT_POINT_SUBJECT_FACTORY =
+ new SubjectFactory<ConnectPoint>(ConnectPoint.class, "ports") {
+ @Override
+ public ConnectPoint createSubject(String key) {
+ return ConnectPoint.deviceConnectPoint(key);
+ }
+ };
+
+ public static final SubjectFactory<HostId> HOST_SUBJECT_FACTORY =
+ new SubjectFactory<HostId>(HostId.class, "hosts") {
+ @Override
+ public HostId createSubject(String key) {
+ return HostId.hostId(key);
+ }
+ };
+
+ public static final SubjectFactory<LinkKey> LINK_SUBJECT_FACTORY =
+ new SubjectFactory<LinkKey>(LinkKey.class, "links") {
+ @Override
+ public LinkKey createSubject(String key) {
+ String[] cps = key.split("-");
+ checkArgument(cps.length == 2, "Incorrect link key format: %s", key);
+ return LinkKey.linkKey(ConnectPoint.deviceConnectPoint(cps[0]),
+ ConnectPoint.deviceConnectPoint(cps[1]));
+ }
+ };
+
+ /**
+ * Provides reference to the core service, which is required for
+ * application subject factory.
+ *
+ * @param service core service reference
+ */
+ public static void setCoreService(CoreService service) {
+ coreService = service;
+ }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/package-info.java b/core/api/src/main/java/org/onosproject/net/config/basics/package-info.java
new file mode 100644
index 0000000..4d0f27e
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/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.
+ */
+
+/**
+ * Various basic builtin network configurations.
+ */
+package org.onosproject.net.config.basics;
\ No newline at end of file
diff --git a/core/net/pom.xml b/core/net/pom.xml
index dfd5f21..4ba04c5 100644
--- a/core/net/pom.xml
+++ b/core/net/pom.xml
@@ -60,6 +60,13 @@
<dependency>
<groupId>org.onosproject</groupId>
+ <artifactId>onos-core-dist</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
<artifactId>onos-incubator-api</artifactId>
<scope>test</scope>
<classifier>tests</classifier>
diff --git a/core/net/src/main/java/org/onosproject/net/config/impl/BasicNetworkConfigs.java b/core/net/src/main/java/org/onosproject/net/config/impl/BasicNetworkConfigs.java
new file mode 100644
index 0000000..ec0b124
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/config/impl/BasicNetworkConfigs.java
@@ -0,0 +1,114 @@
+/*
+ * 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.net.config.impl;
+
+import com.google.common.collect.ImmutableSet;
+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.onosproject.core.CoreService;
+import org.onosproject.incubator.net.config.basics.InterfaceConfig;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.LinkKey;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.basics.BasicDeviceConfig;
+import org.onosproject.net.config.basics.BasicHostConfig;
+import org.onosproject.net.config.basics.BasicLinkConfig;
+import org.onosproject.net.config.basics.OpticalPortConfig;
+import org.onosproject.net.config.basics.SubjectFactories;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
+
+import static org.onosproject.net.config.basics.SubjectFactories.*;
+
+/**
+ * Component for registration of builtin basic network configurations.
+ */
+@Component(immediate = true)
+public class BasicNetworkConfigs {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private final Set<ConfigFactory> factories = ImmutableSet.of(
+ new ConfigFactory<DeviceId, BasicDeviceConfig>(DEVICE_SUBJECT_FACTORY,
+ BasicDeviceConfig.class,
+ "basic") {
+ @Override
+ public BasicDeviceConfig createConfig() {
+ return new BasicDeviceConfig();
+ }
+ },
+ new ConfigFactory<ConnectPoint, InterfaceConfig>(CONNECT_POINT_SUBJECT_FACTORY,
+ InterfaceConfig.class,
+ "interfaces") {
+ @Override
+ public InterfaceConfig createConfig() {
+ return new InterfaceConfig();
+ }
+ },
+ new ConfigFactory<HostId, BasicHostConfig>(HOST_SUBJECT_FACTORY,
+ BasicHostConfig.class,
+ "basic") {
+ @Override
+ public BasicHostConfig createConfig() {
+ return new BasicHostConfig();
+ }
+ },
+ new ConfigFactory<LinkKey, BasicLinkConfig>(LINK_SUBJECT_FACTORY,
+ BasicLinkConfig.class,
+ "basic") {
+ @Override
+ public BasicLinkConfig createConfig() {
+ return new BasicLinkConfig();
+ }
+ },
+ new ConfigFactory<ConnectPoint, OpticalPortConfig>(CONNECT_POINT_SUBJECT_FACTORY,
+ OpticalPortConfig.class,
+ "basic") {
+ @Override
+ public OpticalPortConfig createConfig() {
+ return new OpticalPortConfig();
+ }
+ }
+ );
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigRegistry registry;
+
+ @Activate
+ public void activate() {
+ SubjectFactories.setCoreService(coreService);
+ factories.forEach(registry::registerConfigFactory);
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ factories.forEach(registry::unregisterConfigFactory);
+ log.info("Stopped");
+ }
+
+}
diff --git a/core/net/src/main/java/org/onosproject/net/config/impl/NetworkConfigLoader.java b/core/net/src/main/java/org/onosproject/net/config/impl/NetworkConfigLoader.java
new file mode 100644
index 0000000..e66c81b
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/config/impl/NetworkConfigLoader.java
@@ -0,0 +1,212 @@
+/*
+ * 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.net.config.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Maps;
+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.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * Component for loading the initial network configuration.
+ */
+@Component(immediate = true)
+public class NetworkConfigLoader {
+
+ private static final File CFG_FILE = new File("../config/network-cfg.json");
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigService networkConfigService;
+
+ // FIXME: Add mutual exclusion to make sure this happens only once per startup.
+
+ private Map<InnerConfigPosition, ObjectNode> jsons = Maps.newHashMap();
+
+ private final NetworkConfigListener configListener = new InnerConfigListener();
+
+ ObjectNode root;
+
+ @Activate
+ public void activate() {
+ //TODO Maybe this should be at the bottom to avoid a potential race
+ networkConfigService.addListener(configListener);
+ try {
+ if (CFG_FILE.exists()) {
+ root = (ObjectNode) new ObjectMapper().readTree(CFG_FILE);
+
+ populateConfigurations();
+
+ applyConfigurations();
+
+ log.info("Loaded initial network configuration from {}", CFG_FILE);
+ }
+ } catch (Exception e) {
+ log.warn("Unable to load initial network configuration from {}",
+ CFG_FILE, e);
+ }
+ }
+
+ @Deactivate
+ public void deactivate() {
+ networkConfigService.removeListener(configListener);
+ }
+ // sweep through pending config jsons and try to add them
+
+ /**
+ * Inner class that allows for handling of newly added NetConfig types.
+ */
+ private final class InnerConfigListener implements NetworkConfigListener {
+
+ @Override
+ public void event(NetworkConfigEvent event) {
+ //TODO should this be done for other types of NetworkConfigEvents?
+ if (event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED ||
+ event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
+ applyConfigurations();
+ }
+
+ }
+ }
+
+ /**
+ * Inner class that allows for tracking of JSON class configurations.
+ */
+ private final class InnerConfigPosition {
+ private String subjectKey, subject, classKey;
+
+ private String getSubjectKey() {
+ return subjectKey;
+ }
+
+ private String getSubject() {
+ return subject;
+ }
+
+ private String getClassKey() {
+ return classKey;
+ }
+
+ private InnerConfigPosition(String subjectKey, String subject, String classKey) {
+ this.subjectKey = subjectKey;
+ this.subject = subject;
+ this.classKey = classKey;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof InnerConfigPosition) {
+ final InnerConfigPosition that = (InnerConfigPosition) obj;
+ return Objects.equals(this.subjectKey, that.subjectKey) && Objects.equals(this.subject, that.subject)
+ && Objects.equals(this.classKey, that.classKey);
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(subjectKey, subject, classKey);
+ }
+ }
+
+ /**
+ * Save the JSON leaves associated with a specific subject key.
+ *
+ * @param sk the subject key string.
+ * @param node the node associated with the subject key.
+ */
+ private void saveJson(String sk, ObjectNode node) {
+ node.fieldNames().forEachRemaining(s ->
+ saveSubjectJson(sk, s, (ObjectNode) node.path(s)));
+ }
+
+ /**
+ * Save the JSON leaves of the tree rooted as the node 'node' with subject key 'sk'.
+ *
+ * @param sk the string of the subject key.
+ * @param s the subject name.
+ * @param node the node rooting this subtree.
+ */
+ private void saveSubjectJson(String sk,
+ String s, ObjectNode node) {
+ node.fieldNames().forEachRemaining(c ->
+ this.jsons.put(new InnerConfigPosition(sk, s, c), (ObjectNode) node.path(c)));
+ }
+
+ /**
+ * Iterate through the JSON and populate a list of the leaf nodes of the structure.
+ */
+ private void populateConfigurations() {
+ root.fieldNames().forEachRemaining(sk ->
+ saveJson(sk, (ObjectNode) root.path(sk)));
+
+ }
+
+ /**
+ * Apply the configurations associated with all of the config classes that are imported and have not yet been
+ * applied.
+ */
+ protected void applyConfigurations() {
+ Iterator<Map.Entry<InnerConfigPosition, ObjectNode>> iter = jsons.entrySet().iterator();
+
+ Map.Entry<InnerConfigPosition, ObjectNode> entry;
+ InnerConfigPosition key;
+ ObjectNode node;
+ String subjectKey;
+ String subject;
+ String classKey;
+
+ while (iter.hasNext()) {
+ entry = iter.next();
+ node = entry.getValue();
+ key = entry.getKey();
+ subjectKey = key.getSubjectKey();
+ subject = key.getSubject();
+ classKey = key.getClassKey();
+ //Check that the config class has been imported
+ if (networkConfigService.getConfigClass(subjectKey, subject) != null) {
+
+ //Apply the configuration
+ networkConfigService.applyConfig(networkConfigService.getSubjectFactory(subjectKey).
+ createSubject(subject),
+ networkConfigService.getConfigClass(subjectKey, classKey), node);
+
+ //Now that it has been applied the corresponding JSON entry is no longer needed
+ jsons.remove(key);
+ }
+
+ }
+ }
+
+}
diff --git a/core/net/src/main/java/org/onosproject/net/config/impl/NetworkConfigManager.java b/core/net/src/main/java/org/onosproject/net/config/impl/NetworkConfigManager.java
new file mode 100644
index 0000000..5c6cc0e
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/config/impl/NetworkConfigManager.java
@@ -0,0 +1,288 @@
+/*
+ * 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.net.config.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+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.event.AbstractListenerManager;
+import org.onosproject.net.config.Config;
+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.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.NetworkConfigStore;
+import org.onosproject.net.config.NetworkConfigStoreDelegate;
+import org.onosproject.net.config.SubjectFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Implementation of the network configuration subsystem.
+ */
+@Component(immediate = true)
+@Service
+public class NetworkConfigManager
+ extends AbstractListenerManager<NetworkConfigEvent, NetworkConfigListener>
+ implements NetworkConfigRegistry, NetworkConfigService {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String NULL_FACTORY_MSG = "Factory cannot be null";
+ private static final String NULL_SCLASS_MSG = "Subject class cannot be null";
+ private static final String NULL_CCLASS_MSG = "Config class cannot be null";
+ private static final String NULL_SUBJECT_MSG = "Subject cannot be null";
+
+ // Inventory of configuration factories
+ private final Map<ConfigKey, ConfigFactory> factories = Maps.newConcurrentMap();
+
+ // Secondary indices to retrieve subject and config classes by keys
+ private final Map<String, SubjectFactory> subjectClasses = Maps.newConcurrentMap();
+ private final Map<Class, SubjectFactory> subjectClassKeys = Maps.newConcurrentMap();
+ private final Map<ConfigIdentifier, Class<? extends Config>> configClasses = Maps.newConcurrentMap();
+
+ private final NetworkConfigStoreDelegate storeDelegate = new InternalStoreDelegate();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected NetworkConfigStore store;
+
+
+ @Activate
+ public void activate() {
+ eventDispatcher.addSink(NetworkConfigEvent.class, listenerRegistry);
+ store.setDelegate(storeDelegate);
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ eventDispatcher.removeSink(NetworkConfigEvent.class);
+ store.unsetDelegate(storeDelegate);
+ log.info("Stopped");
+ }
+
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public void registerConfigFactory(ConfigFactory configFactory) {
+ checkNotNull(configFactory, NULL_FACTORY_MSG);
+ factories.put(key(configFactory), configFactory);
+ configClasses.put(identifier(configFactory), configFactory.configClass());
+
+ SubjectFactory subjectFactory = configFactory.subjectFactory();
+ subjectClasses.putIfAbsent(subjectFactory.subjectKey(), subjectFactory);
+ subjectClassKeys.putIfAbsent(subjectFactory.subjectClass(), subjectFactory);
+
+ store.addConfigFactory(configFactory);
+ }
+
+ @Override
+ public void unregisterConfigFactory(ConfigFactory configFactory) {
+ checkNotNull(configFactory, NULL_FACTORY_MSG);
+ factories.remove(key(configFactory));
+ configClasses.remove(identifier(configFactory));
+
+ // Note that we are deliberately not removing subject factory key bindings.
+ store.removeConfigFactory(configFactory);
+ }
+
+ @Override
+ public Set<ConfigFactory> getConfigFactories() {
+ return ImmutableSet.copyOf(factories.values());
+ }
+
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <S, C extends Config<S>> Set<ConfigFactory<S, C>> getConfigFactories(Class<S> subjectClass) {
+ ImmutableSet.Builder<ConfigFactory<S, C>> builder = ImmutableSet.builder();
+ factories.forEach((key, factory) -> {
+ if (factory.subjectFactory().subjectClass().equals(subjectClass)) {
+ builder.add(factory);
+ }
+ });
+ return builder.build();
+ }
+
+ @Override
+ public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
+ checkNotNull(configClass, NULL_CCLASS_MSG);
+ return store.getConfigFactory(configClass);
+ }
+
+
+ @Override
+ public Set<Class> getSubjectClasses() {
+ ImmutableSet.Builder<Class> builder = ImmutableSet.builder();
+ factories.forEach((k, v) -> builder.add(k.subjectClass));
+ return builder.build();
+ }
+
+ @Override
+ public SubjectFactory getSubjectFactory(String subjectKey) {
+ return subjectClasses.get(subjectKey);
+ }
+
+ @Override
+ public SubjectFactory getSubjectFactory(Class subjectClass) {
+ return subjectClassKeys.get(subjectClass);
+ }
+
+ @Override
+ public Class<? extends Config> getConfigClass(String subjectKey, String configKey) {
+ return configClasses.get(new ConfigIdentifier(subjectKey, configKey));
+ }
+
+ @Override
+ public <S> Set<S> getSubjects(Class<S> subjectClass) {
+ checkNotNull(subjectClass, NULL_SCLASS_MSG);
+ return store.getSubjects(subjectClass);
+ }
+
+ @Override
+ public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
+ checkNotNull(subjectClass, NULL_SCLASS_MSG);
+ checkNotNull(configClass, NULL_CCLASS_MSG);
+ return store.getSubjects(subjectClass, configClass);
+ }
+
+ @Override
+ public <S> Set<Config<S>> getConfigs(S subject) {
+ checkNotNull(subject, NULL_SUBJECT_MSG);
+ Set<Class<? extends Config<S>>> configClasses = store.getConfigClasses(subject);
+ ImmutableSet.Builder<Config<S>> cfg = ImmutableSet.builder();
+ configClasses.forEach(cc -> cfg.add(store.getConfig(subject, cc)));
+ return cfg.build();
+ }
+
+ @Override
+ public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) {
+ checkNotNull(subject, NULL_SUBJECT_MSG);
+ checkNotNull(configClass, NULL_CCLASS_MSG);
+ return store.getConfig(subject, configClass);
+ }
+
+
+ @Override
+ public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
+ checkNotNull(subject, NULL_SUBJECT_MSG);
+ checkNotNull(configClass, NULL_CCLASS_MSG);
+ return store.createConfig(subject, configClass);
+ }
+
+ @Override
+ public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, ObjectNode json) {
+ checkNotNull(subject, NULL_SUBJECT_MSG);
+ checkNotNull(configClass, NULL_CCLASS_MSG);
+ return store.applyConfig(subject, configClass, json);
+ }
+
+ @Override
+ public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) {
+ checkNotNull(subject, NULL_SUBJECT_MSG);
+ checkNotNull(configClass, NULL_CCLASS_MSG);
+ store.clearConfig(subject, configClass);
+ }
+
+ // Auxiliary store delegate to receive notification about changes in
+ // the network configuration store state - by the store itself.
+ private class InternalStoreDelegate implements NetworkConfigStoreDelegate {
+ @Override
+ public void notify(NetworkConfigEvent event) {
+ post(event);
+ }
+ }
+
+
+ // Produces a key for uniquely tracking a config factory.
+ private static ConfigKey key(ConfigFactory factory) {
+ return new ConfigKey(factory.subjectFactory().subjectClass(), factory.configClass());
+ }
+
+ // Auxiliary key to track config factories.
+ protected static final class ConfigKey {
+ final Class subjectClass;
+ final Class configClass;
+
+ protected ConfigKey(Class subjectClass, Class configClass) {
+ this.subjectClass = subjectClass;
+ this.configClass = configClass;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(subjectClass, configClass);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof ConfigKey) {
+ final ConfigKey other = (ConfigKey) obj;
+ return Objects.equals(this.subjectClass, other.subjectClass)
+ && Objects.equals(this.configClass, other.configClass);
+ }
+ return false;
+ }
+ }
+
+ private static ConfigIdentifier identifier(ConfigFactory factory) {
+ return new ConfigIdentifier(factory.subjectFactory().subjectKey(), factory.configKey());
+ }
+
+ static final class ConfigIdentifier {
+ final String subjectKey;
+ final String configKey;
+
+ protected ConfigIdentifier(String subjectKey, String configKey) {
+ this.subjectKey = subjectKey;
+ this.configKey = configKey;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(subjectKey, configKey);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof ConfigIdentifier) {
+ final ConfigIdentifier other = (ConfigIdentifier) obj;
+ return Objects.equals(this.subjectKey, other.subjectKey)
+ && Objects.equals(this.configKey, other.configKey);
+ }
+ return false;
+ }
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/config/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/config/impl/package-info.java
new file mode 100644
index 0000000..38f7694
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/config/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 network configuration subsystem.
+ */
+package org.onosproject.net.config.impl;
\ No newline at end of file
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java b/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
index ae6a3b2..36686d8 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
@@ -18,7 +18,7 @@
import static org.slf4j.LoggerFactory.getLogger;
import org.onosproject.net.config.ConfigOperator;
-import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
+import org.onosproject.net.config.basics.BasicDeviceConfig;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.Device;
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
index 83c4a69..24ea9f9 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/DeviceManager.java
@@ -31,7 +31,7 @@
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigService;
-import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
+import org.onosproject.net.config.basics.BasicDeviceConfig;
import org.onosproject.mastership.MastershipEvent;
import org.onosproject.mastership.MastershipListener;
import org.onosproject.mastership.MastershipService;
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java b/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java
index 7c551eb..ca43f95 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/OpticalPortOperator.java
@@ -18,7 +18,7 @@
import static org.slf4j.LoggerFactory.getLogger;
import org.onosproject.net.config.ConfigOperator;
-import org.onosproject.incubator.net.config.basics.OpticalPortConfig;
+import org.onosproject.net.config.basics.OpticalPortConfig;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.PortNumber;
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java b/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
index f0e100c..68aa27f 100644
--- a/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
@@ -19,7 +19,7 @@
import org.slf4j.Logger;
import org.onosproject.net.config.ConfigOperator;
-import org.onosproject.incubator.net.config.basics.BasicHostConfig;
+import org.onosproject.net.config.basics.BasicHostConfig;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.SparseAnnotations;
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java b/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
index b94ca68..fe369ae 100644
--- a/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/HostManager.java
@@ -29,7 +29,7 @@
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigService;
-import org.onosproject.incubator.net.config.basics.BasicHostConfig;
+import org.onosproject.net.config.basics.BasicHostConfig;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Host;
diff --git a/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java b/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java
index 6a21fdd..a6b08f6 100644
--- a/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java
@@ -21,7 +21,7 @@
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.config.ConfigOperator;
-import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
+import org.onosproject.net.config.basics.BasicLinkConfig;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.Link;
import org.onosproject.net.SparseAnnotations;
diff --git a/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java b/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java
index 04e7939..b4cc17c 100644
--- a/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java
+++ b/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java
@@ -29,7 +29,7 @@
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigService;
-import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
+import org.onosproject.net.config.basics.BasicLinkConfig;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
diff --git a/core/net/src/test/java/org/onosproject/net/config/impl/NetworkConfigManagerTest.java b/core/net/src/test/java/org/onosproject/net/config/impl/NetworkConfigManagerTest.java
new file mode 100644
index 0000000..2d03bfc
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/config/impl/NetworkConfigManagerTest.java
@@ -0,0 +1,242 @@
+/*
+ * 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.net.config.impl;
+
+import java.util.Set;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onosproject.event.EventDeliveryServiceAdapter;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.SubjectFactory;
+import org.onosproject.net.NetTestTools;
+import org.onosproject.store.config.impl.DistributedNetworkConfigStore;
+import org.onosproject.store.service.TestStorageService;
+
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.testing.EqualsTester;
+
+/**
+ * Unit tests for network config registry.
+ */
+public class NetworkConfigManagerTest {
+ private NetworkConfigManager manager;
+ private NetworkConfigRegistry registry;
+ private NetworkConfigService configService;
+ private DistributedNetworkConfigStore configStore;
+
+ /**
+ * Config classes for testing.
+ */
+ public class BasicConfig1 extends Config<String> { }
+ public class BasicConfig2 extends Config<String> { }
+
+ public class MockSubjectFactory extends SubjectFactory<String> {
+ protected MockSubjectFactory(Class<String> subjectClass, String subjectKey) {
+ super(subjectClass, subjectKey);
+ }
+
+ @Override
+ public String createSubject(String subjectKey) {
+ return subjectKey + "-subject";
+ }
+ }
+
+ /**
+ * Config factory classes for testing.
+ */
+ public class MockConfigFactory1 extends ConfigFactory<String, BasicConfig1> {
+ protected MockConfigFactory1(SubjectFactory<String> subjectFactory,
+ Class<BasicConfig1> configClass, String configKey) {
+ super(subjectFactory, configClass, configKey);
+ }
+ @Override
+ public BasicConfig1 createConfig() {
+ return new BasicConfig1();
+ }
+ }
+
+ public class MockConfigFactory2 extends ConfigFactory<String, BasicConfig2> {
+ protected MockConfigFactory2(SubjectFactory<String> subjectFactory,
+ Class<BasicConfig2> configClass, String configKey) {
+ super(subjectFactory, configClass, configKey);
+ }
+ @Override
+ public BasicConfig2 createConfig() {
+ return new BasicConfig2();
+ }
+ }
+
+ MockSubjectFactory factory1 = new MockSubjectFactory(String.class,
+ "key1");
+ MockSubjectFactory factory2 = new MockSubjectFactory(String.class,
+ "key2");
+
+ MockConfigFactory1 config1Factory = new MockConfigFactory1(factory1,
+ BasicConfig1.class, "config1");
+ MockConfigFactory2 config2Factory = new MockConfigFactory2(factory2,
+ BasicConfig2.class, "config2");
+
+
+ @Before
+ public void setUp() throws Exception {
+ configStore = new DistributedNetworkConfigStore();
+ TestUtils.setField(configStore, "storageService", new TestStorageService());
+ configStore.activate();
+ manager = new NetworkConfigManager();
+ manager.store = configStore;
+ NetTestTools.injectEventDispatcher(manager, new EventDeliveryServiceAdapter());
+ manager.activate();
+ registry = manager;
+ configService = manager;
+ }
+
+ @After
+ public void tearDown() {
+ configStore.deactivate();
+ manager.deactivate();
+ }
+
+ @Test
+ public void testRegistry() {
+ assertThat(registry.getConfigFactories(), hasSize(0));
+ assertThat(registry.getConfigFactories(String.class), hasSize(0));
+ assertThat(registry.getConfigFactory(BasicConfig1.class), nullValue());
+
+ registry.registerConfigFactory(config1Factory);
+ registry.registerConfigFactory(config2Factory);
+
+ assertThat(registry.getConfigFactories(), hasSize(2));
+ assertThat(registry.getConfigFactories(String.class), hasSize(2));
+
+ ConfigFactory queried = registry.getConfigFactory(BasicConfig1.class);
+ assertThat(queried, is(config1Factory));
+
+ registry.unregisterConfigFactory(queried);
+ // Factory associations are not removed according to code documentation
+ assertThat(registry.getConfigFactories(), hasSize(1));
+ assertThat(registry.getConfigFactories(String.class), hasSize(1));
+ assertThat(registry.getConfigFactory(BasicConfig1.class), nullValue());
+ }
+
+ @Test
+ public void configIdEquals() {
+ NetworkConfigManager.ConfigIdentifier id1 =
+ new NetworkConfigManager.ConfigIdentifier("s1", "c1");
+ NetworkConfigManager.ConfigIdentifier likeId1 =
+ new NetworkConfigManager.ConfigIdentifier("s1", "c1");
+ NetworkConfigManager.ConfigIdentifier id2 =
+ new NetworkConfigManager.ConfigIdentifier("s1", "c2");
+ NetworkConfigManager.ConfigIdentifier id3 =
+ new NetworkConfigManager.ConfigIdentifier("s2", "c1");
+
+ new EqualsTester().addEqualityGroup(id1, likeId1)
+ .addEqualityGroup(id2)
+ .addEqualityGroup(id3)
+ .testEquals();
+ }
+
+ @Test
+ public void configKeyEquals() {
+ NetworkConfigManager.ConfigKey key1 =
+ new NetworkConfigManager.ConfigKey(String.class, String.class);
+ NetworkConfigManager.ConfigKey likeKey1 =
+ new NetworkConfigManager.ConfigKey(String.class, String.class);
+ NetworkConfigManager.ConfigKey key2 =
+ new NetworkConfigManager.ConfigKey(String.class, Integer.class);
+ NetworkConfigManager.ConfigKey key3 =
+ new NetworkConfigManager.ConfigKey(Integer.class, String.class);
+
+ new EqualsTester().addEqualityGroup(key1, likeKey1)
+ .addEqualityGroup(key2)
+ .addEqualityGroup(key3)
+ .testEquals();
+ }
+
+ /**
+ * Tests creation, query and removal of a factory.
+ */
+ @Test
+ public void testAddConfig() {
+
+ assertThat(configService.getSubjectFactory(String.class), nullValue());
+ assertThat(configService.getSubjectFactory("key"), nullValue());
+
+ registry.registerConfigFactory(config1Factory);
+ registry.registerConfigFactory(config2Factory);
+ configService.addConfig("configKey", BasicConfig1.class);
+
+ Config newConfig = configService.getConfig("configKey", BasicConfig1.class);
+ assertThat(newConfig, notNullValue());
+
+ assertThat(configService.getSubjectFactory(String.class), notNullValue());
+ assertThat(configService.getSubjectFactory("key1"), notNullValue());
+
+ Set<Class> classes = configService.getSubjectClasses();
+ assertThat(classes, hasSize(1));
+
+ Set<String> subjectsForClass =
+ configService.getSubjects(String.class);
+ assertThat(subjectsForClass, hasSize(1));
+
+ Set<String> subjectsForConfig =
+ configService.getSubjects(String.class, BasicConfig1.class);
+ assertThat(subjectsForConfig, hasSize(1));
+
+ Class queriedConfigClass = configService.getConfigClass("key1", "config1");
+ assertThat(queriedConfigClass == BasicConfig1.class, is(true));
+
+ Set<? extends Config> configs = configService.getConfigs("configKey");
+ assertThat(configs.size(), is(1));
+ configs.forEach(c -> assertThat(c, instanceOf(BasicConfig1.class)));
+
+ configService.removeConfig("configKey", BasicConfig1.class);
+ Config newConfigAfterRemove = configService.getConfig("configKey", BasicConfig1.class);
+ assertThat(newConfigAfterRemove, nullValue());
+ }
+
+ /**
+ * Tests creation, query and removal of a factory.
+ */
+ @Test
+ public void testApplyConfig() {
+
+ assertThat(configService.getSubjectFactory(String.class), nullValue());
+ assertThat(configService.getSubjectFactory("key"), nullValue());
+
+ registry.registerConfigFactory(config1Factory);
+ registry.registerConfigFactory(config2Factory);
+ configService.applyConfig("configKey", BasicConfig1.class, new ObjectMapper().createObjectNode());
+
+ Config newConfig = configService.getConfig("configKey", BasicConfig1.class);
+ assertThat(newConfig, notNullValue());
+
+ assertThat(configService.getSubjectFactory(String.class), notNullValue());
+ assertThat(configService.getSubjectFactory("key1"), notNullValue());
+ }
+}
diff --git a/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java b/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java
index 66d74c5..8827c55 100644
--- a/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/device/impl/BasicDeviceOperatorTest.java
@@ -26,7 +26,7 @@
import org.onlab.packet.ChassisId;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.ConfigApplyDelegate;
-import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
+import org.onosproject.net.config.basics.BasicDeviceConfig;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.DeviceId;
diff --git a/core/net/src/test/java/org/onosproject/net/device/impl/OpticalPortOperatorTest.java b/core/net/src/test/java/org/onosproject/net/device/impl/OpticalPortOperatorTest.java
index 83aa8a0..78bc08e 100644
--- a/core/net/src/test/java/org/onosproject/net/device/impl/OpticalPortOperatorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/device/impl/OpticalPortOperatorTest.java
@@ -4,7 +4,7 @@
import org.junit.Test;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.ConfigApplyDelegate;
-import org.onosproject.incubator.net.config.basics.OpticalPortConfig;
+import org.onosproject.net.config.basics.OpticalPortConfig;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultAnnotations;
diff --git a/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java b/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
index fe0d97a..e7f14b5 100644
--- a/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/host/impl/BasicHostOperatorTest.java
@@ -24,7 +24,7 @@
import org.onlab.packet.VlanId;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.ConfigApplyDelegate;
-import org.onosproject.incubator.net.config.basics.BasicHostConfig;
+import org.onosproject.net.config.basics.BasicHostConfig;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.DeviceId;
import org.onosproject.net.HostId;
diff --git a/core/net/src/test/java/org/onosproject/net/link/impl/BasicLinkOperatorTest.java b/core/net/src/test/java/org/onosproject/net/link/impl/BasicLinkOperatorTest.java
index 610c974..fe9e37c 100644
--- a/core/net/src/test/java/org/onosproject/net/link/impl/BasicLinkOperatorTest.java
+++ b/core/net/src/test/java/org/onosproject/net/link/impl/BasicLinkOperatorTest.java
@@ -24,7 +24,7 @@
import org.junit.Test;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.ConfigApplyDelegate;
-import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
+import org.onosproject.net.config.basics.BasicLinkConfig;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultAnnotations;
diff --git a/core/store/dist/src/main/java/org/onosproject/store/config/impl/DistributedNetworkConfigStore.java b/core/store/dist/src/main/java/org/onosproject/store/config/impl/DistributedNetworkConfigStore.java
new file mode 100644
index 0000000..5ef4045
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/config/impl/DistributedNetworkConfigStore.java
@@ -0,0 +1,284 @@
+/*
+ * 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.store.config.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.BooleanNode;
+import com.fasterxml.jackson.databind.node.DoubleNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.LongNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.ShortNode;
+import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+
+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.onlab.util.KryoNamespace;
+import org.onlab.util.Tools;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigApplyDelegate;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigStore;
+import org.onosproject.net.config.NetworkConfigStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.ConsistentMapException;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static org.onosproject.net.config.NetworkConfigEvent.Type.*;
+
+/**
+ * Implementation of a distributed network configuration store.
+ */
+@Component(immediate = true)
+@Service
+public class DistributedNetworkConfigStore
+ extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate>
+ implements NetworkConfigStore {
+
+ private static final int MAX_BACKOFF = 10;
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected StorageService storageService;
+
+ private ConsistentMap<ConfigKey, ObjectNode> configs;
+
+ private final Map<String, ConfigFactory> factoriesByConfig = Maps.newConcurrentMap();
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final ConfigApplyDelegate applyDelegate = new InternalApplyDelegate();
+ private final MapEventListener<ConfigKey, ObjectNode> listener = new InternalMapListener();
+
+ @Activate
+ public void activate() {
+ KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
+ .register(KryoNamespaces.API)
+ .register(ConfigKey.class, ObjectNode.class, ArrayNode.class,
+ JsonNodeFactory.class, LinkedHashMap.class,
+ TextNode.class, BooleanNode.class,
+ LongNode.class, DoubleNode.class, ShortNode.class);
+
+ configs = storageService.<ConfigKey, ObjectNode>consistentMapBuilder()
+ .withSerializer(Serializer.using(kryoBuilder.build()))
+ .withName("onos-network-configs")
+ .withRelaxedReadConsistency()
+ .build();
+ configs.addListener(listener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ configs.removeListener(listener);
+ log.info("Stopped");
+ }
+
+ @Override
+ public void addConfigFactory(ConfigFactory configFactory) {
+ factoriesByConfig.put(configFactory.configClass().getName(), configFactory);
+ notifyDelegate(new NetworkConfigEvent(CONFIG_REGISTERED, configFactory.configKey(),
+ configFactory.configClass()));
+ }
+
+ @Override
+ public void removeConfigFactory(ConfigFactory configFactory) {
+ factoriesByConfig.remove(configFactory.configClass().getName());
+ notifyDelegate(new NetworkConfigEvent(CONFIG_UNREGISTERED, configFactory.configKey(),
+ configFactory.configClass()));
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
+ return (ConfigFactory<S, C>) factoriesByConfig.get(configClass.getName());
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <S> Set<S> getSubjects(Class<S> subjectClass) {
+ ImmutableSet.Builder<S> builder = ImmutableSet.builder();
+ configs.keySet().forEach(k -> {
+ if (subjectClass.isInstance(k.subject)) {
+ builder.add((S) k.subject);
+ }
+ });
+ return builder.build();
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
+ ImmutableSet.Builder<S> builder = ImmutableSet.builder();
+ String cName = configClass.getName();
+ configs.keySet().forEach(k -> {
+ if (subjectClass.isInstance(k.subject) && cName.equals(k.configClass)) {
+ builder.add((S) k.subject);
+ }
+ });
+ return builder.build();
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <S> Set<Class<? extends Config<S>>> getConfigClasses(S subject) {
+ ImmutableSet.Builder<Class<? extends Config<S>>> builder = ImmutableSet.builder();
+ configs.keySet().forEach(k -> {
+ if (Objects.equals(subject, k.subject) && delegate != null) {
+ builder.add(factoriesByConfig.get(k.configClass).configClass());
+ }
+ });
+ return builder.build();
+ }
+
+ @Override
+ public <S, T extends Config<S>> T getConfig(S subject, Class<T> configClass) {
+ // TODO: need to identify and address the root cause for timeouts.
+ Versioned<ObjectNode> json = Tools.retryable(configs::get, ConsistentMapException.class, 1, MAX_BACKOFF)
+ .apply(key(subject, configClass));
+ return json != null ? createConfig(subject, configClass, json.value()) : null;
+ }
+
+
+ @Override
+ public <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass) {
+ Versioned<ObjectNode> json = configs.computeIfAbsent(key(subject, configClass),
+ k -> mapper.createObjectNode());
+ return createConfig(subject, configClass, json.value());
+ }
+
+ @Override
+ public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, ObjectNode json) {
+ return createConfig(subject, configClass,
+ configs.putAndGet(key(subject, configClass), json).value());
+ }
+
+ @Override
+ public <S, C extends Config<S>> void clearConfig(S subject, Class<C> configClass) {
+ configs.remove(key(subject, configClass));
+ }
+
+ /**
+ * Produces a config from the specified subject, config class and raw JSON.
+ *
+ * @param subject config subject
+ * @param configClass config class
+ * @param json raw JSON data
+ * @return config object or null of no factory found or if the specified
+ * JSON is null
+ */
+ @SuppressWarnings("unchecked")
+ private <S, C extends Config<S>> C createConfig(S subject, Class<C> configClass,
+ ObjectNode json) {
+ if (json != null) {
+ ConfigFactory<S, C> factory = factoriesByConfig.get(configClass.getName());
+ if (factory != null) {
+ C config = factory.createConfig();
+ config.init(subject, factory.configKey(), json, mapper, applyDelegate);
+ return config;
+ }
+ }
+ return null;
+ }
+
+
+ // Auxiliary delegate to receive notifications about changes applied to
+ // the network configuration - by the apps.
+ private class InternalApplyDelegate implements ConfigApplyDelegate {
+ @Override
+ public void onApply(Config config) {
+ configs.put(key(config.subject(), config.getClass()), config.node());
+ }
+ }
+
+ // Produces a key for uniquely tracking a subject config.
+ private static ConfigKey key(Object subject, Class<?> configClass) {
+ return new ConfigKey(subject, configClass);
+ }
+
+ // Auxiliary key to track subject configurations.
+ private static final class ConfigKey {
+ final Object subject;
+ final String configClass;
+
+ private ConfigKey(Object subject, Class<?> configClass) {
+ this.subject = subject;
+ this.configClass = configClass.getName();
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(subject, configClass);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof ConfigKey) {
+ final ConfigKey other = (ConfigKey) obj;
+ return Objects.equals(this.subject, other.subject)
+ && Objects.equals(this.configClass, other.configClass);
+ }
+ return false;
+ }
+ }
+
+ private class InternalMapListener implements MapEventListener<ConfigKey, ObjectNode> {
+ @Override
+ public void event(MapEvent<ConfigKey, ObjectNode> event) {
+ NetworkConfigEvent.Type type;
+ switch (event.type()) {
+ case INSERT:
+ type = CONFIG_ADDED;
+ break;
+ case UPDATE:
+ type = CONFIG_UPDATED;
+ break;
+ case REMOVE:
+ default:
+ type = CONFIG_REMOVED;
+ break;
+ }
+ ConfigFactory factory = factoriesByConfig.get(event.key().configClass);
+ if (factory != null) {
+ notifyDelegate(new NetworkConfigEvent(type, event.key().subject,
+ factory.configClass()));
+ }
+ }
+ }
+}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/config/impl/package-info.java b/core/store/dist/src/main/java/org/onosproject/store/config/impl/package-info.java
new file mode 100644
index 0000000..0e1264e
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onosproject/store/config/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 network configuration distributed store.
+ */
+package org.onosproject.store.config.impl;
\ No newline at end of file
diff --git a/core/store/dist/src/test/java/org/onosproject/store/config/impl/DistributedNetworkConfigStoreTest.java b/core/store/dist/src/test/java/org/onosproject/store/config/impl/DistributedNetworkConfigStoreTest.java
new file mode 100644
index 0000000..06fe7b3
--- /dev/null
+++ b/core/store/dist/src/test/java/org/onosproject/store/config/impl/DistributedNetworkConfigStoreTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.store.config.impl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.net.config.Config;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.SubjectFactory;
+import org.onosproject.store.service.TestStorageService;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class DistributedNetworkConfigStoreTest {
+ private DistributedNetworkConfigStore configStore;
+
+ /**
+ * Sets up the config store and the storage service test harness.
+ */
+ @Before
+ public void setUp() {
+ configStore = new DistributedNetworkConfigStore();
+ configStore.storageService = new TestStorageService();
+ configStore.setDelegate(event -> { });
+ configStore.activate();
+ }
+
+ /**
+ * Tears down the config store.
+ */
+ @After
+ public void tearDown() {
+ configStore.deactivate();
+ }
+
+ /**
+ * Config class for testing.
+ */
+ public class BasicConfig extends Config<String> { }
+
+ /**
+ * Config factory class for testing.
+ */
+ public class MockConfigFactory extends ConfigFactory<String, BasicConfig> {
+ protected MockConfigFactory(SubjectFactory<String> subjectFactory,
+ Class<BasicConfig> configClass, String configKey) {
+ super(subjectFactory, configClass, configKey);
+ }
+ @Override
+ public BasicConfig createConfig() {
+ return new BasicConfig();
+ }
+ }
+
+ /**
+ * Tests creation, query and removal of a config.
+ */
+ @Test
+ public void testCreateConfig() {
+ configStore.addConfigFactory(new MockConfigFactory(null, BasicConfig.class, "config1"));
+
+ configStore.createConfig("config1", BasicConfig.class);
+ assertThat(configStore.getConfigClasses("config1"), hasSize(1));
+ assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1));
+ assertThat(configStore.getSubjects(String.class), hasSize(1));
+
+ BasicConfig queried = configStore.getConfig("config1", BasicConfig.class);
+ assertThat(queried, notNullValue());
+
+ configStore.clearConfig("config1", BasicConfig.class);
+ assertThat(configStore.getConfigClasses("config1"), hasSize(0));
+ assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(0));
+ assertThat(configStore.getSubjects(String.class), hasSize(0));
+
+ BasicConfig queriedAfterClear = configStore.getConfig("config1", BasicConfig.class);
+ assertThat(queriedAfterClear, nullValue());
+ }
+
+ /**
+ * Tests creation, query and removal of a factory.
+ */
+ @Test
+ public void testCreateFactory() {
+ MockConfigFactory mockFactory = new MockConfigFactory(null, BasicConfig.class, "config1");
+
+ assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue());
+
+ configStore.addConfigFactory(mockFactory);
+ assertThat(configStore.getConfigFactory(BasicConfig.class), is(mockFactory));
+
+ configStore.removeConfigFactory(mockFactory);
+ assertThat(configStore.getConfigFactory(BasicConfig.class), nullValue());
+ }
+
+ /**
+ * Tests applying a config.
+ */
+ @Test
+ public void testApplyConfig() {
+ configStore.addConfigFactory(new MockConfigFactory(null, BasicConfig.class, "config1"));
+
+ configStore.applyConfig("config1", BasicConfig.class, new ObjectMapper().createObjectNode());
+ assertThat(configStore.getConfigClasses("config1"), hasSize(1));
+ assertThat(configStore.getSubjects(String.class, BasicConfig.class), hasSize(1));
+ assertThat(configStore.getSubjects(String.class), hasSize(1));
+ }
+}