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 | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 20 | import static org.onosproject.net.optical.config.OpticalPortConfig.NAME; |
| 21 | import static org.onosproject.net.optical.config.OpticalPortConfig.PORT; |
| 22 | import static org.onosproject.net.optical.config.OpticalPortConfig.STATIC_LAMBDA; |
| 23 | import static org.onosproject.net.optical.config.OpticalPortConfig.STATIC_PORT; |
| 24 | import static org.onosproject.net.optical.config.OpticalPortConfig.TYPE; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.util.Iterator; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 32 | import org.onosproject.net.config.Config; |
| 33 | import org.onosproject.net.config.ConfigApplyDelegate; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.Port; |
| 37 | import org.onosproject.net.PortNumber; |
| 38 | |
| 39 | import com.fasterxml.jackson.databind.JsonNode; |
| 40 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 41 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 42 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 43 | import com.google.common.collect.Lists; |
| 44 | |
| 45 | public 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 122 | assertEquals(jn1.path(PORT).asText(), op1.numberName()); |
| 123 | assertEquals("", op1.name()); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 124 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 136 | op2.staticLambda(jn2.path("annotations").path(STATIC_LAMBDA).asLong()); |
| 137 | op2.staticPort(jn2.path("annotations").path(STATIC_PORT).asText()); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 138 | |
| 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 | } |