9d56ca231cca231dae10a8bae24e29206791b983
[onosfw.git] /
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.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;
10
11 import java.io.IOException;
12 import java.util.Iterator;
13 import java.util.List;
14
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;
23
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;
30
31 public class OpticalPortConfigTest {
32     private static final String FIELD = "ports";
33     private static final String KEY = "opc-test";
34
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 = "{" +
39             "\"ports\": [" +
40             // config entity 0
41                 "{" +
42                     "\"name\": \"1-10-E1_WPORT\"," +
43                     "\"type\": \"OMS\"" +
44                 "}," +
45             // config entity 1
46                 "{" +
47                     "\"type\": \"OCH\"," +
48                     "\"speed\": 0," +
49                     "\"port\": 10" +
50                 "}," +
51             // config entity 2
52                 "{" +
53                     "\"name\": \"1-1-E1_LPORT\"," +
54                     "\"type\": \"OCH\"," +
55                     "\"annotations\": {" +
56                         "\"staticLambda\": 1," +
57                         "\"staticPort\": \"1-22-E1_WPORT\"" +
58                     "}" +
59                 "}" +
60             "]" +
61             "}";
62
63     private final ConfigApplyDelegate delegate = new MockCfgDelegate();
64     private final ObjectMapper mapper = new ObjectMapper();
65
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();
70
71     @Before
72     public void setUp() {
73         try {
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);
82
83                 testNodes.add(jn);
84                 opcl.add(opc);
85             }
86         } catch (IOException e) {
87             e.printStackTrace();
88         }
89     }
90
91     @Test
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);
96         // config 2 - no name
97         OpticalPortConfig op2 = opcl.get(2);
98         JsonNode jn0 = testNodes.get(0);
99         JsonNode jn1 = testNodes.get(1);
100
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());
105
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());
111     }
112
113     @Test
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);
119         Long sl = 1L;
120
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());
124
125         assertEquals(sl, op2.staticLambda().get());
126         assertFalse(op1.staticLambda().isPresent());
127         assertEquals("1-22-E1_WPORT", op2.staticPort());
128         assertEquals("", op1.staticPort());
129
130         op2.staticLambda(null);
131         assertFalse(op2.staticLambda().isPresent());
132     }
133
134     private class MockCfgDelegate implements ConfigApplyDelegate {
135
136         @Override
137         public void onApply(@SuppressWarnings("rawtypes") Config config) {
138             config.apply();
139         }
140
141     }
142 }