janani b | f7060cd | 2017-03-28 19:06:30 +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.drivers.huawei; |
| 18 | |
| 19 | import org.onosproject.l3vpn.netl3vpn.BgpDriverInfo; |
| 20 | import org.onosproject.l3vpn.netl3vpn.BgpInfo; |
| 21 | import org.onosproject.l3vpn.netl3vpn.BgpModelIdLevel; |
| 22 | import org.onosproject.l3vpn.netl3vpn.ProtocolInfo; |
| 23 | import org.onosproject.l3vpn.netl3vpn.RouteProtocol; |
| 24 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.DefaultDevices; |
| 25 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.Devices; |
| 26 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.DefaultDevice; |
| 27 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.Device; |
| 28 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.DeviceKeys; |
| 29 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.Bgp; |
| 30 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.DefaultBgp; |
| 31 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.Bgpcomm; |
| 32 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.DefaultBgpcomm; |
| 33 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.BgpVrfs; |
| 34 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.DefaultBgpVrfs; |
| 35 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.BgpVrf; |
| 36 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.DefaultBgpVrf; |
| 37 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.BgpVrfAfs; |
| 38 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.DefaultBgpVrfAfs; |
| 39 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.bgpvrfafs.BgpVrfAf; |
| 40 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.bgpvrfafs.DefaultBgpVrfAf; |
| 41 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.bgpvrfafs.bgpvrfaf.DefaultImportRoutes; |
| 42 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.bgpvrfafs.bgpvrfaf.ImportRoutes; |
| 43 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.bgpvrfafs.bgpvrfaf.importroutes.DefaultImportRoute; |
| 44 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.nebgpcomm.devices.device.bgp.bgpcomm.bgpvrfs.bgpvrf.bgpvrfafs.bgpvrfaf.importroutes.ImportRoute; |
| 45 | import org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.BgpcommImRouteProtocol; |
| 46 | import org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommimrouteprotocol.BgpcommImRouteProtocolEnum; |
| 47 | import org.onosproject.yang.model.InnerModelObject; |
| 48 | import org.onosproject.yang.model.ModelObjectData; |
| 49 | import org.onosproject.yang.model.ModelObjectId; |
| 50 | |
| 51 | import java.util.LinkedList; |
| 52 | import java.util.List; |
| 53 | import java.util.Map; |
| 54 | |
| 55 | import static org.onosproject.drivers.huawei.DriverUtil.UNSUPPORTED_MODEL_LVL; |
| 56 | import static org.onosproject.drivers.huawei.DriverUtil.getData; |
| 57 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.BgpcommPrefixType.of; |
| 58 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommimrouteprotocol.BgpcommImRouteProtocolEnum.DIRECT; |
| 59 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommimrouteprotocol.BgpcommImRouteProtocolEnum.OSPF; |
| 60 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommimrouteprotocol.BgpcommImRouteProtocolEnum.RIP; |
| 61 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommimrouteprotocol.BgpcommImRouteProtocolEnum.RIPNG; |
| 62 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommimrouteprotocol.BgpcommImRouteProtocolEnum.STATIC; |
| 63 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommprefixtype.BgpcommPrefixTypeEnum.IPV4UNI; |
| 64 | import static org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.nebgpcommtype.bgpcommprefixtype.BgpcommPrefixTypeEnum.IPV6UNI; |
| 65 | |
| 66 | /** |
| 67 | * Representation of utility for BGP creation and deletion. |
| 68 | */ |
| 69 | public final class BgpConstructionUtil { |
| 70 | |
| 71 | /** |
| 72 | * Error message for unsupported protocol type. |
| 73 | */ |
| 74 | private static final String UNSUPPORTED_PRO_TYPE = "Unsupported route " + |
| 75 | "protocol type is found."; |
| 76 | |
| 77 | // No instantiation. |
| 78 | private BgpConstructionUtil() { |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the created model object data of BGP info of huawei device |
| 83 | * from the standard model object data. |
| 84 | * |
| 85 | * @param bgpInfo device BGP info |
| 86 | * @param config BGP driver info |
| 87 | * @return driver model object data |
| 88 | */ |
| 89 | static ModelObjectData getCreateBgp(BgpInfo bgpInfo, |
| 90 | BgpDriverInfo config) { |
| 91 | String devId = config.devId(); |
| 92 | BgpModelIdLevel modIdLevel = config.modIdLevel(); |
| 93 | |
| 94 | Bgp bgp = new DefaultBgp(); |
| 95 | Bgpcomm bgpBuilder = new DefaultBgpcomm(); |
| 96 | BgpVrfs bgpVrfs = new DefaultBgpVrfs(); |
| 97 | BgpVrf bgpVrf = new DefaultBgpVrf(); |
| 98 | BgpVrfAfs bgpVrfAfs = new DefaultBgpVrfAfs(); |
| 99 | List<BgpVrf> bgpVrfList = new LinkedList<>(); |
| 100 | |
| 101 | bgpVrf.vrfName(bgpInfo.vpnName()); |
| 102 | Map<RouteProtocol, ProtocolInfo> proMap = bgpInfo.protocolInfo(); |
| 103 | addRouteProtocol(proMap, bgpVrfAfs); |
| 104 | |
| 105 | bgpVrf.bgpVrfAfs(bgpVrfAfs); |
| 106 | bgpVrfList.add(bgpVrf); |
| 107 | bgpVrfs.bgpVrf(bgpVrfList); |
| 108 | bgpBuilder.bgpVrfs(bgpVrfs); |
| 109 | bgp.bgpcomm(bgpBuilder); |
| 110 | return getModObjData(modIdLevel, bgp, devId, bgpVrf); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Adds route protocol from the standard device model to the BGP address |
| 115 | * family respectively. |
| 116 | * |
| 117 | * @param proMap protocol map |
| 118 | * @param bgpVrfAfs BGP address family |
| 119 | */ |
| 120 | private static void addRouteProtocol(Map<RouteProtocol, ProtocolInfo> proMap, |
| 121 | BgpVrfAfs bgpVrfAfs) { |
| 122 | BgpVrfAf ipv4 = new DefaultBgpVrfAf(); |
| 123 | ImportRoutes ipv4Routes = new DefaultImportRoutes(); |
| 124 | ipv4.afType(of(IPV4UNI)); |
| 125 | |
| 126 | BgpVrfAf ipv6 = new DefaultBgpVrfAf(); |
| 127 | ImportRoutes ipv6Routes = new DefaultImportRoutes(); |
| 128 | ipv6.afType(of(IPV6UNI)); |
| 129 | for (Map.Entry<RouteProtocol, ProtocolInfo> info : proMap.entrySet()) { |
| 130 | RouteProtocol protocol = info.getKey(); |
| 131 | ProtocolInfo proInfo = info.getValue(); |
| 132 | if (proInfo.isIpv4Af()) { |
| 133 | addImportRoute(ipv4Routes, proInfo, protocol); |
| 134 | } |
| 135 | if (proInfo.isIpv6Af()) { |
| 136 | addImportRoute(ipv6Routes, proInfo, protocol); |
| 137 | } |
| 138 | } |
| 139 | if (ipv4Routes.importRoute() != null && |
| 140 | !ipv4Routes.importRoute().isEmpty()) { |
| 141 | addToBgpVrf(ipv4Routes, ipv4, bgpVrfAfs); |
| 142 | } |
| 143 | if (ipv6Routes.importRoute() != null && |
| 144 | !ipv6Routes.importRoute().isEmpty()) { |
| 145 | addToBgpVrf(ipv6Routes, ipv6, bgpVrfAfs); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Adds the routes to BGP VRF in driver model. |
| 151 | * |
| 152 | * @param routes routes |
| 153 | * @param vrfAf VRF address family |
| 154 | * @param bgpVrfAfs BGP address family |
| 155 | */ |
| 156 | private static void addToBgpVrf(ImportRoutes routes, BgpVrfAf vrfAf, |
| 157 | BgpVrfAfs bgpVrfAfs) { |
| 158 | List<BgpVrfAf> ipList = new LinkedList<>(); |
| 159 | vrfAf.importRoutes(routes); |
| 160 | ipList.add(vrfAf); |
| 161 | bgpVrfAfs.bgpVrfAf(ipList); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Adds the import route to the routes, according to the protocol info |
| 166 | * from the standard device model. |
| 167 | * |
| 168 | * @param routes routes object |
| 169 | * @param proInfo protocol info |
| 170 | * @param protocol route protocol |
| 171 | */ |
| 172 | private static void addImportRoute(ImportRoutes routes, ProtocolInfo proInfo, |
| 173 | RouteProtocol protocol) { |
| 174 | List<ImportRoute> routeList = new LinkedList<>(); |
| 175 | ImportRoute route = buildAfBgp(proInfo, protocol); |
| 176 | routeList.add(route); |
| 177 | routes.importRoute(routeList); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Builds the import route details from the route protocol and the |
| 182 | * process id. |
| 183 | * |
| 184 | * @param proInfo protocol info |
| 185 | * @param protocol route protocol |
| 186 | * @return import route object |
| 187 | */ |
| 188 | private static ImportRoute buildAfBgp(ProtocolInfo proInfo, |
| 189 | RouteProtocol protocol) { |
| 190 | BgpcommImRouteProtocolEnum rpEnum = getProtocolType(protocol); |
| 191 | ImportRoute impRoute = new DefaultImportRoute(); |
| 192 | impRoute.importProcessId(proInfo.processId()); |
| 193 | impRoute.importProtocol(BgpcommImRouteProtocol.of(rpEnum)); |
| 194 | return impRoute; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Returns the huawei route protocol corresponding to standard device |
| 199 | * route protocol. |
| 200 | * |
| 201 | * @param protocol device route protocol |
| 202 | * @return driver route protocol |
| 203 | */ |
| 204 | private static BgpcommImRouteProtocolEnum getProtocolType(RouteProtocol protocol) { |
| 205 | switch (protocol) { |
| 206 | case DIRECT: |
| 207 | return DIRECT; |
| 208 | |
| 209 | case OSPF: |
| 210 | return OSPF; |
| 211 | |
| 212 | case RIP: |
| 213 | return RIP; |
| 214 | |
| 215 | case RIP_NG: |
| 216 | return RIPNG; |
| 217 | |
| 218 | case STATIC: |
| 219 | return STATIC; |
| 220 | |
| 221 | case BGP: |
| 222 | case VRRP: |
| 223 | default: |
| 224 | throw new IllegalArgumentException(UNSUPPORTED_PRO_TYPE); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns the driver model object data, according to the levels it has |
| 230 | * to be constructed. |
| 231 | * |
| 232 | * @param modIdLevel model id level |
| 233 | * @param bgp driver BGP object |
| 234 | * @param devId device id |
| 235 | * @param bgpVrf driver BGP VRF object |
| 236 | * @return model object data |
| 237 | */ |
| 238 | public static ModelObjectData getModObjData(BgpModelIdLevel modIdLevel, |
| 239 | Bgp bgp, String devId, |
| 240 | BgpVrf bgpVrf) { |
| 241 | switch (modIdLevel) { |
| 242 | case ROOT: |
| 243 | return getRootModObj(bgp, devId); |
| 244 | |
| 245 | case DEVICES: |
| 246 | return getDevicesModObj(bgp, devId); |
| 247 | |
| 248 | case DEVICE: |
| 249 | return getDevModObj(bgpVrf, devId); |
| 250 | |
| 251 | default: |
| 252 | throw new IllegalArgumentException(UNSUPPORTED_MODEL_LVL); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Returns the driver model object data with device in model object id, |
| 258 | * till BGP VRF. |
| 259 | * |
| 260 | * @param bgpVrf BGP VRF object |
| 261 | * @param devId device id |
| 262 | * @return model object data |
| 263 | */ |
| 264 | private static ModelObjectData getDevModObj(BgpVrf bgpVrf, String devId) { |
| 265 | DeviceKeys key = new DeviceKeys(); |
| 266 | key.deviceid(devId); |
| 267 | ModelObjectId id = ModelObjectId.builder() |
| 268 | .addChild(DefaultDevices.class) |
| 269 | .addChild(DefaultDevice.class, key) |
| 270 | .addChild(DefaultBgp.class) |
| 271 | .addChild(DefaultBgpcomm.class) |
| 272 | .addChild(DefaultBgpVrfs.class).build(); |
| 273 | return getData(id, (InnerModelObject) bgpVrf); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Returns the driver model object data with devices in model object id. |
| 278 | * |
| 279 | * @param bgp BGP object |
| 280 | * @param devId device id |
| 281 | * @return model object data |
| 282 | */ |
| 283 | private static ModelObjectData getDevicesModObj(Bgp bgp, String devId) { |
| 284 | ModelObjectId modelId = ModelObjectId.builder() |
| 285 | .addChild(DefaultDevices.class).build(); |
| 286 | Device device = getDevInfo(bgp, devId); |
| 287 | return getData(modelId, (InnerModelObject) device); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Returns the driver root model object without model object id. |
| 292 | * |
| 293 | * @param bgp driver BGP |
| 294 | * @param devId device id |
| 295 | * @return model object data |
| 296 | */ |
| 297 | private static ModelObjectData getRootModObj(Bgp bgp, String devId) { |
| 298 | Devices devices = new DefaultDevices(); |
| 299 | List<Device> devList = new LinkedList<>(); |
| 300 | Device device = getDevInfo(bgp, devId); |
| 301 | devList.add(device); |
| 302 | devices.device(devList); |
| 303 | return getData(null, (InnerModelObject) devices); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Returns the driver BGP from the device object. |
| 308 | * |
| 309 | * @param bgp BGP object |
| 310 | * @param devId device id |
| 311 | * @return device object |
| 312 | */ |
| 313 | private static Device getDevInfo(Bgp bgp, String devId) { |
| 314 | Device device = new DefaultDevice(); |
| 315 | device.deviceid(devId); |
| 316 | device.bgp(bgp); |
| 317 | return device; |
| 318 | } |
| 319 | } |