blob: e82ee64e1f21aff594e68537c922d45dd6b8460c [file] [log] [blame]
chengfan8ea6a562016-11-26 16:37:02 +08001/*
2 * Copyright 2016 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
17package org.onosproject.teyang.utils.tunnel;
18
19import com.google.common.collect.Lists;
20import org.onosproject.tetopology.management.api.TeTopologyKey;
21import org.onosproject.tetopology.management.api.node.TeNodeKey;
22import org.onosproject.tetopology.management.api.node.TtpKey;
23import org.onosproject.tetunnel.api.tunnel.DefaultTeTunnel;
24import org.onosproject.tetunnel.api.tunnel.TeTunnel;
25import org.onosproject.tetunnel.api.tunnel.TeTunnelKey;
26import org.onosproject.tetunnel.api.tunnel.path.DefaultTePath;
27import org.onosproject.tetunnel.api.tunnel.path.DefaultTeRouteUnnumberedLink;
28import org.onosproject.tetunnel.api.tunnel.path.TePath;
29import org.onosproject.tetunnel.api.tunnel.path.TeRouteSubobject;
30import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.IetfTe;
31import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.IetfTeOpParam;
32import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.DefaultTe;
33import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.Te;
34import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.pathparamsconfig.Type;
Henry Yuad441142016-12-01 17:00:37 -050035import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.pathparamsconfig.type
36 .DefaultDynamic;
37import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.pathparamsconfig.type
38 .DefaultExplicit;
39import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.pathparamsconfig.type
40 .explicit.DefaultExplicitRouteObjects;
41import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.pathparamsconfig.type
42 .explicit.ExplicitRouteObjects;
chengfan8ea6a562016-11-26 16:37:02 +080043import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelproperties.Config;
Henry Yuad441142016-12-01 17:00:37 -050044import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelproperties
45 .DefaultPrimaryPaths;
chengfan8ea6a562016-11-26 16:37:02 +080046import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelproperties.DefaultState;
47import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelproperties.PrimaryPaths;
48import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelproperties.State;
Henry Yuad441142016-12-01 17:00:37 -050049import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelsgrouping
50 .DefaultTunnels;
chengfan8ea6a562016-11-26 16:37:02 +080051import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelsgrouping.Tunnels;
Henry Yuad441142016-12-01 17:00:37 -050052import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelsgrouping.tunnels
53 .DefaultTunnel;
54import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.rev20160705.ietfte.tunnelsgrouping.tunnels
55 .Tunnel;
chengfan8ea6a562016-11-26 16:37:02 +080056import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.LspProt1Forn;
57import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.LspProtBidir1To1;
58import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.LspProtReroute;
Henry Yuad441142016-12-01 17:00:37 -050059import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes
60 .LspProtRerouteExtra;
chengfan8ea6a562016-11-26 16:37:02 +080061import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.LspProtType;
62import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.LspProtUnidir1To1;
63import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.LspProtUnprotected;
Henry Yuad441142016-12-01 17:00:37 -050064import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes
65 .PathSignalingRsvpte;
chengfan8ea6a562016-11-26 16:37:02 +080066import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.PathSignalingSr;
67import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.RouteIncludeEro;
68import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.StateDown;
69import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.StateUp;
70import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.TunnelP2Mp;
71import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.TunnelP2p;
72import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.TunnelType;
Henry Yuad441142016-12-01 17:00:37 -050073import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes
74 .explicitroutesubobject.type.DefaultUnnumberedLink;
75import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.types.rev20160705.ietftetypes
76 .explicitroutesubobject.type.UnnumberedLink;
chengfan8ea6a562016-11-26 16:37:02 +080077import org.slf4j.Logger;
78
79import java.util.ArrayList;
80import java.util.List;
81
82import static org.onosproject.tetunnel.api.tunnel.TeTunnel.LspProtectionType;
83import static org.onosproject.tetunnel.api.tunnel.path.TeRouteSubobject.Type.UNNUMBERED_LINK;
84import static org.onosproject.teyang.utils.tunnel.BasicConverter.bytesToLong;
85import static org.onosproject.teyang.utils.tunnel.BasicConverter.ipToLong;
86import static org.onosproject.teyang.utils.tunnel.BasicConverter.longToByte;
87import static org.onosproject.teyang.utils.tunnel.BasicConverter.longToIp;
88import static org.slf4j.LoggerFactory.getLogger;
89
90
91/**
92 * Tunnel convert utils.
93 */
94public final class TunnelConverter {
95
96 private static final Logger log = getLogger(TunnelConverter.class);
97 private static final String DEFAULT_PATH_NAME = "ietfPath";
98 private static final String DEFAULT_PATH_CONSTRAINT = "ietfPath";
99 private static final int DEFAULT_PATH_PREFERENCE = 1;
100 private static final boolean DEFAULT_CSPF_STATE = true;
101 private static final boolean DEFAULT_LOCKDOWN_STATE = true;
102
103 // no instantiation
104 private TunnelConverter() {
105 }
106
107 /**
108 * Build a general IETF TE object with a giving tunnel list. for there are
109 * many kind of attributes in IETF TE, now we only care about the tunnel
110 * attributes.
111 *
112 * @param tunnels tunnels in the TE network
113 * @return IETF te info in the TE network
114 */
115 public static IetfTe buildIetfTeWithTunnels(List<Tunnel> tunnels) {
116 Tunnels teTunnels = new DefaultTunnels
117 .TunnelsBuilder()
118 .tunnel(tunnels)
119 .build();
120 Te te = new DefaultTe
121 .TeBuilder()
122 .tunnels(teTunnels)
123 .build();
124 return new IetfTeOpParam
125 .IetfTeBuilder()
126 .te(te)
127 .yangIetfTeOpType(IetfTe.OnosYangOpType.NONE)
128 .build();
129 }
130
131 public static IetfTe buildIetfTe(TeTunnel teTunnel) {
132 Tunnel tunnel = te2YangTunnelConverter(teTunnel);
133 return buildIetfTeWithTunnels(Lists.newArrayList(tunnel));
134 }
135
136 /**
137 * Converts a specific te tunnel defined in the APP to the general te tunnel
138 * defined in YANG model.
139 *
140 * @param tunnel te tunnel defined in APP
141 * @return tunnel defined in YANG model
142 */
143 public static Tunnel te2YangTunnelConverter(TeTunnel tunnel) {
144 List<PrimaryPaths> pathsList = new ArrayList<>();
145 //TODO add path info after yang tool change to guava from google-collect
146 tunnel.primaryPaths()
147 .forEach(tePath -> pathsList.add(te2YangPrimaryPath(tePath)));
148
149 Tunnel.TunnelBuilder builder = new DefaultTunnel
150 .TunnelBuilder()
151 .type(te2YangTunnelType(tunnel.type()))
152 .name(tunnel.name())
153 .identifier(tunnel.teTunnelKey().teTunnelId())
154 .state(te2YangTunnelState(tunnel))
155 .primaryPaths(pathsList);
156
157 return builder.build();
158 }
159
160 private static State te2YangTunnelState(TeTunnel tunnel) {
161 State.StateBuilder stateBuilder = new DefaultState.StateBuilder();
162 stateBuilder.name(tunnel.name())
163 .identifier((int) tunnel.teTunnelKey().teTunnelId())
164 .source((longToIp(tunnel.srcNode().teNodeId())))
165 .destination((longToIp(tunnel.dstNode().teNodeId())))
166 .srcTpId(longToByte(tunnel.srcTp().ttpId()))
167 .dstTpId(longToByte(tunnel.dstTp().ttpId()))
168 .adminStatus(te2YangStateType(tunnel.adminStatus()))
169 .lspProtectionType(
170 te2YangProtectionType(tunnel.lspProtectionType()))
171 .type(te2YangTunnelType(tunnel.type()))
172 .build();
173 return stateBuilder.build();
174 }
175
176 private static PrimaryPaths te2YangPrimaryPath(TePath tePath) {
177 DefaultPrimaryPaths.PrimaryPathsBuilder builder = new DefaultPrimaryPaths
178 .PrimaryPathsBuilder()
179 .name(DEFAULT_PATH_NAME)
180 .preference(DEFAULT_PATH_PREFERENCE)
181 .state(te2YangPrimaryPathState(tePath))
182 .yangPrimaryPathsOpType(IetfTe.OnosYangOpType.NONE);
183 return builder.build();
184 }
185
186 private static org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
187 .te.rev20160705.ietfte.p2pprimarypathparams.State
188 te2YangPrimaryPathState(TePath tePath) {
189
190 List<TeRouteSubobject> teRouteSubobjects = tePath.explicitRoute();
191
192 List<ExplicitRouteObjects> routeObjects = new ArrayList<>();
193 teRouteSubobjects.forEach(teRouteSubobject -> {
194 routeObjects.add(te2YangRouteSubobject(teRouteSubobject));
195 });
196 DefaultExplicit.ExplicitBuilder explicitBuilder =
197 DefaultExplicit.builder().explicitRouteObjects(routeObjects);
198
199 return new org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.
200 te.rev20160705.ietfte.p2pprimarypathparams.DefaultState
201 .StateBuilder()
202 .type(explicitBuilder.build())
203 .pathNamedConstraint(DEFAULT_PATH_CONSTRAINT)
204 .noCspf(DEFAULT_CSPF_STATE)
205 .lockdown(DEFAULT_LOCKDOWN_STATE)
206 .build();
207 }
208
209 private static ExplicitRouteObjects
210 te2YangRouteSubobject(TeRouteSubobject routeSubobject) {
211
212 TeRouteSubobject.Type type = routeSubobject.type();
213 UnnumberedLink yanglink = null;
214 //TODO implement other kind of TeRouteSubobject type
215 if (type == UNNUMBERED_LINK) {
216 DefaultTeRouteUnnumberedLink unnumberedLink =
217 (DefaultTeRouteUnnumberedLink) routeSubobject;
218 TeNodeKey nodeKey = unnumberedLink.node();
219 TtpKey ttpKey = unnumberedLink.ttp();
220
221 yanglink = DefaultUnnumberedLink.builder()
222 .routerId(longToIp(nodeKey.teNodeId()))
223 .interfaceId(ttpKey.ttpId())
224 .build();
225
226 }
227
228 //TODO implement other kind of explicitRoute usage type
229 return DefaultExplicitRouteObjects.builder()
230 .type(yanglink)
231 .explicitRouteUsage(RouteIncludeEro.class)
232 .build();
233 }
234
235 /**
236 * Converts a YANG TE tunnel defined in the YANG model to a specific TE
237 * tunnel defined in the TE tunnel APP.
238 *
Henry Yuad441142016-12-01 17:00:37 -0500239 * @param tunnel yang tunnel object
240 * @param topologyKey key of the TE topology to which this tunnel belongs
chengfan8ea6a562016-11-26 16:37:02 +0800241 * @return default Te tunnel defined in TE tunnel APP
242 */
243 public static DefaultTeTunnel yang2TeTunnel(org.onosproject.yang.gen.v1.
244 urn.ietf.params.xml.
245 ns.yang.ietf.te.
246 rev20160705.ietfte.
247 tunnelsgrouping.
248 tunnels.Tunnel
249 tunnel,
250 TeTopologyKey topologyKey) {
251 //get config info
252 Config config = tunnel.config();
253
254 //build basic attribute, node and ttp
255 TeNodeKey srcNodeKey = new TeNodeKey(topologyKey, ipToLong(config.source()));
256 TeNodeKey dstNodeKey = new TeNodeKey(topologyKey, ipToLong(config.destination()));
257
258 TtpKey srcTtpKey = new TtpKey(srcNodeKey, bytesToLong(config.srcTpId()));
259 TtpKey dstTtpKey = new TtpKey(srcNodeKey, bytesToLong(config.dstTpId()));
260
261 //check if paths have been set
262 List<PrimaryPaths> primaryPaths = tunnel.primaryPaths();
263 List<TePath> paths = new ArrayList<>();
264 primaryPaths.forEach(primaryPath -> paths.add(
265 yang2TePrimaryPaths(primaryPath, topologyKey)));
266
267 //build the te tunnel
268 DefaultTeTunnel.Builder builder = new DefaultTeTunnel.Builder();
269
270 return builder.teTunnelKey(new TeTunnelKey(topologyKey, config.identifier()))
271 .name(config.name())
272 .type(yang2TeTunnelType(config.type()))
273 .lspProtectionType(yang2TeProtectionType(config.lspProtectionType()))
274 .adminState(yang2TeStateType(config.adminStatus()))
275 .srcNode(srcNodeKey)
276 .dstNode(dstNodeKey)
277 .srcTp(srcTtpKey)
278 .dstTp(dstTtpKey)
279 .primaryPaths(paths).build();
280 }
281
282 private static TePath yang2TePrimaryPaths(PrimaryPaths primaryPath,
283 TeTopologyKey topologyKey) {
284 org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.
285 rev20160705.ietfte.p2pprimarypathparams.Config
286 pathConfig = primaryPath.config();
287
288 TePath tePath;
289 TePath.Type tePathType = null;
290 Type type = pathConfig.type();
291
292 if (type == null) {
293 return new DefaultTePath(TePath.Type.DYNAMIC,
294 Lists.newArrayList(),
295 Lists.newArrayList(),
296 Lists.newArrayList());
297 }
298
299 Class<? extends Type> typeClass = type.getClass();
300
301 List<TeRouteSubobject> routeSubobjects = new ArrayList<>();
302
303 if (typeClass.isAssignableFrom(DefaultExplicit.class)) {
304 DefaultExplicit explicitPath = (DefaultExplicit) type;
305 explicitPath
306 .explicitRouteObjects()
307 .forEach(o -> routeSubobjects.add(
308 yang2TeRouteSubobject(o, topologyKey)));
309 tePathType = TePath.Type.EXPLICIT;
310
311 } else if (typeClass.isAssignableFrom(DefaultDynamic.class)) {
312 tePathType = TePath.Type.DYNAMIC;
313 }
314
315 tePath = new DefaultTePath(tePathType,
316 Lists.newArrayList(),
317 routeSubobjects,
318 Lists.newArrayList());
319 return tePath;
320 }
321
322 private static TeRouteSubobject
323 yang2TeRouteSubobject(ExplicitRouteObjects routeObject,
324 TeTopologyKey topologyKey) {
325
326 //TODO implement other types of route type
327 DefaultUnnumberedLink type = (DefaultUnnumberedLink) routeObject.type();
328 TeNodeKey nodeKey = new TeNodeKey(topologyKey, ipToLong(type.routerId()));
329 TtpKey tpId = new TtpKey(nodeKey, type.interfaceId());
330 return new DefaultTeRouteUnnumberedLink(nodeKey, tpId);
331 }
332
333 private static TeTunnel.Type yang2TeTunnelType(Class type) {
334 TeTunnel.Type tunnelType = null;
335 if (type.isAssignableFrom(TunnelP2Mp.class)) {
336 tunnelType = TeTunnel.Type.P2MP;
337 } else if (type.isAssignableFrom(TunnelP2p.class)) {
338 tunnelType = TeTunnel.Type.P2P;
339 } else if (type.isAssignableFrom(PathSignalingRsvpte.class)) {
340 tunnelType = TeTunnel.Type.PATH_SIGNALING_RSVPTE;
341 } else if (type.isAssignableFrom(PathSignalingSr.class)) {
342 tunnelType = TeTunnel.Type.PATH_SIGNALING_SR;
343 }
344 return tunnelType;
345 }
346
347
348 private static Class<? extends TunnelType> te2YangTunnelType(TeTunnel.Type type) {
349 Class<? extends TunnelType> tunnelType = null;
350 switch (type) {
351
352 case P2P:
353 tunnelType = TunnelP2p.class;
354 break;
355 case P2MP:
356 tunnelType = TunnelP2Mp.class;
357 break;
358 case PATH_SIGNALING_RSVPTE:
359 tunnelType = PathSignalingRsvpte.class;
360
361 break;
362 case PATH_SIGNALING_SR:
363 tunnelType = PathSignalingSr.class;
364 break;
365 default:
366 log.error("Unknown te tunnel type {}", type.toString());
367 }
368 return tunnelType;
369 }
370
371 private static LspProtectionType
372 yang2TeProtectionType(Class<? extends LspProtType> protType) {
373 LspProtectionType type = null;
374 if (protType.isAssignableFrom(LspProt1Forn.class)) {
375 type = LspProtectionType.LSP_PROT_1_FOR_N;
376 } else if (protType.isAssignableFrom(LspProtBidir1To1.class)) {
377 type = LspProtectionType.LSP_PROT_BIDIR_1_TO_1;
378 } else if (protType.isAssignableFrom(LspProtReroute.class)) {
379 type = LspProtectionType.LSP_PROT_REROUTE;
380 } else if (protType.isAssignableFrom(LspProtRerouteExtra.class)) {
381 type = LspProtectionType.LSP_PROT_REROUTE_EXTRA;
382 } else if (protType.isAssignableFrom(LspProtUnidir1To1.class)) {
383 type = LspProtectionType.LSP_PROT_UNIDIR_1_TO_1;
384 } else if (protType.isAssignableFrom(LspProtUnprotected.class)) {
385 type = LspProtectionType.LSP_PROT_UNPROTECTED;
386 }
387 return type;
388 }
389
390 private static Class<? extends LspProtType>
391 te2YangProtectionType(LspProtectionType protType) {
392 Class<? extends LspProtType> type = null;
393 switch (protType) {
394
395 case LSP_PROT_UNPROTECTED:
396 type = LspProtUnprotected.class;
397 break;
398 case LSP_PROT_REROUTE:
399 type = LspProtReroute.class;
400 break;
401 case LSP_PROT_REROUTE_EXTRA:
402 type = LspProtRerouteExtra.class;
403 break;
404 case LSP_PROT_UNIDIR_1_TO_1:
405 type = LspProtUnidir1To1.class;
406 break;
407 case LSP_PROT_BIDIR_1_TO_1:
408 type = LspProtBidir1To1.class;
409 break;
410 case LSP_PROT_1_FOR_N:
411 type = LspProt1Forn.class;
412 break;
413 default:
414 log.error("Unknown te tunnel type {}", protType.toString());
415 }
416 return type;
417 }
418
419 private static TeTunnel.State
420 yang2TeStateType(Class<? extends org.onosproject.yang.gen.v1.urn.ietf.
421 params.xml.ns.yang.ietf.te.types.
422 rev20160705.ietftetypes.StateType> stateType) {
423 TeTunnel.State teStateType = null;
424 if (stateType.isAssignableFrom(StateUp.class)) {
425 teStateType = TeTunnel.State.UP;
426 } else if (stateType.isAssignableFrom(StateDown.class)) {
427 teStateType = TeTunnel.State.DOWN;
428 }
429 return teStateType;
430 }
431
432 private static Class<? extends org.onosproject.yang.gen.v1.urn.ietf.params.
433 xml.ns.yang.ietf.te.types.rev20160705.ietftetypes.StateType>
434 te2YangStateType(TeTunnel.State stateType) {
435 Class<? extends org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.
436 ietf.te.types.rev20160705.ietftetypes.StateType> state = null;
437
438 switch (stateType) {
439
440 case DOWN:
441 state = StateDown.class;
442 break;
443 case UP:
444 state = StateUp.class;
445 break;
446 default:
447 log.error("Unknown te tunnel type {}", stateType.toString());
448
449 }
450 return state;
451 }
452}