blob: a713fc3b6af31945d8c374c519eb645520f8b9c0 [file] [log] [blame]
janani b35f6cbc2017-03-24 21:56:58 +05301/*
2 * Copyright 2017-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
17package org.onosproject.l3vpn.netl3vpn.impl;
18
19import org.onosproject.l3vpn.netl3vpn.AccessInfo;
20import org.onosproject.l3vpn.netl3vpn.BgpDriverInfo;
21import org.onosproject.l3vpn.netl3vpn.BgpInfo;
22import org.onosproject.l3vpn.netl3vpn.InterfaceInfo;
23import org.onosproject.l3vpn.netl3vpn.NetL3VpnException;
24import org.onosproject.l3vpn.netl3vpn.VpnType;
25import org.onosproject.net.DeviceId;
26import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20140508.ietfinterfaces.devices.device.Interfaces;
27import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.l3vpn.svc.rev20160730.ietfl3vpnsvc.DefaultL3VpnSvc;
28import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.l3vpn.svc.rev20160730.ietfl3vpnsvc.SiteRole;
29import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.l3vpn.svc.rev20160730.ietfl3vpnsvc.l3vpnsvc.DefaultSites;
30import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.DefaultDevices;
31import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.Devices;
32import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.DefaultDevice;
33import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.Device;
34import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.DeviceKeys;
35import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.NetworkInstances;
36import org.onosproject.yang.model.DataNode;
37import org.onosproject.yang.model.DefaultModelObjectData;
38import org.onosproject.yang.model.InnerModelObject;
39import org.onosproject.yang.model.ModelObjectData;
40import org.onosproject.yang.model.ModelObjectId;
41import org.onosproject.yang.model.ResourceData;
42import org.onosproject.yang.model.ResourceId;
43import org.onosproject.yang.runtime.DefaultResourceData;
44
45import java.util.LinkedList;
46import java.util.List;
47import java.util.Map;
48
49import static org.onosproject.l3vpn.netl3vpn.BgpModelIdLevel.DEVICE;
50import static org.onosproject.l3vpn.netl3vpn.BgpModelIdLevel.DEVICES;
51import static org.onosproject.l3vpn.netl3vpn.BgpModelIdLevel.ROOT;
52import static org.onosproject.l3vpn.netl3vpn.VpnType.ANY_TO_ANY;
53import static org.onosproject.l3vpn.netl3vpn.VpnType.HUB;
54import static org.onosproject.l3vpn.netl3vpn.VpnType.SPOKE;
55
56/**
57 * Representation of utility for YANG tree builder.
58 */
59public final class NetL3VpnUtil {
60
61 /**
janani b35f6cbc2017-03-24 21:56:58 +053062 * Error message for site VPN name being not present in global VPN.
63 */
64 static final String SITE_VPN_MISMATCH = "Site VPN instance name did not " +
65 "match any of the global VPN names";
66
67 /**
68 * Error message for VPN attachment object being null.
69 */
70 static final String VPN_ATTACHMENT_NULL = "The VPN attachment information" +
71 " cannot be null";
72
73 /**
74 * Error message for VPN policy being not supported.
75 */
76 static final String VPN_POLICY_NOT_SUPPORTED = "VPN policy implementation" +
77 " is not supported.";
78
79 /**
80 * Static constant value for hundred.
81 */
82 static final String CONS_HUNDRED = "100:";
83
84 /**
85 * Error message for site role being not present in site network access.
86 */
87 static final String SITE_ROLE_NULL = "There must be a site role available" +
88 " for the VPN in site network access.";
89
90 /**
91 * Error message for bearer object information being null.
92 */
93 static final String BEARER_NULL = "The bearer information of the access " +
94 "is not available";
95
96 /**
97 * Error message for requested type or ip connect being null.
98 */
99 static final String IP_INT_INFO_NULL = "The required information of " +
100 "request type or ip connection is not available";
101
102 /**
103 * Error message for device info being not available from augment.
104 */
105 static final String DEVICE_INFO_NULL = "Bearer of site does not have any " +
106 "device information in the augment info.";
107
108 /**
janani b176905a2017-03-28 17:36:18 +0530109 * Static constant value for ip address.
janani b35f6cbc2017-03-24 21:56:58 +0530110 */
janani b176905a2017-03-28 17:36:18 +0530111 static final String IP = "ipaddress";
janani b35f6cbc2017-03-24 21:56:58 +0530112
113 /**
114 * Error message for VPN type being not supported.
115 */
116 static final String VPN_TYPE_UNSUPPORTED = "The VPN type is not supported";
117
118 /**
119 * Error message when the generated ID has crossed the limit.
120 */
121 static final String ID_LIMIT_EXCEEDED = "The ID generation has got " +
122 "exceeded";
123
124 /**
125 * Static constant value ID management limit.
126 */
127 static final Long ID_LIMIT = 4294967295L;
128
129 /**
130 * Error message for interface information being not available.
131 */
132 static final String INT_INFO_NULL = "Requested type does not have any " +
133 "interface information in the augment info.";
134
135 /**
136 * Static constant value of port name.
137 */
138 static final String PORT_NAME = "portName";
139
janani b176905a2017-03-28 17:36:18 +0530140 /**
141 * Static constants to use with accumulator for maximum number of events.
142 */
143 static final int MAX_EVENTS = 1000;
144
145 /**
146 * Static constants to use with accumulator for maximum number of millis.
147 */
148 static final int MAX_BATCH_MS = 5000;
149
150 /**
151 * Static constants to use with accumulator for maximum number of idle
152 * millis.
153 */
154 static final int MAX_IDLE_MS = 1000;
155
156 /**
157 * Static constants for timer name.
158 */
159 static final String TIMER = "dynamic-config-l3vpn-timer";
160
161 /**
162 * Error message for unknown event being occurred.
163 */
164 static final String UNKNOWN_EVENT = "NetL3VPN listener: unknown event: {}";
165
166 /**
167 * Error message for event being null.
168 */
169 static final String EVENT_NULL = "Event cannot be null";
170
janani b35f6cbc2017-03-24 21:56:58 +0530171 private static final String SITE_ROLE_INVALID = "The given site role is " +
172 "invalid";
173 private static final String ANY_TO_ANY_ROLE = "AnyToAnyRole";
174 private static final String HUB_ROLE = "HubRole";
175 private static final String SPOKE_ROLE = "SpokeRole";
176
177 // No instantiation.
178 private NetL3VpnUtil() {
179 }
180
181 /**
182 * Returns the model object id for service L3VPN container.
183 *
184 * @return model object id
185 */
186 static ModelObjectId getModIdForL3VpnSvc() {
187 return ModelObjectId.builder().addChild(DefaultL3VpnSvc.class).build();
188 }
189
190 /**
191 * Returns the model object id for service sites container.
192 *
193 * @return model object id
194 */
195 static ModelObjectId getModIdForSites() {
196 return ModelObjectId.builder().addChild(DefaultL3VpnSvc.class)
197 .addChild(DefaultSites.class).build();
198 }
199
200 /**
201 * Returns the resource data from the data node and the resource id.
202 *
203 * @param dataNode data node
204 * @param resId resource id
205 * @return resource data
206 */
207 static ResourceData getResourceData(DataNode dataNode, ResourceId resId) {
208 return DefaultResourceData.builder().addDataNode(dataNode)
209 .resourceId(resId).build();
210 }
211
212 /**
213 * Returns the VPN role from the service site role.
214 *
215 * @param siteRole service site role
216 * @return VPN type
217 */
218 static VpnType getRole(Class<? extends SiteRole> siteRole) {
219 switch (siteRole.getSimpleName()) {
220 case ANY_TO_ANY_ROLE:
221 return ANY_TO_ANY;
222
223 case HUB_ROLE:
224 return HUB;
225
226 case SPOKE_ROLE:
227 return SPOKE;
228
229 default:
230 throw new NetL3VpnException(SITE_ROLE_INVALID);
231 }
232 }
233
234 /**
235 * Returns error message for management ip being unavailable in device.
236 *
237 * @param ip management ip
238 * @return error message
239 */
240 static String getMgmtIpUnAvailErr(String ip) {
241 return "The device with management ip " + ip + " is not available.";
242 }
243
244 /**
245 * Returns true if device id present in the interface map; false otherwise.
246 *
247 * @param info interface map
248 * @param id device id
249 * @return true if device id available; false otherwise
250 */
251 private static boolean isDevIdPresent(Map<AccessInfo, InterfaceInfo> info,
252 String id) {
253 for (Map.Entry<AccessInfo, InterfaceInfo> inter : info.entrySet()) {
254 InterfaceInfo interfaceInfo = inter.getValue();
255 DeviceId devId = interfaceInfo.devInfo().deviceId();
256 if (devId.toString().equals(id)) {
257 return true;
258 }
259 }
260 return false;
261 }
262
263 /**
264 * Builds the device model VPN instance model object data, with respect to
265 * the device level.
266 *
267 * @param id device id
268 * @param ins VPN instance
269 * @return model object data, with device level
270 */
271 private static ModelObjectData buildInsModDataDevice(String id,
272 NetworkInstances ins) {
273 DeviceKeys devKeys = new DeviceKeys();
274 devKeys.deviceid(id);
275 ModelObjectId modelId = ModelObjectId.builder()
276 .addChild(DefaultDevices.class)
277 .addChild(DefaultDevice.class, devKeys)
278 .build();
Thomas Vachuskad17bc732017-03-24 11:46:55 -0700279 return DefaultModelObjectData.builder().identifier(modelId)
janani b35f6cbc2017-03-24 21:56:58 +0530280 .addModelObject((InnerModelObject) ins).build();
281 }
282
283 /**
284 * Builds the device model VPN instance model object data, with respect to
285 * the devices level.
286 *
287 * @param id device id
288 * @param ins VPN instance
289 * @return model object data, with devices level
290 */
291 private static ModelObjectData buildInsModDataDevices(String id,
292 NetworkInstances ins) {
293 ModelObjectId modelId = ModelObjectId.builder()
294 .addChild(DefaultDevices.class).build();
295 Device device = new DefaultDevice();
296 device.deviceid(id);
297 device.networkInstances(ins);
Thomas Vachuskad17bc732017-03-24 11:46:55 -0700298 return DefaultModelObjectData.builder().identifier(modelId)
janani b35f6cbc2017-03-24 21:56:58 +0530299 .addModelObject((InnerModelObject) device).build();
300 }
301
302 /**
303 * Builds the device model VPN instance model object data, with respect to
304 * root level.
305 *
306 * @param id device id
307 * @param ins VPN instance
308 * @return model object data, with root level
309 */
310 private static ModelObjectData buildInsModDataRoot(String id,
311 NetworkInstances ins) {
312 Devices devices = new DefaultDevices();
313 Device device = new DefaultDevice();
314 List<Device> deviceList = new LinkedList<>();
315 device.deviceid(id);
316 device.networkInstances(ins);
317 deviceList.add(device);
318 devices.device(deviceList);
319 return DefaultModelObjectData.builder()
320 .addModelObject((InnerModelObject) devices).build();
321 }
322
323 /**
324 * Builds the device model interface model object data, with respect to
325 * device level.
326 *
327 * @param id device id
328 * @param ifs interface object
329 * @return model object data, with device level
330 */
331 private static ModelObjectData buildIntModDataDevice(String id,
332 Interfaces ifs) {
333 org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
334 .rev20140508.ietfinterfaces.devices.DeviceKeys keys =
335 new org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang
336 .ietf.interfaces.rev20140508.ietfinterfaces.devices
337 .DeviceKeys();
338 keys.deviceid(id);
339 ModelObjectId modelId = ModelObjectId.builder()
340 .addChild(org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns
341 .yang.ietf.interfaces.rev20140508
342 .ietfinterfaces.DefaultDevices.class)
343 .addChild(org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns
344 .yang.ietf.interfaces.rev20140508
345 .ietfinterfaces.devices.DefaultDevice.class,
346 keys)
347 .build();
Thomas Vachuskad17bc732017-03-24 11:46:55 -0700348 return DefaultModelObjectData.builder().identifier(modelId)
janani b35f6cbc2017-03-24 21:56:58 +0530349 .addModelObject((InnerModelObject) ifs).build();
350 }
351
352 /**
353 * Returns the VPN instance create model object data.
354 *
355 * @param intMap interface map
356 * @param instances VPN instances
357 * @param id device id
358 * @return VPN instance model object data
359 */
360 static ModelObjectData getVpnCreateModObj(Map<AccessInfo, InterfaceInfo> intMap,
361 NetworkInstances instances,
362 String id) {
363 ModelObjectData modData;
364 boolean devAdded = isDevIdPresent(intMap, id);
365 if (devAdded) {
366 modData = buildInsModDataDevice(id, instances);
367 } else if (intMap.size() != 0) {
368 modData = buildInsModDataDevices(id, instances);
369 } else {
370 modData = buildInsModDataRoot(id, instances);
371 }
372 return modData;
373 }
374
375 /**
376 * Returns error message for interface being unavailable in device.
377 *
378 * @param intName interface name
379 * @return error message
380 */
381 static String getIntNotAvailable(String intName) {
382 return "The interface " + intName + " is not available.";
383 }
384
385 /**
386 * Returns the interface create model object data.
387 *
388 * @param intMap interface map
389 * @param ifs interface instance
390 * @param id device id
391 * @return interface model object data
392 */
393 static ModelObjectData getIntCreateModObj(Map<AccessInfo, InterfaceInfo> intMap,
394 Interfaces ifs, String id) {
395 ModelObjectData modData;
396 boolean intAdded = isDevIdPresent(intMap, id);
397 if (intAdded) {
398 modData = buildIntModDataDevice(id, ifs);
399 } else {
400 modData = buildIntModDataRoot(id, ifs);
401 }
402 return modData;
403 }
404
405 /**
406 * Builds the device model interface model object data, with respect to
407 * root level.
408 *
409 * @param id device id
410 * @param ifs interface object
411 * @return model object data, with root level
412 */
413 private static ModelObjectData buildIntModDataRoot(String id,
414 Interfaces ifs) {
415 org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
416 .rev20140508.ietfinterfaces.Devices devices =
417 new org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang
418 .ietf.interfaces.rev20140508.ietfinterfaces
419 .DefaultDevices();
420 org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
421 .rev20140508.ietfinterfaces.devices.Device device =
422 new org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang
423 .ietf.interfaces.rev20140508.ietfinterfaces.devices
424 .DefaultDevice();
425 List<org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
426 .interfaces.rev20140508.ietfinterfaces.devices
427 .Device> deviceList = new LinkedList<>();
428
429 device.deviceid(id);
430 device.interfaces(ifs);
431 deviceList.add(device);
432 devices.device(deviceList);
433 return DefaultModelObjectData.builder()
434 .addModelObject((InnerModelObject) devices).build();
435 }
436
437 /**
438 * Returns the BGP create driver info.
439 *
440 * @param bgpMap BGP map
441 * @param id device id
442 * @return driver config info
443 */
444 static BgpDriverInfo getBgpCreateConfigObj(Map<BgpInfo, DeviceId> bgpMap,
445 String id) {
446 boolean isDevIdPresent = isDevIdBgpPresent(bgpMap, id);
447 BgpDriverInfo info;
448 if (isDevIdPresent) {
449 info = new BgpDriverInfo(DEVICE, id);
450
451 } else if (bgpMap.size() != 0) {
452 info = new BgpDriverInfo(DEVICES, id);
453 } else {
454 info = new BgpDriverInfo(ROOT, id);
455 }
456 return info;
457 }
458
459 /**
460 * Returns true if the device is present in the BGP map; false otherwise.
461 *
462 * @param bgpMap BGP map
463 * @param id device id
464 * @return true if device is present; false otherwise
465 */
466 private static boolean isDevIdBgpPresent(Map<BgpInfo, DeviceId> bgpMap,
467 String id) {
468 for (Map.Entry<BgpInfo, DeviceId> info : bgpMap.entrySet()) {
469 DeviceId devId = info.getValue();
470 if (devId.toString().equals(id)) {
471 return true;
472 }
473 }
474 return false;
475 }
476}