Consolidate ConnectivityUtils
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / topology / TopologyUtils.java
1 /*
2  * Copyright © 2021 Nokia, Inc. 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 package org.opendaylight.transportpce.tapi.topology;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.nio.charset.Charset;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.NoSuchElementException;
18 import java.util.Optional;
19 import java.util.Set;
20 import java.util.UUID;
21 import java.util.concurrent.ExecutionException;
22 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.transportpce.common.InstanceIdentifiers;
27 import org.opendaylight.transportpce.common.NetworkUtils;
28 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
29 import org.opendaylight.transportpce.tapi.TapiStringConstants;
30 import org.opendaylight.transportpce.tapi.impl.TapiProvider;
31 import org.opendaylight.transportpce.tapi.utils.TapiLink;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.Mapping;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.MappingKey;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.network.Nodes;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.network.NodesKey;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.Link1;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.TerminationPoint1;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmLinkType;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmNodeType;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmTpType;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
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.onf.otcc.yang.tapi.common.rev221121.Context;
48 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.LayerProtocolName;
49 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
50 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.Name;
51 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.NameBuilder;
52 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePoint;
53 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePointKey;
54 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext;
55 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePoint;
56 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePointBuilder;
57 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePointKey;
58 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.LinkKey;
59 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node;
60 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.NodeBuilder;
61 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.NodeKey;
62 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology;
63 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyBuilder;
64 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyKey;
65 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 public final class TopologyUtils {
70
71     private final NetworkTransactionService networkTransactionService;
72     private final DataBroker dataBroker;
73     private static final Logger LOG = LoggerFactory.getLogger(TopologyUtils.class);
74     private Map<ServiceInterfacePointKey, ServiceInterfacePoint> tapiSips;
75     private final TapiLink tapiLink;
76     private static final String TOPOLOGICAL_MODE = TapiProvider.TOPOLOGICAL_MODE;
77     public static final String NOOPMODEDECLARED = "No operational mode declared in Topo for Tp {}, assumes by default ";
78
79     public TopologyUtils(
80             NetworkTransactionService networkTransactionService, DataBroker dataBroker, TapiLink tapiLink) {
81         this.networkTransactionService = networkTransactionService;
82         this.dataBroker = dataBroker;
83         this.tapiSips = new HashMap<>();
84         this.tapiLink = tapiLink;
85         // TODO: Initially set topological mode to Full. Shall be set through the setter at controller initialization
86     }
87
88     public Network readTopology(InstanceIdentifier<Network> networkIID) throws TapiTopologyException {
89         ListenableFuture<Optional<Network>> topologyFuture =
90                 this.networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, networkIID);
91         try {
92             return topologyFuture.get().orElseThrow();
93         } catch (InterruptedException e) {
94             Thread.currentThread().interrupt();
95             throw new TapiTopologyException(
96                 "Unable to get from mdsal topology: " + networkIID.firstKeyOf(Network.class).getNetworkId().getValue(),
97                 e);
98         } catch (ExecutionException e) {
99             throw new TapiTopologyException(
100                 "Unable to get from mdsal topology: " + networkIID.firstKeyOf(Network.class).getNetworkId().getValue(),
101                 e);
102         } catch (NoSuchElementException e) {
103             return null;
104         }
105     }
106
107     public List<String> readTopologyName(Uuid topoUuid) throws TapiTopologyException {
108         Topology topology = null;
109         InstanceIdentifier<Topology> topoIID = InstanceIdentifier.builder(Context.class)
110             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1.class)
111             .child(TopologyContext.class)
112             .child(Topology.class, new TopologyKey(topoUuid))
113             .build();
114         ListenableFuture<Optional<Topology>> topologyFuture =
115                 this.networkTransactionService.read(LogicalDatastoreType.OPERATIONAL, topoIID);
116         try {
117             topology = topologyFuture.get().orElseThrow();
118         } catch (InterruptedException e) {
119             Thread.currentThread().interrupt();
120             throw new TapiTopologyException(
121                 "Unable to get from mdsal topology: " + topoIID.firstKeyOf(Topology.class).getUuid().getValue(), e);
122         } catch (ExecutionException e) {
123             throw new TapiTopologyException(
124                 "Unable to get from mdsal topology: " + topoIID.firstKeyOf(Topology.class).getUuid().getValue(), e);
125         } catch (NoSuchElementException e) {
126             return null;
127         }
128         List<String> nameList = new ArrayList<>();
129         for (Name value : topology.getName().values()) {
130             nameList.add(value.getValue());
131         }
132         LOG.debug("Topology nameList {} = ", nameList);
133         return nameList;
134     }
135
136     public Topology createOtnTopology() throws TapiTopologyException {
137         // read openroadm-topology
138         Network openroadmTopo = readTopology(InstanceIdentifiers.OVERLAY_NETWORK_II);
139         String topoType = TOPOLOGICAL_MODE.equals("Full") ? TapiStringConstants.T0_FULL_MULTILAYER
140             : TapiStringConstants.T0_TAPI_MULTILAYER;
141         LOG.info("TOPOUTILS, createOtnTopology, the TOPOLOGICAL_MODE is {} ",topoType);
142         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(topoType.getBytes(Charset.forName("UTF-8"))).toString());
143         Name name = new NameBuilder().setValue(topoType).setValueName("TAPI Topology Name").build();
144         var topoBdr = new TopologyBuilder()
145                 .setName(Map.of(name.key(), name))
146                 .setUuid(topoUuid)
147                 .setLayerProtocolName(Set.of(
148                         LayerProtocolName.PHOTONICMEDIA, LayerProtocolName.ODU,
149                         LayerProtocolName.DSR, LayerProtocolName.DIGITALOTN));
150         if (openroadmTopo == null) {
151             return topoBdr.build();
152         }
153         List<Link> linkList = openroadmTopo.augmentation(Network1.class) == null ? new ArrayList<>()
154             : new ArrayList<>(openroadmTopo.augmentation(Network1.class).getLink().values());
155         List<Link> xponderOutLinkList = new ArrayList<>();
156         List<Link> xponderInLinkList = new ArrayList<>();
157         for (Link lk : linkList) {
158             switch (lk.augmentation(Link1.class).getLinkType()) {
159                 case XPONDEROUTPUT:
160                     xponderOutLinkList.add(lk);
161                     break;
162                 case XPONDERINPUT:
163                     xponderInLinkList.add(lk);
164                     break;
165                 default:
166                     break;
167             }
168         }
169         // read otn-topology
170         Network otnTopo = readTopology(InstanceIdentifiers.OTN_NETWORK_II);
171         Map<NodeId,
172             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node>
173                     otnNodeMap =
174             otnTopo.nonnullNode().values().stream()
175                 .collect(Collectors.toMap(
176                         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226
177                             .networks.network.Node::getNodeId, node -> node));
178         Map<String, List<String>> networkPortMap = new HashMap<>();
179         for (var entry : otnNodeMap.entrySet()) {
180             var entVal = entry.getValue();
181             String portMappingNodeId = entVal.getSupportingNode().values().stream()
182                 .filter(sn -> sn.getNetworkRef().getValue().equals(NetworkUtils.UNDERLAY_NETWORK_ID))
183                 .findFirst()
184                 .orElseThrow().getNodeRef().getValue();
185             List<String> networkPortList = new ArrayList<>();
186             var entKeyVal = entry.getKey().getValue();
187             for (TerminationPoint tp: entVal.augmentation(Node1.class).getTerminationPoint().values()) {
188                 // TODO -> why are we checking with respect to XPDR links?? Is there a real purpose on doing that?
189                 if (tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERNETWORK)
190                         && checkTp(entKeyVal, portMappingNodeId, tp, xponderOutLinkList, xponderInLinkList)) {
191                     networkPortList.add(tp.getTpId().getValue());
192                 }
193             }
194             if (!networkPortList.isEmpty()) {
195                 networkPortMap.put(entKeyVal, networkPortList);
196             }
197         }
198         Map<NodeKey, org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node>
199             tapiNodeList = new HashMap<>();
200         Map<LinkKey, org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Link>
201             tapiLinkList = new HashMap<>();
202         ConvertORTopoToTapiFullTopo tapiFullFactory = new ConvertORTopoToTapiFullTopo(topoUuid, this.tapiLink);
203         ConvertORToTapiTopology tapiFactory = new ConvertORToTapiTopology(topoUuid);
204         for (var entry : networkPortMap.entrySet()) {
205             tapiFactory.convertNode(otnNodeMap.get(new NodeId(entry.getKey())), entry.getValue());
206             this.tapiSips.putAll(tapiFactory.getTapiSips());
207             tapiFullFactory.setTapiNodes(tapiFactory.getTapiNodes());
208             tapiFullFactory.setTapiSips(tapiFactory.getTapiSips());
209             tapiNodeList.putAll(tapiFactory.getTapiNodes());
210             tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
211         }
212         // roadm infrastructure not abstracted
213         // read openroadm-network
214         Network openroadmNet = readTopology(InstanceIdentifiers.UNDERLAY_NETWORK_II);
215         List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226
216                 .networks.network.Node> rdmList =
217             openroadmNet == null ? new ArrayList<>()
218                 : openroadmNet.nonnullNode().values().stream()
219                     .filter(nt -> nt
220                         .augmentation(
221                             org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.Node1.class)
222                         .getNodeType()
223                         .equals(OpenroadmNodeType.ROADM))
224                     .collect(Collectors.toList());
225         if (rdmList.isEmpty()) {
226             LOG.warn("No roadm nodes exist in the network");
227         } else {
228             // map roadm nodes
229             if (TOPOLOGICAL_MODE.equals("Full")) {
230                 for (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226
231                         .networks.network.Node roadm : rdmList) {
232                     tapiFullFactory.convertRoadmNode(roadm, openroadmTopo, "Full");
233                     this.tapiSips.putAll(tapiFullFactory.getTapiSips());
234                     tapiNodeList.putAll(tapiFullFactory.getTapiNodes());
235                 }
236                 tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
237                 // map roadm to roadm link
238                 List<Link> rdmTordmLinkList = linkList.stream()
239                     .filter(lk -> lk.augmentation(Link1.class).getLinkType()
240                         .equals(OpenroadmLinkType.ROADMTOROADM))
241                     .collect(Collectors.toList());
242                 tapiFullFactory.convertRdmToRdmLinks(rdmTordmLinkList);
243             } else {
244                 tapiFullFactory.convertRoadmNode(null, openroadmTopo, "Abstracted");
245                 this.tapiSips.putAll(tapiFullFactory.getTapiSips());
246                 tapiNodeList.putAll(tapiFullFactory.getTapiNodes());
247                 tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
248             }
249         }
250         // map xpdr_input to roadm and xpdr_output to roadm links.
251         xponderInLinkList.addAll(xponderOutLinkList);
252         tapiFullFactory.convertXpdrToRdmLinks(xponderInLinkList);
253         tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
254         // Retrieve created sips map in TapiFactory when mapping all the nodes
255         this.tapiSips.putAll(tapiFullFactory.getTapiSips());
256         return topoBdr.setNode(tapiNodeList).setLink(tapiLinkList).build();
257     }
258
259     public boolean checkTp(
260                 String nodeIdTopo, String nodeIdPortMap, TerminationPoint tp, List<Link> xpdOut, List<Link> xpdIn) {
261         LOG.info("Inside Checktp for node {}-{}", nodeIdTopo, nodeIdPortMap);
262         String networkLcp = tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERCLIENT)
263             ? tp.augmentation(
264                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.TerminationPoint1.class)
265                 .getAssociatedConnectionMapTp().iterator().next().getValue()
266             : tp.getTpId().getValue();
267         LOG.info("Network LCP associated = {}", networkLcp);
268         @NonNull
269         FluentFuture<Optional<Mapping>> mappingOpt = this.dataBroker.newReadOnlyTransaction().read(
270                 LogicalDatastoreType.CONFIGURATION,
271                 InstanceIdentifier.create(
272                     org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.Network.class)
273                 .child(Nodes.class, new NodesKey(nodeIdPortMap))
274                 .child(Mapping.class, new MappingKey(networkLcp)));
275         if (!mappingOpt.isDone()) {
276             LOG.error("Impossible to get mapping of associated network port {} of tp {}",
277                 networkLcp, tp.getTpId().getValue());
278             return false;
279         }
280         Mapping mapping = null;
281         try {
282             mapping = mappingOpt.get().orElseThrow();
283         } catch (InterruptedException | ExecutionException e) {
284             LOG.error("Error getting mapping for {}", networkLcp, e);
285             return false;
286         }
287         LOG.info("Mapping found = {}", mapping);
288         switch (mapping.getPortDirection()) {
289             // TODO -> remove the part of counting only if the Network LCP is part of a Link.
290             //  We want to have all OTN nodes in the TAPI topology
291             case "bidirectional":
292                 return true;
293             case "tx":
294             case "rx":
295                 LOG.info("PartnerLCP = {}", mapping.getPartnerLcp());
296                 return true;
297             default:
298                 LOG.error("Invalid port direction for {}", networkLcp);
299                 return false;
300         }
301     }
302
303     public org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121
304             .get.topology.details.output.Topology transformTopology(Topology topology) {
305         var topologyBuilder = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121
306                 .get.topology.details.output.TopologyBuilder()
307             .setUuid(topology.getUuid())
308             .setName(topology.getName())
309             .setLayerProtocolName(topology.getLayerProtocolName())
310             .setLink(topology.getLink());
311         if (topology.nonnullNode().isEmpty()) {
312             return topologyBuilder.build();
313         }
314         Map<NodeKey, Node> mapNode = new HashMap<>();
315         for (Node node: topology.nonnullNode().values()) {
316             Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap = new HashMap<>();
317             for (OwnedNodeEdgePoint onep: node.nonnullOwnedNodeEdgePoint().values()) {
318                 OwnedNodeEdgePoint newOnep = new OwnedNodeEdgePointBuilder()
319                         .setUuid(onep.getUuid())
320                         .setLayerProtocolName(onep.getLayerProtocolName())
321                         .setName(onep.getName())
322                         .setSupportedCepLayerProtocolQualifierInstances(onep
323                             .getSupportedCepLayerProtocolQualifierInstances())
324                         .setAdministrativeState(onep.getAdministrativeState())
325                         .setOperationalState(onep.getOperationalState())
326                         .setLifecycleState(onep.getLifecycleState())
327 //                            .setTerminationDirection(onep.getTerminationDirection())
328 //                            .setTerminationState(onep.getTerminationState())
329                         .setDirection(onep.getDirection())
330                         .setSupportedPayloadStructure(onep.getSupportedPayloadStructure())
331                         .setAvailablePayloadStructure(onep.getAvailablePayloadStructure())
332                         .setLinkPortRole(onep.getLinkPortRole())
333                         .setMappedServiceInterfacePoint(onep.nonnullMappedServiceInterfacePoint())
334                         .build();
335                 onepMap.put(newOnep.key(), newOnep);
336             }
337             Node newNode = new NodeBuilder()
338                     .setUuid(node.getUuid())
339                     .setName(node.getName())
340                     .setOperationalState(node.getOperationalState())
341                     .setAdministrativeState(node.getAdministrativeState())
342                     .setLifecycleState(node.getLifecycleState())
343                     .setLayerProtocolName(node.getLayerProtocolName())
344                     .setNodeRuleGroup(node.getNodeRuleGroup())
345                     .setInterRuleGroup(node.getInterRuleGroup())
346                     .setOwnedNodeEdgePoint(onepMap)
347                     .build();
348             mapNode.put(newNode.key(), newNode);
349         }
350         return topologyBuilder.setNode(mapNode).build();
351     }
352
353     public Map<ServiceInterfacePointKey, ServiceInterfacePoint> getSipMap() {
354         return tapiSips;
355     }
356
357 }