Abort power setup if setting gainloss fails
[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.utils.TapiLink;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.Mapping;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.MappingKey;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.network.Nodes;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.network.NodesKey;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.Link1;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.TerminationPoint1;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmLinkType;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmNodeType;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmTpType;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1;
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.node.TerminationPoint;
46 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
47 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.LayerProtocolName;
48 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
49 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.Name;
50 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.NameBuilder;
51 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePoint;
52 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePointKey;
53 //import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.OwnedNodeEdgePoint1;
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 String topologicalMode;
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         this.topologicalMode = "Full";
87     }
88
89     public Network readTopology(InstanceIdentifier<Network> networkIID) throws TapiTopologyException {
90         ListenableFuture<Optional<Network>> topologyFuture =
91                 this.networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, networkIID);
92         try {
93             return topologyFuture.get().orElseThrow();
94         } catch (InterruptedException e) {
95             Thread.currentThread().interrupt();
96             throw new TapiTopologyException(
97                 "Unable to get from mdsal topology: " + networkIID.firstKeyOf(Network.class).getNetworkId().getValue(),
98                 e);
99         } catch (ExecutionException e) {
100             throw new TapiTopologyException(
101                 "Unable to get from mdsal topology: " + networkIID.firstKeyOf(Network.class).getNetworkId().getValue(),
102                 e);
103         } catch (NoSuchElementException e) {
104             return null;
105         }
106     }
107
108     public List<String> readTopologyName(Uuid topoUuid) throws TapiTopologyException {
109         Topology topology = null;
110         InstanceIdentifier<Topology> topoIID = InstanceIdentifier.builder(Context.class)
111             .augmentation(org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1.class)
112             .child(TopologyContext.class)
113             .child(Topology.class, new TopologyKey(topoUuid))
114             .build();
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(
122                 "Unable to get from mdsal topology: " + topoIID.firstKeyOf(Topology.class).getUuid().getValue(), e);
123         } catch (ExecutionException e) {
124             throw new TapiTopologyException(
125                 "Unable to get from mdsal topology: " + topoIID.firstKeyOf(Topology.class).getUuid().getValue(), e);
126         } catch (NoSuchElementException e) {
127             return null;
128         }
129         List<String> nameList = new ArrayList<>();
130         for (Name value : topology.getName().values()) {
131             nameList.add(value.getValue());
132         }
133         LOG.debug("Topology nameList {} = ", nameList);
134         return nameList;
135     }
136
137     public Topology createFullOtnTopology() throws TapiTopologyException {
138         // read openroadm-topology
139         Network openroadmTopo = readTopology(InstanceIdentifiers.OVERLAY_NETWORK_II);
140         String topoType = this.topologicalMode.equals("Full") ? TapiStringConstants.T0_FULL_MULTILAYER
141             : TapiStringConstants.T0_TAPI_MULTILAYER;
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 (this.topologicalMode.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                     tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
236                     tapiFullFactory.convertRdmToRdmLinks(
237                     // map roadm to roadm link
238                         linkList.stream()
239                             .filter(lk -> lk.augmentation(Link1.class).getLinkType()
240                                 .equals(OpenroadmLinkType.ROADMTOROADM))
241                             .collect(Collectors.toList()));
242                     tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
243                 }
244             } else {
245                 tapiFullFactory.convertRoadmNode(null, openroadmTopo, "Abstracted");
246                 this.tapiSips.putAll(tapiFullFactory.getTapiSips());
247                 tapiNodeList.putAll(tapiFullFactory.getTapiNodes());
248                 tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
249             }
250         }
251         // map xpdr_input to roadm and xpdr_output to roadm links.
252         xponderInLinkList.addAll(xponderOutLinkList);
253         tapiFullFactory.convertXpdrToRdmLinks(xponderInLinkList);
254         tapiLinkList.putAll(tapiFullFactory.getTapiLinks());
255         // Retrieve created sips map in TapiFactory when mapping all the nodes
256         this.tapiSips.putAll(tapiFullFactory.getTapiSips());
257         return topoBdr.setNode(tapiNodeList).setLink(tapiLinkList).build();
258     }
259
260     public boolean checkTp(
261                 String nodeIdTopo, String nodeIdPortMap, TerminationPoint tp, List<Link> xpdOut, List<Link> xpdIn) {
262         LOG.info("Inside Checktp for node {}-{}", nodeIdTopo, nodeIdPortMap);
263         String networkLcp = tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERCLIENT)
264             ? tp.augmentation(
265                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.TerminationPoint1.class)
266                 .getAssociatedConnectionMapTp().iterator().next().getValue()
267             : tp.getTpId().getValue();
268         LOG.info("Network LCP associated = {}", networkLcp);
269         @NonNull
270         FluentFuture<Optional<Mapping>> mappingOpt = this.dataBroker.newReadOnlyTransaction().read(
271                 LogicalDatastoreType.CONFIGURATION,
272                 InstanceIdentifier.create(
273                     org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.Network.class)
274                 .child(Nodes.class, new NodesKey(nodeIdPortMap))
275                 .child(Mapping.class, new MappingKey(networkLcp)));
276         if (!mappingOpt.isDone()) {
277             LOG.error("Impossible to get mapping of associated network port {} of tp {}",
278                 networkLcp, tp.getTpId().getValue());
279             return false;
280         }
281         Mapping mapping = null;
282         try {
283             mapping = mappingOpt.get().orElseThrow();
284         } catch (InterruptedException | ExecutionException e) {
285             LOG.error("Error getting mapping for {}", networkLcp, e);
286             return false;
287         }
288         LOG.info("Mapping found = {}", mapping);
289         switch (mapping.getPortDirection()) {
290             // TODO -> remove the part of counting only if the Network LCP is part of a Link.
291             //  We want to have all OTN nodes in the TAPI topology
292             case "bidirectional":
293                 return true;
294             case "tx":
295             case "rx":
296                 LOG.info("PartnerLCP = {}", mapping.getPartnerLcp());
297                 return true;
298             default:
299                 LOG.error("Invalid port direction for {}", networkLcp);
300                 return false;
301         }
302     }
303
304     public org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121
305             .get.topology.details.output.Topology transformTopology(Topology topology) {
306         var topologyBuilder = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121
307                 .get.topology.details.output.TopologyBuilder()
308             .setUuid(topology.getUuid())
309             .setName(topology.getName())
310             .setLayerProtocolName(topology.getLayerProtocolName())
311             .setLink(topology.getLink());
312         if (topology.nonnullNode().isEmpty()) {
313             return topologyBuilder.build();
314         }
315         Map<NodeKey, Node> mapNode = new HashMap<>();
316         for (Node node: topology.nonnullNode().values()) {
317             Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap = new HashMap<>();
318             for (OwnedNodeEdgePoint onep: node.nonnullOwnedNodeEdgePoint().values()) {
319 //                    OwnedNodeEdgePoint1 onep1 = onep.augmentation(OwnedNodeEdgePoint1.class);
320 //                    if (onep1 == null) {
321 //                        onepMap.put(onep.key(), onep);
322 //                        continue;
323 //                    }
324                 OwnedNodeEdgePoint newOnep = new OwnedNodeEdgePointBuilder()
325                         .setUuid(onep.getUuid())
326                         .setLayerProtocolName(onep.getLayerProtocolName())
327                         .setName(onep.getName())
328                         .setSupportedCepLayerProtocolQualifierInstances(onep
329                             .getSupportedCepLayerProtocolQualifierInstances())
330                         .setAdministrativeState(onep.getAdministrativeState())
331                         .setOperationalState(onep.getOperationalState())
332                         .setLifecycleState(onep.getLifecycleState())
333 //                            .setTerminationDirection(onep.getTerminationDirection())
334 //                            .setTerminationState(onep.getTerminationState())
335                         .setDirection(onep.getDirection())
336                         .setSupportedPayloadStructure(onep.getSupportedPayloadStructure())
337                         .setAvailablePayloadStructure(onep.getAvailablePayloadStructure())
338                         .setLinkPortRole(onep.getLinkPortRole())
339                         .setMappedServiceInterfacePoint(onep.nonnullMappedServiceInterfacePoint())
340                         .build();
341                 onepMap.put(newOnep.key(), newOnep);
342             }
343             Node newNode = new NodeBuilder()
344                     .setUuid(node.getUuid())
345                     .setName(node.getName())
346                     .setOperationalState(node.getOperationalState())
347                     .setAdministrativeState(node.getAdministrativeState())
348                     .setLifecycleState(node.getLifecycleState())
349                     .setLayerProtocolName(node.getLayerProtocolName())
350                     .setNodeRuleGroup(node.getNodeRuleGroup())
351                     .setInterRuleGroup(node.getInterRuleGroup())
352                     .setOwnedNodeEdgePoint(onepMap)
353                     .build();
354             mapNode.put(newNode.key(), newNode);
355         }
356         return topologyBuilder.setNode(mapNode).build();
357     }
358
359     public Map<ServiceInterfacePointKey, ServiceInterfacePoint> getSipMap() {
360         return tapiSips;
361     }
362
363     public void setTopologicalMode(String topoMode) {
364         this.topologicalMode = topoMode;
365     }
366
367     public String getTopologicalMode() {
368         return topologicalMode;
369     }
370
371 }