Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 21 | import static junit.framework.TestCase.fail; |
tom | 747a213 | 2014-10-02 08:18:41 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertEquals; |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertTrue; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 24 | import static org.onosproject.net.ConnectPointTest.Relate.AFTER; |
| 25 | import static org.onosproject.net.ConnectPointTest.Relate.BEFORE; |
| 26 | import static org.onosproject.net.ConnectPointTest.Relate.SAME_AS; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import static org.onosproject.net.DeviceId.deviceId; |
| 28 | import static org.onosproject.net.PortNumber.portNumber; |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 31 | * Test of the connection point entity. |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 32 | */ |
| 33 | public class ConnectPointTest { |
| 34 | |
tom | 747a213 | 2014-10-02 08:18:41 -0700 | [diff] [blame] | 35 | private static final DeviceId DID1 = deviceId("1"); |
| 36 | private static final DeviceId DID2 = deviceId("2"); |
| 37 | private static final PortNumber P1 = portNumber(1); |
| 38 | private static final PortNumber P2 = portNumber(2); |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 39 | |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 40 | private ConnectPoint cp(DeviceId d, PortNumber p) { |
| 41 | return new ConnectPoint(d, p); |
| 42 | } |
| 43 | |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 44 | @Test |
| 45 | public void basics() { |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 46 | ConnectPoint p = cp(DID1, P2); |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 47 | assertEquals("incorrect element id", DID1, p.deviceId()); |
| 48 | assertEquals("incorrect element id", P2, p.port()); |
| 49 | } |
| 50 | |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 51 | @Test |
| 52 | public void testEquality() { |
| 53 | new EqualsTester() |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 54 | .addEqualityGroup(cp(DID1, P1), cp(DID1, P1)) |
| 55 | .addEqualityGroup(cp(DID1, P2), cp(DID1, P2)) |
| 56 | .addEqualityGroup(cp(DID2, P1), cp(DID2, P1)) |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 57 | .testEquals(); |
| 58 | } |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 59 | |
| 60 | @Test |
| 61 | public void testParseDeviceConnectPoint() { |
| 62 | String cp = "of:0011223344556677/1"; |
| 63 | |
| 64 | ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(cp); |
| 65 | assertEquals("of:0011223344556677", connectPoint.deviceId().toString()); |
| 66 | assertEquals("1", connectPoint.port().toString()); |
| 67 | |
| 68 | expectDeviceParseException(""); |
| 69 | expectDeviceParseException("1/"); |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 70 | expectDeviceParseException("of:0011223344556677/word"); |
| 71 | } |
| 72 | |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 73 | @Test |
pierventre | 57f7bbb | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 74 | public void testParseFromStringOF() { |
| 75 | String cp = "of:0011223344556677/1"; |
| 76 | |
| 77 | ConnectPoint connectPoint = ConnectPoint.fromString(cp); |
| 78 | assertEquals("of:0011223344556677", connectPoint.deviceId().toString()); |
| 79 | assertEquals("1", connectPoint.port().toString()); |
| 80 | |
| 81 | expectStringParseException("["); |
| 82 | expectStringParseException("1/["); |
| 83 | expectStringParseException("1/[aasksk"); |
| 84 | expectStringParseException("1/[alksas]"); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testParseFromStringNetconf() { |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 89 | String cp = "netconf:127.0.0.1/[TYPE](1)"; |
| 90 | |
| 91 | ConnectPoint connectPoint = ConnectPoint.fromString(cp); |
| 92 | assertEquals("netconf:127.0.0.1", connectPoint.deviceId().toString()); |
| 93 | assertEquals("[TYPE](1)", connectPoint.port().toString()); |
| 94 | assertEquals(connectPoint, ConnectPoint.fromString(connectPoint.toString())); |
pierventre | 57f7bbb | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 95 | } |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 96 | |
pierventre | 57f7bbb | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 97 | @Test |
| 98 | public void testParseFromStringBmv2() { |
| 99 | String cp = "device:leaf1/[leaf1-eth4](1)"; |
| 100 | |
| 101 | ConnectPoint connectPoint = ConnectPoint.fromString(cp); |
| 102 | assertEquals("device:leaf1", connectPoint.deviceId().toString()); |
| 103 | assertEquals("[leaf1-eth4](1)", connectPoint.port().toString()); |
| 104 | assertEquals(connectPoint, ConnectPoint.fromString(connectPoint.toString())); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testParseFromStringStratum() { |
| 109 | String cp = "device:leaf1/[3/0](1)"; |
| 110 | |
| 111 | ConnectPoint connectPoint = ConnectPoint.fromString(cp); |
| 112 | assertEquals("device:leaf1", connectPoint.deviceId().toString()); |
| 113 | assertEquals("[3/0](1)", connectPoint.port().toString()); |
| 114 | assertEquals(connectPoint, ConnectPoint.fromString(connectPoint.toString())); |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Parse a device connect point and expect an exception to be thrown. |
| 119 | * |
| 120 | * @param string string to parse |
| 121 | */ |
| 122 | private static void expectDeviceParseException(String string) { |
| 123 | try { |
| 124 | ConnectPoint.deviceConnectPoint(string); |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 125 | fail(String.format("Expected exception was not thrown for '%s'", string)); |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 126 | } catch (Exception e) { |
| 127 | assertTrue(true); |
| 128 | } |
| 129 | } |
| 130 | |
pierventre | 57f7bbb | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 131 | /** |
| 132 | * Parse a string connect point and expect an exception to be thrown. |
| 133 | * |
| 134 | * @param string string to parse |
| 135 | */ |
| 136 | private static void expectStringParseException(String string) { |
| 137 | try { |
| 138 | ConnectPoint.fromString(string); |
| 139 | fail(String.format("Expected exception was not thrown for '%s'", string)); |
| 140 | } catch (Exception e) { |
| 141 | assertTrue(true); |
| 142 | } |
| 143 | } |
| 144 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 145 | @Test |
| 146 | public void testParseHostConnectPoint() { |
| 147 | String cp = "16:3A:BD:6E:31:E4/-1/1"; |
| 148 | |
| 149 | ConnectPoint connectPoint = ConnectPoint.hostConnectPoint(cp); |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 150 | assertEquals("16:3A:BD:6E:31:E4/None", connectPoint.hostId().toString()); |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 151 | assertEquals("1", connectPoint.port().toString()); |
| 152 | |
| 153 | expectHostParseException(""); |
| 154 | expectHostParseException("1/"); |
| 155 | expectHostParseException("1/1"); |
| 156 | expectHostParseException("1/1/1/1"); |
| 157 | expectHostParseException("16:3A:BD:6E:31:E4/word/1"); |
| 158 | expectHostParseException("16:3A:BD:6E:31:E4/1/word"); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Parse a host connect point and expect an exception to be thrown. |
| 163 | * |
| 164 | * @param string string to parse |
| 165 | */ |
| 166 | private static void expectHostParseException(String string) { |
| 167 | try { |
| 168 | ConnectPoint.hostConnectPoint(string); |
| 169 | fail("Expected exception was not thrown"); |
| 170 | } catch (Exception e) { |
| 171 | assertTrue(true); |
| 172 | } |
| 173 | } |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 174 | |
| 175 | enum Relate { BEFORE, SAME_AS, AFTER } |
| 176 | |
| 177 | private void checkComparison(ConnectPoint cpA, Relate r, ConnectPoint cpB) { |
| 178 | switch (r) { |
| 179 | case BEFORE: |
| 180 | assertTrue("Bad before", cpA.compareTo(cpB) < 0); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 181 | assertTrue("Bad before", cpB.compareTo(cpA) > 0); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 182 | break; |
| 183 | case SAME_AS: |
| 184 | assertTrue("Bad same_as", cpA.compareTo(cpB) == 0); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 185 | assertTrue("Bad same_as", cpB.compareTo(cpA) == 0); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 186 | break; |
| 187 | case AFTER: |
| 188 | assertTrue("Bad after", cpA.compareTo(cpB) > 0); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 189 | assertTrue("Bad after", cpB.compareTo(cpA) < 0); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 190 | break; |
| 191 | default: |
| 192 | fail("Bad relation"); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | public void comparator() { |
| 198 | checkComparison(cp(DID1, P1), SAME_AS, cp(DID1, P1)); |
| 199 | checkComparison(cp(DID1, P1), BEFORE, cp(DID1, P2)); |
| 200 | checkComparison(cp(DID1, P2), AFTER, cp(DID1, P1)); |
| 201 | |
| 202 | checkComparison(cp(DID1, P1), BEFORE, cp(DID2, P1)); |
| 203 | checkComparison(cp(DID1, P2), BEFORE, cp(DID2, P1)); |
| 204 | checkComparison(cp(DID1, P1), BEFORE, cp(DID2, P2)); |
| 205 | checkComparison(cp(DID1, P2), BEFORE, cp(DID2, P2)); |
| 206 | } |
Yuta HIGUCHI | 885be1d | 2014-10-04 21:47:26 -0700 | [diff] [blame] | 207 | } |