Ayaka Koshibe
Committed by Gerrit Code Review

link config operator

Combination operator for link-related configuration information,
including tests.

Change-Id: I25ce27b3465e87acc718fe50063bc72ce68d095f
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.link.impl;
17 +
18 +import static org.slf4j.LoggerFactory.getLogger;
19 +
20 +import java.time.Duration;
21 +
22 +import org.onosproject.net.AnnotationKeys;
23 +import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
24 +import org.onosproject.net.DefaultAnnotations;
25 +import org.onosproject.net.Link;
26 +import org.onosproject.net.SparseAnnotations;
27 +import org.onosproject.net.link.DefaultLinkDescription;
28 +import org.onosproject.net.link.LinkDescription;
29 +import org.slf4j.Logger;
30 +
31 +/**
32 + * Implementations of merge policies for various sources of link configuration
33 + * information. This includes applications, provides, and network configurations.
34 + */
35 +public final class BasicLinkOperator {
36 +
37 + private static final Logger log = getLogger(BasicLinkOperator.class);
38 +
39 + private BasicLinkOperator() {
40 + }
41 +
42 + /**
43 + * Generates a LinkDescription containing fields from a LinkDescription and
44 + * a LinkConfig.
45 + *
46 + * @param cfg the link config entity from network config
47 + * @param descr a LinkDescription
48 + * @return LinkDescription based on both sources
49 + */
50 + public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) {
51 + if (cfg == null) {
52 + return descr;
53 + }
54 +
55 + // cfg.type() defaults to DIRECT, so there is a risk of unwanted override.
56 + // do we want this behavior?
57 + Link.Type type = descr.type();
58 + if (cfg.type() != type) {
59 + type = cfg.type();
60 + }
61 +
62 + SparseAnnotations sa = combine(cfg, descr.annotations());
63 + return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa);
64 + }
65 +
66 + /**
67 + * Generates an annotation from an existing annotation and LinkConfig.
68 + *
69 + * @param cfg the link config entity from network config
70 + * @param an the annotation
71 + * @return annotation combining both sources
72 + */
73 + public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) {
74 + DefaultAnnotations.Builder b = DefaultAnnotations.builder();
75 + if (cfg.latency() != Duration.ofNanos(-1)) {
76 + b.set(AnnotationKeys.LATENCY, cfg.latency().toString());
77 + }
78 + if (cfg.bandwidth() != -1) {
79 + b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth()));
80 + }
81 + return DefaultAnnotations.union(an, b.build());
82 + }
83 +}
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.net.link.impl;
17 +
18 +import static org.onosproject.net.DeviceId.deviceId;
19 +import static org.onosproject.net.PortNumber.portNumber;
20 +import static org.junit.Assert.assertEquals;
21 +
22 +import java.time.Duration;
23 +import org.junit.Before;
24 +import org.junit.Test;
25 +import org.onosproject.incubator.net.config.Config;
26 +import org.onosproject.incubator.net.config.ConfigApplyDelegate;
27 +import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
28 +import org.onosproject.net.AnnotationKeys;
29 +import org.onosproject.net.ConnectPoint;
30 +import org.onosproject.net.DefaultAnnotations;
31 +import org.onosproject.net.DeviceId;
32 +import org.onosproject.net.Link;
33 +import org.onosproject.net.LinkKey;
34 +import org.onosproject.net.PortNumber;
35 +import org.onosproject.net.SparseAnnotations;
36 +import org.onosproject.net.link.DefaultLinkDescription;
37 +import org.onosproject.net.link.LinkDescription;
38 +
39 +import com.fasterxml.jackson.databind.ObjectMapper;
40 +import com.fasterxml.jackson.databind.node.JsonNodeFactory;
41 +
42 +public class BasicLinkOperatorTest {
43 +
44 + private static final DeviceId DID1 = deviceId("of:foo");
45 + private static final DeviceId DID2 = deviceId("of:bar");
46 + private static final PortNumber P1 = portNumber(1);
47 +
48 + private static final ConnectPoint SRC = new ConnectPoint(DID1, P1);
49 + private static final ConnectPoint DST = new ConnectPoint(DID2, P1);
50 + private static final LinkKey LK = LinkKey.linkKey(SRC, DST);
51 + private static final Duration NTIME = Duration.ofNanos(200);
52 +
53 + private static final SparseAnnotations SA = DefaultAnnotations.builder()
54 + .set(AnnotationKeys.DURABLE, "true").build();
55 + private static final LinkDescription LD = new DefaultLinkDescription(SRC, DST, Link.Type.DIRECT, SA);
56 + private final ConfigApplyDelegate delegate = new ConfigApplyDelegate() {
57 + @Override
58 + public void onApply(Config config) {
59 + }
60 + };
61 + private final ObjectMapper mapper = new ObjectMapper();
62 +
63 + private static final BasicLinkConfig BLC = new BasicLinkConfig();
64 +
65 + @Before
66 + public void setUp() {
67 + BLC.init(LK, "optest", JsonNodeFactory.instance.objectNode(), mapper, delegate);
68 + BLC.latency(NTIME);
69 + }
70 +
71 + @Test
72 + public void testDescOps() {
73 + LinkDescription desc = BasicLinkOperator.combine(BLC, LD);
74 + assertEquals(NTIME.toString(), desc.annotations().value(AnnotationKeys.LATENCY));
75 + assertEquals("true", desc.annotations().value(AnnotationKeys.DURABLE));
76 + }
77 +}
...@@ -63,9 +63,9 @@ public class BasicLinkConfig extends AllowedEntityConfig<LinkKey> { ...@@ -63,9 +63,9 @@ public class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
63 * @param latency new latency; null to clear 63 * @param latency new latency; null to clear
64 * @return self 64 * @return self
65 */ 65 */
66 - public BasicElementConfig latency(Duration latency) { 66 + public BasicLinkConfig latency(Duration latency) {
67 Long nanos = latency == null ? null : latency.toNanos(); 67 Long nanos = latency == null ? null : latency.toNanos();
68 - return (BasicElementConfig) setOrClear(LATENCY, nanos); 68 + return (BasicLinkConfig) setOrClear(LATENCY, nanos);
69 } 69 }
70 70
71 /** 71 /**
...@@ -83,8 +83,8 @@ public class BasicLinkConfig extends AllowedEntityConfig<LinkKey> { ...@@ -83,8 +83,8 @@ public class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
83 * @param bandwidth new bandwidth; null to clear 83 * @param bandwidth new bandwidth; null to clear
84 * @return self 84 * @return self
85 */ 85 */
86 - public BasicElementConfig bandwidth(Long bandwidth) { 86 + public BasicLinkConfig bandwidth(Long bandwidth) {
87 - return (BasicElementConfig) setOrClear(BANDWIDTH, bandwidth); 87 + return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
88 } 88 }
89 89
90 } 90 }
......