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