blob: 918c5fda11c160bd78a941751cfe481bc196e718 [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +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.drivers.huawei;
18
19import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.DefaultDevice;
20import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.DeviceKeys;
21import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.DefaultL3Vpn;
22import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.L3Vpn;
23import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.DefaultL3Vpncomm;
24import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.L3Vpncomm;
25import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.DefaultL3VpnInstances;
26import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.L3VpnInstances;
27import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.DefaultL3VpnInstance;
28import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.L3VpnInstance;
29import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.L3VpnInstanceKeys;
30import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.DefaultVpnInstAfs;
31import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.VpnInstAfs;
32import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.DefaultVpnInstAf;
33import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.VpnInstAf;
34import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.DefaultVpnTargets;
35import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.VpnTargets;
36import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.vpntargets.DefaultVpnTarget;
37import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.vpntargets.VpnTarget;
38import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.devices.device.networkinstances.networkinstance.DefaultAugmentedNiNetworkInstance;
39import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.ipv4.Unicast;
40import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.Rts;
41import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum;
42import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.DefaultDevices;
43import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.Device;
44import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.NetworkInstances;
45import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.networkinstances.DefaultNetworkInstance;
46import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.networkinstances.NetworkInstance;
47import org.onosproject.yang.model.InnerModelObject;
48import org.onosproject.yang.model.ModelObjectData;
49import org.onosproject.yang.model.ModelObjectId;
50
51import java.util.Iterator;
52import java.util.LinkedList;
53import java.util.List;
54
55import static org.onosproject.drivers.huawei.DriverUtil.CONS_DEVICES;
56import static org.onosproject.drivers.huawei.DriverUtil.DEVICE_NULL;
57import static org.onosproject.drivers.huawei.DriverUtil.INS_NULL;
58import static org.onosproject.drivers.huawei.DriverUtil.OBJECT_NULL;
59import static org.onosproject.drivers.huawei.DriverUtil.UNSUPPORTED_MODEL_LVL;
60import static org.onosproject.drivers.huawei.DriverUtil.getData;
61import static org.onosproject.drivers.huawei.DriverUtil.getIdFromModId;
62import static org.onosproject.drivers.huawei.DriverUtil.getModObjIdDriDevices;
63import static org.onosproject.drivers.huawei.DriverUtil.getObjFromModData;
64import static org.onosproject.drivers.huawei.ModelIdLevel.DEVICE;
65import static org.onosproject.drivers.huawei.ModelIdLevel.DEVICES;
66import static org.onosproject.drivers.huawei.ModelIdLevel.ROOT;
67import static org.onosproject.yang.gen.v1.l3vpn.comm.type.rev20141225.nel3vpncommtype.L3VpncommonL3VpnPrefixType.of;
68import static org.onosproject.yang.gen.v1.l3vpn.comm.type.rev20141225.nel3vpncommtype.L3VpncommonVrfRtType.fromString;
69import static org.onosproject.yang.gen.v1.l3vpn.comm.type.rev20141225.nel3vpncommtype.l3vpncommonl3vpnprefixtype.L3VpncommonL3VpnPrefixTypeEnum.IPV4UNI;
70import static org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum.BOTH;
71
72/**
73 * Representation of utility for instance creation and deletion.
74 */
75public final class InsConstructionUtil {
76
77 /**
78 * Static constant for route target export.
79 */
80 private static final String EXP_COMM = "export_extcommunity";
81
82 /**
83 * Static constant for route target import.
84 */
85 private static final String IMP_COMM = "import_extcommunity";
86
87 /**
88 * Error message for unsupported RT type.
89 */
90 private static final String UNSUPPORTED_RT_TYPE = "The RT type is not " +
91 "supported";
92
93 // No instantiation.
94 private InsConstructionUtil() {
95 }
96
97 /**
98 * Returns the created model object data of VPN instance of huawei device
99 * from the standard model object data.
100 *
101 * @param modObj model object data
102 * @param isDevAvail if devices available
103 * @return driver model object data
104 */
105 static ModelObjectData getCreateVpnIns(ModelObjectData modObj,
106 boolean isDevAvail) {
107 ModelIdLevel modIdLvl = DEVICE;
108 String id = getIdFromModId(modObj.identifier());
109 Object obj = getObjFromModData(modObj);
110
111 if (obj == null) {
112 throw new IllegalArgumentException(OBJECT_NULL);
113 }
114
115 if (id == null) {
116 id = getDevIdFromRootObj(obj);
117 obj = getObjFromRootObj(obj);
118 if (isDevAvail) {
119 modIdLvl = DEVICES;
120 } else {
121 modIdLvl = ROOT;
122 }
123 } else if (id.equals(CONS_DEVICES)) {
124 modIdLvl = DEVICES;
125 id = ((Device) obj).deviceid();
126 obj = ((Device) obj).networkInstances();
127 }
128 org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi
129 .DefaultDevices devices = getDriverDevices(
130 id, (NetworkInstances) obj);
131 return getCreateModObjData(modIdLvl, id, devices);
132 }
133
134 /**
135 * Returns the driver model object data, according to the levels it has
136 * to be constructed.
137 *
138 * @param modIdLvl model id level
139 * @param devId device id
140 * @param devices devices object
141 * @return model object data
142 */
143 private static ModelObjectData getCreateModObjData(ModelIdLevel modIdLvl,
144 String devId,
145 org.onosproject.yang.gen
146 .v1.ne.l3vpn.api
147 .rev20141225
148 .nel3vpnapi
149 .DefaultDevices
150 devices) {
151 ModelObjectId id;
152 List<org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi
153 .devices.Device> devList = devices.device();
154 Iterator<org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi
155 .devices.Device> it = devList.iterator();
156 org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi
157 .devices.Device device = it.next();
158 Iterator<L3VpnInstance> instIt = device.l3Vpn().l3Vpncomm()
159 .l3VpnInstances().l3VpnInstance().iterator();
160 L3VpnInstance ins = instIt.next();
161
162 switch (modIdLvl) {
163
164 case ROOT:
165 return getData(null, devices);
166
167 case DEVICES:
168 id = getModObjIdDriDevices();
169 return getData(id, (InnerModelObject) device);
170
171 case DEVICE:
172 id = getModelObjIdForIns(devId).build();
173 return getData(id, (InnerModelObject) ins);
174
175 default:
176 throw new IllegalArgumentException(UNSUPPORTED_MODEL_LVL);
177 }
178 }
179
180 /**
181 * Returns the devices object of the huawei VPN instance model. This
182 * constructs all the required information in the device for L3VPN
183 * instance creation.
184 *
185 * @param id device id
186 * @param obj network instances object
187 * @return driver VPN instance's devices
188 */
189 private static org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225
190 .nel3vpnapi.DefaultDevices getDriverDevices(String id,
191 NetworkInstances obj) {
192
193 org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.
194 DefaultDevices devices = new org.onosproject.yang.gen.v1.ne
195 .l3vpn.api.rev20141225.nel3vpnapi.DefaultDevices();
196 org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices
197 .Device device = new org.onosproject.yang.gen.v1.ne.l3vpn.api
198 .rev20141225.nel3vpnapi.devices.DefaultDevice();
199
200 L3Vpn l3Vpn = new DefaultL3Vpn();
201 L3Vpncomm l3VpnComm = new DefaultL3Vpncomm();
202 L3VpnInstances instances = new DefaultL3VpnInstances();
203 L3VpnInstance ins = new DefaultL3VpnInstance();
204 List<L3VpnInstance> insList = new LinkedList<>();
205 List<org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi
206 .devices.Device> devList = new LinkedList<>();
207
208 createDriIns(obj, ins);
209 insList.add(ins);
210 instances.l3VpnInstance(insList);
211 l3VpnComm.l3VpnInstances(instances);
212 l3Vpn.l3Vpncomm(l3VpnComm);
213
214 device.deviceid(id);
215 device.l3Vpn(l3Vpn);
216 devList.add(device);
217 devices.device(devList);
218 return devices;
219 }
220
221 /**
222 * Creates driver instance value from standard device instance.
223 *
224 * @param ins standard device instance
225 * @param driIns driver instance
226 */
227 private static void createDriIns(NetworkInstances ins,
228 L3VpnInstance driIns) {
229 NetworkInstance networkInstance = ins.networkInstance().iterator()
230 .next();
231 driIns.vrfName(networkInstance.name());
232 DefaultAugmentedNiNetworkInstance augIns =
233 ((DefaultNetworkInstance) networkInstance).augmentation(
234 DefaultAugmentedNiNetworkInstance.class);
235 VpnInstAfs vpnInstAfs = processL3VpnAf(augIns.l3Vpn());
236 driIns.vpnInstAfs(vpnInstAfs);
237 }
238
239 /**
240 * Returns the device id from the root object.
241 *
242 * @param obj root object
243 * @return device id
244 */
245 private static String getDevIdFromRootObj(Object obj) {
246 Device dev = getDevFromRootObj(obj);
247 return dev.deviceid();
248 }
249
250 /**
251 * Returns the first device from the root object. If no device is
252 * present, it returns null.
253 *
254 * @param obj root object
255 * @return device object
256 */
257 private static Device getDevFromRootObj(Object obj) {
258 List<Device> deviceList = ((DefaultDevices) obj).device();
259 Iterator<Device> it = deviceList.iterator();
260 if (it.hasNext()) {
261 return it.next();
262 }
263 throw new IllegalArgumentException(DEVICE_NULL);
264 }
265
266 /**
267 * Returns the network instances object from the root object.
268 *
269 * @param obj root object
270 * @return network instances
271 */
272 private static NetworkInstances getObjFromRootObj(Object obj) {
273 Device dev = getDevFromRootObj(obj);
274 return dev.networkInstances();
275 }
276
277 /**
278 * Returns model object id builder that has to be constructed for driver
279 * instance level addition, with the device id.
280 *
281 * @param id device id
282 * @return model object id
283 */
284 private static ModelObjectId.Builder getModelObjIdForIns(String id) {
285 ModelObjectId.Builder device = getModObjIdDriDevice(id);
286 return device.addChild(DefaultL3Vpn.class)
287 .addChild(DefaultL3Vpncomm.class)
288 .addChild(DefaultL3VpnInstances.class);
289 }
290
291 /**
292 * Processes standard device model L3VPN address family and returns the
293 * driver L3VPN address family.
294 *
295 * @param l3Vpn standard device L3VPN
296 * @return driver address family
297 */
298 private static VpnInstAfs processL3VpnAf(org.onosproject.yang.gen.v1.urn
299 .ietf.params.xml.ns.yang
300 .ietf.bgp.l3vpn.rev20160909
301 .ietfbgpl3vpn.devices
302 .device.networkinstances
303 .networkinstance
304 .augmentedninetworkinstance
305 .L3Vpn l3Vpn) {
306 // TODO: Need to handle the ipv6 case
307 Unicast ipv4Unicast = l3Vpn.ipv4().unicast();
308 VpnInstAfs vpnInstAfs = new DefaultVpnInstAfs();
309 VpnInstAf vpnInstAf = new DefaultVpnInstAf();
310 VpnTargets vpnTargets = new DefaultVpnTargets();
311 List<VpnInstAf> afList = new LinkedList<>();
312
313 vpnInstAf.vrfRd(l3Vpn.routeDistinguisher().config().rd());
314 vpnInstAf.afType(of(IPV4UNI));
315
316 List<Rts> rts = ipv4Unicast.routeTargets().config().rts();
317 addVpnTarget(vpnTargets, rts);
318
319 vpnInstAf.vpnTargets(vpnTargets);
320 afList.add(vpnInstAf);
321 vpnInstAfs.vpnInstAf(afList);
322 return vpnInstAfs;
323 }
324
325 /**
326 * Adds VPN target to the target list from the list of RTs available in
327 * the standard device model.
328 *
329 * @param vpnTgts VPN targets
330 * @param rts rts
331 */
332 private static void addVpnTarget(VpnTargets vpnTgts, List<Rts> rts) {
333 List<VpnTarget> tgtList = new LinkedList<>();
334 for (Rts rt : rts) {
335 if (rt == null) {
336 continue;
337 }
338 if (rt.rtType() == BOTH) {
339 VpnTarget expTgt = addRt(rt.rt(), EXP_COMM);
340 VpnTarget impTgt = addRt(rt.rt(), IMP_COMM);
341 tgtList.add(expTgt);
342 tgtList.add(impTgt);
343 } else {
344 String rtType = getRtVal(rt.rtType());
345 VpnTarget tgt = addRt(rt.rt(), rtType);
346 tgtList.add(tgt);
347 }
348 }
349 vpnTgts.vpnTarget(tgtList);
350 }
351
352 /**
353 * Returns the RT value according to the RT type available.
354 *
355 * @param type RT type
356 * @return RT value
357 */
358 private static String getRtVal(RtTypeEnum type) {
359 switch (type) {
360
361 case EXPORT:
362 return EXP_COMM;
363
364 case IMPORT:
365 return IMP_COMM;
366
367 default:
368 throw new IllegalArgumentException(UNSUPPORTED_RT_TYPE);
369 }
370 }
371
372 /**
373 * Adds RT to the VPN target with the RT value and RT type.
374 *
375 * @param rt RT value
376 * @param rtType RT type
377 * @return VPN target
378 */
379 private static VpnTarget addRt(String rt, String rtType) {
380 VpnTarget vpnTarget = new DefaultVpnTarget();
381 vpnTarget.vrfRtvalue(rt);
382 vpnTarget.vrfRttype(fromString(rtType));
383 return vpnTarget;
384 }
385
386 /**
387 * Returns the deletable model object data of VPN instance of huawei device
388 * from the standard model object data.
389 *
390 * @param modObj model object data
391 * @return driver model object data
392 */
393 static Object getDeleteVpnIns(ModelObjectData modObj) {
394 ModelIdLevel modIdLvl = DEVICE;
395 String id = getIdFromModId(modObj.identifier());
396 Object obj = getObjFromModData(modObj);
397
398 if (obj == null) {
399 throw new IllegalArgumentException(OBJECT_NULL);
400 }
401
402 if (id == null) {
403 modIdLvl = ROOT;
404 id = getDevIdFromRootObj(obj);
405 obj = getObjFromRootObj(obj);
406 } else if (id.equals(CONS_DEVICES)) {
407 modIdLvl = DEVICES;
408 id = ((Device) obj).deviceid();
409 obj = ((Device) obj).networkInstances();
410 }
411 List<NetworkInstance> ins = ((NetworkInstances) obj).networkInstance();
412 Iterator<NetworkInstance> it = ins.iterator();
413 NetworkInstance instance;
414 if (it.hasNext()) {
415 instance = it.next();
416 } else {
417 throw new IllegalArgumentException(INS_NULL);
418 }
419 return getDelModObjData(modIdLvl, id, instance.name());
420 }
421
422 /**
423 * Returns the driver model object data for delete, according to the
424 * levels it has to be constructed.
425 *
426 * @param modIdLvl model id level
427 * @param id device id
428 * @param name VPN name
429 * @return driver model object data
430 */
431 private static ModelObjectData getDelModObjData(ModelIdLevel modIdLvl,
432 String id, String name) {
433 ModelObjectId modId;
434 switch (modIdLvl) {
435 case ROOT:
436 modId = getModObjIdDriDevices();
437 break;
438
439 case DEVICES:
440 modId = getModObjIdDriDevice(id).build();
441 break;
442
443 case DEVICE:
444 modId = getModObjIdDriVpn(id, name);
445 break;
446
447 default:
448 throw new IllegalArgumentException(UNSUPPORTED_MODEL_LVL);
449 }
450 return getData(modId, null);
451 }
452
453 /**
454 * Returns the model object id, with device id and VPN name.
455 *
456 * @param id device id
457 * @param name VPN name
458 * @return model object id
459 */
460 private static ModelObjectId getModObjIdDriVpn(String id, String name) {
461 ModelObjectId.Builder ins = getModelObjIdForIns(id);
462 L3VpnInstanceKeys key = new L3VpnInstanceKeys();
463 key.vrfName(name);
464 return ins.addChild(DefaultL3VpnInstance.class, key).build();
465 }
466
467 /**
468 * Returns the model object id builder of the driver with respect to device.
469 *
470 * @param id device id
471 * @return model object id builder
472 */
473 private static ModelObjectId.Builder getModObjIdDriDevice(String id) {
474 DeviceKeys key = new DeviceKeys();
475 key.deviceid(id);
476 return ModelObjectId.builder()
477 .addChild(org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225
478 .nel3vpnapi.DefaultDevices.class)
479 .addChild(DefaultDevice.class, key);
480 }
481}