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.FullMeshVpnConfig; |
| 20 | import org.onosproject.l3vpn.netl3vpn.HubSpokeVpnConfig; |
| 21 | import org.onosproject.l3vpn.netl3vpn.VpnInstance; |
| 22 | import org.onosproject.l3vpn.netl3vpn.VpnSiteRole; |
| 23 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.devices.device.networkinstances.networkinstance.AugmentedNiNetworkInstance; |
| 24 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.devices.device.networkinstances.networkinstance.DefaultAugmentedNiNetworkInstance; |
| 25 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.devices.device.networkinstances.networkinstance.augmentedninetworkinstance.DefaultL3Vpn; |
| 26 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.devices.device.networkinstances.networkinstance.augmentedninetworkinstance.L3Vpn; |
| 27 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.DefaultIpv4; |
| 28 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.DefaultIpv6; |
| 29 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.Ipv4; |
| 30 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.Ipv6; |
| 31 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.ipv4.DefaultUnicast; |
| 32 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams.ipv4.Unicast; |
| 33 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routedistinguisherparams.DefaultRouteDistinguisher; |
| 34 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routedistinguisherparams.RouteDistinguisher; |
| 35 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetparams.DefaultRouteTargets; |
| 36 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetparams.RouteTargets; |
| 37 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetparams.routetargets.Config; |
| 38 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetparams.routetargets.DefaultConfig; |
| 39 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.DefaultRts; |
| 40 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.Rts; |
| 41 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum; |
| 42 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.l3vpn.svc.rev20160730.ietfl3vpnsvc.siteattachmentipconnection.IpConnection; |
| 43 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.DefaultNetworkInstances; |
| 44 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.NetworkInstances; |
| 45 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.networkinstances.DefaultNetworkInstance; |
| 46 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.networkinstances.NetworkInstance; |
| 47 | |
| 48 | import java.util.LinkedList; |
| 49 | import java.util.List; |
| 50 | |
| 51 | import static org.onosproject.l3vpn.netl3vpn.VpnType.ANY_TO_ANY; |
| 52 | import static org.onosproject.l3vpn.netl3vpn.VpnType.HUB; |
| 53 | import static org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum.BOTH; |
| 54 | import static org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum.EXPORT; |
| 55 | import static org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.routetargetset.rts.RtTypeEnum.IMPORT; |
| 56 | |
| 57 | /** |
| 58 | * Representation of utility for instance creation and deletion. |
| 59 | */ |
| 60 | public final class InsConstructionUtil { |
| 61 | |
| 62 | // No instantiation. |
| 63 | private InsConstructionUtil() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates network instance with augmented info such as RD and RT. |
| 68 | * |
| 69 | * @param vpnIns VPN instance |
| 70 | * @param role VPN role |
| 71 | * @param connect ip connection |
| 72 | * @return network instance |
| 73 | */ |
| 74 | public static NetworkInstances createInstance(VpnInstance vpnIns, |
| 75 | VpnSiteRole role, |
| 76 | IpConnection connect) { |
| 77 | NetworkInstance ins = new DefaultNetworkInstance(); |
| 78 | NetworkInstances instances = new DefaultNetworkInstances(); |
| 79 | List<NetworkInstance> insList = new LinkedList<>(); |
| 80 | |
| 81 | L3Vpn l3Vpn = buildRd(vpnIns); |
| 82 | DefaultAugmentedNiNetworkInstance augIns = |
| 83 | buildRt(connect, role, l3Vpn, vpnIns); |
| 84 | ins.name(vpnIns.vpnName()); |
| 85 | insList.add(ins); |
| 86 | ((DefaultNetworkInstance) ins).addAugmentation(augIns); |
| 87 | instances.networkInstance(insList); |
| 88 | return instances; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Builds RT from l3 VPN according to the address family VPN belongs to. |
| 93 | * It returns built aug network instance from l3 VPN. |
| 94 | * |
| 95 | * @param con ip connection |
| 96 | * @param role site VPN role |
| 97 | * @param l3Vpn l3 VPN |
| 98 | * @param ins VPN instance |
| 99 | * @return aug network instance |
| 100 | */ |
| 101 | private static DefaultAugmentedNiNetworkInstance buildRt(IpConnection con, |
| 102 | VpnSiteRole role, |
| 103 | L3Vpn l3Vpn, |
| 104 | VpnInstance ins) { |
| 105 | Ipv4 ipv4 = null; |
| 106 | Ipv6 ipv6 = null; |
| 107 | if (con.ipv4() != null && con.ipv4().addresses() |
| 108 | .providerAddress() != null) { |
| 109 | ipv4 = buildIpv4Rt(role, ins); |
| 110 | } |
| 111 | if (con.ipv6() != null && con.ipv6() |
| 112 | .addresses().providerAddress() != null) { |
| 113 | ipv6 = buildIpv6Rt(role, ins); |
| 114 | } |
| 115 | l3Vpn.ipv4(ipv4); |
| 116 | l3Vpn.ipv6(ipv6); |
| 117 | |
| 118 | AugmentedNiNetworkInstance augInst = |
| 119 | new DefaultAugmentedNiNetworkInstance(); |
| 120 | augInst.l3Vpn(l3Vpn); |
| 121 | return (DefaultAugmentedNiNetworkInstance) augInst; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Builds ipv6 RT in the device model. |
| 126 | * |
| 127 | * @param role site VPN role |
| 128 | * @param vpnIns VPN instance |
| 129 | * @return ipv6 |
| 130 | */ |
| 131 | private static Ipv6 buildIpv6Rt(VpnSiteRole role, VpnInstance vpnIns) { |
| 132 | RouteTargets rts6 = new DefaultRouteTargets(); |
| 133 | Ipv6 v6 = new DefaultIpv6(); |
| 134 | org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn |
| 135 | .rev20160909.ietfbgpl3vpn.l3vpnvrfparams.ipv6.Unicast uni6 = |
| 136 | new org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang |
| 137 | .ietf.bgp.l3vpn.rev20160909.ietfbgpl3vpn.l3vpnvrfparams |
| 138 | .ipv6.DefaultUnicast(); |
| 139 | |
| 140 | Config configV6 = configRouteTarget(vpnIns, role); |
| 141 | rts6.config(configV6); |
| 142 | uni6.routeTargets(rts6); |
| 143 | v6.unicast(uni6); |
| 144 | return v6; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Builds ipv4 RT in the device model. |
| 149 | * |
| 150 | * @param role site VPN role |
| 151 | * @param vpnIns VPN instance |
| 152 | * @return ipv4 |
| 153 | */ |
| 154 | private static Ipv4 buildIpv4Rt(VpnSiteRole role, VpnInstance vpnIns) { |
| 155 | RouteTargets rts4 = new DefaultRouteTargets(); |
| 156 | Unicast uni4 = new DefaultUnicast(); |
| 157 | Ipv4 v4 = new DefaultIpv4(); |
| 158 | |
| 159 | Config configV4 = configRouteTarget(vpnIns, role); |
| 160 | rts4.config(configV4); |
| 161 | uni4.routeTargets(rts4); |
| 162 | v4.unicast(uni4); |
| 163 | return v4; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Configures route target according to the site VPN role from the stored |
| 168 | * VPN instance. |
| 169 | * |
| 170 | * @param ins VPN instance |
| 171 | * @param role site VPN role |
| 172 | * @return route target config |
| 173 | */ |
| 174 | private static Config configRouteTarget(VpnInstance ins, |
| 175 | VpnSiteRole role) { |
| 176 | Rts rts1; |
| 177 | Config config = new DefaultConfig(); |
| 178 | List<Rts> rtsList = new LinkedList<>(); |
| 179 | |
| 180 | if (ins.type() == ANY_TO_ANY) { |
| 181 | String rtVal = ((FullMeshVpnConfig) ins.vpnConfig()).rt(); |
| 182 | rts1 = getRtsVal(rtVal, BOTH); |
| 183 | } else { |
| 184 | String rtVal1; |
| 185 | String rtVal2; |
| 186 | HubSpokeVpnConfig conf = (HubSpokeVpnConfig) ins.vpnConfig(); |
| 187 | if (role.role() == HUB) { |
| 188 | rtVal1 = conf.hubImpRt(); |
| 189 | rtVal2 = conf.hubExpRt(); |
| 190 | } else { |
| 191 | rtVal1 = conf.spokeImpRt(); |
| 192 | rtVal2 = conf.spokeExpRt(); |
| 193 | } |
| 194 | rts1 = getRtsVal(rtVal1, IMPORT); |
| 195 | Rts rts2 = getRtsVal(rtVal2, EXPORT); |
| 196 | rtsList.add(rts2); |
| 197 | } |
| 198 | rtsList.add(rts1); |
| 199 | config.rts(rtsList); |
| 200 | return config; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Returns the device model RT from the RT type and RT value after |
| 205 | * building it. |
| 206 | * |
| 207 | * @param rtVal RT value |
| 208 | * @param type RT type |
| 209 | * @return device model RT |
| 210 | */ |
| 211 | private static Rts getRtsVal(String rtVal, RtTypeEnum type) { |
| 212 | Rts rts = new DefaultRts(); |
| 213 | rts.rt(rtVal); |
| 214 | rts.rtType(type); |
| 215 | return rts; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Builds RD from the stored device model VPN instance. |
| 220 | * |
| 221 | * @param vpn VPN instance |
| 222 | * @return l3 VPN object |
| 223 | */ |
| 224 | private static L3Vpn buildRd(VpnInstance vpn) { |
| 225 | |
| 226 | String rd = vpn.vpnConfig().rd(); |
| 227 | org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn |
| 228 | .rev20160909.ietfbgpl3vpn.routedistinguisherparams |
| 229 | .routedistinguisher.Config config = new org.onosproject.yang |
| 230 | .gen.v1.urn.ietf.params.xml.ns.yang.ietf.bgp.l3vpn.rev20160909 |
| 231 | .ietfbgpl3vpn.routedistinguisherparams.routedistinguisher |
| 232 | .DefaultConfig(); |
| 233 | config.rd(rd); |
| 234 | RouteDistinguisher dist = new DefaultRouteDistinguisher(); |
| 235 | dist.config(config); |
| 236 | L3Vpn l3vpn = new DefaultL3Vpn(); |
| 237 | l3vpn.routeDistinguisher(dist); |
| 238 | return l3vpn; |
| 239 | } |
| 240 | } |