nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | package org.onosproject.vpls.config; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.TestApplicationId; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.config.Config; |
| 25 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 26 | |
| 27 | import java.util.Arrays; |
| 28 | import java.util.HashSet; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertNotNull; |
| 33 | import static org.junit.Assert.assertNull; |
| 34 | import static org.junit.Assert.assertFalse; |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | |
| 37 | /** |
| 38 | * Tests for the {@link VplsConfig} class. |
| 39 | */ |
| 40 | public class VplsConfigTest { |
| 41 | private static final String APP_NAME = "org.onosproject.vpls"; |
| 42 | private static final ApplicationId APP_ID = new TestApplicationId(APP_NAME); |
| 43 | private static final String VPLS = "vplsNetworks"; |
| 44 | private static final String NAME = "name"; |
| 45 | private static final String INTERFACE = "interfaces"; |
| 46 | private static final String NET1 = "net1"; |
| 47 | private static final String NET2 = "net2"; |
| 48 | private static final String NEWNET = "newnet"; |
| 49 | |
| 50 | private static final String IF1 = "sw5-4-100"; |
| 51 | private static final String IF2 = "sw5-4-200"; |
| 52 | private static final String IF3 = "sw5-5-100"; |
| 53 | private static final String IF4 = "sw6-5-100"; |
| 54 | private static final String IF5 = "sw6-5-400"; |
| 55 | private static final String IF_NON_EXIST = "sw7-5-100"; |
| 56 | |
| 57 | private static final String JSON_TREE = "{\"" + VPLS + |
| 58 | "\" : [{\"" + NAME + "\" : \"net1\"," + |
| 59 | "\"" + INTERFACE + "\" : [" + |
| 60 | "\"sw5-4-100\",\"sw5-4-200\",\"sw5-5-100\"]}]}"; |
| 61 | private static final String EMPTY_JSON_TREE = "{}"; |
| 62 | |
| 63 | private final ObjectMapper mapper = new ObjectMapper(); |
| 64 | private final ConfigApplyDelegate delegate = new MockCfgDelegate(); |
| 65 | private final VplsNetworkConfig initialNetwork = createInitialNetwork(); |
| 66 | |
| 67 | private Set<VplsNetworkConfig> networks = new HashSet<>(); |
| 68 | private VplsConfig vplsConfig = new VplsConfig(); |
| 69 | private VplsConfig emptyVplsConfig = new VplsConfig(); |
| 70 | |
| 71 | @Before |
| 72 | public void setUp() throws Exception { |
| 73 | JsonNode tree = new ObjectMapper().readTree(JSON_TREE); |
| 74 | vplsConfig.init(APP_ID, APP_NAME, tree, mapper, delegate); |
| 75 | JsonNode emptyTree = new ObjectMapper().readTree(EMPTY_JSON_TREE); |
| 76 | emptyVplsConfig.init(APP_ID, APP_NAME, emptyTree, mapper, delegate); |
| 77 | networks.add(initialNetwork); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Tests if a VPLS configuration can be retrieved from JSON. |
| 82 | */ |
| 83 | @Test |
| 84 | public void testVplsNetworks() { |
| 85 | assertEquals(networks, vplsConfig.vplsNetworks()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Tests an empty VPLS application configuration is retrieved from JSON. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testEmptyVplsNetworks() { |
| 93 | assertTrue(emptyVplsConfig.vplsNetworks().isEmpty()); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests if a VPLS can be found by name. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testGetVplsWithName() { |
| 101 | assertNotNull(vplsConfig.getVplsWithName(NET1)); |
| 102 | assertNull(vplsConfig.getVplsWithName(NET2)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Tests the addition of a new VPLS. |
| 107 | */ |
| 108 | @Test |
| 109 | public void testAddNetwork() { |
| 110 | int initialSize = vplsConfig.vplsNetworks().size(); |
| 111 | VplsNetworkConfig newNetwork = createNewNetwork(); |
| 112 | vplsConfig.addVpls(newNetwork); |
| 113 | assertEquals(initialSize + 1, vplsConfig.vplsNetworks().size()); |
| 114 | networks.add(newNetwork); |
| 115 | assertEquals(networks, vplsConfig.vplsNetworks()); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Tests the addition of new VPLS to an empty configuration. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testAddNetworkToEmpty() { |
| 123 | VplsNetworkConfig newNetwork = createNewNetwork(); |
| 124 | emptyVplsConfig.addVpls(newNetwork); |
| 125 | |
| 126 | assertFalse(emptyVplsConfig.vplsNetworks().isEmpty()); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Tests the removal of an existing VPLS from the configuration. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testRemoveExistingNetwork() { |
| 134 | int initialSize = vplsConfig.vplsNetworks().size(); |
| 135 | vplsConfig.removeVpls(NET1); |
| 136 | |
| 137 | assertEquals(initialSize - 1, vplsConfig.vplsNetworks().size()); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests the removal of a non-existing VPLS from the configuration. |
| 142 | */ |
| 143 | @Test |
| 144 | public void testRemoveInexistingNetwork() { |
| 145 | int initialSize = vplsConfig.vplsNetworks().size(); |
| 146 | vplsConfig.removeVpls(NET2); |
| 147 | |
| 148 | assertEquals(initialSize, vplsConfig.vplsNetworks().size()); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests the addition of a new interface. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testAddInterfaceToNetwork() { |
| 156 | int initialSize = vplsConfig.getVplsWithName(NET1).ifaces().size(); |
| 157 | vplsConfig.addInterfaceToVpls(NET1, IF4); |
| 158 | |
| 159 | assertEquals(initialSize + 1, vplsConfig.getVplsWithName(NET1).ifaces().size()); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests the addition of a new interface when it already exists. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testAddExistingInterfaceToNetwork() { |
| 167 | int initialSize = vplsConfig.getVplsWithName(NET1).ifaces().size(); |
| 168 | vplsConfig.addInterfaceToVpls(NET1, IF1); |
| 169 | |
| 170 | assertEquals(initialSize, vplsConfig.getVplsWithName(NET1).ifaces().size()); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Tests the retrieval of a VPLS, given an interface name. |
| 175 | */ |
| 176 | @Test |
| 177 | public void testgetNetworkFromInterface() { |
| 178 | assertNotNull(vplsConfig.getVplsFromInterface(IF1)); |
| 179 | assertNull(vplsConfig.getVplsFromInterface(IF_NON_EXIST)); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Tests the removal of an interface. |
| 184 | */ |
| 185 | @Test |
| 186 | public void testRemoveExistingInterfaceFromNetwork() { |
| 187 | int initialSize = vplsConfig.getVplsWithName(NET1).ifaces().size(); |
| 188 | vplsConfig.removeInterfaceFromVpls(initialNetwork, IF1); |
| 189 | |
| 190 | assertEquals(initialSize - 1, vplsConfig.getVplsWithName(NET1).ifaces().size()); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Tests the removal of an interface from a VPLS when it does not exist. |
| 195 | */ |
| 196 | @Test |
| 197 | public void testRemoveNonExistingInterfaceFromNetwork() { |
| 198 | int initialSize = vplsConfig.getVplsWithName(NET1).ifaces().size(); |
| 199 | vplsConfig.removeInterfaceFromVpls(initialNetwork, IF_NON_EXIST); |
| 200 | |
| 201 | assertEquals(initialSize, vplsConfig.getVplsWithName(NET1).ifaces().size()); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Tests if the two interfaces are attached to the network |
| 206 | * while one of the interface is attached and another one is not. |
| 207 | */ |
| 208 | @Test |
| 209 | public void testIsAttached() { |
| 210 | VplsNetworkConfig network = createNewNetwork(); |
| 211 | |
| 212 | assertTrue(network.isAttached(IF4)); |
| 213 | assertFalse(network.isAttached(IF_NON_EXIST)); |
| 214 | } |
| 215 | |
| 216 | private class MockCfgDelegate implements ConfigApplyDelegate { |
| 217 | |
| 218 | @Override |
| 219 | public void onApply(@SuppressWarnings("rawtypes") Config config) { |
| 220 | config.apply(); |
| 221 | } |
| 222 | |
| 223 | } |
| 224 | |
| 225 | private VplsNetworkConfig createInitialNetwork() { |
| 226 | Set<String> ifaces = new HashSet<>(Arrays.asList(IF1, IF2, IF3)); |
| 227 | |
| 228 | return new VplsNetworkConfig(NET1, ifaces); |
| 229 | } |
| 230 | |
| 231 | private VplsNetworkConfig createNewNetwork() { |
| 232 | Set<String> ifaces = new HashSet<>(Arrays.asList(IF4, IF5)); |
| 233 | |
| 234 | return new VplsNetworkConfig(NEWNET, ifaces); |
| 235 | } |
| 236 | } |