blob: a30582e55701b622cddc2646a4ae7faf98259d31 [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;
8
9import java.io.IOException;
10import java.util.Iterator;
11import java.util.List;
12
13import org.junit.Before;
14import org.junit.Test;
15import org.onosproject.incubator.net.config.Config;
16import org.onosproject.incubator.net.config.ConfigApplyDelegate;
17import org.onosproject.net.AnnotationKeys;
18import org.onosproject.net.ConnectPoint;
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.Port;
21import org.onosproject.net.PortNumber;
22
23import com.fasterxml.jackson.databind.JsonNode;
24import com.fasterxml.jackson.databind.ObjectMapper;
25import com.fasterxml.jackson.databind.node.JsonNodeFactory;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27import com.google.common.collect.Lists;
28
29public 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}