blob: 02f9645dbcffc793681139345cebdaff908d781e [file] [log] [blame]
Luca Prete092e8952016-10-26 16:25:56 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Luca Prete092e8952016-10-26 16:25:56 +02003 *
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 */
16package org.onosproject.vpls.config;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.TestApplicationId;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.EncapsulationType;
25import org.onosproject.net.config.Config;
26import org.onosproject.net.config.ConfigApplyDelegate;
27
28import java.util.Arrays;
29import java.util.HashSet;
30import java.util.Set;
31
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010032import static org.junit.Assert.*;
Luca Prete092e8952016-10-26 16:25:56 +020033
34/**
35 * Tests for the {@link VplsAppConfig} class.
36 */
37public class VplsAppConfigTest {
38 private static final String APP_NAME = "org.onosproject.vpls";
39 private static final ApplicationId APP_ID = new TestApplicationId(APP_NAME);
40 private static final String VPLS = "vplsList";
41 private static final String NAME = "name";
42 private static final String INTERFACE = "interfaces";
43 private static final String ENCAPSULATION = "encapsulation";
44 private static final String VPLS1 = "vpls1";
45 private static final String VPLS2 = "vpls2";
46 private static final String NEWVPLS = "newvpls";
47
48 private static final String IF1 = "sw5-4-100";
49 private static final String IF2 = "sw5-4-200";
50 private static final String IF3 = "sw5-5-100";
51 private static final String IF4 = "sw6-5-100";
52 private static final String IF5 = "sw6-5-400";
53 private static final String IF_NON_EXIST = "sw7-5-100";
54
55 private static final String JSON_TREE = "{\"" + VPLS + "\" : [{\"" + NAME +
56 "\" : \"" + VPLS1 + "\"," + "\"" + INTERFACE + "\" : [" + "\"" +
57 IF1 + "\",\"" + IF2 + "\",\"" + IF3 + "\"]" + ",\"" +
58 ENCAPSULATION + "\" : \"none\"}]}";
59
60 private static final String EMPTY_JSON_TREE = "{}";
61
62 private final ObjectMapper mapper = new ObjectMapper();
63 private final ConfigApplyDelegate delegate = new MockCfgDelegate();
64 private final VplsConfig initialVpls = createInitialVpls();
65
66 private Set<VplsConfig> vplss = new HashSet<>();
67 private VplsAppConfig vplsAppConfig = new VplsAppConfig();
68 private VplsAppConfig emptyVplsAppConfig = new VplsAppConfig();
69
70 @Before
71 public void setUp() throws Exception {
72 JsonNode tree = new ObjectMapper().readTree(JSON_TREE);
73 vplsAppConfig.init(APP_ID, APP_NAME, tree, mapper, delegate);
74 JsonNode emptyTree = new ObjectMapper().readTree(EMPTY_JSON_TREE);
75 emptyVplsAppConfig.init(APP_ID, APP_NAME, emptyTree, mapper, delegate);
76 vplss.add(initialVpls);
77 }
78
79 /**
80 * Tests if a VPLS configuration can be retrieved from JSON.
81 */
82 @Test
83 public void vplss() {
84 assertEquals("Cannot load VPLS configuration or unexpected configuration" +
85 "loaded", vplss, vplsAppConfig.vplss());
86 }
87
88 /**
89 * Tests an empty VPLS application configuration is retrieved from JSON.
90 */
91 @Test
92 public void emptyVplss() {
93 assertTrue("Configuration retrieved from JSON was not empty",
94 emptyVplsAppConfig.vplss().isEmpty());
95 }
96
97 /**
98 * Tests if a VPLS can be found by name. Tries also to find a VPLS that
99 * does not exist in the configuration.
100 */
101 @Test
102 public void getVplsWithName() {
103 assertNotNull("Configuration for VPLS not found",
104 vplsAppConfig.getVplsWithName(VPLS1));
105 assertNull("Unexpected configuration for VPLS found",
106 vplsAppConfig.getVplsWithName(VPLS2));
107 }
108
109 /**
110 * Tests the addition of a new VPLS.
111 */
112 @Test
113 public void addVpls() {
114 int initialSize = vplsAppConfig.vplss().size();
115 VplsConfig newVpls = createNewVpls();
116 vplsAppConfig.addVpls(newVpls);
117 assertEquals("The new VPLS has not been added correctly to the list of" +
118 "existing VPLSs",
119 initialSize + 1,
120 vplsAppConfig.vplss().size());
121 vplss.add(newVpls);
122 }
123
124 /**
125 * Tests the addition of new VPLS to an empty configuration.
126 */
127 @Test
128 public void addVplsToEmpty() {
129 VplsConfig newVpls = createNewVpls();
130 emptyVplsAppConfig.addVpls(newVpls);
131
132 assertFalse("The new VPLS has not been added correctly",
133 emptyVplsAppConfig.vplss().isEmpty());
134 }
135
136 /**
137 * Tests the removal of an existing VPLS from the configuration.
138 */
139 @Test
140 public void removeExistingVpls() {
141 int initialSize = vplsAppConfig.vplss().size();
142 vplsAppConfig.removeVpls(VPLS1);
143
144 assertEquals("The VPLS has not been removed correctly",
145 initialSize - 1, vplsAppConfig.vplss().size());
146 }
147
148 /**
149 * Tests the removal of a non-existing VPLS from the configuration.
150 */
151 @Test
152 public void removeInexistingVpls() {
153 int initialSize = vplsAppConfig.vplss().size();
154 vplsAppConfig.removeVpls(VPLS2);
155
156 assertEquals("Non-configured VPLS has been unexpectedly removed",
157 initialSize, vplsAppConfig.vplss().size());
158 }
159
160 /**
161 * Tests the addition of a new interface.
162 */
163 @Test
164 public void addInterfaceToVpls() {
165 int initialSize = vplsAppConfig.getVplsWithName(VPLS1).ifaces().size();
166 vplsAppConfig.addIface(VPLS1, IF4);
167
168 assertEquals("The interface has not been added to the VPLS",
169 initialSize + 1,
170 vplsAppConfig.getVplsWithName(VPLS1).ifaces().size());
171 }
172
173 /**
174 * Tests the addition of a new interface when it already exists.
175 */
176 @Test
177 public void addExistingInterfaceToVpls() {
178 int initialSize = vplsAppConfig.getVplsWithName(VPLS1).ifaces().size();
179 vplsAppConfig.addIface(VPLS1, IF1);
180
181 assertEquals("The interface has been unexpectedly added twice to the" +
182 "same VPLS",
183 initialSize,
184 vplsAppConfig.getVplsWithName(VPLS1).ifaces().size());
185 }
186
187 /**
188 * Tests the retrieval of a VPLS, given an interface name.
189 */
190 @Test
191 public void getVplsFromInterface() {
192 assertNotNull("VPLS not found", vplsAppConfig.vplsFromIface(IF1));
193 assertNull("VPLS unexpectedly found",
194 vplsAppConfig.vplsFromIface(IF_NON_EXIST));
195 }
196
197 /**
198 * Tests the removal of an interface.
199 */
200 @Test
201 public void removeExistingInterfaceFromVpls() {
202 int initialSize = vplsAppConfig.getVplsWithName(VPLS1).ifaces().size();
203 vplsAppConfig.removeIface(initialVpls, IF1);
204
205 assertEquals("Interface has not been removed correctly from the VPLS",
206 initialSize - 1,
207 vplsAppConfig.getVplsWithName(VPLS1).ifaces().size());
208 }
209
210 /**
211 * Tests the removal of an interface from a VPLS when it does not exist.
212 */
213 @Test
214 public void removeNonExistingInterfaceFromVpls() {
215 int initialSize = vplsAppConfig.getVplsWithName(VPLS1).ifaces().size();
216 vplsAppConfig.removeIface(initialVpls, IF_NON_EXIST);
217
218 assertEquals("Interface unexpectedly removed from the VPLS",
219 initialSize, vplsAppConfig.getVplsWithName(VPLS1).ifaces().size());
220 }
221
222 /**
223 * Tests if the two interfaces are attached to the VPLS, while one of them
224 * is also attached and another.
225 */
226 @Test
227 public void isAttached() {
228 VplsConfig vpls = createNewVpls();
229
230 assertTrue("Interface not correctly attached to the VPLS",
231 vpls.isAttached(IF4));
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100232 assertTrue("Interface not correctly attached to the VPLS",
233 vpls.isAttached(IF4));
Luca Prete092e8952016-10-26 16:25:56 +0200234 assertFalse("Unexpected interface attached to the VPLS",
235 vpls.isAttached(IF_NON_EXIST));
236 }
237
238 /**
239 * Tests if encapsulation is set correctly.
240 */
241 @Test
242 public void encap() {
243 vplsAppConfig.setEncap(VPLS1, EncapsulationType.VLAN);
244 assertEquals("Wrong encapsulation type found",
245 EncapsulationType.VLAN,
246 vplsAppConfig.getVplsWithName(VPLS1).encap());
247 }
248
Yi Tsengf4e13e32017-03-30 15:38:39 -0700249 public static class MockCfgDelegate implements ConfigApplyDelegate {
Luca Prete092e8952016-10-26 16:25:56 +0200250 @Override
251 public void onApply(@SuppressWarnings("rawtypes") Config config) {
252 config.apply();
253 }
Luca Prete092e8952016-10-26 16:25:56 +0200254 }
255
Yi Tsengf4e13e32017-03-30 15:38:39 -0700256 /**
257 * Creates a basic VPLS config.
258 *
259 * @return the VPLS config
260 */
Luca Prete092e8952016-10-26 16:25:56 +0200261 private VplsConfig createInitialVpls() {
262 Set<String> ifaces = new HashSet<>(Arrays.asList(IF1, IF2, IF3));
Luca Prete092e8952016-10-26 16:25:56 +0200263 return new VplsConfig(VPLS1, ifaces, EncapsulationType.NONE);
264 }
265
Yi Tsengf4e13e32017-03-30 15:38:39 -0700266 /**
267 * Creates another basic VPLS config.
268 *
269 * @return the VPLS config
270 */
Luca Prete092e8952016-10-26 16:25:56 +0200271 private VplsConfig createNewVpls() {
272 Set<String> ifaces = new HashSet<>(Arrays.asList(IF4, IF5));
Luca Prete092e8952016-10-26 16:25:56 +0200273 return new VplsConfig(NEWVPLS, ifaces, EncapsulationType.NONE);
274 }
275}