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.yang.gen.v1.l3vpn.comm.type.rev20141225.nel3vpncommtype.Ipv4Address; |
| 20 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.DeviceKeys; |
| 21 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.DefaultL3Vpn; |
| 22 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.DefaultL3Vpncomm; |
| 23 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.DefaultL3VpnInstances; |
| 24 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.DefaultL3VpnInstance; |
| 25 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.L3VpnInstanceKeys; |
| 26 | import org.onosproject.yang.gen.v1.ne.l3vpn.comm.rev20141225.nel3vpncomm.l3vpnifs.DefaultL3VpnIfs; |
| 27 | import org.onosproject.yang.gen.v1.ne.l3vpn.comm.rev20141225.nel3vpncomm.l3vpnifs.L3VpnIfs; |
| 28 | import org.onosproject.yang.gen.v1.ne.l3vpn.comm.rev20141225.nel3vpncomm.l3vpnifs.l3vpnifs.DefaultL3VpnIf; |
| 29 | import org.onosproject.yang.gen.v1.ne.l3vpn.comm.rev20141225.nel3vpncomm.l3vpnifs.l3vpnifs.L3VpnIf; |
| 30 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20140508.ietfinterfaces.DefaultDevices; |
| 31 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20140508.ietfinterfaces.devices.Device; |
| 32 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20140508.ietfinterfaces.devices.device.Interfaces; |
| 33 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20140508.ietfinterfaces.devices.device.interfaces.DefaultYangAutoPrefixInterface; |
| 34 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev20140508.ietfinterfaces.devices.device.interfaces.YangAutoPrefixInterface; |
| 35 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev20140616.ietfip.devices.device.interfaces.yangautoprefixinterface.augmentedifinterface.Ipv4; |
| 36 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev20140616.ietfip.devices.device.interfaces.yangautoprefixinterface.augmentedifinterface.ipv4.Address; |
| 37 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev20140616.ietfip.devices.device.interfaces.yangautoprefixinterface.augmentedifinterface.ipv4.address.subnet.PrefixLength; |
| 38 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.instance.rev20160623.ietfnetworkinstance.devices.device.interfaces.yangautoprefixinterface.DefaultAugmentedIfInterface; |
| 39 | import org.onosproject.yang.model.InnerModelObject; |
| 40 | import org.onosproject.yang.model.ModelObjectData; |
| 41 | import org.onosproject.yang.model.ModelObjectId; |
| 42 | |
| 43 | import java.net.InetAddress; |
| 44 | import java.net.UnknownHostException; |
| 45 | import java.util.Iterator; |
| 46 | import java.util.LinkedList; |
| 47 | import java.util.List; |
| 48 | |
| 49 | import static org.onosproject.drivers.huawei.DriverUtil.DEVICE_NULL; |
| 50 | import static org.onosproject.drivers.huawei.DriverUtil.OBJECT_NULL; |
| 51 | import static org.onosproject.drivers.huawei.DriverUtil.getData; |
| 52 | import static org.onosproject.drivers.huawei.DriverUtil.getIdFromModId; |
| 53 | import static org.onosproject.drivers.huawei.DriverUtil.getObjFromModData; |
| 54 | import static org.onosproject.yang.gen.v1.l3vpn.comm.type.rev20141225.nel3vpncommtype.Ipv4Address.fromString; |
| 55 | |
| 56 | /** |
| 57 | * Representation of utility for interface creation and deletion. |
| 58 | */ |
| 59 | public final class IntConstructionUtil { |
| 60 | |
| 61 | /** |
| 62 | * Error message for illegal length of mask. |
| 63 | */ |
| 64 | private static final String ILLEGAL_MASK_LENGTH = "Illegal length of mask" + |
| 65 | " is not allowed."; |
| 66 | |
| 67 | // No instantiation. |
| 68 | private IntConstructionUtil() { |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the created model object data of VPN bounded interface of huawei |
| 73 | * device from the standard model object data. |
| 74 | * |
| 75 | * @param modObj model object data |
| 76 | * @return driver interface model object data |
| 77 | */ |
| 78 | static ModelObjectData getCreateInt(ModelObjectData modObj) { |
| 79 | boolean isModIdAvail = true; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 80 | String id = getIdFromModId(modObj.identifier(), false); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 81 | Object obj = getObjFromModData(modObj); |
| 82 | if (id == null) { |
| 83 | isModIdAvail = false; |
| 84 | id = getIdFromRootObj(obj); |
| 85 | obj = getObjFromDevObj(obj); |
| 86 | } |
| 87 | if (obj == null) { |
| 88 | throw new IllegalArgumentException(OBJECT_NULL); |
| 89 | } |
| 90 | List<YangAutoPrefixInterface> intList = ((Interfaces) obj) |
| 91 | .yangAutoPrefixInterface(); |
| 92 | YangAutoPrefixInterface l3Int = intList.get(0); |
| 93 | DefaultAugmentedIfInterface ifInt = |
| 94 | ((DefaultYangAutoPrefixInterface) l3Int).augmentation( |
| 95 | DefaultAugmentedIfInterface.class); |
| 96 | String insName = ifInt.bindNetworkInstanceName(); |
| 97 | L3VpnIfs l3VpnIfs = getDriverInterfaces(l3Int); |
| 98 | return getDriModObj(id, insName, l3VpnIfs, isModIdAvail); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the driver model object data, according to the levels it has |
| 103 | * to be constructed. |
| 104 | * |
| 105 | * @param id device id |
| 106 | * @param insName VPN name |
| 107 | * @param l3VpnIfs driver VPN if object |
| 108 | * @param isModIdAvail model id availability |
| 109 | * @return driver model object data |
| 110 | */ |
| 111 | private static ModelObjectData getDriModObj(String id, String insName, |
| 112 | L3VpnIfs l3VpnIfs, |
| 113 | boolean isModIdAvail) { |
| 114 | List<L3VpnIf> intList = l3VpnIfs.l3VpnIf(); |
| 115 | Iterator<L3VpnIf> it = intList.iterator(); |
| 116 | L3VpnIf l3VpnIf = it.next(); |
| 117 | ModelObjectData data; |
| 118 | ModelObjectId.Builder objId = getModIdBuilder(id, insName); |
| 119 | if (isModIdAvail) { |
| 120 | objId.addChild(DefaultL3VpnIfs.class); |
| 121 | data = getData(objId.build(), (InnerModelObject) l3VpnIf); |
| 122 | } else { |
| 123 | data = getData(objId.build(), (InnerModelObject) l3VpnIfs); |
| 124 | } |
| 125 | return data; |
| 126 | } |
| 127 | |
| 128 | private static ModelObjectId.Builder getModIdBuilder(String id, |
| 129 | String vpnName) { |
| 130 | DeviceKeys key = new DeviceKeys(); |
| 131 | key.deviceid(id); |
| 132 | L3VpnInstanceKeys insKey = new L3VpnInstanceKeys(); |
| 133 | insKey.vrfName(vpnName); |
| 134 | return ModelObjectId.builder() |
| 135 | .addChild(org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225 |
| 136 | .nel3vpnapi.DefaultDevices.class) |
| 137 | .addChild(org.onosproject.yang.gen.v1.ne.l3vpn.api |
| 138 | .rev20141225.nel3vpnapi.devices |
| 139 | .DefaultDevice.class, key) |
| 140 | .addChild(DefaultL3Vpn.class) |
| 141 | .addChild(DefaultL3Vpncomm.class) |
| 142 | .addChild(DefaultL3VpnInstances.class) |
| 143 | .addChild(DefaultL3VpnInstance.class, insKey); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the driver interfaces from the standard device model interfaces. |
| 148 | * |
| 149 | * @param ifs standard device model interfaces |
| 150 | * @return driver interfaces |
| 151 | */ |
| 152 | private static L3VpnIfs getDriverInterfaces(YangAutoPrefixInterface ifs) { |
| 153 | L3VpnIfs l3VpnIfs = new DefaultL3VpnIfs(); |
| 154 | L3VpnIf l3VpnIf = new DefaultL3VpnIf(); |
| 155 | List<L3VpnIf> l3VpnIfList = new LinkedList<>(); |
| 156 | l3VpnIf.ifName(ifs.name()); |
| 157 | org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip |
| 158 | .rev20140616.ietfip.devices.device.interfaces |
| 159 | .yangautoprefixinterface.DefaultAugmentedIfInterface ipAug = |
| 160 | ((DefaultYangAutoPrefixInterface) ifs).augmentation( |
| 161 | org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang |
| 162 | .ietf.ip.rev20140616.ietfip.devices.device |
| 163 | .interfaces.yangautoprefixinterface |
| 164 | .DefaultAugmentedIfInterface.class); |
| 165 | |
| 166 | if (ipAug != null && ipAug.ipv4() != null) { |
| 167 | Ipv4 ipAddress = ipAug.ipv4(); |
| 168 | for (Address add : ipAddress.address()) { |
| 169 | Ipv4Address v4Add = fromString(add.ip().ipv4Address() |
| 170 | .toString()); |
| 171 | Ipv4Address subnet = fromString(getSubnet( |
| 172 | ((PrefixLength) add.subnet()).prefixLength())); |
| 173 | l3VpnIf.ipv4Addr(v4Add); |
| 174 | l3VpnIf.subnetMask(subnet); |
| 175 | } |
| 176 | } |
| 177 | l3VpnIfList.add(l3VpnIf); |
| 178 | l3VpnIfs.l3VpnIf(l3VpnIfList); |
| 179 | return l3VpnIfs; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Returns the device id from the root object. |
| 184 | * |
| 185 | * @param obj root object |
| 186 | * @return device id |
| 187 | */ |
| 188 | private static String getIdFromRootObj(Object obj) { |
| 189 | Device dev = getDevFromRootObj(obj); |
| 190 | return dev.deviceid(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns the device from the root object. |
| 195 | * |
| 196 | * @param obj root object |
| 197 | * @return device object |
| 198 | */ |
| 199 | private static Device getDevFromRootObj(Object obj) { |
| 200 | List<Device> deviceList = ((DefaultDevices) obj).device(); |
| 201 | Iterator<Device> it = deviceList.iterator(); |
| 202 | if (it.hasNext()) { |
| 203 | return it.next(); |
| 204 | } |
| 205 | throw new IllegalArgumentException(DEVICE_NULL); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns the interfaces object from the root object. |
| 210 | * |
| 211 | * @param obj root object |
| 212 | * @return interfaces object |
| 213 | */ |
| 214 | private static Interfaces getObjFromDevObj(Object obj) { |
| 215 | Device dev = getDevFromRootObj(obj); |
| 216 | return dev.interfaces(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns the subnet address from the mask value. |
| 221 | * |
| 222 | * @param mask mask value |
| 223 | * @return subnet address |
| 224 | */ |
| 225 | private static String getSubnet(short mask) { |
| 226 | int value = 0xffffffff << (32 - mask); |
| 227 | byte[] bytes = new byte[]{ |
| 228 | (byte) (value >>> 24), (byte) (value >> 16 & 0xff), |
| 229 | (byte) (value >> 8 & 0xff), (byte) (value & 0xff)}; |
| 230 | InetAddress netAdd; |
| 231 | try { |
| 232 | netAdd = InetAddress.getByAddress(bytes); |
| 233 | } catch (UnknownHostException e) { |
| 234 | throw new IllegalArgumentException(ILLEGAL_MASK_LENGTH); |
| 235 | } |
| 236 | return netAdd.getHostAddress(); |
| 237 | } |
| 238 | } |