1 package org.onosproject.incubator.net.config.basics;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.onosproject.net.config.basics.OpticalPortConfig.TYPE;
6 import static org.onosproject.net.config.basics.OpticalPortConfig.NAME;
7 import static org.onosproject.net.config.basics.OpticalPortConfig.PORT;
8 import static org.onosproject.net.config.basics.OpticalPortConfig.STATIC_LAMBDA;
9 import static org.onosproject.net.config.basics.OpticalPortConfig.STATIC_PORT;
11 import java.io.IOException;
12 import java.util.Iterator;
13 import java.util.List;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.onosproject.net.config.Config;
18 import org.onosproject.net.config.ConfigApplyDelegate;
19 import org.onosproject.net.ConnectPoint;
20 import org.onosproject.net.DeviceId;
21 import org.onosproject.net.Port;
22 import org.onosproject.net.PortNumber;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
27 import com.fasterxml.jackson.databind.node.ObjectNode;
28 import com.google.common.collect.Lists;
29 import org.onosproject.net.config.basics.OpticalPortConfig;
31 public class OpticalPortConfigTest {
32 private static final String FIELD = "ports";
33 private static final String KEY = "opc-test";
35 private static final DeviceId DID = DeviceId.deviceId(KEY);
36 private static final PortNumber PN = PortNumber.portNumber(100);
37 private static final ConnectPoint CPT = new ConnectPoint(DID, PN);
38 private static final String DEMOTREE = "{" +
42 "\"name\": \"1-10-E1_WPORT\"," +
47 "\"type\": \"OCH\"," +
53 "\"name\": \"1-1-E1_LPORT\"," +
54 "\"type\": \"OCH\"," +
55 "\"annotations\": {" +
56 "\"staticLambda\": 1," +
57 "\"staticPort\": \"1-22-E1_WPORT\"" +
63 private final ConfigApplyDelegate delegate = new MockCfgDelegate();
64 private final ObjectMapper mapper = new ObjectMapper();
66 // one OPC per port in DEMOTREE
67 private List<OpticalPortConfig> opcl = Lists.newArrayList();
68 // JsonNodes representing each port.
69 private List<JsonNode> testNodes = Lists.newArrayList();
74 JsonNode tree = new ObjectMapper().readTree(DEMOTREE);
75 Iterator<JsonNode> pitr = tree.get(FIELD).elements();
76 while (pitr.hasNext()) {
77 // initialize a config entity, add to lists
78 JsonNode jn = pitr.next();
79 OpticalPortConfig opc = new OpticalPortConfig();
80 ObjectNode node = JsonNodeFactory.instance.objectNode();
81 opc.init(CPT, KEY, node, mapper, delegate);
86 } catch (IOException e) {
92 public void testBaseAttrs() {
93 // configs 0 and 1 - port with and without alphanumeric names
94 OpticalPortConfig op0 = opcl.get(0);
95 OpticalPortConfig op1 = opcl.get(1);
97 OpticalPortConfig op2 = opcl.get(2);
98 JsonNode jn0 = testNodes.get(0);
99 JsonNode jn1 = testNodes.get(1);
101 op0.portType(Port.Type.valueOf(jn0.path(TYPE).asText()))
102 .portName(jn0.path(NAME).asText());
103 op1.portType(Port.Type.valueOf(jn1.path(TYPE).asText()))
104 .portNumberName(jn1.path(PORT).asLong());
106 assertEquals(Port.Type.OMS, op0.type());
107 assertEquals(jn0.path(NAME).asText(), op0.name());
108 assertEquals(jn1.path(PORT).asText(), op1.numberName());
109 assertEquals("", op1.name());
110 assertEquals("", op2.name());
114 public void testAdditionalAttrs() {
115 // config 1 has no annotations, 2 has predefined ones
116 OpticalPortConfig op1 = opcl.get(1);
117 OpticalPortConfig op2 = opcl.get(2);
118 JsonNode jn2 = testNodes.get(2);
121 // see config entity 2 in DEMOTREE
122 op2.staticLambda(jn2.path("annotations").path(STATIC_LAMBDA).asLong());
123 op2.staticPort(jn2.path("annotations").path(STATIC_PORT).asText());
125 assertEquals(sl, op2.staticLambda().get());
126 assertFalse(op1.staticLambda().isPresent());
127 assertEquals("1-22-E1_WPORT", op2.staticPort());
128 assertEquals("", op1.staticPort());
130 op2.staticLambda(null);
131 assertFalse(op2.staticLambda().isPresent());
134 private class MockCfgDelegate implements ConfigApplyDelegate {
137 public void onApply(@SuppressWarnings("rawtypes") Config config) {