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