blob: 11b19fae96ace6d98d97448d6aaeadfac745cdc0 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska58de4162015-09-10 16:15:33 -07003 *
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 */
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070016package org.onosproject.net.optical.config;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070017
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070020import static org.onosproject.net.optical.config.OpticalPortConfig.NAME;
21import static org.onosproject.net.optical.config.OpticalPortConfig.PORT;
22import static org.onosproject.net.optical.config.OpticalPortConfig.STATIC_LAMBDA;
23import static org.onosproject.net.optical.config.OpticalPortConfig.STATIC_PORT;
24import static org.onosproject.net.optical.config.OpticalPortConfig.TYPE;
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;
44
45public class OpticalPortConfigTest {
46 private static final String FIELD = "ports";
47 private static final String KEY = "opc-test";
48
49 private static final DeviceId DID = DeviceId.deviceId(KEY);
50 private static final PortNumber PN = PortNumber.portNumber(100);
51 private static final ConnectPoint CPT = new ConnectPoint(DID, PN);
52 private static final String DEMOTREE = "{" +
53 "\"ports\": [" +
54 // config entity 0
55 "{" +
56 "\"name\": \"1-10-E1_WPORT\"," +
57 "\"type\": \"OMS\"" +
58 "}," +
59 // config entity 1
60 "{" +
61 "\"type\": \"OCH\"," +
62 "\"speed\": 0," +
63 "\"port\": 10" +
64 "}," +
65 // config entity 2
66 "{" +
67 "\"name\": \"1-1-E1_LPORT\"," +
68 "\"type\": \"OCH\"," +
69 "\"annotations\": {" +
70 "\"staticLambda\": 1," +
71 "\"staticPort\": \"1-22-E1_WPORT\"" +
72 "}" +
73 "}" +
74 "]" +
75 "}";
76
77 private final ConfigApplyDelegate delegate = new MockCfgDelegate();
78 private final ObjectMapper mapper = new ObjectMapper();
79
80 // one OPC per port in DEMOTREE
81 private List<OpticalPortConfig> opcl = Lists.newArrayList();
82 // JsonNodes representing each port.
83 private List<JsonNode> testNodes = Lists.newArrayList();
84
85 @Before
86 public void setUp() {
87 try {
88 JsonNode tree = new ObjectMapper().readTree(DEMOTREE);
89 Iterator<JsonNode> pitr = tree.get(FIELD).elements();
90 while (pitr.hasNext()) {
91 // initialize a config entity, add to lists
92 JsonNode jn = pitr.next();
93 OpticalPortConfig opc = new OpticalPortConfig();
94 ObjectNode node = JsonNodeFactory.instance.objectNode();
95 opc.init(CPT, KEY, node, mapper, delegate);
96
97 testNodes.add(jn);
98 opcl.add(opc);
99 }
100 } catch (IOException e) {
101 e.printStackTrace();
102 }
103 }
104
105 @Test
106 public void testBaseAttrs() {
107 // configs 0 and 1 - port with and without alphanumeric names
108 OpticalPortConfig op0 = opcl.get(0);
109 OpticalPortConfig op1 = opcl.get(1);
110 // config 2 - no name
111 OpticalPortConfig op2 = opcl.get(2);
112 JsonNode jn0 = testNodes.get(0);
113 JsonNode jn1 = testNodes.get(1);
114
115 op0.portType(Port.Type.valueOf(jn0.path(TYPE).asText()))
116 .portName(jn0.path(NAME).asText());
117 op1.portType(Port.Type.valueOf(jn1.path(TYPE).asText()))
118 .portNumberName(jn1.path(PORT).asLong());
119
120 assertEquals(Port.Type.OMS, op0.type());
121 assertEquals(jn0.path(NAME).asText(), op0.name());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700122 assertEquals(jn1.path(PORT).asText(), op1.numberName());
123 assertEquals("", op1.name());
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700124 assertEquals("", op2.name());
125 }
126
127 @Test
128 public void testAdditionalAttrs() {
129 // config 1 has no annotations, 2 has predefined ones
130 OpticalPortConfig op1 = opcl.get(1);
131 OpticalPortConfig op2 = opcl.get(2);
132 JsonNode jn2 = testNodes.get(2);
133 Long sl = 1L;
134
135 // see config entity 2 in DEMOTREE
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700136 op2.staticLambda(jn2.path("annotations").path(STATIC_LAMBDA).asLong());
137 op2.staticPort(jn2.path("annotations").path(STATIC_PORT).asText());
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700138
139 assertEquals(sl, op2.staticLambda().get());
140 assertFalse(op1.staticLambda().isPresent());
141 assertEquals("1-22-E1_WPORT", op2.staticPort());
142 assertEquals("", op1.staticPort());
143
144 op2.staticLambda(null);
145 assertFalse(op2.staticLambda().isPresent());
146 }
147
148 private class MockCfgDelegate implements ConfigApplyDelegate {
149
150 @Override
151 public void onApply(@SuppressWarnings("rawtypes") Config config) {
152 config.apply();
153 }
154
155 }
156}