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