Merge changes I5958bef2,Icbca02a2
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / Rdm2XpdrLink.java
1 /*
2  * Copyright © 2016 AT&T and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.transportpce.networkmodel;
10
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.util.concurrent.FluentFuture;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.ReadTransaction;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.transportpce.common.NetworkUtils;
22 import org.opendaylight.transportpce.networkmodel.util.LinkIdUtil;
23 import org.opendaylight.transportpce.networkmodel.util.TopologyUtils;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev220630.links.input.grouping.LinksInput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.Link1Builder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev230526.TerminationPoint1;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev230526.TerminationPoint1Builder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev230526.networks.network.node.termination.point.XpdrNetworkAttributesBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmLinkType;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.NodeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1Builder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1Builder;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.TpId;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.LinkBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.node.TerminationPoint;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.node.TerminationPointBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.node.TerminationPointKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54 final class Rdm2XpdrLink {
55
56     private static final Logger LOG = LoggerFactory.getLogger(Rdm2XpdrLink.class);
57
58     public static boolean createXpdrRdmLinks(LinksInput linksInput, DataBroker dataBroker) {
59         String srcNode =
60             new StringBuilder(linksInput.getXpdrNode()).append("-XPDR").append(linksInput.getXpdrNum()).toString();
61         String srcTp = new StringBuilder("XPDR").append(linksInput.getXpdrNum()).append("-NETWORK")
62             .append(linksInput.getNetworkNum()).toString();
63         String destNode =
64             new StringBuilder(linksInput.getRdmNode()).append("-SRG").append(linksInput.getSrgNum()).toString();
65         String destTp = linksInput.getTerminationPointNum();
66         // update tail-equipment-id for tp of link
67         TerminationPoint xpdrTp = getTpofNode(srcNode, srcTp, dataBroker);
68         TerminationPoint rdmTp = getTpofNode(destNode, destTp, dataBroker);
69
70         NetworkBuilder networkBldr = createNetworkBuilder(srcNode, srcTp, destNode, destTp, false, xpdrTp, rdmTp);
71         if (networkBldr == null) {
72             return false;
73         }
74         Network network = networkBldr.build();
75         InstanceIdentifier.Builder<Network> nwIID = InstanceIdentifier.builder(Networks.class)
76             .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID)));
77         WriteTransaction wrtx = dataBroker.newWriteOnlyTransaction();
78         wrtx.merge(LogicalDatastoreType.CONFIGURATION, nwIID.build(), network);
79
80         FluentFuture<? extends @NonNull CommitInfo> commit = wrtx.commit();
81
82         try {
83             commit.get();
84             LOG.info("Post successful");
85             return true;
86         } catch (InterruptedException | ExecutionException e) {
87             LOG.warn("Failed to create Xponder to Roadm link in the Topo layer ");
88             return false;
89         }
90     }
91
92     public static boolean createRdmXpdrLinks(LinksInput linksInput, DataBroker dataBroker) {
93         String srcNode =
94             new StringBuilder(linksInput.getRdmNode()).append("-SRG").append(linksInput.getSrgNum()).toString();
95         String srcTp = linksInput.getTerminationPointNum();
96         String destNode =
97             new StringBuilder(linksInput.getXpdrNode()).append("-XPDR").append(linksInput.getXpdrNum()).toString();
98         String destTp = new StringBuilder("XPDR").append(linksInput.getXpdrNum()).append("-NETWORK")
99             .append(linksInput.getNetworkNum()).toString();
100         TerminationPoint xpdrTp = getTpofNode(destNode, destTp, dataBroker);
101         TerminationPoint rdmTp = getTpofNode(srcNode, srcTp, dataBroker);
102
103         NetworkBuilder networkBldr = createNetworkBuilder(srcNode, srcTp, destNode, destTp, true, xpdrTp, rdmTp);
104         if (networkBldr == null) {
105             return false;
106         }
107         Network network = networkBldr.build();
108         InstanceIdentifier.Builder<Network> nwIID =
109             InstanceIdentifier.builder(Networks.class).child(Network.class,
110                 new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID)));
111         WriteTransaction wrtx = dataBroker.newWriteOnlyTransaction();
112         wrtx.merge(LogicalDatastoreType.CONFIGURATION, nwIID.build(), network);
113         FluentFuture<? extends @NonNull CommitInfo> commit = wrtx.commit();
114         try {
115             commit.get();
116             LOG.info("Post successful");
117             return true;
118
119         } catch (InterruptedException | ExecutionException e) {
120             LOG.warn("Failed to create Xponder to Roadm link in the Topo layer ");
121             return false;
122         }
123     }
124
125     private static NetworkBuilder createNetworkBuilder(String srcNode, String srcTp, String destNode, String destTp,
126         boolean isXponderInput, TerminationPoint xpdrTp, TerminationPoint rdmTp) {
127         if (xpdrTp == null || rdmTp == null) {
128             return null;
129         }
130         //update tp of nodes
131         TerminationPointBuilder xpdrTpBldr = new TerminationPointBuilder(xpdrTp);
132         if (xpdrTpBldr.augmentation(TerminationPoint1.class) != null) {
133             LOG.warn("Rewritting tail-equipment-id {} on tp {} of node {}", xpdrTpBldr
134                 .augmentation(TerminationPoint1.class).getXpdrNetworkAttributes().getTailEquipmentId(), srcTp, srcNode);
135         }
136         TerminationPoint1Builder tp1Bldr = new TerminationPoint1Builder();
137         if (isXponderInput) {
138             tp1Bldr.setXpdrNetworkAttributes(new XpdrNetworkAttributesBuilder()
139                 .setTailEquipmentId(srcNode + "--" + srcTp).build());
140         } else {
141             tp1Bldr.setXpdrNetworkAttributes(new XpdrNetworkAttributesBuilder()
142                 .setTailEquipmentId(destNode + "--" + destTp).build());
143         }
144         xpdrTpBldr.addAugmentation(tp1Bldr.build());
145         TerminationPoint newXpdrTp = xpdrTpBldr.build();
146         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1 node1 =
147             new Node1Builder().setTerminationPoint(ImmutableMap.of(newXpdrTp.key(),newXpdrTp)).build();
148         NodeBuilder nodeBldr = new NodeBuilder().addAugmentation(node1);
149         if (isXponderInput) {
150             nodeBldr.setNodeId(new NodeId(destNode));
151         } else {
152             nodeBldr.setNodeId(new NodeId(srcNode));
153         }
154
155         Link1Builder lnk2bldr
156             = new Link1Builder()
157                 .setLinkType(isXponderInput ? OpenroadmLinkType.XPONDERINPUT : OpenroadmLinkType.XPONDEROUTPUT)
158                 .setOppositeLink(LinkIdUtil.getOppositeLinkId(srcNode, srcTp, destNode, destTp));
159         // If both TPs of the Xpdr2Rdm link are inService --> link inService. Otherwise outOfService
160         if (State.InService.equals(xpdrTp.augmentation(org.opendaylight.yang.gen.v1.http
161                     .org.openroadm.common.network.rev230526.TerminationPoint1.class).getOperationalState())
162                 && State.InService.equals(rdmTp.augmentation(org.opendaylight.yang.gen.v1.http
163                     .org.openroadm.common.network.rev230526.TerminationPoint1.class).getOperationalState())) {
164             lnk2bldr.setOperationalState(State.InService).setAdministrativeState(AdminStates.InService);
165         } else {
166             lnk2bldr.setOperationalState(State.OutOfService).setAdministrativeState(AdminStates.OutOfService);
167         }
168         LinkBuilder linkBuilder = TopologyUtils.createLink(srcNode, destNode, srcTp, destTp, null)
169             .addAugmentation(lnk2bldr.build());
170
171         LOG.info("Creating Link with id {}", linkBuilder.getLinkId());
172         Link link = linkBuilder.build();
173         Network1Builder nwBldr1 = new Network1Builder().setLink(ImmutableMap.of(link.key(),link));
174
175         NetworkId nwId = new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID);
176         Node node = nodeBldr.build();
177         return new NetworkBuilder()
178             .setNetworkId(nwId)
179             .withKey(new NetworkKey(nwId))
180             .addAugmentation(nwBldr1.build())
181             .setNode(ImmutableMap.of(node.key(),node));
182     }
183
184     private static TerminationPoint getTpofNode(String srcNode, String srcTp, DataBroker dataBroker) {
185         InstanceIdentifier<TerminationPoint> iiTp = InstanceIdentifier.builder(Networks.class)
186             .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID)))
187             .child(Node.class, new NodeKey(new NodeId(srcNode)))
188             .augmentation(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226
189                 .Node1.class)
190             .child(TerminationPoint.class, new TerminationPointKey(new TpId(srcTp)))
191             .build();
192         @NonNull
193         ReadTransaction readTransaction = dataBroker.newReadOnlyTransaction();
194         @NonNull
195         FluentFuture<Optional<TerminationPoint>> tpFf = readTransaction.read(LogicalDatastoreType.CONFIGURATION, iiTp);
196         if (tpFf.isDone()) {
197             try {
198                 Optional<TerminationPoint> tpOpt;
199                 tpOpt = tpFf.get();
200                 if (tpOpt.isPresent()) {
201                     return tpOpt.orElseThrow();
202                 }
203             } catch (InterruptedException | ExecutionException e) {
204                 LOG.error("Impossible to get tp-id {} of node {} from {}", srcTp, srcNode,
205                     NetworkUtils.OVERLAY_NETWORK_ID, e);
206             }
207         }
208         return null;
209     }
210
211     private Rdm2XpdrLink() {
212     }
213
214 }