janani b | 35f6cbc | 2017-03-24 21:56:58 +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 | 35f6cbc | 2017-03-24 21:56:58 +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.l3vpn.netl3vpn.impl; |
| 18 | |
| 19 | import org.onosproject.l3vpn.netl3vpn.AccessInfo; |
| 20 | import org.onosproject.l3vpn.netl3vpn.BgpInfo; |
| 21 | import org.onosproject.l3vpn.netl3vpn.DeviceInfo; |
| 22 | import org.onosproject.l3vpn.netl3vpn.NetL3VpnException; |
| 23 | import org.onosproject.l3vpn.netl3vpn.ProtocolInfo; |
| 24 | import org.onosproject.l3vpn.netl3vpn.RouteProtocol; |
Vidyashree Rama | 04147ca | 2017-05-26 11:32:47 +0530 | [diff] [blame] | 25 | import org.onosproject.yang.gen.v1.ietfl3vpnsvc.rev20160730.ietfl3vpnsvc.RoutingProtocolType; |
| 26 | import org.onosproject.yang.gen.v1.ietfl3vpnsvc.rev20160730.ietfl3vpnsvc.siteattachmentipconnection.IpConnection; |
| 27 | import org.onosproject.yang.gen.v1.ietfl3vpnsvc.rev20160730.ietfl3vpnsvc.siterouting.routingprotocols.RoutingProtocol; |
janani b | 35f6cbc | 2017-03-24 21:56:58 +0530 | [diff] [blame] | 28 | |
| 29 | import java.util.List; |
| 30 | import java.util.Map; |
| 31 | |
| 32 | import static org.onosproject.l3vpn.netl3vpn.RouteProtocol.DIRECT; |
| 33 | import static org.onosproject.l3vpn.netl3vpn.RouteProtocol.STATIC; |
| 34 | import static org.onosproject.l3vpn.netl3vpn.RouteProtocol.getProType; |
| 35 | |
| 36 | /** |
| 37 | * Representation of utility for BGP info creation and deletion. |
| 38 | */ |
| 39 | public final class BgpConstructionUtil { |
| 40 | |
| 41 | private static final String ZERO = "0"; |
| 42 | |
| 43 | // No instantiation. |
| 44 | private BgpConstructionUtil() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates the BGP info instance, from the routing protocols available. |
| 49 | * It returns BGP info if for the first time, else returns null. |
| 50 | * |
| 51 | * @param routes route protocol |
| 52 | * @param info device info |
| 53 | * @param vpnName VPN name |
| 54 | * @param connect ip connection |
| 55 | * @param access access info |
| 56 | * @return BGP info instance |
| 57 | */ |
| 58 | public static BgpInfo createBgpInfo(List<RoutingProtocol> routes, |
| 59 | DeviceInfo info, String vpnName, |
| 60 | IpConnection connect, AccessInfo access) { |
| 61 | BgpInfo devBgp = info.bgpInfo(); |
| 62 | BgpInfo infoBgp = new BgpInfo(); |
| 63 | infoBgp.vpnName(vpnName); |
| 64 | if (devBgp != null) { |
| 65 | infoBgp = updateDevBgpInfo(devBgp, infoBgp, connect, routes, access); |
| 66 | } else { |
| 67 | infoBgp = updateDevBgpInfo(null, infoBgp, connect, routes, access); |
| 68 | info.bgpInfo(infoBgp); |
| 69 | } |
| 70 | if (infoBgp == null || infoBgp.protocolInfo() == null) { |
| 71 | return null; |
| 72 | } |
| 73 | return infoBgp; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Updates the device BGP info and also creates the BGP info which has to |
| 78 | * be sent to driver, if it is called for the first time. |
| 79 | * |
| 80 | * @param devBgp device BGP info |
| 81 | * @param driBgp driver BGP info |
| 82 | * @param connect ip connection |
| 83 | * @param routes route protocols |
| 84 | * @param access access info |
| 85 | * @return driver BGP info |
| 86 | */ |
| 87 | private static BgpInfo updateDevBgpInfo(BgpInfo devBgp, BgpInfo driBgp, |
| 88 | IpConnection connect, |
| 89 | List<RoutingProtocol> routes, |
| 90 | AccessInfo access) { |
| 91 | for (RoutingProtocol route : routes) { |
| 92 | ProtocolInfo ifInfo = getRoutePro(route.type(), connect, access); |
| 93 | if (ifInfo != null) { |
| 94 | if (devBgp != null) { |
| 95 | ProtocolInfo info = addToDevBgp(ifInfo, devBgp, access); |
| 96 | ifInfo = getUpdatedProInfo(info, ifInfo); |
| 97 | } |
| 98 | if (ifInfo != null) { |
| 99 | driBgp.addProtocolInfo(ifInfo.routeProtocol(), ifInfo); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | return driBgp; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns the updated protocol info that has to be sent to driver. If |
| 108 | * the protocol info is for the second time or more, the driver info's |
| 109 | * protocol info will not be sent. It will return null if no info is |
| 110 | * present or nothing to be sent to driver. |
| 111 | * |
| 112 | * @param devInfo device protocol info |
| 113 | * @param driInfo driver protocol info |
| 114 | * @return updated driver protocol info |
| 115 | */ |
| 116 | private static ProtocolInfo getUpdatedProInfo(ProtocolInfo devInfo, |
| 117 | ProtocolInfo driInfo) { |
| 118 | if (driInfo.isIpv4Af() && driInfo.isIpv6Af()) { |
| 119 | if ((getV4Size(devInfo) > 1) && (getV6Size(devInfo) > 1)) { |
| 120 | return null; |
| 121 | } |
| 122 | if ((getV4Size(devInfo) > 1) && !(getV6Size(devInfo) > 1)) { |
| 123 | driInfo.ipv4Af(false); |
| 124 | } else if (!(getV4Size(devInfo) > 1) && (getV6Size(devInfo) > 1)) { |
| 125 | driInfo.ipv6Af(false); |
| 126 | } |
| 127 | } |
| 128 | if (driInfo.isIpv4Af() && !driInfo.isIpv6Af()) { |
| 129 | if (getV4Size(devInfo) > 1) { |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | if (!driInfo.isIpv4Af() && driInfo.isIpv6Af()) { |
| 134 | if (getV6Size(devInfo) > 1) { |
| 135 | return null; |
| 136 | } |
| 137 | } |
| 138 | return driInfo; |
| 139 | } |
| 140 | |
| 141 | private static int getV4Size(ProtocolInfo proInfo) { |
| 142 | return proInfo.v4Accesses().size(); |
| 143 | } |
| 144 | |
| 145 | private static int getV6Size(ProtocolInfo proInfo) { |
| 146 | return proInfo.v6Accesses().size(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Adds the protocol info to the device BGP info. |
| 151 | * |
| 152 | * @param proInfo protocol info |
| 153 | * @param devBgp device BGP |
| 154 | * @param access access info |
| 155 | * @return protocol info |
| 156 | */ |
| 157 | private static ProtocolInfo addToDevBgp(ProtocolInfo proInfo, |
| 158 | BgpInfo devBgp, AccessInfo access) { |
| 159 | Map<RouteProtocol, ProtocolInfo> devMap = devBgp.protocolInfo(); |
| 160 | ProtocolInfo devInfo = devMap.get(proInfo.routeProtocol()); |
| 161 | if (devInfo != null) { |
| 162 | if (proInfo.isIpv4Af()) { |
| 163 | devInfo.ipv4Af(proInfo.isIpv4Af()); |
| 164 | devInfo.addV4Access(access); |
| 165 | } |
| 166 | if (proInfo.isIpv6Af()) { |
| 167 | devInfo.ipv6Af(proInfo.isIpv6Af()); |
| 168 | devInfo.addV6Access(access); |
| 169 | } |
| 170 | } else { |
| 171 | devInfo = proInfo; |
| 172 | devBgp.addProtocolInfo(proInfo.routeProtocol(), devInfo); |
| 173 | } |
| 174 | return devInfo; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * Returns the protocol info of BGP by taking values from the service files. |
| 180 | * |
| 181 | * @param type protocol type |
| 182 | * @param connect IP connection |
| 183 | * @param access access info |
| 184 | * @return protocol info |
| 185 | */ |
| 186 | private static ProtocolInfo getRoutePro(Class<? extends RoutingProtocolType> type, |
| 187 | IpConnection connect, AccessInfo access) { |
| 188 | ProtocolInfo protocolInfo = new ProtocolInfo(); |
| 189 | RouteProtocol protocol = getProType(type.getSimpleName()); |
| 190 | switch (protocol) { |
| 191 | case DIRECT: |
| 192 | protocolInfo.routeProtocol(DIRECT); |
| 193 | protocolInfo.processId(ZERO); |
| 194 | setAddressFamily(protocolInfo, connect, access); |
| 195 | return protocolInfo; |
| 196 | |
| 197 | case STATIC: |
| 198 | protocolInfo.routeProtocol(STATIC); |
| 199 | protocolInfo.processId(ZERO); |
| 200 | setAddressFamily(protocolInfo, connect, access); |
| 201 | return protocolInfo; |
| 202 | |
| 203 | case BGP: |
| 204 | case OSPF: |
| 205 | case RIP: |
| 206 | case RIP_NG: |
| 207 | case VRRP: |
| 208 | default: |
| 209 | throw new NetL3VpnException(getRouteProErr( |
| 210 | type.getSimpleName())); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns the route protocol error message for unsupported type. |
| 216 | * |
| 217 | * @param type route protocol type |
| 218 | * @return error message |
| 219 | */ |
| 220 | private static String getRouteProErr(String type) { |
| 221 | return type + " routing protocol is not supported."; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Sets the address family of the protocol info. |
| 226 | * |
| 227 | * @param proInfo protocol info |
| 228 | * @param connect ip connection |
| 229 | * @param access access info |
| 230 | */ |
| 231 | private static void setAddressFamily(ProtocolInfo proInfo, |
| 232 | IpConnection connect, AccessInfo access) { |
| 233 | if (connect.ipv4() != null && connect.ipv4().addresses() != null && |
| 234 | connect.ipv4().addresses().providerAddress() != null) { |
| 235 | proInfo.ipv4Af(true); |
| 236 | proInfo.addV4Access(access); |
| 237 | } |
| 238 | if (connect.ipv6() != null && connect.ipv6().addresses() != null && |
| 239 | connect.ipv6().addresses().providerAddress() != null) { |
| 240 | proInfo.ipv6Af(true); |
| 241 | proInfo.addV6Access(access); |
| 242 | } |
| 243 | } |
| 244 | } |