blob: d4c7a6f970a7aef2595abeb02fc7a7d436e598cf [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
janani bf7060cd2017-03-28 19:06:30 +05303 *
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
17package org.onosproject.drivers.huawei;
18
Vidyashree Rama04147ca2017-05-26 11:32:47 +053019import org.onosproject.yang.gen.v1.ietfinterfaces.rev20140508.ietfinterfaces.DefaultDevices;
20import org.onosproject.yang.gen.v1.ietfinterfaces.rev20140508.ietfinterfaces.devices.Device;
21import org.onosproject.yang.gen.v1.ietfinterfaces.rev20140508.ietfinterfaces.devices.device.Interfaces;
22import org.onosproject.yang.gen.v1.ietfinterfaces.rev20140508.ietfinterfaces.devices.device.interfaces.DefaultYangAutoPrefixInterface;
23import org.onosproject.yang.gen.v1.ietfinterfaces.rev20140508.ietfinterfaces.devices.device.interfaces.YangAutoPrefixInterface;
24import org.onosproject.yang.gen.v1.ietfip.rev20140616.ietfip.devices.device.interfaces.yangautoprefixinterface.augmentedifinterface.Ipv4;
25import org.onosproject.yang.gen.v1.ietfip.rev20140616.ietfip.devices.device.interfaces.yangautoprefixinterface.augmentedifinterface.ipv4.Address;
26import org.onosproject.yang.gen.v1.ietfip.rev20140616.ietfip.devices.device.interfaces.yangautoprefixinterface.augmentedifinterface.ipv4.address.subnet.PrefixLength;
27import org.onosproject.yang.gen.v1.ietfnetworkinstance.rev20160623.ietfnetworkinstance.devices.device.interfaces.yangautoprefixinterface.DefaultAugmentedIfInterface;
28import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.DeviceKeys;
29import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.DefaultL3Vpn;
30import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.DefaultL3Vpncomm;
31import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.DefaultL3VpnInstances;
32import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.DefaultL3VpnInstance;
33import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.L3VpnInstanceKeys;
34import org.onosproject.yang.gen.v1.nel3vpncomm.rev20141225.nel3vpncomm.l3vpnifs.DefaultL3VpnIfs;
35import org.onosproject.yang.gen.v1.nel3vpncomm.rev20141225.nel3vpncomm.l3vpnifs.L3VpnIfs;
36import org.onosproject.yang.gen.v1.nel3vpncomm.rev20141225.nel3vpncomm.l3vpnifs.l3vpnifs.DefaultL3VpnIf;
37import org.onosproject.yang.gen.v1.nel3vpncomm.rev20141225.nel3vpncomm.l3vpnifs.l3vpnifs.L3VpnIf;
38import org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.Ipv4Address;
janani bf7060cd2017-03-28 19:06:30 +053039import org.onosproject.yang.model.InnerModelObject;
40import org.onosproject.yang.model.ModelObjectData;
41import org.onosproject.yang.model.ModelObjectId;
42
43import java.net.InetAddress;
44import java.net.UnknownHostException;
45import java.util.Iterator;
46import java.util.LinkedList;
47import java.util.List;
48
49import static org.onosproject.drivers.huawei.DriverUtil.DEVICE_NULL;
50import static org.onosproject.drivers.huawei.DriverUtil.OBJECT_NULL;
51import static org.onosproject.drivers.huawei.DriverUtil.getData;
52import static org.onosproject.drivers.huawei.DriverUtil.getIdFromModId;
53import static org.onosproject.drivers.huawei.DriverUtil.getObjFromModData;
Vidyashree Rama04147ca2017-05-26 11:32:47 +053054import static org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.Ipv4Address.fromString;
janani bf7060cd2017-03-28 19:06:30 +053055
56/**
57 * Representation of utility for interface creation and deletion.
58 */
59public 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 bd821b182017-03-30 16:34:49 +053080 String id = getIdFromModId(modObj.identifier(), false);
janani bf7060cd2017-03-28 19:06:30 +053081 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()
Vidyashree Rama04147ca2017-05-26 11:32:47 +0530135 .addChild(org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225
janani bf7060cd2017-03-28 19:06:30 +0530136 .nel3vpnapi.DefaultDevices.class)
Vidyashree Rama04147ca2017-05-26 11:32:47 +0530137 .addChild(org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225
138 .nel3vpnapi.devices.DefaultDevice.class, key)
janani bf7060cd2017-03-28 19:06:30 +0530139 .addChild(DefaultL3Vpn.class)
140 .addChild(DefaultL3Vpncomm.class)
141 .addChild(DefaultL3VpnInstances.class)
142 .addChild(DefaultL3VpnInstance.class, insKey);
143 }
144
145 /**
146 * Returns the driver interfaces from the standard device model interfaces.
147 *
148 * @param ifs standard device model interfaces
149 * @return driver interfaces
150 */
151 private static L3VpnIfs getDriverInterfaces(YangAutoPrefixInterface ifs) {
152 L3VpnIfs l3VpnIfs = new DefaultL3VpnIfs();
153 L3VpnIf l3VpnIf = new DefaultL3VpnIf();
154 List<L3VpnIf> l3VpnIfList = new LinkedList<>();
155 l3VpnIf.ifName(ifs.name());
Vidyashree Rama04147ca2017-05-26 11:32:47 +0530156 org.onosproject.yang.gen.v1.ietfip
janani bf7060cd2017-03-28 19:06:30 +0530157 .rev20140616.ietfip.devices.device.interfaces
158 .yangautoprefixinterface.DefaultAugmentedIfInterface ipAug =
159 ((DefaultYangAutoPrefixInterface) ifs).augmentation(
Vidyashree Rama04147ca2017-05-26 11:32:47 +0530160 org.onosproject.yang.gen.v1.ietfip.rev20140616
161 .ietfip.devices.device.interfaces
162 .yangautoprefixinterface
janani bf7060cd2017-03-28 19:06:30 +0530163 .DefaultAugmentedIfInterface.class);
164
165 if (ipAug != null && ipAug.ipv4() != null) {
166 Ipv4 ipAddress = ipAug.ipv4();
167 for (Address add : ipAddress.address()) {
168 Ipv4Address v4Add = fromString(add.ip().ipv4Address()
169 .toString());
170 Ipv4Address subnet = fromString(getSubnet(
171 ((PrefixLength) add.subnet()).prefixLength()));
172 l3VpnIf.ipv4Addr(v4Add);
173 l3VpnIf.subnetMask(subnet);
174 }
175 }
176 l3VpnIfList.add(l3VpnIf);
177 l3VpnIfs.l3VpnIf(l3VpnIfList);
178 return l3VpnIfs;
179 }
180
181 /**
182 * Returns the device id from the root object.
183 *
184 * @param obj root object
185 * @return device id
186 */
187 private static String getIdFromRootObj(Object obj) {
188 Device dev = getDevFromRootObj(obj);
189 return dev.deviceid();
190 }
191
192 /**
193 * Returns the device from the root object.
194 *
195 * @param obj root object
196 * @return device object
197 */
198 private static Device getDevFromRootObj(Object obj) {
199 List<Device> deviceList = ((DefaultDevices) obj).device();
200 Iterator<Device> it = deviceList.iterator();
201 if (it.hasNext()) {
202 return it.next();
203 }
204 throw new IllegalArgumentException(DEVICE_NULL);
205 }
206
207 /**
208 * Returns the interfaces object from the root object.
209 *
210 * @param obj root object
211 * @return interfaces object
212 */
213 private static Interfaces getObjFromDevObj(Object obj) {
214 Device dev = getDevFromRootObj(obj);
215 return dev.interfaces();
216 }
217
218 /**
219 * Returns the subnet address from the mask value.
220 *
221 * @param mask mask value
222 * @return subnet address
223 */
224 private static String getSubnet(short mask) {
225 int value = 0xffffffff << (32 - mask);
226 byte[] bytes = new byte[]{
227 (byte) (value >>> 24), (byte) (value >> 16 & 0xff),
228 (byte) (value >> 8 & 0xff), (byte) (value & 0xff)};
229 InetAddress netAdd;
230 try {
231 netAdd = InetAddress.getByAddress(bytes);
232 } catch (UnknownHostException e) {
233 throw new IllegalArgumentException(ILLEGAL_MASK_LENGTH);
234 }
235 return netAdd.getHostAddress();
236 }
237}