blob: 53ebcd9ce027bacff7477e446d884dbe5e96ec99 [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;
Thomas Vachuskaa4d5a492017-05-10 12:02:43 -070043import org.onosproject.yang.model.DefaultResourceData;
janani b35f6cbc2017-03-24 21:56:58 +053044
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;
janani bd821b182017-03-30 16:34:49 +053052import static org.onosproject.l3vpn.netl3vpn.BgpModelIdLevel.VPN;
janani b35f6cbc2017-03-24 21:56:58 +053053import static org.onosproject.l3vpn.netl3vpn.VpnType.ANY_TO_ANY;
54import static org.onosproject.l3vpn.netl3vpn.VpnType.HUB;
55import static org.onosproject.l3vpn.netl3vpn.VpnType.SPOKE;
56
57/**
58 * Representation of utility for YANG tree builder.
59 */
60public final class NetL3VpnUtil {
61
62 /**
janani b35f6cbc2017-03-24 21:56:58 +053063 * Error message for site VPN name being not present in global VPN.
64 */
65 static final String SITE_VPN_MISMATCH = "Site VPN instance name did not " +
66 "match any of the global VPN names";
67
68 /**
69 * Error message for VPN attachment object being null.
70 */
71 static final String VPN_ATTACHMENT_NULL = "The VPN attachment information" +
72 " cannot be null";
73
74 /**
75 * Error message for VPN policy being not supported.
76 */
77 static final String VPN_POLICY_NOT_SUPPORTED = "VPN policy implementation" +
78 " is not supported.";
79
80 /**
81 * Static constant value for hundred.
82 */
83 static final String CONS_HUNDRED = "100:";
84
85 /**
86 * Error message for site role being not present in site network access.
87 */
88 static final String SITE_ROLE_NULL = "There must be a site role available" +
89 " for the VPN in site network access.";
90
91 /**
92 * Error message for bearer object information being null.
93 */
94 static final String BEARER_NULL = "The bearer information of the access " +
95 "is not available";
96
97 /**
98 * Error message for requested type or ip connect being null.
99 */
100 static final String IP_INT_INFO_NULL = "The required information of " +
101 "request type or ip connection is not available";
102
103 /**
104 * Error message for device info being not available from augment.
105 */
106 static final String DEVICE_INFO_NULL = "Bearer of site does not have any " +
107 "device information in the augment info.";
108
109 /**
janani b176905a2017-03-28 17:36:18 +0530110 * Static constant value for ip address.
janani b35f6cbc2017-03-24 21:56:58 +0530111 */
janani b176905a2017-03-28 17:36:18 +0530112 static final String IP = "ipaddress";
janani b35f6cbc2017-03-24 21:56:58 +0530113
114 /**
115 * Error message for VPN type being not supported.
116 */
117 static final String VPN_TYPE_UNSUPPORTED = "The VPN type is not supported";
118
119 /**
120 * Error message when the generated ID has crossed the limit.
121 */
122 static final String ID_LIMIT_EXCEEDED = "The ID generation has got " +
123 "exceeded";
124
125 /**
126 * Static constant value ID management limit.
127 */
128 static final Long ID_LIMIT = 4294967295L;
129
130 /**
131 * Error message for interface information being not available.
132 */
133 static final String INT_INFO_NULL = "Requested type does not have any " +
134 "interface information in the augment info.";
135
136 /**
137 * Static constant value of port name.
138 */
139 static final String PORT_NAME = "portName";
140
janani b176905a2017-03-28 17:36:18 +0530141 /**
142 * Static constants to use with accumulator for maximum number of events.
143 */
144 static final int MAX_EVENTS = 1000;
145
146 /**
147 * Static constants to use with accumulator for maximum number of millis.
148 */
149 static final int MAX_BATCH_MS = 5000;
150
151 /**
152 * Static constants to use with accumulator for maximum number of idle
153 * millis.
154 */
155 static final int MAX_IDLE_MS = 1000;
156
157 /**
158 * Static constants for timer name.
159 */
160 static final String TIMER = "dynamic-config-l3vpn-timer";
161
162 /**
163 * Error message for unknown event being occurred.
164 */
165 static final String UNKNOWN_EVENT = "NetL3VPN listener: unknown event: {}";
166
167 /**
168 * Error message for event being null.
169 */
170 static final String EVENT_NULL = "Event cannot be null";
171
janani b35f6cbc2017-03-24 21:56:58 +0530172 private static final String SITE_ROLE_INVALID = "The given site role is " +
173 "invalid";
174 private static final String ANY_TO_ANY_ROLE = "AnyToAnyRole";
175 private static final String HUB_ROLE = "HubRole";
176 private static final String SPOKE_ROLE = "SpokeRole";
177
178 // No instantiation.
179 private NetL3VpnUtil() {
180 }
181
182 /**
183 * Returns the model object id for service L3VPN container.
184 *
185 * @return model object id
186 */
187 static ModelObjectId getModIdForL3VpnSvc() {
188 return ModelObjectId.builder().addChild(DefaultL3VpnSvc.class).build();
189 }
190
191 /**
192 * Returns the model object id for service sites container.
193 *
194 * @return model object id
195 */
196 static ModelObjectId getModIdForSites() {
197 return ModelObjectId.builder().addChild(DefaultL3VpnSvc.class)
198 .addChild(DefaultSites.class).build();
199 }
200
201 /**
202 * Returns the resource data from the data node and the resource id.
203 *
204 * @param dataNode data node
205 * @param resId resource id
206 * @return resource data
207 */
208 static ResourceData getResourceData(DataNode dataNode, ResourceId resId) {
209 return DefaultResourceData.builder().addDataNode(dataNode)
210 .resourceId(resId).build();
211 }
212
213 /**
214 * Returns the VPN role from the service site role.
215 *
216 * @param siteRole service site role
217 * @return VPN type
218 */
219 static VpnType getRole(Class<? extends SiteRole> siteRole) {
220 switch (siteRole.getSimpleName()) {
221 case ANY_TO_ANY_ROLE:
222 return ANY_TO_ANY;
223
224 case HUB_ROLE:
225 return HUB;
226
227 case SPOKE_ROLE:
228 return SPOKE;
229
230 default:
231 throw new NetL3VpnException(SITE_ROLE_INVALID);
232 }
233 }
234
235 /**
236 * Returns error message for management ip being unavailable in device.
237 *
238 * @param ip management ip
239 * @return error message
240 */
241 static String getMgmtIpUnAvailErr(String ip) {
242 return "The device with management ip " + ip + " is not available.";
243 }
244
245 /**
246 * Returns true if device id present in the interface map; false otherwise.
247 *
248 * @param info interface map
249 * @param id device id
250 * @return true if device id available; false otherwise
251 */
252 private static boolean isDevIdPresent(Map<AccessInfo, InterfaceInfo> info,
253 String id) {
254 for (Map.Entry<AccessInfo, InterfaceInfo> inter : info.entrySet()) {
255 InterfaceInfo interfaceInfo = inter.getValue();
256 DeviceId devId = interfaceInfo.devInfo().deviceId();
257 if (devId.toString().equals(id)) {
258 return true;
259 }
260 }
261 return false;
262 }
263
264 /**
265 * Builds the device model VPN instance model object data, with respect to
266 * the device level.
267 *
268 * @param id device id
269 * @param ins VPN instance
270 * @return model object data, with device level
271 */
272 private static ModelObjectData buildInsModDataDevice(String id,
273 NetworkInstances ins) {
274 DeviceKeys devKeys = new DeviceKeys();
275 devKeys.deviceid(id);
276 ModelObjectId modelId = ModelObjectId.builder()
277 .addChild(DefaultDevices.class)
278 .addChild(DefaultDevice.class, devKeys)
279 .build();
Thomas Vachuskad17bc732017-03-24 11:46:55 -0700280 return DefaultModelObjectData.builder().identifier(modelId)
janani b35f6cbc2017-03-24 21:56:58 +0530281 .addModelObject((InnerModelObject) ins).build();
282 }
283
284 /**
285 * Builds the device model VPN instance model object data, with respect to
286 * the devices level.
287 *
288 * @param id device id
289 * @param ins VPN instance
290 * @return model object data, with devices level
291 */
292 private static ModelObjectData buildInsModDataDevices(String id,
293 NetworkInstances ins) {
294 ModelObjectId modelId = ModelObjectId.builder()
295 .addChild(DefaultDevices.class).build();
296 Device device = new DefaultDevice();
297 device.deviceid(id);
298 device.networkInstances(ins);
Thomas Vachuskad17bc732017-03-24 11:46:55 -0700299 return DefaultModelObjectData.builder().identifier(modelId)
janani b35f6cbc2017-03-24 21:56:58 +0530300 .addModelObject((InnerModelObject) device).build();
301 }
302
303 /**
304 * Builds the device model VPN instance model object data, with respect to
305 * root level.
306 *
307 * @param id device id
308 * @param ins VPN instance
309 * @return model object data, with root level
310 */
311 private static ModelObjectData buildInsModDataRoot(String id,
312 NetworkInstances ins) {
313 Devices devices = new DefaultDevices();
314 Device device = new DefaultDevice();
315 List<Device> deviceList = new LinkedList<>();
316 device.deviceid(id);
317 device.networkInstances(ins);
318 deviceList.add(device);
319 devices.device(deviceList);
320 return DefaultModelObjectData.builder()
321 .addModelObject((InnerModelObject) devices).build();
322 }
323
324 /**
325 * Builds the device model interface model object data, with respect to
326 * device level.
327 *
328 * @param id device id
329 * @param ifs interface object
330 * @return model object data, with device level
331 */
332 private static ModelObjectData buildIntModDataDevice(String id,
333 Interfaces ifs) {
334 org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
335 .rev20140508.ietfinterfaces.devices.DeviceKeys keys =
336 new org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang
337 .ietf.interfaces.rev20140508.ietfinterfaces.devices
338 .DeviceKeys();
339 keys.deviceid(id);
340 ModelObjectId modelId = ModelObjectId.builder()
341 .addChild(org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns
342 .yang.ietf.interfaces.rev20140508
343 .ietfinterfaces.DefaultDevices.class)
344 .addChild(org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns
345 .yang.ietf.interfaces.rev20140508
346 .ietfinterfaces.devices.DefaultDevice.class,
347 keys)
348 .build();
Thomas Vachuskad17bc732017-03-24 11:46:55 -0700349 return DefaultModelObjectData.builder().identifier(modelId)
janani b35f6cbc2017-03-24 21:56:58 +0530350 .addModelObject((InnerModelObject) ifs).build();
351 }
352
353 /**
354 * Returns the VPN instance create model object data.
355 *
356 * @param intMap interface map
357 * @param instances VPN instances
358 * @param id device id
359 * @return VPN instance model object data
360 */
361 static ModelObjectData getVpnCreateModObj(Map<AccessInfo, InterfaceInfo> intMap,
362 NetworkInstances instances,
363 String id) {
364 ModelObjectData modData;
365 boolean devAdded = isDevIdPresent(intMap, id);
366 if (devAdded) {
367 modData = buildInsModDataDevice(id, instances);
368 } else if (intMap.size() != 0) {
369 modData = buildInsModDataDevices(id, instances);
370 } else {
371 modData = buildInsModDataRoot(id, instances);
372 }
373 return modData;
374 }
375
376 /**
377 * Returns error message for interface being unavailable in device.
378 *
379 * @param intName interface name
380 * @return error message
381 */
382 static String getIntNotAvailable(String intName) {
383 return "The interface " + intName + " is not available.";
384 }
385
386 /**
387 * Returns the interface create model object data.
388 *
janani bd821b182017-03-30 16:34:49 +0530389 * @param ifNames interfaces
390 * @param ifs interface instance
391 * @param id device id
janani b35f6cbc2017-03-24 21:56:58 +0530392 * @return interface model object data
393 */
janani bd821b182017-03-30 16:34:49 +0530394 static ModelObjectData getIntCreateModObj(List<String> ifNames,
janani b35f6cbc2017-03-24 21:56:58 +0530395 Interfaces ifs, String id) {
396 ModelObjectData modData;
janani bd821b182017-03-30 16:34:49 +0530397 if (ifNames.size() > 1) {
janani b35f6cbc2017-03-24 21:56:58 +0530398 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
janani bd821b182017-03-30 16:34:49 +0530442 * @param devBgp device BGP info
443 * @param intBgp interface BGP info
444 * @return BGP driver config
janani b35f6cbc2017-03-24 21:56:58 +0530445 */
446 static BgpDriverInfo getBgpCreateConfigObj(Map<BgpInfo, DeviceId> bgpMap,
janani bd821b182017-03-30 16:34:49 +0530447 String id, BgpInfo devBgp,
448 BgpInfo intBgp) {
janani b35f6cbc2017-03-24 21:56:58 +0530449 boolean isDevIdPresent = isDevIdBgpPresent(bgpMap, id);
450 BgpDriverInfo info;
janani bd821b182017-03-30 16:34:49 +0530451 if (devBgp != intBgp) {
452 //TODO: With ipv6 BGP it has to be changed
453 info = new BgpDriverInfo(VPN, id);
454 } else if (isDevIdPresent) {
janani b35f6cbc2017-03-24 21:56:58 +0530455 info = new BgpDriverInfo(DEVICE, id);
janani b35f6cbc2017-03-24 21:56:58 +0530456 } else if (bgpMap.size() != 0) {
457 info = new BgpDriverInfo(DEVICES, id);
458 } else {
459 info = new BgpDriverInfo(ROOT, id);
460 }
461 return info;
462 }
463
464 /**
465 * Returns true if the device is present in the BGP map; false otherwise.
466 *
467 * @param bgpMap BGP map
468 * @param id device id
469 * @return true if device is present; false otherwise
470 */
471 private static boolean isDevIdBgpPresent(Map<BgpInfo, DeviceId> bgpMap,
472 String id) {
473 for (Map.Entry<BgpInfo, DeviceId> info : bgpMap.entrySet()) {
474 DeviceId devId = info.getValue();
475 if (devId.toString().equals(id)) {
476 return true;
477 }
478 }
479 return false;
480 }
janani bd821b182017-03-30 16:34:49 +0530481
482 /**
483 * Returns the model object data for VPN instance deletion.
484 *
485 * @param intMap interface map
486 * @param ins VPN instance
487 * @param id device id
488 * @return model object data
489 */
490 static ModelObjectData getVpnDelModObj(Map<AccessInfo, InterfaceInfo> intMap,
491 NetworkInstances ins,
492 String id) {
493 boolean isDevIdPresent = isDevIdPresent(intMap, id);
494 ModelObjectData modData;
495 if (intMap.size() == 0) {
496 modData = buildInsModDataRoot(id, ins);
497 } else if (isDevIdPresent) {
498 modData = buildInsModDataDevice(id, ins);
499 } else {
500 modData = buildInsModDataDevices(id, ins);
501 }
502 return modData;
503 }
504
505 /**
506 * Returns the BGP driver info for VPN BGP instance deletion.
507 *
508 * @param bgpMap BGP map
509 * @param id device id
510 * @return BGP driver info
511 */
512 static BgpDriverInfo getVpnBgpDelModObj(Map<BgpInfo, DeviceId> bgpMap,
513 String id) {
514 boolean isDevIdPresent = isDevIdBgpPresent(bgpMap, id);
515 BgpDriverInfo driInfo;
516 if (bgpMap.size() == 0) {
517 driInfo = new BgpDriverInfo(ROOT, id);
518 } else if (isDevIdPresent) {
519 driInfo = new BgpDriverInfo(DEVICE, id);
520 } else {
521 driInfo = new BgpDriverInfo(DEVICES, id);
522 }
523 return driInfo;
524 }
janani b35f6cbc2017-03-24 21:56:58 +0530525}