Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | f4e13e3 | 2017-03-30 15:38:39 -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 | */ |
| 16 | |
| 17 | package org.onosproject.vpls.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.net.EncapsulationType; |
| 26 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 27 | import org.onosproject.net.config.NetworkConfigEvent; |
| 28 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 29 | import org.onosproject.vpls.VplsTest; |
| 30 | import org.onosproject.vpls.api.VplsData; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.util.Collection; |
| 34 | |
| 35 | import static org.junit.Assert.*; |
| 36 | public class VplsConfigManagerTest extends VplsTest { |
| 37 | VplsConfigManager vplsConfigManager; |
| 38 | |
| 39 | @Before |
| 40 | public void setup() { |
| 41 | vplsConfigManager = new VplsConfigManager(); |
| 42 | vplsConfigManager.configService = new TestConfigService(); |
| 43 | vplsConfigManager.coreService = new TestCoreService(); |
| 44 | vplsConfigManager.interfaceService = new TestInterfaceService(); |
| 45 | vplsConfigManager.registry = new NetworkConfigRegistryAdapter(); |
| 46 | vplsConfigManager.vpls = new TestVpls(); |
| 47 | vplsConfigManager.leadershipService = new TestLeadershipService(); |
| 48 | |
| 49 | vplsConfigManager.activate(); |
| 50 | } |
| 51 | |
| 52 | @After |
| 53 | public void tearDown() { |
| 54 | vplsConfigManager.deactivate(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Reloads network configuration by sending a network config event. |
| 59 | */ |
| 60 | @Test |
| 61 | public void testReloadConfig() { |
| 62 | NetworkConfigEvent event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 63 | null, |
| 64 | VplsAppConfig.class); |
| 65 | ((TestConfigService) vplsConfigManager.configService).sendEvent(event); |
| 66 | |
| 67 | Collection<VplsData> vplss = vplsConfigManager.vpls.getAllVpls(); |
| 68 | assertEquals(2, vplss.size()); |
| 69 | |
| 70 | VplsData expect = VplsData.of(VPLS1); |
| 71 | expect.addInterfaces(ImmutableSet.of(V100H1, V100H2)); |
| 72 | expect.state(VplsData.VplsState.ADDED); |
| 73 | assertTrue(vplss.contains(expect)); |
| 74 | |
| 75 | expect = VplsData.of(VPLS2, EncapsulationType.VLAN); |
| 76 | expect.addInterfaces(ImmutableSet.of(V200H1, V200H2)); |
| 77 | expect.state(VplsData.VplsState.ADDED); |
| 78 | System.out.println(vplss); |
| 79 | assertTrue(vplss.contains(expect)); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Sends a network config event with null network config. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testReloadNullConfig() { |
| 87 | ((TestConfigService) vplsConfigManager.configService).setConfig(null); |
| 88 | NetworkConfigEvent event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 89 | null, |
| 90 | VplsAppConfig.class); |
| 91 | ((TestConfigService) vplsConfigManager.configService).sendEvent(event); |
| 92 | |
| 93 | Collection<VplsData> vplss = vplsConfigManager.vpls.getAllVpls(); |
| 94 | assertEquals(0, vplss.size()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Updates VPLSs by sending new VPLS config. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testReloadConfigUpdateVpls() { |
| 102 | ((TestVpls) vplsConfigManager.vpls).initSampleData(); |
| 103 | |
| 104 | VplsAppConfig vplsAppConfig = new VplsAppConfig(); |
| 105 | final ObjectMapper mapper = new ObjectMapper(); |
| 106 | final ConfigApplyDelegate delegate = new VplsAppConfigTest.MockCfgDelegate(); |
| 107 | JsonNode tree = null; |
| 108 | try { |
| 109 | tree = new ObjectMapper().readTree(TestConfigService.EMPTY_JSON_TREE); |
| 110 | } catch (IOException e) { |
| 111 | e.printStackTrace(); |
| 112 | } |
| 113 | vplsAppConfig.init(APPID, APP_NAME, tree, mapper, delegate); |
| 114 | VplsConfig vplsConfig = new VplsConfig(VPLS1, |
| 115 | ImmutableSet.of(V100H1.name()), |
| 116 | EncapsulationType.MPLS); |
| 117 | vplsAppConfig.addVpls(vplsConfig); |
| 118 | ((TestConfigService) vplsConfigManager.configService).setConfig(vplsAppConfig); |
| 119 | NetworkConfigEvent event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 120 | null, |
| 121 | VplsAppConfig.class); |
| 122 | ((TestConfigService) vplsConfigManager.configService).sendEvent(event); |
| 123 | |
| 124 | Collection<VplsData> vplss = vplsConfigManager.vpls.getAllVpls(); |
| 125 | assertEquals(1, vplss.size()); |
| 126 | |
| 127 | VplsData expect = VplsData.of(VPLS1, EncapsulationType.MPLS); |
| 128 | expect.addInterfaces(ImmutableSet.of(V100H1)); |
| 129 | expect.state(VplsData.VplsState.ADDED); |
| 130 | assertTrue(vplss.contains(expect)); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Remvoes all VPLS by sending CONFIG_REMOVED event. |
| 135 | */ |
| 136 | @Test |
| 137 | public void testRemoveConfig() { |
| 138 | NetworkConfigEvent event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_REMOVED, |
| 139 | null, |
| 140 | VplsAppConfig.class); |
| 141 | ((TestConfigService) vplsConfigManager.configService).sendEvent(event); |
| 142 | |
| 143 | Collection<VplsData> vplss = vplsConfigManager.vpls.getAllVpls(); |
| 144 | assertEquals(0, vplss.size()); |
| 145 | } |
| 146 | } |