Andrea Campanella | e1e3e44 | 2019-10-21 13:45:32 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.odtn.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreServiceAdapter; |
| 25 | import org.onosproject.core.DefaultApplicationId; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DefaultLink; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.GridType; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.OchSignal; |
| 32 | import org.onosproject.net.Path; |
| 33 | import org.onosproject.net.link.LinkServiceAdapter; |
| 34 | import org.onosproject.net.provider.ProviderId; |
| 35 | import org.onosproject.store.service.TestStorageService; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.util.HashMap; |
| 39 | import java.util.Iterator; |
| 40 | import java.util.List; |
| 41 | import java.util.Map; |
| 42 | import java.util.Set; |
| 43 | import java.util.stream.Collectors; |
| 44 | import java.util.stream.StreamSupport; |
| 45 | |
| 46 | import static junit.framework.Assert.assertTrue; |
| 47 | import static junit.framework.TestCase.assertEquals; |
| 48 | |
| 49 | /** |
| 50 | * Test for parsing gNPY json files. |
| 51 | */ |
| 52 | public class GnpyManagerTest { |
| 53 | |
| 54 | private static final String REQUEST = "{\"path-request\":[{\"request-id\":\"onos-0\"," + |
| 55 | "\"source\":\"netconf:10.0.254.93:830\"," + |
| 56 | "\"destination\":\"netconf:10.0.254.94:830\",\"src-tp-id\":" + |
| 57 | "\"netconf:10.0.254.93:830\",\"dst-tp-id\":" + |
| 58 | "\"netconf:10.0.254.94:830\",\"bidirectional\":true," + |
| 59 | "\"path-constraints\":{\"te-bandwidth\":" + |
| 60 | "{\"technology\":\"flexi-grid\",\"trx_type\":\"Cassini\"," + |
| 61 | "\"trx_mode\":null,\"effective-freq-slot\":" + |
| 62 | "[{\"N\":\"null\",\"M\":\"null\"}],\"spacing\":5.0E10," + |
| 63 | "\"max-nb-of-channel\":null,\"output-power\":null," + |
| 64 | "\"path_bandwidth\":1.0E11}}}]}"; |
| 65 | |
| 66 | private ConnectPoint tx1 = ConnectPoint.fromString("netconf:10.0.254.93:830/1"); |
| 67 | private ConnectPoint rdm1tx1 = ConnectPoint.fromString("netconf:10.0.254.107:830/1"); |
| 68 | private ConnectPoint rdm1ln1 = ConnectPoint.fromString("netconf:10.0.254.107:830/2"); |
| 69 | private ConnectPoint ln1rdm1 = ConnectPoint.fromString("netconf:10.0.254.101:830/1"); |
| 70 | private ConnectPoint ln1ln2 = ConnectPoint.fromString("netconf:10.0.254.101:830/2"); |
| 71 | private ConnectPoint ln2ln1 = ConnectPoint.fromString("netconf:10.0.254.102:830/1"); |
| 72 | private ConnectPoint ln2rdm2 = ConnectPoint.fromString("netconf:10.0.254.102:830/2"); |
| 73 | private ConnectPoint rdm2ln2 = ConnectPoint.fromString("netconf:10.0.254.225:830/1"); |
| 74 | private ConnectPoint rdm2tx2 = ConnectPoint.fromString("netconf:10.0.254.225:830/2"); |
| 75 | private ConnectPoint tx2 = ConnectPoint.fromString("netconf:10.0.254.94:830/1"); |
| 76 | private Link tx1rdm1Link = DefaultLink.builder().type(Link.Type.OPTICAL) |
| 77 | .providerId(ProviderId.NONE).src(rdm1tx1).dst(tx1).build(); |
| 78 | private Link rmd1ln1Link = DefaultLink.builder().type(Link.Type.OPTICAL) |
| 79 | .providerId(ProviderId.NONE).src(ln1rdm1).dst(rdm1ln1).build(); |
| 80 | private Link ln1ln2Link = DefaultLink.builder().type(Link.Type.OPTICAL) |
| 81 | .providerId(ProviderId.NONE).src(ln2ln1).dst(ln1ln2).build(); |
| 82 | private Link ln2rdm2Link = DefaultLink.builder().type(Link.Type.OPTICAL) |
| 83 | .providerId(ProviderId.NONE).src(rdm2ln2).dst(ln2rdm2).build(); |
| 84 | private Link tx2rmd2Link = DefaultLink.builder().type(Link.Type.OPTICAL) |
| 85 | .providerId(ProviderId.NONE).src(tx2).dst(rdm2tx2).build(); |
| 86 | |
| 87 | private GnpyManager manager; |
| 88 | private JsonNode reply; |
| 89 | |
| 90 | @Before |
| 91 | public void setUp() throws Exception { |
| 92 | ObjectMapper mapper = new ObjectMapper(); |
| 93 | reply = mapper.readTree(this.getClass().getResourceAsStream("gnpy-response.json")); |
| 94 | manager = new GnpyManager(); |
| 95 | manager.storageService = new TestStorageService(); |
| 96 | manager.linkService = new InternalLinkService(); |
| 97 | manager.coreService = new InternalCoreService(); |
| 98 | manager.activate(); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testCreateSuggestedPath() throws IOException { |
| 103 | Map<DeviceId, Double> deviceAtoBPowerMap = new HashMap<>(); |
| 104 | Map<DeviceId, Double> deviceBtoAPowerMap = new HashMap<>(); |
| 105 | List<DeviceId> deviceIds = manager.getDeviceAndPopulatePowerMap(reply, deviceAtoBPowerMap, |
| 106 | deviceBtoAPowerMap, "second"); |
| 107 | Path path = manager.createSuggestedPath(deviceIds); |
| 108 | assertTrue(path.links().contains(tx1rdm1Link)); |
| 109 | assertTrue(path.links().contains(rmd1ln1Link)); |
| 110 | assertTrue(path.links().contains(ln1ln2Link)); |
| 111 | assertTrue(path.links().contains(ln2rdm2Link)); |
| 112 | assertTrue(path.links().contains(tx2rmd2Link)); |
| 113 | assertEquals(path.src(), tx2); |
| 114 | assertEquals(path.dst(), tx1); |
| 115 | |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testgetDevicePowerMap() throws IOException { |
| 120 | Map<DeviceId, Double> deviceAtoBPowerMap = new HashMap<>(); |
| 121 | Map<DeviceId, Double> deviceBtoAPowerMap = new HashMap<>(); |
| 122 | manager.getDeviceAndPopulatePowerMap(reply, deviceAtoBPowerMap, deviceBtoAPowerMap, "second"); |
| 123 | assertEquals(-25.0, deviceAtoBPowerMap.get(DeviceId.deviceId("netconf:10.0.254.107:830"))); |
| 124 | assertEquals(-12.0, deviceAtoBPowerMap.get(DeviceId.deviceId("netconf:10.0.254.225:830"))); |
| 125 | assertEquals(-12.0, deviceBtoAPowerMap.get(DeviceId.deviceId("netconf:10.0.254.225:830"))); |
| 126 | assertEquals(-25.0, deviceBtoAPowerMap.get(DeviceId.deviceId("netconf:10.0.254.107:830"))); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testGetLaunchPower() throws IOException { |
| 131 | double power = manager.getLaunchPower(reply); |
| 132 | assertEquals(0.0, power); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testGetPerHopPower() throws IOException { |
| 137 | JsonNode response = reply.get("result").get("response"); |
| 138 | //getting the a-b path. |
| 139 | JsonNode responseObj = response.elements() |
| 140 | .next(); |
| 141 | Iterator<JsonNode> elements = responseObj.get("path-properties") |
| 142 | .get("path-route-objects").elements(); |
| 143 | Iterable<JsonNode> iterable = () -> elements; |
| 144 | List<JsonNode> elementsList = StreamSupport |
| 145 | .stream(iterable.spliterator(), false) |
| 146 | .collect(Collectors.toList()); |
| 147 | double power = manager.getPerHopPower(elementsList.get(5)); |
| 148 | assertEquals(-12.0, power); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testGetOsnr() throws IOException { |
| 153 | double osnr = manager.getOsnr(reply, "second"); |
| 154 | assertEquals(23.47, osnr); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void testCreateOchSignal() throws IOException { |
| 159 | OchSignal signal = manager.createOchSignal(reply); |
| 160 | System.out.println(signal); |
| 161 | assertEquals(signal.gridType(), GridType.DWDM); |
| 162 | assertEquals(signal.slotWidth().asGHz(), 50.000); |
| 163 | assertEquals(-35, signal.spacingMultiplier()); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testCreateGnpyRequest() { |
| 168 | ConnectPoint ingress = ConnectPoint.fromString("netconf:10.0.254.93:830/1"); |
| 169 | ConnectPoint egress = ConnectPoint.fromString("netconf:10.0.254.94:830/1"); |
| 170 | String output = manager.createGnpyRequest(ingress, egress, true).toString(); |
| 171 | System.out.println(output); |
| 172 | assertEquals("Json to create network connectivity is wrong", REQUEST, output); |
| 173 | } |
| 174 | |
| 175 | private class InternalLinkService extends LinkServiceAdapter { |
| 176 | @Override |
| 177 | public Set<Link> getDeviceLinks(DeviceId deviceId) { |
| 178 | if (deviceId.equals(DeviceId.deviceId("netconf:10.0.254.94:830"))) { |
| 179 | return ImmutableSet.of(tx2rmd2Link); |
| 180 | } else if (deviceId.equals(DeviceId.deviceId("netconf:10.0.254.107:830"))) { |
| 181 | return ImmutableSet.of(tx1rdm1Link); |
| 182 | } else if (deviceId.equals(DeviceId.deviceId("netconf:10.0.254.101:830"))) { |
| 183 | return ImmutableSet.of(rmd1ln1Link); |
| 184 | } else if (deviceId.equals(DeviceId.deviceId("netconf:10.0.254.102:830"))) { |
| 185 | return ImmutableSet.of(ln1ln2Link); |
| 186 | } else if (deviceId.equals(DeviceId.deviceId("netconf:10.0.254.225:830"))) { |
| 187 | return ImmutableSet.of(ln2rdm2Link); |
| 188 | } |
| 189 | return ImmutableSet.of(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private class InternalCoreService extends CoreServiceAdapter { |
| 194 | @Override |
| 195 | public ApplicationId getAppId(String name) { |
| 196 | return new DefaultApplicationId(1, name); |
| 197 | } |
| 198 | } |
| 199 | // private class InternalStoreService extends StorageServiceAdapter { |
| 200 | // @Override |
| 201 | // public AtomicCounterBuilder atomicCounterBuilder() { |
| 202 | // return TestAtomicCounter.builder(); |
| 203 | // } |
| 204 | // } |
| 205 | } |