janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.drivers.huawei; |
| 18 | |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 19 | import org.onosproject.yang.gen.v1.ietfbgpl3vpn.rev20160909.ietfbgpl3vpn.devices.device.networkinstances.networkinstance.DefaultAugmentedNiNetworkInstance; |
| 20 | import org.onosproject.yang.gen.v1.ietfbgpl3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.ipv4.Unicast; |
| 21 | import org.onosproject.yang.gen.v1.ietfbgpl3vpn.rev20160909.ietfbgpl3vpn.routetargetset.Rts; |
| 22 | import org.onosproject.yang.gen.v1.ietfbgpl3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum; |
| 23 | import org.onosproject.yang.gen.v1.ietfnetworkinstance.rev20160623.ietfnetworkinstance.DefaultDevices; |
| 24 | import org.onosproject.yang.gen.v1.ietfnetworkinstance.rev20160623.ietfnetworkinstance.devices.Device; |
| 25 | import org.onosproject.yang.gen.v1.ietfnetworkinstance.rev20160623.ietfnetworkinstance.devices.device.NetworkInstances; |
| 26 | import org.onosproject.yang.gen.v1.ietfnetworkinstance.rev20160623.ietfnetworkinstance.devices.device.networkinstances.DefaultNetworkInstance; |
| 27 | import org.onosproject.yang.gen.v1.ietfnetworkinstance.rev20160623.ietfnetworkinstance.devices.device.networkinstances.NetworkInstance; |
| 28 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.DefaultDevice; |
| 29 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.DeviceKeys; |
| 30 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.DefaultL3Vpn; |
| 31 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.L3Vpn; |
| 32 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.DefaultL3Vpncomm; |
| 33 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.L3Vpncomm; |
| 34 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.DefaultL3VpnInstances; |
| 35 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.L3VpnInstances; |
| 36 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.DefaultL3VpnInstance; |
| 37 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.L3VpnInstance; |
| 38 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.L3VpnInstanceKeys; |
| 39 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.DefaultVpnInstAfs; |
| 40 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.VpnInstAfs; |
| 41 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.DefaultVpnInstAf; |
| 42 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.VpnInstAf; |
| 43 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.DefaultVpnTargets; |
| 44 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.VpnTargets; |
| 45 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.vpntargets.DefaultVpnTarget; |
| 46 | import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.vpninstaf.vpntargets.VpnTarget; |
| 47 | import org.onosproject.yang.gen.v1.nel3vpncomm.rev20141225.nel3vpncomm.l3vpnifs.DefaultL3VpnIfs; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 48 | import org.onosproject.yang.model.InnerModelObject; |
| 49 | import org.onosproject.yang.model.ModelObjectData; |
| 50 | import org.onosproject.yang.model.ModelObjectId; |
| 51 | |
| 52 | import java.util.Iterator; |
| 53 | import java.util.LinkedList; |
| 54 | import java.util.List; |
| 55 | |
| 56 | import static org.onosproject.drivers.huawei.DriverUtil.CONS_DEVICES; |
| 57 | import static org.onosproject.drivers.huawei.DriverUtil.DEVICE_NULL; |
| 58 | import static org.onosproject.drivers.huawei.DriverUtil.INS_NULL; |
| 59 | import static org.onosproject.drivers.huawei.DriverUtil.OBJECT_NULL; |
| 60 | import static org.onosproject.drivers.huawei.DriverUtil.UNSUPPORTED_MODEL_LVL; |
| 61 | import static org.onosproject.drivers.huawei.DriverUtil.getData; |
| 62 | import static org.onosproject.drivers.huawei.DriverUtil.getIdFromModId; |
| 63 | import static org.onosproject.drivers.huawei.DriverUtil.getModObjIdDriDevices; |
| 64 | import static org.onosproject.drivers.huawei.DriverUtil.getObjFromModData; |
| 65 | import static org.onosproject.drivers.huawei.ModelIdLevel.DEVICE; |
| 66 | import static org.onosproject.drivers.huawei.ModelIdLevel.DEVICES; |
| 67 | import static org.onosproject.drivers.huawei.ModelIdLevel.ROOT; |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 68 | import static org.onosproject.yang.gen.v1.ietfbgpl3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum.BOTH; |
| 69 | import static org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.L3VpncommonL3VpnPrefixType.of; |
| 70 | import static org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.L3VpncommonVrfRtType.fromString; |
| 71 | import static org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.l3vpncommonl3vpnprefixtype.L3VpncommonL3VpnPrefixTypeEnum.IPV4UNI; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Representation of utility for instance creation and deletion. |
| 75 | */ |
| 76 | public 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 b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 109 | String id = getIdFromModId(modObj.identifier(), true); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 110 | 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 | } |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 129 | org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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 |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 147 | .v1.nel3vpnapi |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 148 | .rev20141225 |
| 149 | .nel3vpnapi |
| 150 | .DefaultDevices |
| 151 | devices) { |
| 152 | ModelObjectId id; |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 153 | List<org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 154 | .devices.Device> devList = devices.device(); |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 155 | Iterator<org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 156 | .devices.Device> it = devList.iterator(); |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 157 | org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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 | */ |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 190 | private static org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225 |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 191 | .nel3vpnapi.DefaultDevices getDriverDevices(String id, |
| 192 | NetworkInstances obj) { |
| 193 | |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 194 | org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi. |
| 195 | DefaultDevices devices = new org.onosproject.yang.gen.v1 |
| 196 | .nel3vpnapi.rev20141225.nel3vpnapi.DefaultDevices(); |
| 197 | org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices |
| 198 | .Device device = new org.onosproject.yang.gen.v1.nel3vpnapi |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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<>(); |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 206 | List<org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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 | */ |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 299 | private static VpnInstAfs processL3VpnAf(org.onosproject.yang.gen.v1 |
| 300 | .ietfbgpl3vpn.rev20160909 |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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 | */ |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 393 | static ModelObjectData getDeleteVpnIns(ModelObjectData modObj) { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 394 | ModelIdLevel modIdLvl = DEVICE; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 395 | String id = getIdFromModId(modObj.identifier(), true); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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(); |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 437 | DefaultDevice device = new DefaultDevice(); |
| 438 | return getData(modId, device); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 439 | |
| 440 | case DEVICES: |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 441 | DefaultL3Vpn l3Vpn = new DefaultL3Vpn(); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 442 | modId = getModObjIdDriDevice(id).build(); |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 443 | return getData(modId, l3Vpn); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 444 | |
| 445 | case DEVICE: |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 446 | DefaultL3VpnIfs l3VpnIfs = new DefaultL3VpnIfs(); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 447 | modId = getModObjIdDriVpn(id, name); |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 448 | return getData(modId, l3VpnIfs); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 449 | |
| 450 | default: |
| 451 | throw new IllegalArgumentException(UNSUPPORTED_MODEL_LVL); |
| 452 | } |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Returns the model object id, with device id and VPN name. |
| 457 | * |
| 458 | * @param id device id |
| 459 | * @param name VPN name |
| 460 | * @return model object id |
| 461 | */ |
| 462 | private static ModelObjectId getModObjIdDriVpn(String id, String name) { |
| 463 | ModelObjectId.Builder ins = getModelObjIdForIns(id); |
| 464 | L3VpnInstanceKeys key = new L3VpnInstanceKeys(); |
| 465 | key.vrfName(name); |
| 466 | return ins.addChild(DefaultL3VpnInstance.class, key).build(); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * Returns the model object id builder of the driver with respect to device. |
| 471 | * |
| 472 | * @param id device id |
| 473 | * @return model object id builder |
| 474 | */ |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 475 | static ModelObjectId.Builder getModObjIdDriDevice(String id) { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 476 | DeviceKeys key = new DeviceKeys(); |
| 477 | key.deviceid(id); |
| 478 | return ModelObjectId.builder() |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 479 | .addChild(org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225 |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 480 | .nel3vpnapi.DefaultDevices.class) |
| 481 | .addChild(DefaultDevice.class, key); |
| 482 | } |
| 483 | } |