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