8bba4dfc153fb3044f3f2956e3e18fc2adb2b9f5
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / topology / TapiTopologyImpl.java
1 /*
2  * Copyright © 2019 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 package org.opendaylight.transportpce.tapi.topology;
9
10 import com.google.common.collect.ImmutableClassToInstanceMap;
11 import com.google.common.util.concurrent.FluentFuture;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.nio.charset.Charset;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import java.util.Set;
24 import java.util.UUID;
25 import java.util.concurrent.ExecutionException;
26 import java.util.stream.Collectors;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.mdsal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.transportpce.common.InstanceIdentifiers;
32 import org.opendaylight.transportpce.common.NetworkUtils;
33 import org.opendaylight.transportpce.tapi.TapiStringConstants;
34 import org.opendaylight.transportpce.tapi.utils.TapiContext;
35 import org.opendaylight.transportpce.tapi.utils.TapiLink;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.mapping.Mapping;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.mapping.MappingKey;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.network.Nodes;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.network.NodesKey;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.Link1;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.TerminationPoint1;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmLinkType;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmNodeType;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev230526.OpenroadmTpType;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.network.Node;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Node1;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.Link;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.networks.network.node.TerminationPoint;
52 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.AdministrativeState;
53 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
54 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.GetServiceInterfacePointDetailsInput;
55 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.GetServiceInterfacePointDetailsOutput;
56 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.GetServiceInterfacePointDetailsOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.GetServiceInterfacePointListInput;
58 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.GetServiceInterfacePointListOutput;
59 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.GetServiceInterfacePointListOutputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.LayerProtocolName;
61 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.LifecycleState;
62 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.OperationalState;
63 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.TapiCommonService;
64 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.UpdateServiceInterfacePointInput;
65 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.UpdateServiceInterfacePointOutput;
66 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
67 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.get.service._interface.point.list.output.Sip;
68 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.get.service._interface.point.list.output.SipBuilder;
69 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.get.service._interface.point.list.output.SipKey;
70 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.Name;
71 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.NameBuilder;
72 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.global._class.NameKey;
73 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePoint;
74 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.tapi.context.ServiceInterfacePointKey;
75 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1;
76 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.FORWARDINGRULEMAYFORWARDACROSSGROUP;
77 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetLinkDetailsInput;
78 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetLinkDetailsOutput;
79 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetLinkDetailsOutputBuilder;
80 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetNodeDetailsInput;
81 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetNodeDetailsOutput;
82 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetNodeDetailsOutputBuilder;
83 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetNodeEdgePointDetailsInput;
84 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetNodeEdgePointDetailsOutput;
85 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetNodeEdgePointDetailsOutputBuilder;
86 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetTopologyDetailsInput;
87 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetTopologyDetailsOutput;
88 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetTopologyDetailsOutputBuilder;
89 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetTopologyListInput;
90 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetTopologyListOutput;
91 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.GetTopologyListOutputBuilder;
92 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.RuleType;
93 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.TapiTopologyService;
94 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.get.link.details.output.LinkBuilder;
95 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.get.node.edge.point.details.output.NodeEdgePointBuilder;
96 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.get.topology.details.output.Topology;
97 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.get.topology.details.output.TopologyBuilder;
98 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.NodeRuleGroup;
99 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.NodeRuleGroupBuilder;
100 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.NodeRuleGroupKey;
101 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePoint;
102 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.OwnedNodeEdgePointKey;
103 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.rule.group.Rule;
104 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.rule.group.RuleBuilder;
105 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.rule.group.RuleKey;
106 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.LinkKey;
107 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.NodeBuilder;
108 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.NodeKey;
109 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyKey;
110 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
111 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
112 import org.opendaylight.yangtools.yang.binding.Rpc;
113 import org.opendaylight.yangtools.yang.common.ErrorTag;
114 import org.opendaylight.yangtools.yang.common.ErrorType;
115 import org.opendaylight.yangtools.yang.common.RpcResult;
116 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
117 import org.slf4j.Logger;
118 import org.slf4j.LoggerFactory;
119
120 public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService {
121
122     private static final Logger LOG = LoggerFactory.getLogger(TapiTopologyImpl.class);
123     private final DataBroker dataBroker;
124     private final TapiContext tapiContext;
125     private final TopologyUtils topologyUtils;
126     private final TapiLink tapiLink;
127
128     public TapiTopologyImpl(DataBroker dataBroker, TapiContext tapiContext, TopologyUtils topologyUtils,
129                             TapiLink tapiLink) {
130         this.dataBroker = dataBroker;
131         this.tapiContext = tapiContext;
132         this.topologyUtils = topologyUtils;
133         this.tapiLink = tapiLink;
134     }
135
136     public ImmutableClassToInstanceMap<Rpc<?, ?>> registerRPCs() {
137         return ImmutableClassToInstanceMap.<Rpc<?, ?>>builder()
138             .put(GetNodeDetails.class, this::getNodeDetails)
139             .put(GetTopologyDetails.class, this::getTopologyDetails)
140             .put(GetNodeEdgePointDetails.class, this::getNodeEdgePointDetails)
141             .put(GetLinkDetails.class, this::getLinkDetails)
142             .put(GetTopologyList.class, this::getTopologyList)
143             .put(GetServiceInterfacePointDetails.class, this::getServiceInterfacePointDetails)
144             .put(GetServiceInterfacePointList.class, this::getServiceInterfacePointList)
145             .put(UpdateServiceInterfacePoint.class, this::updateServiceInterfacePoint)
146             .build();
147     }
148
149     @Override
150     public ListenableFuture<RpcResult<GetNodeDetailsOutput>> getNodeDetails(GetNodeDetailsInput input) {
151         // TODO Auto-generated method stub
152         // TODO -> maybe we get errors when having CEPs?
153         Uuid topoUuid = input.getTopologyId();
154         // Node id: if roadm -> ROADM+PHOTONIC_MEDIA. if xpdr -> XPDR-XPDR+DSR/OTSi
155         Uuid nodeUuid = input.getNodeId();
156         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node node = this.tapiContext
157                 .getTapiNode(topoUuid, nodeUuid);
158         if (node == null) {
159             LOG.error("Invalid TAPI node name");
160             return RpcResultBuilder.<GetNodeDetailsOutput>failed()
161                 .withError(ErrorType.RPC, "Invalid Tapi Node name")
162                 .buildFuture();
163         }
164         return RpcResultBuilder.success(new GetNodeDetailsOutputBuilder()
165                 .setNode(new org.opendaylight.yang.gen.v1.urn
166                         .onf.otcc.yang.tapi.topology.rev221121.get.node.details.output.NodeBuilder(node).build())
167                 .build()).buildFuture();
168     }
169
170     @Override
171     public ListenableFuture<RpcResult<GetTopologyDetailsOutput>> getTopologyDetails(GetTopologyDetailsInput input) {
172         // TODO -> Add check for Full T0 Multilayer
173         List<String> topoNameList = null;
174         try {
175             topoNameList = topologyUtils.readTopologyName(input.getTopologyId());
176         } catch (TapiTopologyException e) {
177             LOG.error("Topology {} not found in datastore", input.getTopologyId());
178             return RpcResultBuilder.<GetTopologyDetailsOutput>failed()
179                 .withError(ErrorType.RPC, "Invalid Topology name")
180                 .buildFuture();
181         }
182         if (!topoNameList.contains(TapiStringConstants.T0_MULTILAYER)
183                 && !topoNameList.contains(TapiStringConstants.TPDR_100G)) {
184             if (topoNameList.contains(TapiStringConstants.T0_FULL_MULTILAYER)) {
185                 Uuid topoUuid = input.getTopologyId();
186                 Context context = this.tapiContext.getTapiContext();
187                 Map<TopologyKey,
188                     org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology>
189                     topologyMap = context.augmentation(Context1.class).getTopologyContext().getTopology();
190                 if (!(topologyMap != null && topologyMap.containsKey(new TopologyKey(topoUuid)))) {
191                     LOG.error("Topology {} not found in datastore", input.getTopologyId());
192                     return RpcResultBuilder.<GetTopologyDetailsOutput>failed()
193                         .withError(ErrorType.RPC, "Invalid Topology name")
194                         .buildFuture();
195                 }
196                 org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology
197                     topology = topologyMap.get(new TopologyKey(topoUuid));
198                 return RpcResultBuilder
199                     .success(new GetTopologyDetailsOutputBuilder()
200                         .setTopology(this.topologyUtils.transformTopology(topology))
201                         .build())
202                     .buildFuture();
203             }
204             LOG.error("Invalid TAPI topology name");
205             return RpcResultBuilder.<GetTopologyDetailsOutput>failed()
206                 .withError(ErrorType.RPC, "Invalid Topology name")
207                 .buildFuture();
208         }
209         Uuid topologyUuid = new Uuid(UUID.nameUUIDFromBytes(TapiStringConstants.TPDR_100G.getBytes(
210             Charset.forName("UTF-8"))).toString());
211         try {
212             LOG.info("Building TAPI Topology abstraction for {}", input.getTopologyId());
213             Topology topology = createAbstractedOtnTopology();
214             if (topologyUuid.equals(input.getTopologyId())) {
215                 topology = createAbstracted100GTpdrTopology(topology);
216             }
217             return RpcResultBuilder.success(new GetTopologyDetailsOutputBuilder().setTopology(topology).build())
218                 .buildFuture();
219         } catch (TapiTopologyException e) {
220             LOG.error("error building TAPI topology");
221             return RpcResultBuilder.<GetTopologyDetailsOutput>failed()
222                 .withError(ErrorType.RPC, "Error building topology")
223                 .buildFuture();
224         }
225     }
226
227     @Override
228     public ListenableFuture<RpcResult<GetNodeEdgePointDetailsOutput>> getNodeEdgePointDetails(
229             GetNodeEdgePointDetailsInput input) {
230         // TODO Auto-generated method stub
231         // TODO -> maybe we get errors when having CEPs?
232         Uuid topoUuid = getUuidFromIput(input.getTopologyIdOrName());
233         // Node id: if roadm -> ROADMid+PHOTONIC_MEDIA. if xpdr -> XPDRid-XPDRnbr+DSR/OTSi
234         Uuid nodeUuid = getUuidFromIput(input.getNodeIdOrName());
235         // NEP id: if roadm -> ROADMid+PHOTONIC_MEDIA/MC/OTSiMC+TPid.
236         // if xpdr -> XPDRid-XPDRnbr+DSR/eODU/iODU/iOTSi/eOTSi/PHOTONIC_MEDIA+TPid
237         Uuid nepUuid = getUuidFromIput(input.getEpIdOrName());
238         OwnedNodeEdgePoint nep = this.tapiContext.getTapiNEP(topoUuid, nodeUuid, nepUuid);
239         if (nep == null) {
240             LOG.error("Invalid TAPI nep name");
241             return RpcResultBuilder.<GetNodeEdgePointDetailsOutput>failed()
242                 .withError(ErrorType.RPC, "Invalid NEP name")
243                 .buildFuture();
244         }
245         return RpcResultBuilder
246             .success(new GetNodeEdgePointDetailsOutputBuilder()
247                 .setNodeEdgePoint(new NodeEdgePointBuilder(nep).build())
248                 .build())
249             .buildFuture();
250     }
251
252     @Override
253     public ListenableFuture<RpcResult<GetLinkDetailsOutput>> getLinkDetails(GetLinkDetailsInput input) {
254         // TODO Auto-generated method stub
255         Uuid topoUuid = getUuidFromIput(input.getTopologyIdOrName());
256         // Link id: same as OR link id
257         Uuid linkUuid = getUuidFromIput(input.getLinkIdOrName());
258         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Link link =
259             this.tapiContext.getTapiLink(topoUuid, linkUuid);
260         if (link == null) {
261             LOG.error("Invalid TAPI link name");
262             return RpcResultBuilder.<GetLinkDetailsOutput>failed()
263                 .withError(ErrorType.RPC, "Invalid Link name")
264                 .buildFuture();
265         }
266         return RpcResultBuilder
267             .success(new GetLinkDetailsOutputBuilder().setLink(new LinkBuilder(link).build()).build())
268             .buildFuture();
269     }
270
271     @Override
272     public ListenableFuture<RpcResult<GetTopologyListOutput>> getTopologyList(GetTopologyListInput input) {
273         // TODO Auto-generated method stub
274         // TODO -> maybe we get errors when having CEPs?
275         Map<TopologyKey,
276                 org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.Topology>
277                 topologyMap = this.tapiContext.getTopologyContext();
278         if (topologyMap.isEmpty()) {
279             LOG.error("No topologies exist in tapi context");
280             return RpcResultBuilder.<GetTopologyListOutput>failed()
281                 .withError(ErrorType.APPLICATION, "No topologies exist in tapi context")
282                 .buildFuture();
283         }
284         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.get.topology.list.output.TopologyKey,
285             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.get.topology.list.output.Topology>
286                 newTopoMap = new HashMap<>();
287         for (org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.Topology
288                 topo:topologyMap.values()) {
289             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.get.topology.list.output.Topology
290                 newTopo = new org.opendaylight.yang.gen.v1.urn
291                     .onf.otcc.yang.tapi.topology.rev181210.get.topology.list.output.TopologyBuilder(topo).build();
292             newTopoMap.put(newTopo.key(), newTopo);
293         }
294         return RpcResultBuilder
295             .success(new GetTopologyListOutputBuilder().setTopology(newTopoMap).build())
296             .buildFuture();
297     }
298
299     @Override
300     public ListenableFuture<RpcResult<GetServiceInterfacePointDetailsOutput>>
301             getServiceInterfacePointDetails(GetServiceInterfacePointDetailsInput input) {
302         Uuid sipUuid = getUuidFromIput(input.getSipIdOrName());
303         Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips =
304             this.tapiContext.getTapiContext().getServiceInterfacePoint();
305         if (sips == null || sips.isEmpty()) {
306             return RpcResultBuilder.<GetServiceInterfacePointDetailsOutput>failed()
307                 .withError(ErrorType.RPC, "No sips in datastore")
308                 .buildFuture();
309         }
310         if (!sips.containsKey(new ServiceInterfacePointKey(sipUuid))) {
311             return RpcResultBuilder.<GetServiceInterfacePointDetailsOutput>failed()
312                 .withError(ErrorType.RPC, "Sip doesnt exist in datastore")
313                 .buildFuture();
314         }
315         var outSip = new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210
316                 .get.service._interface.point.details.output.SipBuilder(sips.get(new ServiceInterfacePointKey(sipUuid)))
317             .build();
318         return RpcResultBuilder
319             .success(new GetServiceInterfacePointDetailsOutputBuilder().setSip(outSip).build())
320             .buildFuture();
321     }
322
323     @Override
324     public ListenableFuture<RpcResult<GetServiceInterfacePointListOutput>>
325             getServiceInterfacePointList(GetServiceInterfacePointListInput input) {
326         Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips =
327             this.tapiContext.getTapiContext().getServiceInterfacePoint();
328         if (sips == null || sips.isEmpty()) {
329             return RpcResultBuilder.<GetServiceInterfacePointListOutput>failed()
330                 .withError(ErrorType.RPC, "No sips in datastore")
331                 .buildFuture();
332         }
333         Map<SipKey, Sip> outSipMap = new HashMap<>();
334         for (ServiceInterfacePoint sip : sips.values()) {
335             Sip si = new SipBuilder(sip).build();
336             outSipMap.put(si.key(), si);
337         }
338         return RpcResultBuilder
339             .success(new GetServiceInterfacePointListOutputBuilder().setSip(outSipMap).build())
340             .buildFuture();
341     }
342
343     @Override
344     public ListenableFuture<RpcResult<UpdateServiceInterfacePointOutput>>
345             updateServiceInterfacePoint(UpdateServiceInterfacePointInput input) {
346         return RpcResultBuilder.<UpdateServiceInterfacePointOutput>failed()
347             .withError(ErrorType.RPC, ErrorTag.OPERATION_NOT_SUPPORTED, "RPC not implemented yet")
348             .buildFuture();
349     }
350
351     private Topology createAbstracted100GTpdrTopology(Topology topology) {
352         List<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node> dsrNodes =
353             topology.nonnullNode().values().stream()
354                 .filter(node -> node.getLayerProtocolName().contains(LayerProtocolName.DSR))
355                 .collect(Collectors.toList());
356         List<OwnedNodeEdgePoint> nep100GTpdrList = new ArrayList<>();
357         for (org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node node2: dsrNodes) {
358             List<OwnedNodeEdgePoint> nepList = node2.getOwnedNodeEdgePoint().values().stream()
359                 .filter(nep -> nep.getName().containsKey(new NameKey("100G-tpdr"))).collect(Collectors.toList());
360             nep100GTpdrList.addAll(nepList);
361         }
362         Name topoName = new NameBuilder().setValue(TapiStringConstants.TPDR_100G)
363             .setValueName("TAPI Topology Name").build();
364         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(
365             TapiStringConstants.TPDR_100G.getBytes(Charset.forName("UTF-8"))).toString());
366         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node node =
367             createTapiNode(nep100GTpdrList, topoUuid);
368         return new TopologyBuilder()
369             .setName(Map.of(topoName.key(), topoName))
370             .setUuid(topoUuid)
371             .setNode(Map.of(node.key(), node))
372             .build();
373     }
374
375     private Network readTopology(InstanceIdentifier<Network> networkIID) throws TapiTopologyException {
376         Network topology = null;
377         FluentFuture<Optional<Network>> topologyFuture = dataBroker.newReadOnlyTransaction()
378             .read(LogicalDatastoreType.CONFIGURATION, networkIID);
379         try {
380             topology = topologyFuture.get().orElseThrow();
381         } catch (InterruptedException e) {
382             Thread.currentThread().interrupt();
383             throw new TapiTopologyException("Unable to get from mdsal topology: " + networkIID
384                 .firstKeyOf(Network.class).getNetworkId().getValue(), e);
385         } catch (ExecutionException e) {
386             throw new TapiTopologyException("Unable to get from mdsal topology: " + networkIID
387                 .firstKeyOf(Network.class).getNetworkId().getValue(), e);
388         }
389         return topology;
390     }
391
392     private Topology createAbstractedOtnTopology() throws TapiTopologyException {
393         // read openroadm-topology
394         Network openroadmTopo = readTopology(InstanceIdentifiers.OVERLAY_NETWORK_II);
395         List<Link> linkList = new ArrayList<>();
396         if (openroadmTopo.augmentation(Network1.class) != null) {
397             linkList.addAll(openroadmTopo.augmentation(Network1.class).getLink().values());
398         }
399         List<Link> xponderOutLinkList = linkList.stream()
400                 .filter(lk -> lk.augmentation(Link1.class).getLinkType().equals(OpenroadmLinkType.XPONDEROUTPUT))
401                 .collect(Collectors.toList());
402         List<Link> xponderInLinkList = linkList.stream()
403                 .filter(lk -> lk.augmentation(Link1.class).getLinkType().equals(OpenroadmLinkType.XPONDERINPUT))
404                 .collect(Collectors.toList());
405         // read otn-topology
406         Network otnTopo = readTopology(InstanceIdentifiers.OTN_NETWORK_II);
407         Map<NodeId, Node> otnNodeMap = otnTopo.nonnullNode().values().stream()
408             .collect(Collectors.toMap(Node::getNodeId, node -> node));
409
410         Map<String, List<String>> networkPortMap = new HashMap<>();
411         Iterator<Entry<NodeId, Node>> itOtnNodeMap = otnNodeMap.entrySet().iterator();
412         while (itOtnNodeMap.hasNext()) {
413             Entry<NodeId, Node> entry = itOtnNodeMap.next();
414             String portMappingNodeId = entry.getValue().getSupportingNode().values().stream()
415                 .filter(sn -> sn.getNetworkRef().getValue().equals(NetworkUtils.UNDERLAY_NETWORK_ID))
416                 .findFirst()
417                 .orElseThrow().getNodeRef().getValue();
418             List<String> networkPortList = new ArrayList<>();
419             for (TerminationPoint tp: entry.getValue().augmentation(Node1.class).getTerminationPoint().values()) {
420                 if (tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERNETWORK)
421                         && checkTp(entry.getKey().getValue(), portMappingNodeId, tp, xponderOutLinkList,
422                         xponderInLinkList)) {
423                     networkPortList.add(tp.getTpId().getValue());
424                 }
425             }
426             if (!networkPortList.isEmpty()) {
427                 networkPortMap.put(entry.getKey().getValue(), networkPortList);
428             }
429         }
430         Map<NodeKey, org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node>
431             tapiNodeList = new HashMap<>();
432         Map<LinkKey, org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Link>
433             tapiLinkList = new HashMap<>();
434         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(TapiStringConstants.T0_MULTILAYER
435             .getBytes(Charset.forName("UTF-8"))).toString());
436         ConvertORTopoToTapiTopo tapiFactory = new ConvertORTopoToTapiTopo(topoUuid, this.tapiLink);
437         Iterator<Entry<String, List<String>>> it = networkPortMap.entrySet().iterator();
438         while (it.hasNext()) {
439             String nodeId = it.next().getKey();
440             tapiFactory.convertNode(otnNodeMap.get(new NodeId(nodeId)), networkPortMap.get(nodeId));
441             tapiNodeList.putAll(tapiFactory.getTapiNodes());
442             tapiLinkList.putAll(tapiFactory.getTapiLinks());
443         }
444         if (openroadmTopo.nonnullNode().values().stream()
445                 .filter(nt -> nt
446                     .augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.Node1.class)
447                     .getNodeType()
448                     .equals(OpenroadmNodeType.SRG))
449                 .count() > 0) {
450             tapiFactory.convertRoadmInfrastructure();
451             tapiNodeList.putAll(tapiFactory.getTapiNodes());
452             tapiLinkList.putAll(tapiFactory.getTapiLinks());
453         } else {
454             LOG.warn("Unable to abstract an ROADM infrasctructure from openroadm-topology");
455         }
456         if (otnTopo.augmentation(Network1.class) != null) {
457             Map<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
458                     .ietf.network.topology.rev180226.networks.network.LinkKey, Link> otnLinkMap =
459                 otnTopo.augmentation(Network1.class).getLink();
460             tapiFactory.convertLinks(otnLinkMap);
461             tapiLinkList.putAll(tapiFactory.getTapiLinks());
462         }
463         Name name = new NameBuilder()
464             .setValue(TapiStringConstants.T0_MULTILAYER)
465             .setValueName("TAPI Topology Name")
466             .build();
467         return new TopologyBuilder()
468                 .setName(Map.of(name.key(), name))
469                 .setUuid(topoUuid)
470                 .setNode(tapiNodeList)
471                 .setLink(tapiLinkList).build();
472     }
473
474     @Override
475     public ListenableFuture<RpcResult<GetNodeEdgePointDetailsOutput>> getNodeEdgePointDetails(
476             GetNodeEdgePointDetailsInput input) {
477         // TODO Auto-generated method stub
478         // TODO -> maybe we get errors when having CEPs?
479         Uuid topoUuid = input.getTopologyId();
480         // Node id: if roadm -> ROADMid+PHOTONIC_MEDIA. if xpdr -> XPDRid-XPDRnbr+DSR/OTSi
481         Uuid nodeUuid = input.getNodeId();
482         // NEP id: if roadm -> ROADMid+PHOTONIC_MEDIA/MC/OTSiMC+TPid.
483         // if xpdr -> XPDRid-XPDRnbr+DSR/eODU/iODU/iOTSi/eOTSi/PHOTONIC_MEDIA+TPid
484         Uuid nepUuid = input.getNodeEdgePointId();
485         OwnedNodeEdgePoint nep = this.tapiContext.getTapiNEP(topoUuid, nodeUuid, nepUuid);
486         if (nep == null) {
487             LOG.error("Invalid TAPI nep name");
488             return RpcResultBuilder.<GetNodeEdgePointDetailsOutput>failed()
489                 .withError(ErrorType.RPC, "Invalid NEP name")
490                 .buildFuture();
491         }
492         return RpcResultBuilder.success(new GetNodeEdgePointDetailsOutputBuilder()
493                 .setNodeEdgePoint(new NodeEdgePointBuilder(nep).build()).build()).buildFuture();
494     }
495
496     @Override
497     public ListenableFuture<RpcResult<GetLinkDetailsOutput>> getLinkDetails(GetLinkDetailsInput input) {
498         // TODO Auto-generated method stub
499         Uuid topoUuid = input.getTopologyId();
500         // Link id: same as OR link id
501         Uuid linkUuid = input.getLinkId();
502         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Link link = this.tapiContext
503                 .getTapiLink(topoUuid, linkUuid);
504         if (link == null) {
505             LOG.error("Invalid TAPI link name");
506             return RpcResultBuilder.<GetLinkDetailsOutput>failed()
507                 .withError(ErrorType.RPC, "Invalid Link name")
508                 .buildFuture();
509         }
510         return RpcResultBuilder.success(new GetLinkDetailsOutputBuilder().setLink(new LinkBuilder(link).build())
511                 .build()).buildFuture();
512     }
513
514     @Override
515     public ListenableFuture<RpcResult<GetTopologyListOutput>> getTopologyList(GetTopologyListInput input) {
516         // TODO Auto-generated method stub
517         // TODO -> maybe we get errors when having CEPs?
518         Map<TopologyKey,
519                 org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology>
520                 topologyMap = this.tapiContext.getTopologyContext();
521         if (topologyMap.isEmpty()) {
522             LOG.error("No topologies exist in tapi context");
523             return RpcResultBuilder.<GetTopologyListOutput>failed()
524                 .withError(ErrorType.APPLICATION, "No topologies exist in tapi context")
525                 .buildFuture();
526         }
527         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.get.topology.list.output.TopologyKey,
528             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.get.topology.list.output.Topology>
529                 newTopoMap = new HashMap<>();
530         for (org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology
531                 topo:topologyMap.values()) {
532             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.get.topology.list.output.Topology
533                 newTopo = new org.opendaylight.yang.gen.v1.urn
534                     .onf.otcc.yang.tapi.topology.rev221121.get.topology.list.output.TopologyBuilder(topo).build();
535             newTopoMap.put(newTopo.key(), newTopo);
536         }
537         return RpcResultBuilder.success(new GetTopologyListOutputBuilder().setTopology(newTopoMap).build())
538                 .buildFuture();
539     }
540
541     private org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Node
542             createTapiNode(List<OwnedNodeEdgePoint> nepList, Uuid topoUuid) {
543         Name name = new NameBuilder().setValueName("Tpdr100g node name").setValue("Tpdr100g over WDM node").build();
544         Map<OwnedNodeEdgePointKey, OwnedNodeEdgePoint> onepMap = new HashMap<>();
545         for (OwnedNodeEdgePoint ownedNodeEdgePoint : nepList) {
546             onepMap.put(ownedNodeEdgePoint.key(), ownedNodeEdgePoint);
547         }
548         Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes(name.getValue().getBytes(Charset.forName("UTF-8"))).toString());
549         return new NodeBuilder()
550             .setUuid(nodeUuid)
551             .setName(Map.of(name.key(), name))
552             .setLayerProtocolName(Set.of(LayerProtocolName.ETH))
553             .setAdministrativeState(AdministrativeState.UNLOCKED)
554             .setOperationalState(OperationalState.ENABLED)
555             .setLifecycleState(LifecycleState.INSTALLED)
556             .setOwnedNodeEdgePoint(onepMap)
557             .setNodeRuleGroup(createNodeRuleGroupFor100gTpdrNode(topoUuid, nodeUuid, nepList))
558             .build();
559     }
560
561     private boolean checkTp(String nodeIdTopo, String nodeIdPortMap, TerminationPoint tp, List<Link> xpdOut,
562             List<Link> xpdIn) {
563         String networkLcp;
564         if (tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERCLIENT)) {
565             networkLcp = tp.augmentation(
566                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev230526.TerminationPoint1.class)
567                 .getAssociatedConnectionMapTp().iterator().next().getValue();
568         } else {
569             networkLcp = tp.getTpId().getValue();
570         }
571         @NonNull
572         KeyedInstanceIdentifier<Mapping, MappingKey> pmIID = InstanceIdentifier.create(
573             org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220922.Network.class)
574             .child(Nodes.class, new NodesKey(nodeIdPortMap)).child(Mapping.class, new MappingKey(networkLcp));
575         @NonNull
576         FluentFuture<Optional<Mapping>> mappingOpt = dataBroker.newReadOnlyTransaction().read(
577             LogicalDatastoreType.CONFIGURATION, pmIID);
578         Mapping mapping = null;
579         if (mappingOpt.isDone()) {
580             try {
581                 mapping = mappingOpt.get().orElseThrow();
582             } catch (InterruptedException | ExecutionException e) {
583                 LOG.error("Error getting mapping for {}", networkLcp, e);
584                 return false;
585             }
586         } else {
587             LOG.error("Impossible to get mapping of associated network port {} of tp {}", networkLcp, tp.getTpId()
588                 .getValue());
589             return false;
590         }
591         String networkPortDirection = mapping.getPortDirection();
592         long count = 0;
593         switch (networkPortDirection) {
594             case "bidirectional":
595                 count += xpdOut.stream().filter(lk -> lk.getSource().getSourceNode().getValue().equals(nodeIdTopo)
596                     && lk.getSource().getSourceTp().getValue().equals(networkLcp)).count();
597                 count += xpdIn.stream().filter(lk -> lk.getDestination().getDestNode().getValue().equals(nodeIdTopo)
598                     && lk.getDestination().getDestTp().getValue().equals(networkLcp)).count();
599                 return (count == 2);
600             case "tx":
601             case "rx":
602                 @Nullable
603                 String partnerLcp = mapping.getPartnerLcp();
604                 if (mapping.getPortQual().equals("tx")) {
605                     count += xpdOut.stream().filter(lk -> lk.getSource().getSourceNode().getValue().equals(nodeIdTopo)
606                         && lk.getSource().getSourceTp().getValue().equals(networkLcp)).count();
607                     count += xpdIn.stream().filter(lk -> lk.getDestination().getDestNode().getValue().equals(nodeIdTopo)
608                         && lk.getDestination().getDestTp().getValue().equals(partnerLcp)).count();
609                 }
610                 if (mapping.getPortQual().equals("rx")) {
611                     count += xpdIn.stream().filter(lk -> lk.getDestination().getDestNode().getValue().equals(nodeIdTopo)
612                         && lk.getDestination().getDestTp().getValue().equals(networkLcp)).count();
613                     count += xpdOut.stream().filter(lk -> lk.getSource().getSourceNode().getValue().equals(nodeIdTopo)
614                         && lk.getSource().getSourceTp().getValue().equals(partnerLcp)).count();
615                 }
616                 return (count == 2);
617             default:
618                 LOG.error("Invalid port direction for {}", networkLcp);
619                 return false;
620         }
621     }
622
623     private Map<NodeRuleGroupKey, NodeRuleGroup> createNodeRuleGroupFor100gTpdrNode(Uuid topoUuid, Uuid nodeUuid,
624             Collection<OwnedNodeEdgePoint> onepl) {
625
626         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.rule.group.NodeEdgePointKey,
627             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.rule.group.NodeEdgePoint>
628             nepMap = new HashMap<>();
629         for (OwnedNodeEdgePoint onep: onepl) {
630             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.rule.group.NodeEdgePoint nep =
631                 new org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.node.rule.group
632                     .NodeEdgePointBuilder()
633                         .setTopologyUuid(topoUuid)
634                         .setNodeUuid(nodeUuid)
635                         .setNodeEdgePointUuid(onep.key().getUuid())
636                         .build();
637             nepMap.put(nep.key(), nep);
638         }
639         Map<NodeRuleGroupKey, NodeRuleGroup> nodeRuleGroupMap = new HashMap<>();
640         Map<RuleKey, Rule> ruleList = new HashMap<>();
641         Set<RuleType> ruleTypes = new HashSet<>();
642         ruleTypes.add(RuleType.FORWARDING);
643         Rule rule = new RuleBuilder()
644             .setLocalId("forward")
645             .setForwardingRule(FORWARDINGRULEMAYFORWARDACROSSGROUP.VALUE)
646             .setRuleType(ruleTypes)
647             .build();
648         ruleList.put(rule.key(), rule);
649         NodeRuleGroup nodeRuleGroup = new NodeRuleGroupBuilder()
650             .setUuid(new Uuid(UUID.nameUUIDFromBytes(("rdm infra node rule group").getBytes(Charset.forName("UTF-8")))
651                 .toString()))
652             .setRule(ruleList)
653             .setNodeEdgePoint(nepMap)
654             .build();
655         nodeRuleGroupMap.put(nodeRuleGroup.key(), nodeRuleGroup);
656         return nodeRuleGroupMap;
657     }
658
659     @Override
660     public ListenableFuture<RpcResult<GetServiceInterfacePointDetailsOutput>>
661             getServiceInterfacePointDetails(GetServiceInterfacePointDetailsInput input) {
662         Uuid sipUuid = input.getUuid();
663         Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips =
664             this.tapiContext.getTapiContext().getServiceInterfacePoint();
665         if (sips == null || sips.isEmpty()) {
666             return RpcResultBuilder.<GetServiceInterfacePointDetailsOutput>failed()
667                 .withError(ErrorType.RPC, "No sips in datastore")
668                 .buildFuture();
669         }
670         if (!sips.containsKey(new ServiceInterfacePointKey(sipUuid))) {
671             return RpcResultBuilder.<GetServiceInterfacePointDetailsOutput>failed()
672                 .withError(ErrorType.RPC, "Sip doesnt exist in datastore")
673                 .buildFuture();
674         }
675         org.opendaylight.yang.gen.v1.urn
676             .onf.otcc.yang.tapi.common.rev221121.get.service._interface.point.details.output.Sip outSip =
677                 new org.opendaylight.yang.gen.v1.urn
678                     .onf.otcc.yang.tapi.common.rev221121.get.service._interface.point.details.output.SipBuilder(
679                         sips.get(new ServiceInterfacePointKey(sipUuid)))
680                     .build();
681         return RpcResultBuilder.success(new GetServiceInterfacePointDetailsOutputBuilder().setSip(outSip).build())
682             .buildFuture();
683     }
684
685     @Override
686     public ListenableFuture<RpcResult<GetServiceInterfacePointListOutput>>
687             getServiceInterfacePointList(GetServiceInterfacePointListInput input) {
688         Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips =
689             this.tapiContext.getTapiContext().getServiceInterfacePoint();
690         if (sips == null || sips.isEmpty()) {
691             return RpcResultBuilder.<GetServiceInterfacePointListOutput>failed()
692                 .withError(ErrorType.RPC, "No sips in datastore")
693                 .buildFuture();
694         }
695         Map<SipKey, Sip> outSipMap = new HashMap<>();
696         for (ServiceInterfacePoint sip : sips.values()) {
697             Sip si = new SipBuilder(sip).build();
698             outSipMap.put(si.key(), si);
699         }
700         return RpcResultBuilder.success(new GetServiceInterfacePointListOutputBuilder().setSip(outSipMap).build())
701             .buildFuture();
702     }
703
704     @Override
705     public ListenableFuture<RpcResult<UpdateServiceInterfacePointOutput>>
706             updateServiceInterfacePoint(UpdateServiceInterfacePointInput input) {
707         // TODO --> not yet implemented
708         return null;
709     }
710
711 }