Ayaka Koshibe
Committed by Gerrit Code Review

Notion of config operators:

Added operator for combining configuration info for Optical ports
from various sources. Also includes minor tweaks to OpticalPortConfig,
and javadoc fixes.

Change-Id: I754b2e29f560b473d1f791025f8b8b18c8d75a13
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.device.impl;
17 +
18 +import static org.slf4j.LoggerFactory.getLogger;
19 +
20 +import org.onosproject.incubator.net.config.ConfigOperator;
21 +import org.onosproject.incubator.net.config.basics.OpticalPortConfig;
22 +import org.onosproject.net.AnnotationKeys;
23 +import org.onosproject.net.DefaultAnnotations;
24 +import org.onosproject.net.PortNumber;
25 +import org.onosproject.net.SparseAnnotations;
26 +import org.onosproject.net.device.DefaultPortDescription;
27 +import org.onosproject.net.device.OchPortDescription;
28 +import org.onosproject.net.device.OduCltPortDescription;
29 +import org.onosproject.net.device.OmsPortDescription;
30 +import org.onosproject.net.device.PortDescription;
31 +import org.slf4j.Logger;
32 +
33 +/**
34 + * Implementations of merge policies for various sources of optical port
35 + * configuration information. This includes applications, provides, and network
36 + * configurations.
37 + */
38 +public final class OpticalPortOperator implements ConfigOperator {
39 +
40 + private static final Logger log = getLogger(OpticalPortOperator.class);
41 +
42 + private OpticalPortOperator() {
43 + }
44 +
45 + /**
46 + * Generates a PortDescription containing fields from a PortDescription and
47 + * an OpticalPortConfig.
48 + *
49 + * @param opc the port config entity from network config
50 + * @param descr a PortDescription
51 + * @return PortDescription based on both sources
52 + */
53 + public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) {
54 + if (opc == null) {
55 + return descr;
56 + }
57 +
58 + PortNumber port = descr.portNumber();
59 + final String name = opc.name();
60 + final String numName = opc.numberName();
61 + // if the description is null, or the current description port name != config name,
62 + // create a new PortNumber.
63 + PortNumber newPort = null;
64 + if (port == null) {
65 + // try to get the portNumber from the numName.
66 + if (!numName.isEmpty()) {
67 + final long pn = Long.valueOf(numName);
68 + newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn);
69 + } else {
70 + // we don't have defining info (a port number value)
71 + throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr);
72 + }
73 + } else if ((!name.isEmpty()) && !name.equals(port.name())) {
74 + final long pn = (numName.isEmpty()) ? port.toLong() : Long.valueOf(numName);
75 + newPort = PortNumber.portNumber(pn, name);
76 + }
77 +
78 + // Port type won't change unless we're overwriting a port completely.
79 + // Watch out for overwrites to avoid class cast craziness.
80 + boolean noOwrite = (opc.type() == descr.type()) ? true : false;
81 +
82 + SparseAnnotations sa = combine(opc, descr.annotations());
83 + if (noOwrite) {
84 + return updateDescription((newPort == null) ? port : newPort, sa, descr);
85 + } else {
86 + // TODO: must reconstruct a different type of PortDescription.
87 + log.info("Type rewrite from {} to {} required", descr.type(), opc.type());
88 + }
89 + return descr;
90 + }
91 +
92 + // updates a port description whose port type has not changed.
93 + private static PortDescription updateDescription(
94 + PortNumber port, SparseAnnotations sa, PortDescription descr) {
95 + switch (descr.type()) {
96 + case OMS:
97 + OmsPortDescription oms = (OmsPortDescription) descr;
98 + return new OmsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
99 + oms.maxFrequency(), oms.grid(), sa);
100 + case OCH:
101 + // We might need to update lambda below with STATIC_LAMBDA.
102 + OchPortDescription och = (OchPortDescription) descr;
103 + return new OchPortDescription(port, och.isEnabled(), och.signalType(),
104 + och.isTunable(), och.lambda(), sa);
105 + case ODUCLT:
106 + OduCltPortDescription odu = (OduCltPortDescription) descr;
107 + return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
108 + case PACKET:
109 + case FIBER:
110 + return new DefaultPortDescription(port, descr.isEnabled(), descr.type(),
111 + descr.portSpeed(), sa);
112 + default:
113 + // this includes copper ports.
114 + log.warn("Unsupported optical port type {} - can't update", descr.type());
115 + return descr;
116 + }
117 + }
118 +
119 + /**
120 + * Generates an annotation from an existing annotation and OptcalPortConfig.
121 + *
122 + * @param opc the port config entity from network config
123 + * @param an the annotation
124 + * @return annotation combining both sources
125 + */
126 + public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
127 + DefaultAnnotations.Builder b = DefaultAnnotations.builder();
128 + if (!opc.staticPort().isEmpty()) {
129 + b.set(AnnotationKeys.STATIC_PORT, opc.staticPort());
130 + }
131 + if (opc.staticLambda().isPresent()) {
132 + b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get()));
133 + }
134 + // The following may not need to be carried.
135 + if (!opc.name().isEmpty()) {
136 + b.set(AnnotationKeys.PORT_NAME, opc.name());
137 + }
138 + return DefaultAnnotations.union(an, b.build());
139 + }
140 +}
1 +package org.onosproject.net.device.impl;
2 +
3 +import org.junit.Before;
4 +import org.junit.Test;
5 +import org.onosproject.incubator.net.config.Config;
6 +import org.onosproject.incubator.net.config.ConfigApplyDelegate;
7 +import org.onosproject.incubator.net.config.basics.OpticalPortConfig;
8 +import org.onosproject.net.AnnotationKeys;
9 +import org.onosproject.net.ConnectPoint;
10 +import org.onosproject.net.DefaultAnnotations;
11 +import org.onosproject.net.DeviceId;
12 +import org.onosproject.net.OduCltPort;
13 +import org.onosproject.net.Port;
14 +import org.onosproject.net.PortNumber;
15 +import org.onosproject.net.SparseAnnotations;
16 +import org.onosproject.net.device.OduCltPortDescription;
17 +
18 +import com.fasterxml.jackson.databind.ObjectMapper;
19 +import com.fasterxml.jackson.databind.node.JsonNodeFactory;
20 +
21 +import static org.junit.Assert.assertEquals;
22 +
23 +public class OpticalPortOperatorTest {
24 + private static final DeviceId DID = DeviceId.deviceId("op-test");
25 + private static final String TPNAME = "test-port-100";
26 + private static final String SPNAME = "out-port-200";
27 + private static final String CFGNAME = "cfg-name";
28 +
29 + private static final PortNumber NAMED = PortNumber.portNumber(100, TPNAME);
30 + private static final PortNumber UNNAMED = PortNumber.portNumber(101);
31 + private static final ConnectPoint NCP = new ConnectPoint(DID, UNNAMED);
32 +
33 + private static final SparseAnnotations SA = DefaultAnnotations.builder()
34 + .set(AnnotationKeys.STATIC_PORT, SPNAME)
35 + .build();
36 +
37 + private static final OduCltPortDescription N_DESC = new OduCltPortDescription(
38 + NAMED, true, OduCltPort.SignalType.CLT_100GBE, SA);
39 + private static final OduCltPortDescription FAULTY = new OduCltPortDescription(
40 + null, true, OduCltPort.SignalType.CLT_100GBE);
41 +
42 + private final ConfigApplyDelegate delegate = new MockCfgDelegate();
43 + private final ObjectMapper mapper = new ObjectMapper();
44 +
45 + private static final OpticalPortConfig N_OPC = new OpticalPortConfig();
46 + private static final OpticalPortConfig UNN_OPC = new OpticalPortConfig();
47 +
48 + @Before
49 + public void setUp() {
50 + N_OPC.init(NCP, TPNAME, JsonNodeFactory.instance.objectNode(), mapper, delegate);
51 + UNN_OPC.init(NCP, TPNAME, JsonNodeFactory.instance.objectNode(), mapper, delegate);
52 +
53 + N_OPC.portName(CFGNAME).portNumberName(101L).portType(Port.Type.ODUCLT).staticLambda(300L);
54 + UNN_OPC.portType(Port.Type.ODUCLT);
55 + }
56 +
57 + @Test(expected = RuntimeException.class)
58 + public void testDescOps() {
59 + // port-null desc + opc with port number name
60 + OduCltPortDescription res = (OduCltPortDescription) OpticalPortOperator.combine(N_OPC, FAULTY);
61 + assertEquals(CFGNAME, res.portNumber().name());
62 + // full desc + opc with name
63 + assertEquals(TPNAME, N_DESC.portNumber().name());
64 + res = (OduCltPortDescription) OpticalPortOperator.combine(N_OPC, N_DESC);
65 + long sl = Long.valueOf(res.annotations().value(AnnotationKeys.STATIC_LAMBDA));
66 + assertEquals(CFGNAME, res.portNumber().name());
67 + assertEquals(300L, sl);
68 + // port-null desc + opc without port number name - throws RE
69 + res = (OduCltPortDescription) OpticalPortOperator.combine(UNN_OPC, FAULTY);
70 + }
71 +
72 + private class MockCfgDelegate implements ConfigApplyDelegate {
73 +
74 + @Override
75 + public void onApply(@SuppressWarnings("rawtypes") Config config) {
76 + config.apply();
77 + }
78 +
79 + }
80 +}
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.incubator.net.config;
17 +
18 +/**
19 + * An interface signifying a class that implements network configuration
20 + * information from multiple sources. There is a natural ordering to the
21 + * precedence of information, depending on its source:
22 + * <ol>
23 + * <li>Intents (from applications), which override</li>
24 + * <li>Configs (from the network configuration subsystem), which override</li>
25 + * <li>Descriptions (from southbound)</li>
26 + * </ol>
27 + * i.e., for a field representing the same attribute, the value from a Config
28 + * entity will be used over that from the Description.
29 + */
30 +public interface ConfigOperator {
31 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -3,7 +3,6 @@ package org.onosproject.incubator.net.config.basics; ...@@ -3,7 +3,6 @@ package org.onosproject.incubator.net.config.basics;
3 import java.util.Optional; 3 import java.util.Optional;
4 4
5 import org.onosproject.incubator.net.config.Config; 5 import org.onosproject.incubator.net.config.Config;
6 -import org.onosproject.net.AnnotationKeys;
7 import org.onosproject.net.ConnectPoint; 6 import org.onosproject.net.ConnectPoint;
8 import org.onosproject.net.Port; 7 import org.onosproject.net.Port;
9 8
...@@ -19,9 +18,11 @@ public class OpticalPortConfig extends Config<ConnectPoint> { ...@@ -19,9 +18,11 @@ public class OpticalPortConfig extends Config<ConnectPoint> {
19 18
20 // port name. "name" is the alphanumeric name of the port, but "port" refers 19 // port name. "name" is the alphanumeric name of the port, but "port" refers
21 // to the port number used as a name string (i.e., for ports without 20 // to the port number used as a name string (i.e., for ports without
22 - // alphanumeric names). this should be linked to ConnectPoint. 21 + // alphanumeric names).
23 public static final String NAME = "name"; 22 public static final String NAME = "name";
24 public static final String PORT = "port"; 23 public static final String PORT = "port";
24 + public static final String STATIC_PORT = "staticPort";
25 + public static final String STATIC_LAMBDA = "staticLambda";
25 26
26 /** 27 /**
27 * Returns the Enum value representing the type of port. 28 * Returns the Enum value representing the type of port.
...@@ -38,14 +39,22 @@ public class OpticalPortConfig extends Config<ConnectPoint> { ...@@ -38,14 +39,22 @@ public class OpticalPortConfig extends Config<ConnectPoint> {
38 39
39 /** 40 /**
40 * Returns the port name associated with this port configuration. The Name 41 * Returns the port name associated with this port configuration. The Name
41 - * may either be an alphanumeric string, or a string representation of the 42 + * is an alphanumeric string.
42 - * port number, falling back on the latter if the former doesn't exist.
43 * 43 *
44 * @return the name of this port, else, an empty string 44 * @return the name of this port, else, an empty string
45 */ 45 */
46 public String name() { 46 public String name() {
47 - String name = getStringValue(NAME); 47 + return getStringValue(NAME);
48 - return name.isEmpty() ? getStringValue(PORT) : name; 48 + }
49 +
50 + /**
51 + * Returns a stringified representation of the port number, configured in
52 + * some port types without an alphanumeric name as the port name.
53 + *
54 + * @return A string representation of the port number
55 + */
56 + public String numberName() {
57 + return getStringValue(PORT);
49 } 58 }
50 59
51 /** 60 /**
...@@ -56,7 +65,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> { ...@@ -56,7 +65,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> {
56 * @return the name of this port, else, an empty string 65 * @return the name of this port, else, an empty string
57 */ 66 */
58 public String staticPort() { 67 public String staticPort() {
59 - return getStringValue(AnnotationKeys.STATIC_PORT); 68 + return getStringValue(STATIC_PORT);
60 } 69 }
61 70
62 private String getStringValue(String field) { 71 private String getStringValue(String field) {
...@@ -71,7 +80,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> { ...@@ -71,7 +80,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> {
71 * @return an Optional that may contain a frequency value. 80 * @return an Optional that may contain a frequency value.
72 */ 81 */
73 public Optional<Long> staticLambda() { 82 public Optional<Long> staticLambda() {
74 - JsonNode sl = node.path(AnnotationKeys.STATIC_LAMBDA); 83 + JsonNode sl = node.path(STATIC_LAMBDA);
75 if (sl.isMissingNode()) { 84 if (sl.isMissingNode()) {
76 return Optional.empty(); 85 return Optional.empty();
77 } 86 }
...@@ -121,7 +130,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> { ...@@ -121,7 +130,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> {
121 * @return this OpticalPortConfig instance 130 * @return this OpticalPortConfig instance
122 */ 131 */
123 public OpticalPortConfig staticPort(String name) { 132 public OpticalPortConfig staticPort(String name) {
124 - return (OpticalPortConfig) setOrClear(AnnotationKeys.STATIC_PORT, name); 133 + return (OpticalPortConfig) setOrClear(STATIC_PORT, name);
125 } 134 }
126 135
127 /** 136 /**
...@@ -132,7 +141,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> { ...@@ -132,7 +141,7 @@ public class OpticalPortConfig extends Config<ConnectPoint> {
132 * @return this OpticalPortConfig instance 141 * @return this OpticalPortConfig instance
133 */ 142 */
134 public OpticalPortConfig staticLambda(Long index) { 143 public OpticalPortConfig staticLambda(Long index) {
135 - return (OpticalPortConfig) setOrClear(AnnotationKeys.STATIC_LAMBDA, index); 144 + return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index);
136 } 145 }
137 146
138 } 147 }
......
...@@ -5,6 +5,8 @@ import static org.junit.Assert.assertFalse; ...@@ -5,6 +5,8 @@ import static org.junit.Assert.assertFalse;
5 import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.TYPE; 5 import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.TYPE;
6 import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.NAME; 6 import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.NAME;
7 import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.PORT; 7 import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.PORT;
8 +import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.STATIC_LAMBDA;
9 +import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.STATIC_PORT;
8 10
9 import java.io.IOException; 11 import java.io.IOException;
10 import java.util.Iterator; 12 import java.util.Iterator;
...@@ -14,7 +16,6 @@ import org.junit.Before; ...@@ -14,7 +16,6 @@ import org.junit.Before;
14 import org.junit.Test; 16 import org.junit.Test;
15 import org.onosproject.incubator.net.config.Config; 17 import org.onosproject.incubator.net.config.Config;
16 import org.onosproject.incubator.net.config.ConfigApplyDelegate; 18 import org.onosproject.incubator.net.config.ConfigApplyDelegate;
17 -import org.onosproject.net.AnnotationKeys;
18 import org.onosproject.net.ConnectPoint; 19 import org.onosproject.net.ConnectPoint;
19 import org.onosproject.net.DeviceId; 20 import org.onosproject.net.DeviceId;
20 import org.onosproject.net.Port; 21 import org.onosproject.net.Port;
...@@ -103,7 +104,8 @@ public class OpticalPortConfigTest { ...@@ -103,7 +104,8 @@ public class OpticalPortConfigTest {
103 104
104 assertEquals(Port.Type.OMS, op0.type()); 105 assertEquals(Port.Type.OMS, op0.type());
105 assertEquals(jn0.path(NAME).asText(), op0.name()); 106 assertEquals(jn0.path(NAME).asText(), op0.name());
106 - assertEquals(jn1.path(PORT).asText(), op1.name()); 107 + assertEquals(jn1.path(PORT).asText(), op1.numberName());
108 + assertEquals("", op1.name());
107 assertEquals("", op2.name()); 109 assertEquals("", op2.name());
108 } 110 }
109 111
...@@ -116,8 +118,8 @@ public class OpticalPortConfigTest { ...@@ -116,8 +118,8 @@ public class OpticalPortConfigTest {
116 Long sl = 1L; 118 Long sl = 1L;
117 119
118 // see config entity 2 in DEMOTREE 120 // see config entity 2 in DEMOTREE
119 - op2.staticLambda(jn2.path("annotations").path(AnnotationKeys.STATIC_LAMBDA).asLong()); 121 + op2.staticLambda(jn2.path("annotations").path(STATIC_LAMBDA).asLong());
120 - op2.staticPort(jn2.path("annotations").path(AnnotationKeys.STATIC_PORT).asText()); 122 + op2.staticPort(jn2.path("annotations").path(STATIC_PORT).asText());
121 123
122 assertEquals(sl, op2.staticLambda().get()); 124 assertEquals(sl, op2.staticLambda().get());
123 assertFalse(op1.staticLambda().isPresent()); 125 assertFalse(op1.staticLambda().isPresent());
......