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