blob: f2b15b096de71b675eef02b38ac3afb8a201cc21 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070016package org.onosproject.incubator.net.config.basics;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070020import static org.onosproject.net.config.basics.OpticalPortConfig.TYPE;
21import static org.onosproject.net.config.basics.OpticalPortConfig.NAME;
22import static org.onosproject.net.config.basics.OpticalPortConfig.PORT;
23import static org.onosproject.net.config.basics.OpticalPortConfig.STATIC_LAMBDA;
24import static org.onosproject.net.config.basics.OpticalPortConfig.STATIC_PORT;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070025
26import java.io.IOException;
27import java.util.Iterator;
28import java.util.List;
29
30import org.junit.Before;
31import org.junit.Test;
Ray Milkeya4122362015-08-18 15:19:08 -070032import org.onosproject.net.config.Config;
33import org.onosproject.net.config.ConfigApplyDelegate;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070034import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.Port;
37import org.onosproject.net.PortNumber;
38
39import com.fasterxml.jackson.databind.JsonNode;
40import com.fasterxml.jackson.databind.ObjectMapper;
41import com.fasterxml.jackson.databind.node.JsonNodeFactory;
42import com.fasterxml.jackson.databind.node.ObjectNode;
43import com.google.common.collect.Lists;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070044import org.onosproject.net.config.basics.OpticalPortConfig;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070045
46public class OpticalPortConfigTest {
47 private static final String FIELD = "ports";
48 private static final String KEY = "opc-test";
49
50 private static final DeviceId DID = DeviceId.deviceId(KEY);
51 private static final PortNumber PN = PortNumber.portNumber(100);
52 private static final ConnectPoint CPT = new ConnectPoint(DID, PN);
53 private static final String DEMOTREE = "{" +
54 "\"ports\": [" +
55 // config entity 0
56 "{" +
57 "\"name\": \"1-10-E1_WPORT\"," +
58 "\"type\": \"OMS\"" +
59 "}," +
60 // config entity 1
61 "{" +
62 "\"type\": \"OCH\"," +
63 "\"speed\": 0," +
64 "\"port\": 10" +
65 "}," +
66 // config entity 2
67 "{" +
68 "\"name\": \"1-1-E1_LPORT\"," +
69 "\"type\": \"OCH\"," +
70 "\"annotations\": {" +
71 "\"staticLambda\": 1," +
72 "\"staticPort\": \"1-22-E1_WPORT\"" +
73 "}" +
74 "}" +
75 "]" +
76 "}";
77
78 private final ConfigApplyDelegate delegate = new MockCfgDelegate();
79 private final ObjectMapper mapper = new ObjectMapper();
80
81 // one OPC per port in DEMOTREE
82 private List<OpticalPortConfig> opcl = Lists.newArrayList();
83 // JsonNodes representing each port.
84 private List<JsonNode> testNodes = Lists.newArrayList();
85
86 @Before
87 public void setUp() {
88 try {
89 JsonNode tree = new ObjectMapper().readTree(DEMOTREE);
90 Iterator<JsonNode> pitr = tree.get(FIELD).elements();
91 while (pitr.hasNext()) {
92 // initialize a config entity, add to lists
93 JsonNode jn = pitr.next();
94 OpticalPortConfig opc = new OpticalPortConfig();
95 ObjectNode node = JsonNodeFactory.instance.objectNode();
96 opc.init(CPT, KEY, node, mapper, delegate);
97
98 testNodes.add(jn);
99 opcl.add(opc);
100 }
101 } catch (IOException e) {
102 e.printStackTrace();
103 }
104 }
105
106 @Test
107 public void testBaseAttrs() {
108 // configs 0 and 1 - port with and without alphanumeric names
109 OpticalPortConfig op0 = opcl.get(0);
110 OpticalPortConfig op1 = opcl.get(1);
111 // config 2 - no name
112 OpticalPortConfig op2 = opcl.get(2);
113 JsonNode jn0 = testNodes.get(0);
114 JsonNode jn1 = testNodes.get(1);
115
116 op0.portType(Port.Type.valueOf(jn0.path(TYPE).asText()))
117 .portName(jn0.path(NAME).asText());
118 op1.portType(Port.Type.valueOf(jn1.path(TYPE).asText()))
119 .portNumberName(jn1.path(PORT).asLong());
120
121 assertEquals(Port.Type.OMS, op0.type());
122 assertEquals(jn0.path(NAME).asText(), op0.name());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700123 assertEquals(jn1.path(PORT).asText(), op1.numberName());
124 assertEquals("", op1.name());
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700125 assertEquals("", op2.name());
126 }
127
128 @Test
129 public void testAdditionalAttrs() {
130 // config 1 has no annotations, 2 has predefined ones
131 OpticalPortConfig op1 = opcl.get(1);
132 OpticalPortConfig op2 = opcl.get(2);
133 JsonNode jn2 = testNodes.get(2);
134 Long sl = 1L;
135
136 // see config entity 2 in DEMOTREE
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700137 op2.staticLambda(jn2.path("annotations").path(STATIC_LAMBDA).asLong());
138 op2.staticPort(jn2.path("annotations").path(STATIC_PORT).asText());
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700139
140 assertEquals(sl, op2.staticLambda().get());
141 assertFalse(op1.staticLambda().isPresent());
142 assertEquals("1-22-E1_WPORT", op2.staticPort());
143 assertEquals("", op1.staticPort());
144
145 op2.staticLambda(null);
146 assertFalse(op2.staticLambda().isPresent());
147 }
148
149 private class MockCfgDelegate implements ConfigApplyDelegate {
150
151 @Override
152 public void onApply(@SuppressWarnings("rawtypes") Config config) {
153 config.apply();
154 }
155
156 }
157}