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