Ayaka Koshibe
Committed by Gerrit Code Review

Config entities for Optical ports and unit test.

Conflicts:
	incubator/net/src/main/java/org/onosproject/incubator/net/config/impl/BasicNetworkConfigs.java

Change-Id: I6ed0b0a9b62b0f3a225514070523778a124f7564
1 +package org.onosproject.incubator.net.config.basics;
2 +
3 +import java.util.Optional;
4 +
5 +import org.onosproject.incubator.net.config.Config;
6 +import org.onosproject.net.AnnotationKeys;
7 +import org.onosproject.net.ConnectPoint;
8 +import org.onosproject.net.Port;
9 +
10 +import com.fasterxml.jackson.databind.JsonNode;
11 +
12 +
13 +/**
14 + * Configurations for an optical port on a device.
15 + */
16 +public class OpticalPortConfig extends Config<ConnectPoint> {
17 + // optical type {OMS, OCH, ODUClt, fiber}
18 + public static final String TYPE = "type";
19 +
20 + // 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
22 + // alphanumeric names). this should be linked to ConnectPoint.
23 + public static final String NAME = "name";
24 + public static final String PORT = "port";
25 +
26 + /**
27 + * Returns the Enum value representing the type of port.
28 + *
29 + * @return the port type, or null if invalid or unset
30 + */
31 + public Port.Type type() {
32 + JsonNode type = node.path(TYPE);
33 + if (type.isMissingNode()) {
34 + return null;
35 + }
36 + return Port.Type.valueOf(type.asText());
37 + }
38 +
39 + /**
40 + * 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 + * port number, falling back on the latter if the former doesn't exist.
43 + *
44 + * @return the name of this port, else, an empty string
45 + */
46 + public String name() {
47 + String name = getStringValue(NAME);
48 + return name.isEmpty() ? getStringValue(PORT) : name;
49 + }
50 +
51 + /**
52 + * Returns the string-representation of name of the output port. This is
53 + * usually an OMS port for an OCH input ports, or an OCH port for ODU input
54 + * ports.
55 + *
56 + * @return the name of this port, else, an empty string
57 + */
58 + public String staticPort() {
59 + return getStringValue(AnnotationKeys.STATIC_PORT);
60 + }
61 +
62 + private String getStringValue(String field) {
63 + JsonNode name = node.path(field);
64 + return name.isMissingNode() ? "" : name.asText();
65 + }
66 +
67 + /**
68 + * Returns the output lambda configured for this port. The lambda value is
69 + * expressed as a frequency value.
70 + *
71 + * @return an Optional that may contain a frequency value.
72 + */
73 + public Optional<Long> staticLambda() {
74 + JsonNode sl = node.path(AnnotationKeys.STATIC_LAMBDA);
75 + if (sl.isMissingNode()) {
76 + return Optional.empty();
77 + }
78 + return Optional.of(sl.asLong());
79 + }
80 +
81 + /**
82 + * Sets the port type, or updates it if it's already set. A null argument removes
83 + * this field.
84 + *
85 + * @param type the port type
86 + * @return this OpticalPortConfig instance
87 + */
88 + public OpticalPortConfig portType(Port.Type type) {
89 + // if unspecified, ideally fall back on FIBER or PACKET.
90 + String pt = (type == null) ? null : type.toString();
91 + return (OpticalPortConfig) setOrClear(TYPE, pt);
92 + }
93 +
94 + /**
95 + * Sets the port name, or updates it if already set. A null argument removes
96 + * this field.
97 + *
98 + * @param name the port's name
99 + * @return this OpticalPortConfig instance
100 + */
101 + public OpticalPortConfig portName(String name) {
102 + return (OpticalPortConfig) setOrClear(NAME, name);
103 + }
104 +
105 + /**
106 + * Sets the port name from port number, or updates it if already set. A null
107 + * argument removes this field.
108 + *
109 + * @param name the port number, to be used as name
110 + * @return this OpticalPortConfig instance
111 + */
112 + public OpticalPortConfig portNumberName(Long name) {
113 + return (OpticalPortConfig) setOrClear(PORT, name);
114 + }
115 +
116 + /**
117 + * Sets the output port name, or updates it if already set. A null argument
118 + * removes this field.
119 + *
120 + * @param name the output port's name
121 + * @return this OpticalPortConfig instance
122 + */
123 + public OpticalPortConfig staticPort(String name) {
124 + return (OpticalPortConfig) setOrClear(AnnotationKeys.STATIC_PORT, name);
125 + }
126 +
127 + /**
128 + * Sets the output lambda index, or updates it if already set. A null argument
129 + * removes this field.
130 + *
131 + * @param index the output lambda
132 + * @return this OpticalPortConfig instance
133 + */
134 + public OpticalPortConfig staticLambda(Long index) {
135 + return (OpticalPortConfig) setOrClear(AnnotationKeys.STATIC_LAMBDA, index);
136 + }
137 +
138 +}
1 +package org.onosproject.incubator.net.config.basics;
2 +
3 +import static org.junit.Assert.assertEquals;
4 +import static org.junit.Assert.assertFalse;
5 +import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.TYPE;
6 +import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.NAME;
7 +import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.PORT;
8 +
9 +import java.io.IOException;
10 +import java.util.Iterator;
11 +import java.util.List;
12 +
13 +import org.junit.Before;
14 +import org.junit.Test;
15 +import org.onosproject.incubator.net.config.Config;
16 +import org.onosproject.incubator.net.config.ConfigApplyDelegate;
17 +import org.onosproject.net.AnnotationKeys;
18 +import org.onosproject.net.ConnectPoint;
19 +import org.onosproject.net.DeviceId;
20 +import org.onosproject.net.Port;
21 +import org.onosproject.net.PortNumber;
22 +
23 +import com.fasterxml.jackson.databind.JsonNode;
24 +import com.fasterxml.jackson.databind.ObjectMapper;
25 +import com.fasterxml.jackson.databind.node.JsonNodeFactory;
26 +import com.fasterxml.jackson.databind.node.ObjectNode;
27 +import com.google.common.collect.Lists;
28 +
29 +public class OpticalPortConfigTest {
30 + private static final String FIELD = "ports";
31 + private static final String KEY = "opc-test";
32 +
33 + private static final DeviceId DID = DeviceId.deviceId(KEY);
34 + private static final PortNumber PN = PortNumber.portNumber(100);
35 + private static final ConnectPoint CPT = new ConnectPoint(DID, PN);
36 + private static final String DEMOTREE = "{" +
37 + "\"ports\": [" +
38 + // config entity 0
39 + "{" +
40 + "\"name\": \"1-10-E1_WPORT\"," +
41 + "\"type\": \"OMS\"" +
42 + "}," +
43 + // config entity 1
44 + "{" +
45 + "\"type\": \"OCH\"," +
46 + "\"speed\": 0," +
47 + "\"port\": 10" +
48 + "}," +
49 + // config entity 2
50 + "{" +
51 + "\"name\": \"1-1-E1_LPORT\"," +
52 + "\"type\": \"OCH\"," +
53 + "\"annotations\": {" +
54 + "\"staticLambda\": 1," +
55 + "\"staticPort\": \"1-22-E1_WPORT\"" +
56 + "}" +
57 + "}" +
58 + "]" +
59 + "}";
60 +
61 + private final ConfigApplyDelegate delegate = new MockCfgDelegate();
62 + private final ObjectMapper mapper = new ObjectMapper();
63 +
64 + // one OPC per port in DEMOTREE
65 + private List<OpticalPortConfig> opcl = Lists.newArrayList();
66 + // JsonNodes representing each port.
67 + private List<JsonNode> testNodes = Lists.newArrayList();
68 +
69 + @Before
70 + public void setUp() {
71 + try {
72 + JsonNode tree = new ObjectMapper().readTree(DEMOTREE);
73 + Iterator<JsonNode> pitr = tree.get(FIELD).elements();
74 + while (pitr.hasNext()) {
75 + // initialize a config entity, add to lists
76 + JsonNode jn = pitr.next();
77 + OpticalPortConfig opc = new OpticalPortConfig();
78 + ObjectNode node = JsonNodeFactory.instance.objectNode();
79 + opc.init(CPT, KEY, node, mapper, delegate);
80 +
81 + testNodes.add(jn);
82 + opcl.add(opc);
83 + }
84 + } catch (IOException e) {
85 + e.printStackTrace();
86 + }
87 + }
88 +
89 + @Test
90 + public void testBaseAttrs() {
91 + // configs 0 and 1 - port with and without alphanumeric names
92 + OpticalPortConfig op0 = opcl.get(0);
93 + OpticalPortConfig op1 = opcl.get(1);
94 + // config 2 - no name
95 + OpticalPortConfig op2 = opcl.get(2);
96 + JsonNode jn0 = testNodes.get(0);
97 + JsonNode jn1 = testNodes.get(1);
98 +
99 + op0.portType(Port.Type.valueOf(jn0.path(TYPE).asText()))
100 + .portName(jn0.path(NAME).asText());
101 + op1.portType(Port.Type.valueOf(jn1.path(TYPE).asText()))
102 + .portNumberName(jn1.path(PORT).asLong());
103 +
104 + assertEquals(Port.Type.OMS, op0.type());
105 + assertEquals(jn0.path(NAME).asText(), op0.name());
106 + assertEquals(jn1.path(PORT).asText(), op1.name());
107 + assertEquals("", op2.name());
108 + }
109 +
110 + @Test
111 + public void testAdditionalAttrs() {
112 + // config 1 has no annotations, 2 has predefined ones
113 + OpticalPortConfig op1 = opcl.get(1);
114 + OpticalPortConfig op2 = opcl.get(2);
115 + JsonNode jn2 = testNodes.get(2);
116 + Long sl = 1L;
117 +
118 + // see config entity 2 in DEMOTREE
119 + op2.staticLambda(jn2.path("annotations").path(AnnotationKeys.STATIC_LAMBDA).asLong());
120 + op2.staticPort(jn2.path("annotations").path(AnnotationKeys.STATIC_PORT).asText());
121 +
122 + assertEquals(sl, op2.staticLambda().get());
123 + assertFalse(op1.staticLambda().isPresent());
124 + assertEquals("1-22-E1_WPORT", op2.staticPort());
125 + assertEquals("", op1.staticPort());
126 +
127 + op2.staticLambda(null);
128 + assertFalse(op2.staticLambda().isPresent());
129 + }
130 +
131 + private class MockCfgDelegate implements ConfigApplyDelegate {
132 +
133 + @Override
134 + public void onApply(@SuppressWarnings("rawtypes") Config config) {
135 + config.apply();
136 + }
137 +
138 + }
139 +}
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.incubator.net.config.impl; 16 package org.onosproject.incubator.net.config.impl;
17 17
18 import com.google.common.collect.ImmutableSet; 18 import com.google.common.collect.ImmutableSet;
19 +
19 import org.apache.felix.scr.annotations.Activate; 20 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component; 21 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate; 22 import org.apache.felix.scr.annotations.Deactivate;
...@@ -28,6 +29,7 @@ import org.onosproject.incubator.net.config.basics.BasicDeviceConfig; ...@@ -28,6 +29,7 @@ import org.onosproject.incubator.net.config.basics.BasicDeviceConfig;
28 import org.onosproject.incubator.net.config.basics.BasicHostConfig; 29 import org.onosproject.incubator.net.config.basics.BasicHostConfig;
29 import org.onosproject.incubator.net.config.basics.BasicLinkConfig; 30 import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
30 import org.onosproject.incubator.net.config.basics.BasicPortConfig; 31 import org.onosproject.incubator.net.config.basics.BasicPortConfig;
32 +import org.onosproject.incubator.net.config.basics.OpticalPortConfig;
31 import org.onosproject.incubator.net.config.basics.SubjectFactories; 33 import org.onosproject.incubator.net.config.basics.SubjectFactories;
32 import org.onosproject.incubator.net.domain.IntentDomainConfig; 34 import org.onosproject.incubator.net.domain.IntentDomainConfig;
33 import org.onosproject.incubator.net.domain.IntentDomainId; 35 import org.onosproject.incubator.net.domain.IntentDomainId;
...@@ -90,6 +92,14 @@ public class BasicNetworkConfigs { ...@@ -90,6 +92,14 @@ public class BasicNetworkConfigs {
90 public IntentDomainConfig createConfig() { 92 public IntentDomainConfig createConfig() {
91 return new IntentDomainConfig(); 93 return new IntentDomainConfig();
92 } 94 }
95 + },
96 + new ConfigFactory<ConnectPoint, OpticalPortConfig>(CONNECT_POINT_SUBJECT_FACTORY,
97 + OpticalPortConfig.class,
98 + "basic") {
99 + @Override
100 + public OpticalPortConfig createConfig() {
101 + return new OpticalPortConfig();
102 + }
93 } 103 }
94 ); 104 );
95 105
......