blob: b207d037dc760caf7cc09fe5de2bfa1acb21ce4f [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 Koshibee39c23a2015-08-03 18:17:43 -070017
18import org.junit.Before;
19import org.junit.Test;
Toru Furusawa72ee30c2016-01-08 13:29:04 -080020import org.onosproject.net.CltSignalType;
Ray Milkeya4122362015-08-18 15:19:08 -070021import org.onosproject.net.config.Config;
22import org.onosproject.net.config.ConfigApplyDelegate;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070023import org.onosproject.net.config.NetworkConfigServiceAdapter;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070024import org.onosproject.net.AnnotationKeys;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DefaultAnnotations;
27import org.onosproject.net.DeviceId;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070028import org.onosproject.net.Port;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.SparseAnnotations;
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070031import org.onosproject.net.device.PortDescription;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070032
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070033import com.fasterxml.jackson.databind.ObjectMapper;
34import com.fasterxml.jackson.databind.node.JsonNodeFactory;
35
36import static org.junit.Assert.assertEquals;
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070037import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070038
39public class OpticalPortOperatorTest {
40 private static final DeviceId DID = DeviceId.deviceId("op-test");
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070041 private static final long PORT_NUMBER = 100;
42 private static final String CFG_KEY = "optical";
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070043
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070044 private static final String CFG_PORT_NAME = "cfg-name";
45 private static final long CFG_STATIC_LAMBDA = 300L;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070046
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070047 private static final String DESC_PORT_NAME = "test-port-100";
48 private static final PortNumber NAMED = PortNumber.portNumber(PORT_NUMBER, DESC_PORT_NAME);
49 private static final PortNumber UNNAMED = PortNumber.portNumber(PORT_NUMBER);
50
51 private static final String DESC_STATIC_PORT = "out-port-200";
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070052 private static final SparseAnnotations SA = DefaultAnnotations.builder()
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070053 .set(AnnotationKeys.STATIC_PORT, DESC_STATIC_PORT)
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070054 .build();
55
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070056 private static final PortDescription N_DESC = oduCltPortDescription(
Toru Furusawa72ee30c2016-01-08 13:29:04 -080057 NAMED, true, CltSignalType.CLT_100GBE, SA);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070058 private static final PortDescription U_DESC = oduCltPortDescription(
59 UNNAMED, true, CltSignalType.CLT_100GBE, SA);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070060
61 private final ConfigApplyDelegate delegate = new MockCfgDelegate();
62 private final ObjectMapper mapper = new ObjectMapper();
63
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070064 private static final ConnectPoint CP = new ConnectPoint(DID, UNNAMED);
65
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070066 private OpticalPortConfig opc;
67
68 private OpticalPortOperator oper;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070069
70 @Before
71 public void setUp() {
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070072 opc = new OpticalPortConfig();
73 opc.init(CP, CFG_KEY, JsonNodeFactory.instance.objectNode(), mapper, delegate);
74
75 oper = new OpticalPortOperator();
76 oper.bindService(new MockNetworkConfigService());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070077 }
78
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070079 @Test
80 public void testConfigPortName() {
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070081 opc.portType(Port.Type.ODUCLT)
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070082 .portNumberName(PORT_NUMBER)
83 .portName(CFG_PORT_NAME);
84
85 PortDescription res;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070086 // full desc + opc with name
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070087 res = oper.combine(CP, N_DESC);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070088 assertEquals("Configured port name expected",
89 CFG_PORT_NAME, res.portNumber().name());
90 assertEquals(DESC_STATIC_PORT, res.annotations().value(AnnotationKeys.STATIC_PORT));
91
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070092 res = oper.combine(CP, U_DESC);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070093 assertEquals("Configured port name expected",
94 CFG_PORT_NAME, res.portNumber().name());
95 assertEquals(DESC_STATIC_PORT, res.annotations().value(AnnotationKeys.STATIC_PORT));
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070096 }
97
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070098 @Test
99 public void testConfigAddStaticLambda() {
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700100 opc.portType(Port.Type.ODUCLT)
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700101 .portNumberName(PORT_NUMBER)
102 .staticLambda(CFG_STATIC_LAMBDA);
103
104 PortDescription res;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700105 res = oper.combine(CP, N_DESC);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700106 assertEquals("Original port name expected",
107 DESC_PORT_NAME, res.portNumber().name());
108 assertEquals(DESC_STATIC_PORT, res.annotations().value(AnnotationKeys.STATIC_PORT));
109 long sl = Long.valueOf(res.annotations().value(AnnotationKeys.STATIC_LAMBDA));
110 assertEquals(CFG_STATIC_LAMBDA, sl);
111 }
112
113 @Test
114 public void testEmptyConfig() {
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700115 opc.portType(Port.Type.ODUCLT)
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700116 .portNumberName(PORT_NUMBER);
117
118 PortDescription res;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700119 res = oper.combine(CP, N_DESC);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700120 assertEquals("Configured port name expected",
121 DESC_PORT_NAME, res.portNumber().name());
122 assertEquals(DESC_STATIC_PORT, res.annotations().value(AnnotationKeys.STATIC_PORT));
123 }
124
125
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700126 private class MockNetworkConfigService
127 extends NetworkConfigServiceAdapter {
128
129 @Override
130 public <S, C extends Config<S>> C getConfig(S subject,
131 Class<C> configClass) {
132 if (configClass == OpticalPortConfig.class) {
133 return (C) opc;
134 }
135 return null;
136 }
137 }
138
139
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700140 private class MockCfgDelegate implements ConfigApplyDelegate {
141
142 @Override
143 public void onApply(@SuppressWarnings("rawtypes") Config config) {
144 config.apply();
145 }
146
147 }
148}