link config operator
Combination operator for link-related configuration information,
including tests.
Change-Id: I25ce27b3465e87acc718fe50063bc72ce68d095f
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
new file mode 100644
index 0000000..37ac066
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2014-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.link.impl;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.time.Duration;
+
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.Link;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+import org.slf4j.Logger;
+
+/**
+ * Implementations of merge policies for various sources of link configuration
+ * information. This includes applications, provides, and network configurations.
+ */
+public final class BasicLinkOperator {
+
+ private static final Logger log = getLogger(BasicLinkOperator.class);
+
+ private BasicLinkOperator() {
+ }
+
+ /**
+ * Generates a LinkDescription containing fields from a LinkDescription and
+ * a LinkConfig.
+ *
+ * @param cfg the link config entity from network config
+ * @param descr a LinkDescription
+ * @return LinkDescription based on both sources
+ */
+ public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) {
+ if (cfg == null) {
+ return descr;
+ }
+
+ // cfg.type() defaults to DIRECT, so there is a risk of unwanted override.
+ // do we want this behavior?
+ Link.Type type = descr.type();
+ if (cfg.type() != type) {
+ type = cfg.type();
+ }
+
+ SparseAnnotations sa = combine(cfg, descr.annotations());
+ return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa);
+ }
+
+ /**
+ * Generates an annotation from an existing annotation and LinkConfig.
+ *
+ * @param cfg the link config entity from network config
+ * @param an the annotation
+ * @return annotation combining both sources
+ */
+ public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) {
+ DefaultAnnotations.Builder b = DefaultAnnotations.builder();
+ if (cfg.latency() != Duration.ofNanos(-1)) {
+ b.set(AnnotationKeys.LATENCY, cfg.latency().toString());
+ }
+ if (cfg.bandwidth() != -1) {
+ b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth()));
+ }
+ return DefaultAnnotations.union(an, b.build());
+ }
+}
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
new file mode 100644
index 0000000..ca01b31
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/link/impl/BasicLinkOperatorTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2014-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.link.impl;
+
+import static org.onosproject.net.DeviceId.deviceId;
+import static org.onosproject.net.PortNumber.portNumber;
+import static org.junit.Assert.assertEquals;
+
+import java.time.Duration;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.incubator.net.config.Config;
+import org.onosproject.incubator.net.config.ConfigApplyDelegate;
+import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.LinkKey;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.SparseAnnotations;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+
+public class BasicLinkOperatorTest {
+
+ private static final DeviceId DID1 = deviceId("of:foo");
+ private static final DeviceId DID2 = deviceId("of:bar");
+ private static final PortNumber P1 = portNumber(1);
+
+ private static final ConnectPoint SRC = new ConnectPoint(DID1, P1);
+ private static final ConnectPoint DST = new ConnectPoint(DID2, P1);
+ private static final LinkKey LK = LinkKey.linkKey(SRC, DST);
+ private static final Duration NTIME = Duration.ofNanos(200);
+
+ private static final SparseAnnotations SA = DefaultAnnotations.builder()
+ .set(AnnotationKeys.DURABLE, "true").build();
+ private static final LinkDescription LD = new DefaultLinkDescription(SRC, DST, Link.Type.DIRECT, SA);
+ private final ConfigApplyDelegate delegate = new ConfigApplyDelegate() {
+ @Override
+ public void onApply(Config config) {
+ }
+ };
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ private static final BasicLinkConfig BLC = new BasicLinkConfig();
+
+ @Before
+ public void setUp() {
+ BLC.init(LK, "optest", JsonNodeFactory.instance.objectNode(), mapper, delegate);
+ BLC.latency(NTIME);
+ }
+
+ @Test
+ public void testDescOps() {
+ LinkDescription desc = BasicLinkOperator.combine(BLC, LD);
+ assertEquals(NTIME.toString(), desc.annotations().value(AnnotationKeys.LATENCY));
+ assertEquals("true", desc.annotations().value(AnnotationKeys.DURABLE));
+ }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/BasicLinkConfig.java b/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/BasicLinkConfig.java
index c881a7b..620a033 100644
--- a/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/BasicLinkConfig.java
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/config/basics/BasicLinkConfig.java
@@ -63,9 +63,9 @@
* @param latency new latency; null to clear
* @return self
*/
- public BasicElementConfig latency(Duration latency) {
+ public BasicLinkConfig latency(Duration latency) {
Long nanos = latency == null ? null : latency.toNanos();
- return (BasicElementConfig) setOrClear(LATENCY, nanos);
+ return (BasicLinkConfig) setOrClear(LATENCY, nanos);
}
/**
@@ -83,8 +83,8 @@
* @param bandwidth new bandwidth; null to clear
* @return self
*/
- public BasicElementConfig bandwidth(Long bandwidth) {
- return (BasicElementConfig) setOrClear(BANDWIDTH, bandwidth);
+ public BasicLinkConfig bandwidth(Long bandwidth) {
+ return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
}
}