papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.routing.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 30 | import org.onosproject.routing.RoutingService; |
| 31 | |
| 32 | import java.util.Arrays; |
| 33 | import java.util.HashSet; |
| 34 | import java.util.Optional; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | import static junit.framework.Assert.assertNotNull; |
| 38 | import static junit.framework.Assert.assertNull; |
| 39 | import static junit.framework.TestCase.assertEquals; |
| 40 | import static org.junit.Assert.assertFalse; |
| 41 | import static org.junit.Assert.assertTrue; |
| 42 | |
| 43 | public class BgpConfigTest { |
| 44 | |
| 45 | private static final ApplicationId APP_ID = |
| 46 | new TestApplicationId(RoutingService.ROUTER_APP_ID); |
| 47 | |
| 48 | private static final IpAddress IP1 = IpAddress.valueOf("10.0.1.1"); |
| 49 | private static final IpAddress IP2 = IpAddress.valueOf("10.0.2.1"); |
| 50 | private static final IpAddress IP3 = IpAddress.valueOf("10.0.3.1"); |
| 51 | private static final IpAddress IP4 = IpAddress.valueOf("10.0.101.1"); |
| 52 | private static final IpAddress IP5 = IpAddress.valueOf("10.0.201.1"); |
| 53 | public static final IpAddress IP_NON_EXIST = IpAddress.valueOf("10.101.1.1"); |
| 54 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 55 | public static final VlanId NO_VLAN = VlanId.NONE; |
| 56 | |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 57 | public static final ConnectPoint CONNECT_POINT1 = ConnectPoint. |
| 58 | deviceConnectPoint("of:0000000000000001/1"); |
| 59 | public static final ConnectPoint CONNECT_POINT2 = ConnectPoint. |
| 60 | deviceConnectPoint("of:00000000000000a3/1"); |
| 61 | |
| 62 | private static final String JSON_TREE = "{\"" + BgpConfig.SPEAKERS + |
| 63 | "\" : [{\"" + BgpConfig.NAME + "\" : \"bgp1\"," + |
| 64 | "\"" + BgpConfig.CONNECT_POINT + |
| 65 | "\" : \"of:0000000000000001/1\"," + |
| 66 | "\"" + BgpConfig.PEERS + "\" : [" + |
| 67 | "\"10.0.1.1\",\"10.0.2.1\",\"10.0.3.1\"]}]}"; |
| 68 | private static final String EMPTY_JSON_TREE = "{}"; |
| 69 | |
| 70 | private final ObjectMapper mapper = new ObjectMapper(); |
| 71 | private final ConfigApplyDelegate delegate = new MockCfgDelegate(); |
| 72 | private final BgpConfig.BgpSpeakerConfig initialSpeaker = createInitialSpeaker(); |
| 73 | |
| 74 | private Set<BgpConfig.BgpSpeakerConfig> speakers = new HashSet<>(); |
| 75 | private BgpConfig bgpConfig = new BgpConfig(); |
| 76 | private BgpConfig emptyBgpConfig = new BgpConfig(); |
| 77 | |
| 78 | @Before |
| 79 | public void setUp() throws Exception { |
| 80 | JsonNode tree = new ObjectMapper().readTree(JSON_TREE); |
| 81 | bgpConfig.init(APP_ID, "bgp-test", tree, mapper, delegate); |
| 82 | JsonNode emptyTree = new ObjectMapper().readTree(EMPTY_JSON_TREE); |
| 83 | emptyBgpConfig.init(APP_ID, "bgp-test", emptyTree, mapper, delegate); |
| 84 | speakers.add(initialSpeaker); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests if speakers can be retrieved from JSON. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testBgpSpeakers() throws Exception { |
| 92 | assertEquals(speakers, bgpConfig.bgpSpeakers()); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tests if speakers can be retrieved from empty JSON. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testEmptyBgpSpeakers() throws Exception { |
| 100 | assertTrue(emptyBgpConfig.bgpSpeakers().isEmpty()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Tests if speaker can be found by name. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testGetSpeakerWithName() throws Exception { |
| 108 | assertNotNull(bgpConfig.getSpeakerWithName("bgp1")); |
| 109 | assertNull(bgpConfig.getSpeakerWithName("bgp2")); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests addition of new speaker. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testAddSpeaker() throws Exception { |
| 117 | int initialSize = bgpConfig.bgpSpeakers().size(); |
| 118 | BgpConfig.BgpSpeakerConfig newSpeaker = createNewSpeaker(); |
| 119 | bgpConfig.addSpeaker(newSpeaker); |
| 120 | assertEquals(initialSize + 1, bgpConfig.bgpSpeakers().size()); |
| 121 | speakers.add(newSpeaker); |
| 122 | assertEquals(speakers, bgpConfig.bgpSpeakers()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests addition of new speaker to empty configuration. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testAddSpeakerToEmpty() throws Exception { |
| 130 | BgpConfig.BgpSpeakerConfig newSpeaker = createNewSpeaker(); |
| 131 | emptyBgpConfig.addSpeaker(newSpeaker); |
| 132 | |
| 133 | assertFalse(emptyBgpConfig.bgpSpeakers().isEmpty()); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Tests removal of existing speaker. |
| 138 | */ |
| 139 | @Test |
| 140 | public void testRemoveExistingSpeaker() throws Exception { |
| 141 | int initialSize = bgpConfig.bgpSpeakers().size(); |
| 142 | bgpConfig.removeSpeaker("bgp1"); |
| 143 | |
| 144 | assertEquals(initialSize - 1, bgpConfig.bgpSpeakers().size()); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Tests removal of non-existing speaker. |
| 149 | */ |
| 150 | @Test |
| 151 | public void testRemoveInexistingSpeaker() throws Exception { |
| 152 | int initialSize = bgpConfig.bgpSpeakers().size(); |
| 153 | bgpConfig.removeSpeaker("bgp2"); |
| 154 | |
| 155 | assertEquals(initialSize, bgpConfig.bgpSpeakers().size()); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Tests addition of new speaker. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testAddPeerToSpeaker() throws Exception { |
| 163 | int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size(); |
| 164 | bgpConfig.addPeerToSpeaker("bgp1", IP4); |
| 165 | |
| 166 | assertEquals(initialSize + 1, bgpConfig.getSpeakerWithName("bgp1").peers().size()); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Tests addition of new speaker when peer already exists. |
| 171 | */ |
| 172 | @Test |
| 173 | public void testAddExistingPeerToSpeaker() throws Exception { |
| 174 | int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size(); |
| 175 | bgpConfig.addPeerToSpeaker("bgp1", IP1); |
| 176 | |
| 177 | assertEquals(initialSize, bgpConfig.getSpeakerWithName("bgp1").peers().size()); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests retrieval of speaker based on peering address. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testGetSpeakerFromPeer() throws Exception { |
| 185 | assertNotNull(bgpConfig.getSpeakerFromPeer(IP1)); |
| 186 | assertNull(bgpConfig.getSpeakerFromPeer(IP_NON_EXIST)); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests removal of peer. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testRemoveExistingPeerFromSpeaker() throws Exception { |
| 194 | int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size(); |
| 195 | bgpConfig.removePeerFromSpeaker(initialSpeaker, IP1); |
| 196 | |
| 197 | assertEquals(initialSize - 1, bgpConfig.getSpeakerWithName("bgp1").peers().size()); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Tests peer removal when peer does not exist. |
| 202 | */ |
| 203 | @Test |
| 204 | public void testRemoveNonExistingPeerFromSpeaker() throws Exception { |
| 205 | int initialSize = bgpConfig.getSpeakerWithName("bgp1").peers().size(); |
| 206 | bgpConfig.removePeerFromSpeaker(initialSpeaker, IP_NON_EXIST); |
| 207 | |
| 208 | assertEquals(initialSize, bgpConfig.getSpeakerWithName("bgp1").peers().size()); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Tests if connections to peers are found. |
| 213 | */ |
| 214 | @Test |
| 215 | public void testIsConnectedToPeer() { |
| 216 | BgpConfig.BgpSpeakerConfig speaker = createNewSpeaker(); |
| 217 | |
| 218 | assertTrue(speaker.isConnectedToPeer(IP4)); |
| 219 | assertFalse(speaker.isConnectedToPeer(IP_NON_EXIST)); |
| 220 | } |
| 221 | |
| 222 | private class MockCfgDelegate implements ConfigApplyDelegate { |
| 223 | |
| 224 | @Override |
| 225 | public void onApply(@SuppressWarnings("rawtypes") Config config) { |
| 226 | config.apply(); |
| 227 | } |
| 228 | |
| 229 | } |
| 230 | |
| 231 | private BgpConfig.BgpSpeakerConfig createInitialSpeaker() { |
| 232 | Optional<String> speakerName = Optional.of("bgp1"); |
| 233 | ConnectPoint connectPoint = CONNECT_POINT1; |
| 234 | Set<IpAddress> connectedPeers = new HashSet<>(Arrays.asList(IP1, IP2, IP3)); |
| 235 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 236 | return new BgpConfig.BgpSpeakerConfig(speakerName, NO_VLAN, |
| 237 | connectPoint, connectedPeers); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | private BgpConfig.BgpSpeakerConfig createNewSpeaker() { |
| 241 | Optional<String> speakerName = Optional.of("newSpeaker"); |
| 242 | ConnectPoint connectPoint = CONNECT_POINT2; |
| 243 | Set<IpAddress> connectedPeers = new HashSet<>( |
| 244 | Arrays.asList(IP4, IP5)); |
| 245 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 246 | return new BgpConfig.BgpSpeakerConfig(speakerName, NO_VLAN, |
| 247 | connectPoint, connectedPeers); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 248 | } |
| 249 | } |