Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.config; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 20 | import org.onosproject.net.CltSignalType; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 21 | import org.onosproject.net.config.Config; |
| 22 | import org.onosproject.net.config.ConfigApplyDelegate; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 23 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 24 | import org.onosproject.net.AnnotationKeys; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
| 27 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Port; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.SparseAnnotations; |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 31 | import org.onosproject.net.device.PortDescription; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 32 | |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 33 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 34 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 35 | |
| 36 | import static org.junit.Assert.assertEquals; |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 37 | import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 38 | |
| 39 | public class OpticalPortOperatorTest { |
| 40 | private static final DeviceId DID = DeviceId.deviceId("op-test"); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 41 | private static final long PORT_NUMBER = 100; |
| 42 | private static final String CFG_KEY = "optical"; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 43 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 44 | private static final String CFG_PORT_NAME = "cfg-name"; |
| 45 | private static final long CFG_STATIC_LAMBDA = 300L; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 46 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 47 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 52 | private static final SparseAnnotations SA = DefaultAnnotations.builder() |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 53 | .set(AnnotationKeys.STATIC_PORT, DESC_STATIC_PORT) |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 54 | .build(); |
| 55 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 56 | private static final PortDescription N_DESC = oduCltPortDescription( |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 57 | NAMED, true, CltSignalType.CLT_100GBE, SA); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 58 | private static final PortDescription U_DESC = oduCltPortDescription( |
| 59 | UNNAMED, true, CltSignalType.CLT_100GBE, SA); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 60 | |
| 61 | private final ConfigApplyDelegate delegate = new MockCfgDelegate(); |
| 62 | private final ObjectMapper mapper = new ObjectMapper(); |
| 63 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 64 | private static final ConnectPoint CP = new ConnectPoint(DID, UNNAMED); |
| 65 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 66 | private OpticalPortConfig opc; |
| 67 | |
| 68 | private OpticalPortOperator oper; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 69 | |
| 70 | @Before |
| 71 | public void setUp() { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 72 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 77 | } |
| 78 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 79 | @Test |
| 80 | public void testConfigPortName() { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 81 | opc.portType(Port.Type.ODUCLT) |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 82 | .portNumberName(PORT_NUMBER) |
| 83 | .portName(CFG_PORT_NAME); |
| 84 | |
| 85 | PortDescription res; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 86 | // full desc + opc with name |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 87 | res = oper.combine(CP, N_DESC); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 88 | 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 HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 92 | res = oper.combine(CP, U_DESC); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 93 | assertEquals("Configured port name expected", |
| 94 | CFG_PORT_NAME, res.portNumber().name()); |
| 95 | assertEquals(DESC_STATIC_PORT, res.annotations().value(AnnotationKeys.STATIC_PORT)); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 96 | } |
| 97 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 98 | @Test |
| 99 | public void testConfigAddStaticLambda() { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 100 | opc.portType(Port.Type.ODUCLT) |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 101 | .portNumberName(PORT_NUMBER) |
| 102 | .staticLambda(CFG_STATIC_LAMBDA); |
| 103 | |
| 104 | PortDescription res; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 105 | res = oper.combine(CP, N_DESC); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 106 | 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 HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 115 | opc.portType(Port.Type.ODUCLT) |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 116 | .portNumberName(PORT_NUMBER); |
| 117 | |
| 118 | PortDescription res; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 119 | res = oper.combine(CP, N_DESC); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 120 | 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 HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 126 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 140 | private class MockCfgDelegate implements ConfigApplyDelegate { |
| 141 | |
| 142 | @Override |
| 143 | public void onApply(@SuppressWarnings("rawtypes") Config config) { |
| 144 | config.apply(); |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | } |