blob: 1ba29dd4915f22dcccd0b81c18ebefe444d9ba39 [file] [log] [blame]
janani b9ed76be2017-08-29 19:11:33 +05301/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.drivers.huawei;
18
19import org.onosproject.l3vpn.netl3vpn.TunnelInfo;
20import org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.Ipv4Address;
21import org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.L3VpncommonL3VpnPrefixType;
22import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.DefaultL3Vpn;
23import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.DefaultL3Vpncomm;
24import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.DefaultL3VpnInstances;
25import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.DefaultL3VpnInstance;
26import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.L3VpnInstanceKeys;
27import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.DefaultVpnInstAfs;
28import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.DefaultVpnInstAf;
29import org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.VpnInstAfKeys;
30import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.DefaultDevices;
31import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.Devices;
32import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.DefaultDevice;
33import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.Device;
34import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.DeviceKeys;
35import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.DefaultTnlm;
36import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.Tnlm;
37import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.DefaultTunnelPolicys;
38import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.TunnelPolicys;
39import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.DefaultTunnelPolicy;
40import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.TunnelPolicy;
41import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.TunnelPolicyKeys;
42import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.DefaultTpNexthops;
43import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.TpNexthops;
44import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.tpnexthops.DefaultTpNexthop;
45import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.tpnexthops.TpNexthop;
46import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.tpnexthops.tpnexthop.DefaultTpTunnels;
47import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.tpnexthops.tpnexthop.TpTunnels;
48import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.tpnexthops.tpnexthop.tptunnels.DefaultTpTunnel;
49import org.onosproject.yang.gen.v1.netnlm.rev20141225.netnlm.devices.device.tnlm.tunnelpolicys.tunnelpolicy.tpnexthops.tpnexthop.tptunnels.TpTunnel;
50import org.onosproject.yang.gen.v1.netnlmtype.rev20141225.netnlmtype.tnlmbasetnlpolicytype.TnlmbaseTnlPolicyTypeEnum;
51import org.onosproject.yang.model.DefaultModelObjectData;
52import org.onosproject.yang.model.InnerModelObject;
53import org.onosproject.yang.model.LeafModelObject;
54import org.onosproject.yang.model.ModelObjectData;
55import org.onosproject.yang.model.ModelObjectId.Builder;
56
57import static org.onosproject.drivers.huawei.DriverUtil.UNSUPPORTED_MODEL_LVL;
58import static org.onosproject.drivers.huawei.DriverUtil.getData;
59import static org.onosproject.drivers.huawei.InsConstructionUtil.getModObjIdDriDevice;
60import static org.onosproject.l3vpn.netl3vpn.ModelIdLevel.TP_HOP;
61import static org.onosproject.yang.gen.v1.nel3vpncommtype.rev20141225.nel3vpncommtype.l3vpncommonl3vpnprefixtype.L3VpncommonL3VpnPrefixTypeEnum.IPV4UNI;
62import static org.onosproject.yang.gen.v1.nel3vpnapi.rev20141225.nel3vpnapi.devices.device.l3vpn.l3vpncomm.l3vpninstances.l3vpninstance.vpninstafs.VpnInstAf.LeafIdentifier.TNLPOLICYNAME;
63import static org.onosproject.yang.gen.v1.netnlmtype.rev20141225.netnlmtype.TnlmbaseTnlPolicyType.of;
64import static org.onosproject.yang.model.ModelObjectId.builder;
65
66/**
67 * Representation of utility for tunnel creation and deletion.
68 */
69public final class TnlConstructionUtil {
70
71 /**
72 * Error message for unsupported device type.
73 */
74 private static final String UNSUPPORTED_DEV_TYPE = "Levels other than " +
75 "devices and device are not permitted.";
76
77 /**
78 * Error message for unsupported tunnel policy type.
79 */
80 private static final String UNSUPPORTED_TNL_POL_TYPE = "Levels other" +
81 " than tnlm and tnl policy are not permitted.";
82
83 // No instantiation.
84 private TnlConstructionUtil() {
85 }
86
87 /**
88 * Returns the created model object data of devices or device level from
89 * the tunnel info.
90 *
91 * @param tnlInfo tunnel info
92 * @return driver model object data
93 */
94 static ModelObjectData getCreateTnlDev(TunnelInfo tnlInfo) {
95 Device device = new DefaultDevice();
96 device.deviceid(tnlInfo.devId());
97
98 switch (tnlInfo.level()) {
99 case DEVICES:
100 Devices devices = new DefaultDevices();
101 devices.addToDevice(device);
102 return getData(null, (InnerModelObject) devices);
103
104 case DEVICE:
105 Builder id = getDevicesId();
106 return getData(id.build(), (InnerModelObject) device);
107
108 default:
109 throw new IllegalArgumentException(UNSUPPORTED_DEV_TYPE);
110 }
111 }
112
113 /**
114 * Returns the created model object data of tunnel policy from the tunnel
115 * info.
116 *
117 * @param tnlInfo tunnel info
118 * @return driver model object data
119 */
120 static ModelObjectData getCreateTnlPol(TunnelInfo tnlInfo) {
121 Builder id = getDeviceId(tnlInfo.devId());
122 TunnelPolicy tnlPol = new DefaultTunnelPolicy();
123 tnlPol.tnlPolicyName(tnlInfo.polName());
124 tnlPol.tnlPolicyType(of(TnlmbaseTnlPolicyTypeEnum.of(2)));
125
126 switch (tnlInfo.level()) {
127 case TNL_M:
128 Tnlm tnlm = new DefaultTnlm();
129 TunnelPolicys tnlPolicys = new DefaultTunnelPolicys();
130 tnlPolicys.addToTunnelPolicy(tnlPol);
131 tnlm.tunnelPolicys(tnlPolicys);
132 return getData(id.build(), (InnerModelObject) tnlm);
133
134 case TNL_POL:
135 id = getTunnelPolicysId(id);
136 return getData(id.build(), (InnerModelObject) tnlPol);
137
138 default:
139 throw new IllegalArgumentException(UNSUPPORTED_TNL_POL_TYPE);
140 }
141 }
142
143 /**
144 * Returns the created model object data of tunnel from the tunnel info.
145 *
146 * @param tnlInfo tunnel info
147 * @return driver model object data
148 */
149 static ModelObjectData getCreateTnl(TunnelInfo tnlInfo) {
150 TunnelPolicyKeys key = new TunnelPolicyKeys();
151 key.tnlPolicyName(tnlInfo.polName());
152
153 Builder id = getDeviceId(tnlInfo.devId());
154 id = getTunnelPolicysId(id);
155 id = id.addChild(DefaultTunnelPolicy.class, key);
156 TpNexthop tpHop = new DefaultTpNexthop();
157 TpTunnels tunnels = new DefaultTpTunnels();
158 TpTunnel tunnel = new DefaultTpTunnel();
159 tunnel.tunnelName(tnlInfo.tnlName());
160 tunnel.autoTunnel(true);
161 tunnels.addToTpTunnel(tunnel);
162 tpHop.tpTunnels(tunnels);
163 tpHop.nexthopIpaddr(Ipv4Address.of(tnlInfo.desIp()));
164
165 if (tnlInfo.level() == TP_HOP) {
166 id.addChild(DefaultTpNexthops.class);
167 return getData(id.build(), (InnerModelObject) tpHop);
168 } else {
169 TpNexthops tpHops = new DefaultTpNexthops();
170 tpHops.addToTpNexthop(tpHop);
171 return getData(id.build(), (InnerModelObject) tpHops);
172 }
173 }
174
175 /**
176 * Returns the created model object data of binding the tunnel policy to
177 * the VPN from the tunnel policy name and device id.
178 *
179 * @param tnlInfo tunnel info
180 * @return driver model object data
181 */
182 static ModelObjectData getBindTnl(TunnelInfo tnlInfo) {
183 L3VpnInstanceKeys vpnKey = new L3VpnInstanceKeys();
184 vpnKey.vrfName(tnlInfo.polName());
185 VpnInstAfKeys afKeys = new VpnInstAfKeys();
186 afKeys.afType(L3VpncommonL3VpnPrefixType.of(IPV4UNI));
187
188 Builder id = getModObjIdDriDevice(tnlInfo.devId());
189 id.addChild(DefaultL3Vpn.class);
190 id.addChild(DefaultL3Vpncomm.class);
191 id.addChild(DefaultL3VpnInstances.class);
192 id.addChild(DefaultL3VpnInstance.class, vpnKey);
193 id.addChild(DefaultVpnInstAfs.class);
194 id.addChild(DefaultVpnInstAf.class, afKeys);
195
196 LeafModelObject leaf = new LeafModelObject();
197 leaf.leafIdentifier(TNLPOLICYNAME);
198 leaf.addValue(tnlInfo.polName());
199 return DefaultModelObjectData.builder().addModelObject(leaf)
200 .identifier(id.build()).build();
201 }
202
203 /**
204 * Returns the driver model object data for delete, according to the
205 * levels it has to be constructed for tunnel policy.
206 *
207 * @param tnlInfo tunnel info
208 * @return driver model object data
209 */
210 static ModelObjectData getDeleteTnl(TunnelInfo tnlInfo) {
211 Builder id = getDeviceId(tnlInfo.devId());
212 switch (tnlInfo.level()) {
213 case DEVICES:
214 return getData(getDevicesId().build(), new DefaultDevice());
215
216 case DEVICE:
217 return getData(id.build(), new DefaultTnlm());
218
219 case TNL_POL:
220 id = getTunnelPolicysId(id);
221 TunnelPolicyKeys polKey = new TunnelPolicyKeys();
222 polKey.tnlPolicyName(tnlInfo.polName());
223 id = id.addChild(DefaultTunnelPolicy.class, polKey);
224 return getData(id.build(), new DefaultTpNexthops());
225
226 default:
227 throw new IllegalArgumentException(UNSUPPORTED_MODEL_LVL);
228 }
229 }
230
231 /**
232 * Returns the model object id of tunnel with devices and device.
233 *
234 * @param id device id
235 * @return model object id
236 */
237 private static Builder getDeviceId(String id) {
238 DeviceKeys devId = new DeviceKeys();
239 devId.deviceid(id);
240 return getDevicesId().addChild(DefaultDevice.class, devId);
241 }
242
243 /**
244 * Returns the model object id of tunnel with devices.
245 *
246 * @return model object id
247 */
248 private static Builder getDevicesId() {
249 return builder().addChild(DefaultDevices.class);
250 }
251
252 /**
253 * Returns the model object id of tunnel policies.
254 *
255 * @param id model object id
256 * @return model object id with tunnel policies
257 */
258 public static Builder getTunnelPolicysId(Builder id) {
259 return id.addChild(DefaultTnlm.class)
260 .addChild(DefaultTunnelPolicys.class);
261 }
262}