Charles Chan | 3958b75 | 2018-07-13 18:08:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.segmentrouting.pwaas; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Rule; |
| 22 | import org.junit.Test; |
| 23 | import org.junit.rules.ExpectedException; |
| 24 | import org.onlab.junit.TestUtils; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DefaultPort; |
| 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.intf.Interface; |
| 35 | import org.onosproject.net.intf.InterfaceService; |
| 36 | import org.onosproject.segmentrouting.MockDevice; |
| 37 | |
| 38 | import static org.easymock.EasyMock.createNiceMock; |
| 39 | import static org.easymock.EasyMock.expect; |
| 40 | import static org.easymock.EasyMock.replay; |
| 41 | |
| 42 | public class PwaasUtilTest { |
| 43 | private static final DeviceId DID1 = DeviceId.deviceId("of:1"); |
| 44 | private static final DeviceId DID2 = DeviceId.deviceId("of:2"); |
| 45 | private static final DeviceId DID99 = DeviceId.deviceId("of:99"); |
| 46 | private static final PortNumber PN1 = PortNumber.portNumber(1); |
| 47 | private static final PortNumber PN2 = PortNumber.portNumber(2); |
| 48 | private static final PortNumber PN99 = PortNumber.portNumber(99); |
| 49 | private static final ConnectPoint CP11 = new ConnectPoint(DID1, PN1); |
| 50 | private static final ConnectPoint CP12 = new ConnectPoint(DID1, PN2); |
| 51 | private static final ConnectPoint CP21 = new ConnectPoint(DID2, PN1); |
| 52 | private static final ConnectPoint CP22 = new ConnectPoint(DID2, PN2); |
| 53 | private static final VlanId V1 = VlanId.vlanId((short) 1); |
| 54 | private static final VlanId V2 = VlanId.vlanId((short) 2); |
| 55 | private static final Device D1 = new MockDevice(DID1, null); |
| 56 | private static final Device D2 = new MockDevice(DID2, null); |
| 57 | private static final Port P11 = new DefaultPort(D1, PN1, true); |
| 58 | private static final Port P12 = new DefaultPort(D1, PN2, true); |
| 59 | private static final Port P21 = new DefaultPort(D2, PN1, true); |
| 60 | private static final Port P22 = new DefaultPort(D2, PN2, true); |
| 61 | private static final Interface I11 = new Interface("I11", CP11, Lists.newArrayList(), MacAddress.NONE, |
| 62 | VlanId.NONE, VlanId.NONE, Sets.newHashSet(VlanId.NONE), VlanId.NONE); |
| 63 | private static final Interface I12 = new Interface("I12", CP12, Lists.newArrayList(), MacAddress.NONE, |
| 64 | VlanId.NONE, VlanId.NONE, Sets.newHashSet(VlanId.NONE), VlanId.NONE); |
| 65 | private static final Interface I21 = new Interface("I21", CP21, Lists.newArrayList(), MacAddress.NONE, |
| 66 | VlanId.NONE, VlanId.NONE, Sets.newHashSet(VlanId.NONE), VlanId.NONE); |
| 67 | private static final Interface I22 = new Interface("I22", CP22, Lists.newArrayList(), MacAddress.NONE, |
| 68 | VlanId.NONE, VlanId.NONE, Sets.newHashSet(VlanId.NONE), VlanId.NONE); |
| 69 | |
| 70 | private ConnectPoint cp1; |
| 71 | private ConnectPoint cp2; |
| 72 | private VlanId ingressInner; |
| 73 | private VlanId ingressOuter; |
| 74 | private VlanId egressInner; |
| 75 | private VlanId egressOuter; |
| 76 | private static final Long TUNNEL_ID = (long) 1234; |
| 77 | |
| 78 | @Before |
| 79 | public void setUp() { |
| 80 | DeviceService deviceService = createNiceMock(DeviceService.class); |
| 81 | InterfaceService intfService = createNiceMock(InterfaceService.class); |
| 82 | TestUtils.setField(PwaasUtil.class, "deviceService", deviceService); |
| 83 | TestUtils.setField(PwaasUtil.class, "intfService", intfService); |
| 84 | |
| 85 | expect(deviceService.getDevice(DID1)).andReturn(D1).anyTimes(); |
| 86 | expect(deviceService.getDevice(DID2)).andReturn(D2).anyTimes(); |
| 87 | expect(deviceService.getPort(CP11)).andReturn(P11).anyTimes(); |
| 88 | expect(deviceService.getPort(CP12)).andReturn(P12).anyTimes(); |
| 89 | expect(deviceService.getPort(CP21)).andReturn(P21).anyTimes(); |
| 90 | expect(deviceService.getPort(CP22)).andReturn(P22).anyTimes(); |
| 91 | expect(intfService.getInterfacesByPort(CP11)).andReturn(Sets.newHashSet(I11)).anyTimes(); |
| 92 | expect(intfService.getInterfacesByPort(CP12)).andReturn(Sets.newHashSet(I12)).anyTimes(); |
| 93 | expect(intfService.getInterfacesByPort(CP21)).andReturn(Sets.newHashSet(I21)).anyTimes(); |
| 94 | expect(intfService.getInterfacesByPort(CP22)).andReturn(Sets.newHashSet(I22)).anyTimes(); |
| 95 | replay(deviceService); |
| 96 | replay(intfService); |
| 97 | } |
| 98 | |
| 99 | @Rule |
| 100 | public final ExpectedException exception = ExpectedException.none(); |
| 101 | |
| 102 | @Test |
| 103 | public void verifyPolicy() { |
| 104 | cp1 = new ConnectPoint(DID1, PN1); |
| 105 | cp2 = new ConnectPoint(DID2, PN2); |
| 106 | ingressInner = V1; |
| 107 | ingressOuter = V2; |
| 108 | egressInner = V1; |
| 109 | egressOuter = V2; |
| 110 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 111 | |
| 112 | ingressInner = VlanId.NONE; |
| 113 | ingressOuter = VlanId.NONE; |
| 114 | egressInner = VlanId.NONE; |
| 115 | egressOuter = VlanId.NONE; |
| 116 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void verifyPolicyOnSameDevice() { |
| 121 | cp1 = new ConnectPoint(DID1, PN1); |
| 122 | cp2 = new ConnectPoint(DID1, PN2); |
| 123 | ingressInner = VlanId.NONE; |
| 124 | ingressOuter = VlanId.NONE; |
| 125 | egressInner = VlanId.NONE; |
| 126 | egressOuter = VlanId.NONE; |
| 127 | exception.expect(IllegalArgumentException.class); |
| 128 | exception.expectMessage(String.format(PwaasUtil.ERR_SAME_DEV, TUNNEL_ID)); |
| 129 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void verifyPolicyEmptyInnerCp1() { |
| 134 | cp1 = new ConnectPoint(DID1, PN1); |
| 135 | cp2 = new ConnectPoint(DID2, PN2); |
| 136 | ingressInner = VlanId.NONE; |
| 137 | ingressOuter = V1; |
| 138 | egressInner = VlanId.NONE; |
| 139 | egressOuter = VlanId.NONE; |
| 140 | exception.expect(IllegalArgumentException.class); |
| 141 | exception.expectMessage(String.format(PwaasUtil.ERR_EMPTY_INNER_WHEN_OUTER_PRESENT, TUNNEL_ID, "cp1")); |
| 142 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void verifyPolicyEmptyInnerCp2() { |
| 147 | cp1 = new ConnectPoint(DID1, PN1); |
| 148 | cp2 = new ConnectPoint(DID2, PN2); |
| 149 | ingressInner = VlanId.NONE; |
| 150 | ingressOuter = VlanId.NONE; |
| 151 | egressInner = VlanId.NONE; |
| 152 | egressOuter = V1; |
| 153 | exception.expect(IllegalArgumentException.class); |
| 154 | exception.expectMessage(String.format(PwaasUtil.ERR_EMPTY_INNER_WHEN_OUTER_PRESENT, TUNNEL_ID, "cp2")); |
| 155 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void verifyPolicyVlanWildcard() { |
| 160 | cp1 = new ConnectPoint(DID1, PN1); |
| 161 | cp2 = new ConnectPoint(DID2, PN2); |
| 162 | ingressInner = VlanId.ANY; |
| 163 | ingressOuter = VlanId.NONE; |
| 164 | egressInner = VlanId.NONE; |
| 165 | egressOuter = VlanId.NONE; |
| 166 | exception.expect(IllegalArgumentException.class); |
| 167 | exception.expectMessage(String.format(PwaasUtil.ERR_WILDCARD_VLAN, TUNNEL_ID)); |
| 168 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | @Test |
| 173 | public void verifyPolicyDeviceServiceNotAvailable() { |
| 174 | TestUtils.setField(PwaasUtil.class, "deviceService", null); |
| 175 | cp1 = new ConnectPoint(DID1, PN1); |
| 176 | cp2 = new ConnectPoint(DID2, PN2); |
| 177 | ingressInner = V1; |
| 178 | ingressOuter = V2; |
| 179 | egressInner = V1; |
| 180 | egressOuter = V2; |
| 181 | exception.expect(IllegalStateException.class); |
| 182 | exception.expectMessage(String.format(PwaasUtil.ERR_SERVICE_UNAVAIL, "DeviceService")); |
| 183 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void verifyPolicyDoubleToUntagged() { |
| 188 | cp1 = new ConnectPoint(DID1, PN1); |
| 189 | cp2 = new ConnectPoint(DID2, PN2); |
| 190 | ingressInner = V1; |
| 191 | ingressOuter = V2; |
| 192 | egressInner = VlanId.NONE; |
| 193 | egressOuter = VlanId.NONE; |
| 194 | exception.expect(IllegalArgumentException.class); |
| 195 | exception.expectMessage(String.format(PwaasUtil.ERR_DOUBLE_TO_UNTAGGED, TUNNEL_ID)); |
| 196 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 197 | } |
| 198 | |
| 199 | @Test |
| 200 | public void verifyPolicyDoubleToSingle() { |
| 201 | cp1 = new ConnectPoint(DID1, PN1); |
| 202 | cp2 = new ConnectPoint(DID2, PN2); |
| 203 | ingressInner = V1; |
| 204 | ingressOuter = V2; |
| 205 | egressInner = V1; |
| 206 | egressOuter = VlanId.NONE; |
| 207 | exception.expect(IllegalArgumentException.class); |
| 208 | exception.expectMessage(String.format(PwaasUtil.ERR_DOUBLE_TO_SINGLE, TUNNEL_ID)); |
| 209 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 210 | } |
| 211 | |
| 212 | @Test |
| 213 | public void verifyPolicySingleToUntagged() { |
| 214 | cp1 = new ConnectPoint(DID1, PN1); |
| 215 | cp2 = new ConnectPoint(DID2, PN2); |
| 216 | ingressInner = V1; |
| 217 | ingressOuter = VlanId.NONE; |
| 218 | egressInner = VlanId.NONE; |
| 219 | egressOuter = VlanId.NONE; |
| 220 | exception.expect(IllegalArgumentException.class); |
| 221 | exception.expectMessage(String.format(PwaasUtil.ERR_SINGLE_TO_UNTAGGED, TUNNEL_ID)); |
| 222 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 223 | } |
| 224 | |
| 225 | @Test |
| 226 | public void verifyPolicyVlanTranslation() { |
| 227 | cp1 = new ConnectPoint(DID1, PN1); |
| 228 | cp2 = new ConnectPoint(DID2, PN2); |
| 229 | ingressInner = V1; |
| 230 | ingressOuter = VlanId.NONE; |
| 231 | egressInner = V2; |
| 232 | egressOuter = VlanId.NONE; |
| 233 | exception.expect(IllegalArgumentException.class); |
| 234 | exception.expectMessage(String.format(PwaasUtil.ERR_VLAN_TRANSLATION, TUNNEL_ID)); |
| 235 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 236 | } |
| 237 | |
| 238 | @Test |
| 239 | public void verifyPolicyDeviceNotFound() { |
| 240 | cp1 = new ConnectPoint(DID99, PN1); |
| 241 | cp2 = new ConnectPoint(DID2, PN2); |
| 242 | ingressInner = V1; |
| 243 | ingressOuter = V2; |
| 244 | egressInner = V1; |
| 245 | egressOuter = V2; |
| 246 | exception.expect(IllegalArgumentException.class); |
| 247 | exception.expectMessage(String.format(PwaasUtil.ERR_DEV_NOT_FOUND, DID99, TUNNEL_ID)); |
| 248 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 249 | } |
| 250 | |
| 251 | @Test |
| 252 | public void verifyPolicyPortNotFound() { |
| 253 | cp1 = new ConnectPoint(DID1, PN99); |
| 254 | cp2 = new ConnectPoint(DID2, PN2); |
| 255 | ingressInner = V1; |
| 256 | ingressOuter = V2; |
| 257 | egressInner = V1; |
| 258 | egressOuter = V2; |
| 259 | exception.expect(IllegalArgumentException.class); |
| 260 | exception.expectMessage(String.format(PwaasUtil.ERR_PORT_NOT_FOUND, PN99, DID1, TUNNEL_ID)); |
| 261 | PwaasUtil.verifyPolicy(cp1, cp2, ingressInner, ingressOuter, egressInner, egressOuter, TUNNEL_ID); |
| 262 | } |
| 263 | } |