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