blob: 9a9a8b393e37398c86b75e49d9e5ec639544c783 [file] [log] [blame]
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -07001package org.onosproject.incubator.net.config.basics;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.TYPE;
6import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.NAME;
7import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.PORT;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -07008import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.STATIC_LAMBDA;
9import static org.onosproject.incubator.net.config.basics.OpticalPortConfig.STATIC_PORT;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070010
11import java.io.IOException;
12import java.util.Iterator;
13import java.util.List;
14
15import org.junit.Before;
16import org.junit.Test;
17import org.onosproject.incubator.net.config.Config;
18import org.onosproject.incubator.net.config.ConfigApplyDelegate;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070019import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.Port;
22import org.onosproject.net.PortNumber;
23
24import com.fasterxml.jackson.databind.JsonNode;
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.fasterxml.jackson.databind.node.JsonNodeFactory;
27import com.fasterxml.jackson.databind.node.ObjectNode;
28import com.google.common.collect.Lists;
29
30public class OpticalPortConfigTest {
31 private static final String FIELD = "ports";
32 private static final String KEY = "opc-test";
33
34 private static final DeviceId DID = DeviceId.deviceId(KEY);
35 private static final PortNumber PN = PortNumber.portNumber(100);
36 private static final ConnectPoint CPT = new ConnectPoint(DID, PN);
37 private static final String DEMOTREE = "{" +
38 "\"ports\": [" +
39 // config entity 0
40 "{" +
41 "\"name\": \"1-10-E1_WPORT\"," +
42 "\"type\": \"OMS\"" +
43 "}," +
44 // config entity 1
45 "{" +
46 "\"type\": \"OCH\"," +
47 "\"speed\": 0," +
48 "\"port\": 10" +
49 "}," +
50 // config entity 2
51 "{" +
52 "\"name\": \"1-1-E1_LPORT\"," +
53 "\"type\": \"OCH\"," +
54 "\"annotations\": {" +
55 "\"staticLambda\": 1," +
56 "\"staticPort\": \"1-22-E1_WPORT\"" +
57 "}" +
58 "}" +
59 "]" +
60 "}";
61
62 private final ConfigApplyDelegate delegate = new MockCfgDelegate();
63 private final ObjectMapper mapper = new ObjectMapper();
64
65 // one OPC per port in DEMOTREE
66 private List<OpticalPortConfig> opcl = Lists.newArrayList();
67 // JsonNodes representing each port.
68 private List<JsonNode> testNodes = Lists.newArrayList();
69
70 @Before
71 public void setUp() {
72 try {
73 JsonNode tree = new ObjectMapper().readTree(DEMOTREE);
74 Iterator<JsonNode> pitr = tree.get(FIELD).elements();
75 while (pitr.hasNext()) {
76 // initialize a config entity, add to lists
77 JsonNode jn = pitr.next();
78 OpticalPortConfig opc = new OpticalPortConfig();
79 ObjectNode node = JsonNodeFactory.instance.objectNode();
80 opc.init(CPT, KEY, node, mapper, delegate);
81
82 testNodes.add(jn);
83 opcl.add(opc);
84 }
85 } catch (IOException e) {
86 e.printStackTrace();
87 }
88 }
89
90 @Test
91 public void testBaseAttrs() {
92 // configs 0 and 1 - port with and without alphanumeric names
93 OpticalPortConfig op0 = opcl.get(0);
94 OpticalPortConfig op1 = opcl.get(1);
95 // config 2 - no name
96 OpticalPortConfig op2 = opcl.get(2);
97 JsonNode jn0 = testNodes.get(0);
98 JsonNode jn1 = testNodes.get(1);
99
100 op0.portType(Port.Type.valueOf(jn0.path(TYPE).asText()))
101 .portName(jn0.path(NAME).asText());
102 op1.portType(Port.Type.valueOf(jn1.path(TYPE).asText()))
103 .portNumberName(jn1.path(PORT).asLong());
104
105 assertEquals(Port.Type.OMS, op0.type());
106 assertEquals(jn0.path(NAME).asText(), op0.name());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700107 assertEquals(jn1.path(PORT).asText(), op1.numberName());
108 assertEquals("", op1.name());
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700109 assertEquals("", op2.name());
110 }
111
112 @Test
113 public void testAdditionalAttrs() {
114 // config 1 has no annotations, 2 has predefined ones
115 OpticalPortConfig op1 = opcl.get(1);
116 OpticalPortConfig op2 = opcl.get(2);
117 JsonNode jn2 = testNodes.get(2);
118 Long sl = 1L;
119
120 // see config entity 2 in DEMOTREE
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700121 op2.staticLambda(jn2.path("annotations").path(STATIC_LAMBDA).asLong());
122 op2.staticPort(jn2.path("annotations").path(STATIC_PORT).asText());
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700123
124 assertEquals(sl, op2.staticLambda().get());
125 assertFalse(op1.staticLambda().isPresent());
126 assertEquals("1-22-E1_WPORT", op2.staticPort());
127 assertEquals("", op1.staticPort());
128
129 op2.staticLambda(null);
130 assertFalse(op2.staticLambda().isPresent());
131 }
132
133 private class MockCfgDelegate implements ConfigApplyDelegate {
134
135 @Override
136 public void onApply(@SuppressWarnings("rawtypes") Config config) {
137 config.apply();
138 }
139
140 }
141}