2 * Copyright © 2019 AT&T and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.networkmodel.util;
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.TimeUnit;
12 import java.util.concurrent.TimeoutException;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.transportpce.common.NetworkUtils;
15 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Link1;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Link1Builder;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.LinkId;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.LinkBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.LinkKey;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.link.DestinationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.link.SourceBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public final class TopologyUtils {
36 private static final Logger LOG = LoggerFactory.getLogger(TopologyUtils.class);
38 private TopologyUtils() {
41 // This method returns the linkBuilder object for given source and destination
42 public static LinkBuilder createLink(String srcNode, String dstNode, String srcTp, String destTp,
45 // Create Destination for link
46 DestinationBuilder dstNodeBldr = new DestinationBuilder()
48 .setDestNode(new NodeId(dstNode));
50 // Create Source for the link
51 SourceBuilder srcNodeBldr = new SourceBuilder()
52 .setSourceNode(new NodeId(srcNode))
56 LinkId oppositeLinkId;
57 if (otnPrefix == null) {
58 linkId = LinkIdUtil.buildLinkId(srcNode, srcTp, dstNode, destTp);
59 oppositeLinkId = LinkIdUtil.buildLinkId(dstNode, destTp, srcNode, srcTp);
61 linkId = LinkIdUtil.buildOtnLinkId(srcNode, srcTp, dstNode, destTp, otnPrefix);
62 oppositeLinkId = LinkIdUtil.buildOtnLinkId(dstNode, destTp, srcNode, srcTp, otnPrefix);
65 Link1 lnk1 = new Link1Builder().setOppositeLink(oppositeLinkId).build();
67 // set link builder attribute
68 LinkBuilder lnkBldr = new LinkBuilder()
69 .setDestination(dstNodeBldr.build())
70 .setSource(srcNodeBldr.build())
72 .withKey(new LinkKey(linkId))
73 .addAugmentation(lnk1);
77 // This method returns the linkBuilder object for given source and destination
78 public static boolean deleteLink(String srcNode, String dstNode, String srcTp, String destTp,
79 NetworkTransactionService networkTransactionService) {
80 LOG.info("deleting link for {}-{}", srcNode, dstNode);
81 LinkId linkId = LinkIdUtil.buildLinkId(srcNode, srcTp, dstNode, destTp);
82 if (deleteLinkLinkId(linkId, networkTransactionService)) {
83 LOG.debug("Link Id {} updated to have admin state down", linkId);
86 LOG.debug("Link Id not found for Source {} and Dest {}", srcNode, dstNode);
91 // This method returns the linkBuilder object for given source and destination
92 public static boolean deleteLinkLinkId(LinkId linkId , NetworkTransactionService networkTransactionService) {
93 LOG.info("deleting link for LinkId: {}", linkId.getValue());
95 InstanceIdentifier.InstanceIdentifierBuilder<Link> linkIID = InstanceIdentifier.builder(Networks.class)
96 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID)))
97 .augmentation(Network1.class).child(Link.class, new LinkKey(linkId));
98 java.util.Optional<Link> link =
99 networkTransactionService.read(LogicalDatastoreType.CONFIGURATION,linkIID.build()).get();
100 if (link.isPresent()) {
101 LinkBuilder linkBuilder = new LinkBuilder(link.get());
102 Link1Builder link1Builder = new Link1Builder(linkBuilder.augmentation(Link1.class));
103 linkBuilder.removeAugmentation(Link1.class);
104 linkBuilder.addAugmentation(link1Builder.build());
105 networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, linkIID.build(),
106 linkBuilder.build());
107 networkTransactionService.commit().get(1, TimeUnit.SECONDS);
110 LOG.error("No link found for given LinkId: {}",
115 } catch (InterruptedException | ExecutionException | TimeoutException e) {
116 LOG.error("Error deleting link {}", linkId.getValue(), e);