Minor changes in the topology extraction for GNPy
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / ExtractTopoDataStoreImpl.java
1 /*
2  * Copyright © 2018 Orange, 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
9 package org.opendaylight.transportpce.pce.gnpy;
10
11 import java.math.BigDecimal;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.concurrent.ExecutionException;
17 import java.util.stream.IntStream;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.transportpce.common.NetworkUtils;
24 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.Coordinate;
25 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.Km;
26 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.edfa.params.Operational;
27 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.edfa.params.OperationalBuilder;
28 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.Edfa;
29 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.EdfaBuilder;
30 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.FiberRoadmBuilder;
31 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.Transceiver;
32 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.TransceiverBuilder;
33 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.fiberroadm.Params;
34 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.fiberroadm.ParamsBuilder;
35 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.fiberroadm.params.fiberroadm.Fiber;
36 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.fiberroadm.params.fiberroadm.FiberBuilder;
37 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.fiberroadm.params.fiberroadm.Roadm;
38 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.element.type.choice.element.type.fiberroadm.params.fiberroadm.RoadmBuilder;
39 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.location.attributes.Location;
40 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.location.attributes.LocationBuilder;
41 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Connections;
42 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.ConnectionsBuilder;
43 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.Elements;
44 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.ElementsBuilder;
45 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.elements.Metadata;
46 import org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.topo.elements.MetadataBuilder;
47 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.RouteIncludeEro;
48 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.TeHopType;
49 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.TeNodeId;
50 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.TePathDisjointness;
51 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.TeTpId;
52 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.common.constraints_config.TeBandwidth;
53 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.common.constraints_config.TeBandwidthBuilder;
54 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.explicit.route.hop.Type;
55 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.explicit.route.hop.type.NumUnnumHopBuilder;
56 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.explicit.route.hop.type.num.unnum.hop.NumUnnumHop;
57 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.generic.path.constraints.PathConstraints;
58 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.generic.path.constraints.PathConstraintsBuilder;
59 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.gnpy.specific.parameters.EffectiveFreqSlot;
60 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.gnpy.specific.parameters.EffectiveFreqSlotBuilder;
61 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.path.route.objects.ExplicitRouteObjects;
62 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.path.route.objects.ExplicitRouteObjectsBuilder;
63 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.path.route.objects.explicit.route.objects.RouteObjectIncludeExclude;
64 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.path.route.objects.explicit.route.objects.RouteObjectIncludeExcludeBuilder;
65 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.service.PathRequest;
66 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.service.PathRequestBuilder;
67 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.synchronization.info.Synchronization;
68 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.synchronization.info.SynchronizationBuilder;
69 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.synchronization.info.synchronization.Svec;
70 import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.synchronization.info.synchronization.SvecBuilder;
71 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestInput;
72 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.amplified.link.attributes.AmplifiedLink;
73 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.amplified.link.attributes.amplified.link.section.element.section.element.Span;
74 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.amplified.link.attributes.amplified.link.section.element.section.element.ila.Ila;
75 import org.opendaylight.yang.gen.v1.http.org.openroadm.link.rev181130.span.attributes.LinkConcatenation;
76 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.rev181130.Node1;
77 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.Link1;
78 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130.networks.network.link.OMSAttributes;
79 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.AToZDirection;
80 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ZToADirection;
81 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
82 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToA;
83 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
84 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
85 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
86 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
87 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
88 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
89 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
90 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.node.SupportingNode;
91 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
92 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
93 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
96
97 /**
98  * Class to create the topology corresponding to GNPy requirements.
99  *
100  * @author Ahmed Triki ( ahmed.triki@orange.com )
101  *
102  */
103
104 public class ExtractTopoDataStoreImpl {
105     private static final Logger LOG = LoggerFactory.getLogger(ExtractTopoDataStoreImpl.class);
106     private final DataBroker dataBroker;
107     private List<Elements> elements = new ArrayList<>();
108     private List<Connections> connections = new ArrayList<>();
109     private List<PathRequest> pathRequest = new ArrayList<>();
110     private List<Synchronization> synchronization = new ArrayList<>();
111     private Map<String, String> mapDisgNodeRefNode = new HashMap<String, String>();
112     private Map<String, IpAddress> mapNodeRefIp = new HashMap<String, IpAddress>();
113     private Map<String, String> mapLinkFiber = new HashMap<String, String>();
114     private Map<String, IpAddress> mapFiberIp = new HashMap<String, IpAddress>();
115     private static int convertKmM = 1000;
116
117     /*
118      * Construct the ExtractTopoDataStoreImpl.
119      */
120     @SuppressWarnings("unchecked")
121     public ExtractTopoDataStoreImpl(final DataBroker dataBroker, PathComputationRequestInput input, AToZDirection atoz,
122             Long requestId) {
123         this.dataBroker = dataBroker;
124         Map<String, List<?>> map = extractTopo();
125         if (map.containsKey("Elements")) {
126             elements = (List<Elements>) map.get("Elements");
127         } else {
128             elements = null;
129         }
130         if (map.containsKey("Connections")) {
131             connections = (List<Connections>) map.get("Connections");
132         } else {
133             connections = null;
134         }
135
136         pathRequest = extractPathRequest(input, atoz, requestId);
137         synchronization = extractSynchronization(requestId);
138     }
139
140     public ExtractTopoDataStoreImpl(final DataBroker dataBroker, PathComputationRequestInput input, ZToADirection ztoa,
141             Long requestId) {
142         this.dataBroker = dataBroker;
143         Map<String, List<?>> map = extractTopo();
144         if (map.containsKey("Elements")) {
145             elements = (List<Elements>) map.get("Elements");
146         } else {
147             elements = null;
148         }
149         if (map.containsKey("Connections")) {
150             connections = (List<Connections>) map.get("Connections");
151         } else {
152             connections = null;
153         }
154         pathRequest = extractPathRequest(input, ztoa, requestId);
155         synchronization = extractSynchronization(requestId);
156     }
157
158     /*
159      * extract the topology: all the elements have ipAddress as uid and maintain
160      * a mapping structure to map between the nodeId and the ipAddress (uid)
161      *
162      */
163     public Map<String, List<?>> extractTopo() {
164         Map<String, List<?>> map = new HashMap<String, List<?>>();
165         // Define the elements
166         List<Elements> topoElements = new ArrayList<>();
167         // Define the connections
168         List<Connections> topoConnections = new ArrayList<>();
169         // Define the instance identifier of the OpenRoadm topology
170         InstanceIdentifier<Network> insIdOpenRoadmTopo = InstanceIdentifier
171                 .builder(Networks.class)
172                 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID))).build();
173         // Define the instance identifier of the OpenRoadm network
174         InstanceIdentifier<Network> insIdrOpenRoadmNet = InstanceIdentifier
175                 .builder(Networks.class)
176                 .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))).build();
177         ReadOnlyTransaction readOnlyTransaction = this.dataBroker.newReadOnlyTransaction();
178         // Read the data broker
179         try {
180             // Initialize the reading of the data broker
181             // read the configuration part of the data broker that concerns
182             // the openRoadm topology and get all the nodes
183             java.util.Optional<Network> openRoadmTopo = readOnlyTransaction
184                     .read(LogicalDatastoreType.CONFIGURATION, insIdOpenRoadmTopo).get().toJavaUtil();
185             java.util.Optional<Network> openRoadmNet = readOnlyTransaction
186                     .read(LogicalDatastoreType.CONFIGURATION, insIdrOpenRoadmNet).get().toJavaUtil();
187             if (openRoadmNet.isPresent()) {
188                 List<Node> openRoadmNetNodeList = openRoadmNet.get().getNode();
189                 if (openRoadmTopo.isPresent()) {
190                     List<Node> openRoadmTopoNodeList = openRoadmTopo.get().getNode();
191                     List<String> nodesList = new ArrayList<>();
192                     // Create the list of nodes
193                     if (!openRoadmTopoNodeList.isEmpty()) {
194                         // Create elements
195                         for (Node openRoadmTopoNode : openRoadmTopoNodeList) {
196                             // Retrieve the supporting node and the type of the
197                             // node in openRoadm network
198                             List<SupportingNode> supportingNodeList = openRoadmTopoNode.getSupportingNode();
199                             for (SupportingNode supportingNode : supportingNodeList) {
200                                 String nodeRef = supportingNode.getNodeRef().getValue();
201                                 IpAddress ipAddress = null;
202                                 // Retrieve the mapping between the openRoadm
203                                 // topology and openRoadm network
204                                 mapDisgNodeRefNode.put(openRoadmTopoNode.getNodeId().getValue(), nodeRef);
205                                 Node1 openRoadmNetNode1 = null;
206                                 for (Node openRoadmNetNode : openRoadmNetNodeList) {
207                                     if (openRoadmNetNode.getNodeId().getValue().equals(nodeRef)) {
208                                         openRoadmNetNode1 = openRoadmNetNode.augmentation(Node1.class);
209                                         ipAddress = openRoadmNetNode1.getIp();
210                                         mapNodeRefIp.put(nodeRef, ipAddress);
211                                         break;
212                                     }
213                                 }
214                                 if (openRoadmNetNode1.getNodeType().getName().equals("ROADM")) {
215                                     if (!nodesList.contains(nodeRef)) {
216                                         Elements element = addElementsRoadm(2, 0, nodeRef, openRoadmNetNode1.getShelf(),
217                                                 -20, ipAddress.getIpv4Address().getValue().toString());
218                                         topoElements.add(element);
219                                         nodesList.add(nodeRef);
220                                     }
221                                 } else if (openRoadmNetNode1.getNodeType().getName().equals("XPONDER")) {
222                                     if (!nodesList.contains(nodeRef)) {
223                                         Elements element = addElementsTransceiver(2, 0, nodeRef,
224                                                 openRoadmNetNode1.getShelf(),
225                                                 ipAddress.getIpv4Address().getValue().toString());
226                                         topoElements.add(element);
227                                         nodesList.add(nodeRef);
228                                     }
229                                 } else {
230                                     LOG.warn("the type is not implemented");
231                                 }
232                             }
233                         }
234                     } else {
235                         LOG.warn("no nodes in the network");
236                     }
237
238                     // Create the list of connections
239                     Network1 nw1 = openRoadmTopo.get().augmentation(Network1.class);
240                     List<Link> linksList = nw1.getLink();
241                     // 1:EXPRESS-LINK    2:ADD-LINK       3:DROP-LINK
242                     // 4:ROADM-To-ROADM  5:XPONDER-INPUT  6:XPONDER-OUTPUT
243                     int[] externalLink = {4,5,6};
244                     int idFiber = 0;
245                     int nbEDFA = 0;
246                     if (!linksList.isEmpty()) {
247                         LOG.debug("The link list is not empty");
248                         for (Link link : linksList) {
249                             Link1 link1 = link.augmentation(Link1.class);
250                             int linkType = link1.getLinkType().getIntValue();
251                             if (IntStream.of(externalLink).anyMatch(x -> x == linkType)) {
252                                 // Verify if the node is a ROADM
253                                 String srcId = mapDisgNodeRefNode.get(link.getSource().getSourceNode().getValue());
254                                 IpAddress srcIp = mapNodeRefIp.get(srcId);
255                                 String destId = null;
256                                 IpAddress destIp = null;
257                                 // Add the links between amplifiers
258                                 OMSAttributes omsAttributes = link1.getOMSAttributes();
259                                 if (omsAttributes != null) {
260                                     if (omsAttributes.getAmplifiedLink() != null) {
261                                         List<AmplifiedLink> amplifiedLinkList = omsAttributes.getAmplifiedLink()
262                                                 .getAmplifiedLink();
263                                         if (!amplifiedLinkList.isEmpty()) {
264                                             for (AmplifiedLink amplifiedLink : amplifiedLinkList) {
265                                                 Elements element1 = null;
266                                                 if (amplifiedLink.getSectionElement()
267                                                         .getSectionElement() instanceof Ila) {
268                                                     Ila ila = (Ila) amplifiedLink.getSectionElement()
269                                                             .getSectionElement();
270                                                     String nodeId = ila.getNodeId().getValue();
271                                                     IpAddress ipEdfa = new IpAddress(
272                                                             new Ipv4Address("1.1.1." + nbEDFA));
273                                                     nbEDFA++;
274                                                     mapDisgNodeRefNode.put(nodeId, nodeId);
275                                                     mapNodeRefIp.put(nodeId, ipEdfa);
276                                                     element1 = addElementsEdfa(2, 0, "RLD", "Lannion_CAS",
277                                                             ila.getGain().getValue(), ila.getTilt().getValue(),
278                                                             ila.getOutVoaAtt().getValue(), "std_medium_gain",
279                                                             ipEdfa.getIpv4Address().getValue().toString());
280                                                 } else if (amplifiedLink.getSectionElement()
281                                                         .getSectionElement() instanceof Span) {
282                                                     // Create the location
283                                                     Span span = (Span) amplifiedLink.getSectionElement()
284                                                             .getSectionElement();
285                                                     String clfi = span.getSpan().getClfi();
286                                                     IpAddress ipFiber = new IpAddress(
287                                                             new Ipv4Address("2.2.2." + idFiber));
288                                                     mapLinkFiber.put(link.getLinkId().getValue(), clfi);
289                                                     mapFiberIp.put(clfi, ipFiber);
290                                                     idFiber++;
291                                                     element1 = addElementsFiber(2, 0, "RLD", "Lannion_CAS",
292                                                             ipFiber.getIpv4Address().getValue(), 20, 0, 0.2, 0, 0,
293                                                             "SSMF");
294                                                 }
295                                                 if (element1 != null) {
296                                                     topoElements.add(element1);
297                                                     destId = element1.getUid();
298                                                     destIp = null;
299                                                     // Create a new link
300                                                     if (srcId != destId) {
301                                                         Connections connection = createNewConnection(srcId, srcIp,
302                                                                 destId, destIp);
303                                                         topoConnections.add(connection);
304                                                         srcId = destId;
305                                                         srcIp = destIp;
306                                                     }
307                                                 }
308                                             }
309                                         }
310                                     } else if (omsAttributes.getSpan() != null) {
311                                         org.opendaylight.yang.gen.v1.http.org.openroadm.network.topology.rev181130
312                                             .networks.network.link.oms.attributes.@Nullable Span span
313                                                 = omsAttributes.getSpan();
314
315                                         String clfi = span.getClfi();
316                                         IpAddress ipFiber = new IpAddress(new Ipv4Address("2.2.2." + idFiber));
317                                         mapLinkFiber.put(link.getLinkId().getValue(), clfi);
318                                         mapFiberIp.put(clfi, ipFiber);
319                                         idFiber++;
320
321                                         double attIn = 0;
322                                         double connIn = 0;
323                                         double connOut = 0;
324                                         String typeVariety = "SSMF";
325
326                                         double length = 0;
327                                         // Compute the length of the link
328                                         List<LinkConcatenation> linkConcatenationList = span.getLinkConcatenation();
329                                         for (LinkConcatenation linkConcatenation : linkConcatenationList) {
330                                             double srlgLength = linkConcatenation.getSRLGLength();
331                                             //convert to kilometer
332                                             length += srlgLength / convertKmM;
333                                         }
334                                         double lossCoef = span.getSpanlossCurrent().getValue().doubleValue() / length;
335
336                                         Elements element1 = addElementsFiber(2, 0, "RLD", "Lannion_CAS",
337                                                 ipFiber.getIpv4Address().getValue(), length, attIn, lossCoef, connIn,
338                                                 connOut, typeVariety);
339
340                                         topoElements.add(element1);
341                                         // Create a new link
342                                         destId = element1.getUid();
343                                         destIp = null;
344                                         if (srcId != destId) {
345                                             Connections connection = createNewConnection(srcId, srcIp, destId, destIp);
346                                             topoConnections.add(connection);
347                                             srcId = destId;
348                                             srcIp = destIp;
349                                         }
350                                     } else {
351                                         // Add a fiber
352                                         String clfi = "Fiber" + idFiber;
353                                         IpAddress ipFiber = new IpAddress(new Ipv4Address("2.2.2." + idFiber));
354                                         mapLinkFiber.put(link.getLinkId().getValue(), clfi);
355                                         mapFiberIp.put(clfi, ipFiber);
356                                         idFiber++;
357                                         // Create a new element
358                                         Elements element1 = addElementsFiber(2, 0, "RLD", "Lannion_CAS",
359                                                 ipFiber.getIpv4Address().getValue(), 20, 0, 0.2, 0, 0, "SSMF");
360                                         topoElements.add(element1);
361                                         // Create a new link
362                                         destId = element1.getUid();
363                                         destIp = null;
364                                         if (srcId != destId) {
365                                             Connections connection = createNewConnection(srcId, srcIp, destId, destIp);
366                                             topoConnections.add(connection);
367                                             srcId = destId;
368                                             srcIp = destIp;
369                                         }
370                                     }
371                                 } else {
372                                     LOG.warn("The oms attributes is null!");
373                                 }
374                                 // Create a new link
375                                 destId = mapDisgNodeRefNode.get(link.getDestination().getDestNode().getValue());
376                                 destIp = mapNodeRefIp.get(destId);
377                                 Connections connection = createNewConnection(srcId, srcIp, destId, destIp);
378                                 topoConnections.add(connection);
379                             }
380                         }
381                     } else {
382                         LOG.warn("no links in the network");
383                     }
384                 } else {
385                     LOG.warn("No nodes in the selected network ...");
386                 }
387             }
388         } catch (InterruptedException | ExecutionException e) {
389             LOG.error("Error reading the topology", e);
390             readOnlyTransaction.close();
391         }
392         readOnlyTransaction.close();
393         map.put("Elements", topoElements);
394         map.put("Connections", topoConnections);
395         return map;
396     }
397
398     /*
399      * Create the pathRequest
400      */
401     public List<PathRequest> extractPathRequest(PathComputationRequestInput input, AToZDirection atoz, Long requestId) {
402         // List of A to Z
403         List<AToZ> listAtoZ = atoz.getAToZ();
404         int atozSize = listAtoZ.size();
405         // Create the path request
406         List<PathRequest> pathRequestList = new ArrayList<>();
407
408         // 1.1 Create explicitRouteObjects
409         // 1.1.1. create RouteObjectIncludeExclude list
410         List<RouteObjectIncludeExclude> routeObjectIncludeExcludes = new ArrayList<>();
411         IpAddress ipAddressCurrent = null;
412         Long index = (long) 0;
413         for (int i = 0; i < atozSize; i++) {
414             String nodeId = null;
415             if (listAtoZ.get(i).getResource()
416                     .getResource() instanceof org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
417                     .pathdescription.rev171017.pce.resource.resource.resource.Node) {
418                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
419                     .resource.resource.resource.Node node = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c
420                             ._interface.pathdescription.rev171017.pce.resource.resource.resource.Node) listAtoZ
421                         .get(i).getResource().getResource();
422                 nodeId = node.getNodeId();
423                 if (nodeId != null) {
424                     String nodeRef = mapDisgNodeRefNode.get(nodeId);
425                     IpAddress ipAddress = mapNodeRefIp.get(nodeRef);
426                     for (Elements element : elements) {
427                         if (element.getUid().contains(ipAddress.getIpv4Address().getValue().toString())) {
428                             if ((ipAddressCurrent == null) || (ipAddressCurrent != ipAddress)) {
429                                 ipAddressCurrent = ipAddress;
430                                 // Fill in routeObjectIncludeExcludes
431                                 RouteObjectIncludeExclude routeObjectIncludeExclude1 = addRouteObjectIncludeExclude(
432                                         ipAddress, 1, index);
433                                 routeObjectIncludeExcludes.add(routeObjectIncludeExclude1);
434                                 index++;
435                             }
436                             break;
437                         }
438                     }
439                 } else {
440                     LOG.warn("node ID is null");
441                 }
442             //TODO else if termination point not implemented in this version
443             } else if (listAtoZ.get(i).getResource()
444                     .getResource() instanceof org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
445                     .pathdescription.rev171017.pce.resource.resource.resource.Link) {
446                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
447                     .resource.resource.resource.Link link = (org.opendaylight.yang.gen.v1.http.org.transportpce
448                             .b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.Link) listAtoZ
449                         .get(i).getResource().getResource();
450                 String clfi = mapLinkFiber.get(link.getLinkId());
451                 IpAddress fiberIp = mapFiberIp.get(clfi);
452                 if (clfi != null) {
453                     RouteObjectIncludeExclude routeObjectIncludeExclude1 = addRouteObjectIncludeExclude(fiberIp, 1,
454                             index);
455                     routeObjectIncludeExcludes.add(routeObjectIncludeExclude1);
456                     index++;
457                 }
458             }
459         }
460         // Create ExplicitRouteObjects
461         ExplicitRouteObjects explicitRouteObjects = new ExplicitRouteObjectsBuilder()
462                 .setRouteObjectIncludeExclude(routeObjectIncludeExcludes).build();
463
464         // 1. Create the path request element 1
465         // Find parameters
466         String sourceNode = input.getServiceAEnd().getNodeId();
467         String destNode = input.getServiceZEnd().getNodeId();
468
469         // 1.2 Create a path constraints
470         Long rate = atoz.getRate();
471         // Create EffectiveFreqSlot
472         List<EffectiveFreqSlot> effectiveFreqSlot = new ArrayList<>();
473         EffectiveFreqSlot effectiveFreqSlot1 = new EffectiveFreqSlotBuilder().setM(5).setN(8).build();
474         effectiveFreqSlot.add(effectiveFreqSlot1);
475         // Create Te-Bandwidth
476         TeBandwidth teBandwidth = new TeBandwidthBuilder().setPathBandwidth(new BigDecimal(rate))
477                 .setTechnology("flexi-grid").setTrxType("openroadm-beta1").setTrxMode("W100G")
478                 .setEffectiveFreqSlot(effectiveFreqSlot).setSpacing(new BigDecimal(50000000000.0)).build();
479         PathConstraints pathConstraints = new PathConstraintsBuilder().setTeBandwidth(teBandwidth).build();
480         PathRequest pathRequest1 = new PathRequestBuilder().setRequestId(requestId)
481                 .setSource(mapNodeRefIp.get(sourceNode)).setDestination(mapNodeRefIp.get(destNode))
482                 .setSrcTpId("srcTpId".getBytes()).setDstTpId("dstTpId".getBytes()).setPathConstraints(pathConstraints)
483                 .setExplicitRouteObjects(explicitRouteObjects).build();
484         pathRequestList.add(pathRequest1);
485         return pathRequestList;
486     }
487
488     public List<PathRequest> extractPathRequest(PathComputationRequestInput input, ZToADirection ztoa, Long requestId) {
489         // List of A to Z
490         List<ZToA> listZToA = ztoa.getZToA();
491         int ztoaSize = listZToA.size();
492         // Create the path request
493         List<PathRequest> servicePathRequest = new ArrayList<>();
494
495         // 1.1 Create explicitRouteObjects
496         // 1.1.1. create RouteObjectIncludeExclude list
497         List<RouteObjectIncludeExclude> routeObjectIncludeExcludes = new ArrayList<>();
498         IpAddress ipAddressCurrent = null;
499         Long index = (long) 0;
500         for (int i = 0; i < ztoaSize; i++) {
501             String nodeId = null;
502             if (listZToA.get(i).getResource()
503                     .getResource() instanceof org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
504                     .pathdescription.rev171017.pce.resource.resource.resource.Node) {
505                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
506                     .resource.resource.resource.Node node = (org.opendaylight.yang.gen.v1.http.org.transportpce.b.c
507                     ._interface.pathdescription.rev171017.pce.resource.resource.resource.Node) listZToA.get(i)
508                     .getResource().getResource();
509                 nodeId = node.getNodeId();
510                 if (nodeId != null) {
511                     String nodeRef = mapDisgNodeRefNode.get(nodeId);
512                     IpAddress ipAddress = mapNodeRefIp.get(nodeRef);
513                     for (Elements element : elements) {
514                         if (element.getUid().contains(ipAddress.getIpv4Address().getValue().toString())) {
515                             if ((ipAddressCurrent == null) || (ipAddressCurrent != ipAddress)) {
516                                 ipAddressCurrent = ipAddress;
517                                 // Fill in routeObjectIncludeExcludes
518                                 RouteObjectIncludeExclude routeObjectIncludeExclude1 = addRouteObjectIncludeExclude(
519                                         ipAddress, 1, index);
520                                 routeObjectIncludeExcludes.add(routeObjectIncludeExclude1);
521                                 index++;
522                             }
523                             break;
524                         }
525                     }
526                 } else {
527                     LOG.warn("node ID is null");
528                 }
529             //TODO else if termination point not implemented in this version
530             } else if (listZToA.get(i).getResource()
531                     .getResource() instanceof org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface
532                     .pathdescription.rev171017.pce.resource.resource.resource.Link) {
533                 org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce
534                     .resource.resource.resource.Link link = (org.opendaylight.yang.gen.v1.http.org.transportpce
535                     .b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.Link) listZToA.get(i)
536                     .getResource().getResource();
537                 String clfi = mapLinkFiber.get(link.getLinkId());
538                 IpAddress fiberIp = mapFiberIp.get(clfi);
539                 if (clfi != null) {
540                     RouteObjectIncludeExclude routeObjectIncludeExclude1 = addRouteObjectIncludeExclude(fiberIp, 1,
541                             index);
542                     routeObjectIncludeExcludes.add(routeObjectIncludeExclude1);
543                     index++;
544                 }
545             }
546         }
547         // Create ExplicitRouteObjects
548         ExplicitRouteObjects explicitRouteObjects = new ExplicitRouteObjectsBuilder()
549                 .setRouteObjectIncludeExclude(routeObjectIncludeExcludes).build();
550
551         // 1. Create the path request element 1
552         // Find parameters
553         String sourceNode = input.getServiceZEnd().getNodeId();
554         String destNode = input.getServiceAEnd().getNodeId();
555
556         // 1.2 Create a path constraints
557         Long rate = ztoa.getRate();
558         // Create EffectiveFreqSlot
559         List<EffectiveFreqSlot> effectiveFreqSlot = new ArrayList<>();
560         EffectiveFreqSlot effectiveFreqSlot1 = new EffectiveFreqSlotBuilder().setM(5).setN(8).build();
561         effectiveFreqSlot.add(effectiveFreqSlot1);
562         // Create Te-Bandwidth
563         TeBandwidth teBandwidth = new TeBandwidthBuilder().setPathBandwidth(new BigDecimal(rate))
564                 .setTechnology("flexi-grid").setTrxType("openroadm-beta1").setTrxMode("W100G")
565                 .setEffectiveFreqSlot(effectiveFreqSlot).setSpacing(new BigDecimal(50000000000.0)).build();
566         PathConstraints pathConstraints = new PathConstraintsBuilder().setTeBandwidth(teBandwidth).build();
567         PathRequest pathRequest1 = new PathRequestBuilder().setRequestId(requestId)
568                 .setSource(mapNodeRefIp.get(sourceNode)).setDestination(mapNodeRefIp.get(destNode))
569                 .setSrcTpId("srcTpId".getBytes()).setDstTpId("dstTpId".getBytes()).setPathConstraints(pathConstraints)
570                 .setExplicitRouteObjects(explicitRouteObjects).build();
571         servicePathRequest.add(pathRequest1);
572         return servicePathRequest;
573     }
574
575     /*
576      * Create the synchronization
577      */
578     public List<Synchronization> extractSynchronization(Long requestId) {
579         // Create RequestIdNumber
580         List<Long> requestIdNumber = new ArrayList<>();
581         requestIdNumber.add(requestId);
582         // Create a synchronization
583         Svec svec = new SvecBuilder().setRelaxable(true).setDisjointness(new TePathDisjointness(true, true, false))
584                 .setRequestIdNumber(requestIdNumber).build();
585         List<Synchronization> synchro = new ArrayList<>();
586         Synchronization synchronization1 = new SynchronizationBuilder().setSynchronizationId(new Long(0)).setSvec(svec)
587                 .build();
588         synchro.add(synchronization1);
589         return (synchro);
590     }
591
592     /*
593      * Method to add Fiber
594      */
595     private Elements addElementsFiber(double latitude, double longitude, String region, String city, String clfi,
596             double length, double attIn, double lossCoef, double connIn, double connOut, String typeVariety) {
597         // Create an amplifier after the roadm
598         Coordinate c1 = new Coordinate(new BigDecimal(latitude));
599         Coordinate c2 = new Coordinate(new BigDecimal(longitude));
600         Location location1 = new LocationBuilder().setRegion(region).setCity(city).setLatitude(c1).setLongitude(c2)
601                 .build();
602         Metadata metadata1 = new MetadataBuilder().setLocation(location1).build();
603         Fiber fiber = new FiberBuilder().setLength(new BigDecimal(length)).setLengthUnits(Km.class)
604                 .setAttIn(new BigDecimal(attIn)).setLossCoef(new BigDecimal(lossCoef)).setConIn(new BigDecimal(connIn))
605                 .setConOut(new BigDecimal(connOut)).build();
606         Params params1 = new ParamsBuilder().setFiberroadm(fiber).build();
607         Elements element1 = new ElementsBuilder().setUid(clfi)
608                 .setType(org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.Fiber.class)
609                 .setTypeVariety(typeVariety).setMetadata(metadata1)
610                 .setElementType(new FiberRoadmBuilder().setParams(params1).build()).build();
611         return element1;
612     }
613
614     /*
615      * Method to add Edfa
616      */
617     private Elements addElementsEdfa(double latitude, double longitude, String region, String city,
618             BigDecimal gainTarget, BigDecimal tiltTarget, BigDecimal outVoa, String typeVariety, String uidEdfa) {
619         // Create an amplifier after the roadm
620         Coordinate c1 = new Coordinate(new BigDecimal(latitude));
621         Coordinate c2 = new Coordinate(new BigDecimal(longitude));
622         Location location1 = new LocationBuilder().setRegion(region).setCity(city).setLatitude(c1).setLongitude(c2)
623                 .build();
624         Metadata metadata1 = new MetadataBuilder().setLocation(location1).build();
625         Operational operational = new OperationalBuilder().setGainTarget(gainTarget).setTiltTarget(tiltTarget)
626                 .setOutVoa(outVoa).build();
627         Edfa edfa = new EdfaBuilder()
628                 .setOperational(operational).build();
629         Elements element1 = new ElementsBuilder().setUid(uidEdfa)
630                 // Choose an ip address
631                 .setType(org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.Edfa.class)
632                 .setMetadata(metadata1).setElementType(edfa).setTypeVariety(typeVariety).build();
633         return element1;
634     }
635
636     /*
637      * Method to add ROADM
638      */
639     private Elements addElementsRoadm(double latitude, double longitude, String region, String city,
640             double targetPchOutDb, String uidRoadm) {
641
642         Coordinate c1 = new Coordinate(new BigDecimal(latitude));
643         Coordinate c2 = new Coordinate(new BigDecimal(longitude));
644         Location location1 = new LocationBuilder().setRegion(region).setCity(city).setLatitude(c1).setLongitude(c2)
645                 .build();
646         Metadata metadata1 = new MetadataBuilder().setLocation(location1).build();
647         // Create the roadm
648         Roadm roadm = new RoadmBuilder().setTargetPchOutDb(new BigDecimal(targetPchOutDb)).build();
649         Params params1 = new ParamsBuilder().setFiberroadm(roadm).build();
650         Elements element1 = new ElementsBuilder().setUid(uidRoadm)
651                 .setType(org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.Roadm.class)
652                 .setMetadata(metadata1).setElementType(new FiberRoadmBuilder().setParams(params1).build()).build();
653         return element1;
654     }
655
656     /*
657      * Method to add Transceiver
658      */
659     private Elements addElementsTransceiver(double latitude, double longitude, String region, String city,
660             String uidTrans) {
661         Coordinate c1 = new Coordinate(new BigDecimal(latitude));
662         Coordinate c2 = new Coordinate(new BigDecimal(longitude));
663         Location location1 = new LocationBuilder().setRegion(region).setCity(city).setLatitude(c1).setLongitude(c2)
664                 .build();
665         Metadata metadata1 = new MetadataBuilder().setLocation(location1).build();
666         Transceiver transceiver = new TransceiverBuilder().build();
667         Elements element1 = new ElementsBuilder().setUid(uidTrans)
668                 .setType(org.opendaylight.yang.gen.v1.gnpy.gnpy.network.topology.rev181214.Transceiver.class)
669                 .setMetadata(metadata1).setElementType(transceiver).build();
670         return element1;
671     }
672
673     /*
674      * Add routeObjectIncludeExclude
675      */
676     private RouteObjectIncludeExclude addRouteObjectIncludeExclude(IpAddress ipAddress, long teTpValue, long index) {
677         TeNodeId teNodeId = new TeNodeId(ipAddress);
678         TeTpId teTpId = new TeTpId(teTpValue);
679         NumUnnumHop numUnnumHop = new org.opendaylight.yang.gen.v1.gnpy.path.rev190502.explicit.route.hop.type.num
680             .unnum.hop.NumUnnumHopBuilder()
681             .setNodeId(teNodeId.getIpv4Address().getValue().toString()).setLinkTpId(teTpId.getUint32().toString())
682             .setHopType(TeHopType.STRICT).build();
683         Type type1 = new NumUnnumHopBuilder().setNumUnnumHop(numUnnumHop).build();
684         // Create routeObjectIncludeExclude element 1
685         RouteObjectIncludeExclude routeObjectIncludeExclude1 = new RouteObjectIncludeExcludeBuilder().setIndex(index)
686                 .setExplicitRouteUsage(RouteIncludeEro.class).setType(type1).build();
687         return routeObjectIncludeExclude1;
688     }
689
690     private Connections createNewConnection(String srcId, IpAddress srcIp, String destId, IpAddress destIp) {
691         String fromNode = srcId;
692         String toNode = destId;
693         if (srcIp != null) {
694             fromNode = srcIp.getIpv4Address().getValue().toString();
695         }
696         if (destIp != null) {
697             toNode = destIp.getIpv4Address().getValue().toString();
698         }
699         Connections connection1 = new ConnectionsBuilder().setFromNode(fromNode).setToNode(toNode).build();
700         return (connection1);
701     }
702
703     public List<Elements> getElements() {
704         return elements;
705     }
706
707     public void setElements(List<Elements> elements) {
708         this.elements = elements;
709     }
710
711     public List<Connections> getConnections() {
712         return connections;
713     }
714
715     public void setConnections(List<Connections> connections) {
716         this.connections = connections;
717     }
718
719     public List<PathRequest> getPathRequest() {
720         return pathRequest;
721     }
722
723     public void setPathRequest(List<PathRequest> pathRequest) {
724         this.pathRequest = pathRequest;
725     }
726
727     public List<Synchronization> getSynchronization() {
728         return synchronization;
729     }
730
731     public void setSynchronization(List<Synchronization> synchronization) {
732         this.synchronization = synchronization;
733     }
734
735     public List<PathRequest> createEmptyPathRequest(PathComputationRequestInput input, AToZDirection atoz) {
736         // Create the path request
737         List<PathRequest> pathRequestList = new ArrayList<>();
738
739         // 1. Create the path request element 1
740         // Find parameters
741         String sourceNode = input.getServiceAEnd().getNodeId();
742         String destNode = input.getServiceZEnd().getNodeId();
743
744         // 1.2 Create a path constraints
745         Long rate = atoz.getRate();
746
747         // Create EffectiveFreqSlot
748         List<EffectiveFreqSlot> effectiveFreqSlot = new ArrayList<>();
749         EffectiveFreqSlot effectiveFreqSlot1 = new EffectiveFreqSlotBuilder().setM(5).setN(8).build();
750         effectiveFreqSlot.add(effectiveFreqSlot1);
751
752         // Create Te-Bandwidth
753         TeBandwidth teBandwidth = new TeBandwidthBuilder().setPathBandwidth(new BigDecimal(rate))
754                 .setTechnology("flexi-grid").setTrxType("openroadm-beta1").setTrxMode("W100G")
755                 .setEffectiveFreqSlot(effectiveFreqSlot).setSpacing(new BigDecimal(50000000000.0)).build();
756         PathConstraints pathConstraints = new PathConstraintsBuilder().setTeBandwidth(teBandwidth).build();
757         PathRequest pathRequest1 = new PathRequestBuilder().setRequestId(new Long(0))
758                 .setSource(mapNodeRefIp.get(sourceNode)).setDestination(mapNodeRefIp.get(destNode))
759                 .setSrcTpId("srcTpId".getBytes()).setDstTpId("dstTpId".getBytes()).setPathConstraints(pathConstraints)
760                 .build();
761         pathRequestList.add(pathRequest1);
762         return pathRequestList;
763     }
764
765 }