Initial OR-TAPI mapping: Services
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / connectivity / ConnectivityUtils.java
1 /*
2  * Copyright © 2018 Orange & 2021 Nokia, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.tapi.connectivity;
9
10 import java.nio.charset.Charset;
11 import java.util.ArrayList;
12 import java.util.Comparator;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.UUID;
18 import java.util.stream.Collectors;
19 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
20 import org.opendaylight.transportpce.tapi.topology.TopologyUtils;
21 import org.opendaylight.transportpce.tapi.utils.GenericServiceEndpoint;
22 import org.opendaylight.transportpce.tapi.utils.ServiceEndpointType;
23 import org.opendaylight.transportpce.tapi.utils.TapiContext;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.equipment.types.rev181130.OpticTypes;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev181130.NodeIdType;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ConnectionType;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.RpcActions;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.Service;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.sdnc.request.header.SdncRequestHeaderBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.RxDirectionBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.endpoint.TxDirectionBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.lgx.LgxBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.port.PortBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceCreateInput;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceCreateInputBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.create.input.ServiceAEnd;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.create.input.ServiceAEndBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.create.input.ServiceZEnd;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.create.input.ServiceZEndBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.PathDescription;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.atoz.direction.AToZ;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.atoz.direction.AToZKey;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.ztoa.direction.ZToA;
45 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.ztoa.direction.ZToAKey;
46 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.pce.resource.resource.resource.Node;
47 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.pce.resource.resource.resource.TerminationPoint;
48 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
49 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.AdministrativeState;
50 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.CapacityUnit;
51 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.ForwardingDirection;
52 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.LayerProtocolName;
53 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.LifecycleState;
54 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.OperationalState;
55 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.PortDirection;
56 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.PortRole;
57 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
58 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.capacity.BandwidthProfileBuilder;
59 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.capacity.TotalSizeBuilder;
60 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.Name;
61 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.NameBuilder;
62 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePoint;
63 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePointKey;
64 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.ProtectionRole;
65 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPoint;
66 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointBuilder;
67 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPointKey;
68 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connection.end.point.ClientNodeEdgePoint;
69 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connection.end.point.ClientNodeEdgePointBuilder;
70 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectivityService;
71 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectivityServiceBuilder;
72 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.Connection;
73 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.ConnectionBuilder;
74 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.ConnectionKey;
75 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.EndPoint;
76 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.EndPointBuilder;
77 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.EndPointKey;
78 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.end.point.CapacityBuilder;
79 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.service.end.point.ServiceInterfacePointBuilder;
80 import org.opendaylight.yangtools.yang.common.Uint32;
81 import org.opendaylight.yangtools.yang.common.Uint64;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
84
85 public final class ConnectivityUtils {
86
87     private static final String LGX_PORT_NAME = "Some lgx-port-name";
88     private static final String PORT_TYPE = "some port type";
89     private static final String LGX_DEVICE_NAME = "Some lgx-device-name";
90     private static final String PORT_RACK_VALUE = "000000.00";
91     private static final String DSR = "DSR";
92     private static final String ODU = "ODU";
93     private static final String E_ODU = "eODU";
94     private static final String I_ODU = "iODU";
95     private static final String OTSI = "OTSi";
96     private static final String E_OTSI = "eOTSi";
97     private static final String I_OTSI = "iOTSi";
98     private static final String PHTNC_MEDIA = "PHOTONIC_MEDIA";
99     private static final String MC = "MEDIA_CHANNEL";
100     private static final String OTSI_MC = "OTSi_MEDIA_CHANNEL";
101     private static final String TP = "TerminationPoint";
102     private static final String NODE = "Node";
103     private static final Logger LOG = LoggerFactory.getLogger(ConnectivityUtils.class);
104
105     private final ServiceDataStoreOperations serviceDataStoreOperations;
106     private final TapiContext tapiContext;
107     private Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap;
108     private final Map<org.opendaylight.yang.gen.v1.urn
109         .onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectionKey,
110         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection>
111         connectionFullMap; // this variable is for complete connection objects
112
113     // TODO -> handle cases for which node id is ROADM-A1 and not ROADMA01 or XPDR-A1 and not XPDRA01
114     public ConnectivityUtils(ServiceDataStoreOperations serviceDataStoreOperations,
115                              Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap, TapiContext tapiContext) {
116         this.serviceDataStoreOperations = serviceDataStoreOperations;
117         this.tapiContext = tapiContext;
118         this.sipMap = sipMap;
119         this.connectionFullMap = new HashMap<>();
120     }
121
122     public static ServiceCreateInput buildServiceCreateInput(GenericServiceEndpoint sepA, GenericServiceEndpoint sepZ) {
123         ServiceAEnd serviceAEnd = getServiceAEnd(sepA, sepZ);
124         ServiceZEnd serviceZEnd = getServiceZEnd(sepA, sepZ);
125         if (serviceAEnd == null || serviceZEnd == null) {
126             LOG.warn("One of the endpoints could not be identified");
127             return null;
128         }
129         return new ServiceCreateInputBuilder()
130             .setCommonId("commonId")
131             .setConnectionType(ConnectionType.Service)
132             .setCustomer("Customer")
133             .setServiceName("service test")
134             .setServiceAEnd(serviceAEnd)
135             .setServiceZEnd(serviceZEnd)
136             .setSdncRequestHeader(new SdncRequestHeaderBuilder().setRequestId("request-1")
137                 .setRpcAction(RpcActions.ServiceCreate).setNotificationUrl("notification url").setRequestSystemId(
138                     "appname")
139                 .build())
140             .build();
141     }
142
143     public static ServiceAEnd buildServiceAEnd(String nodeid, String clli, String txPortDeviceName,
144                                                String txPortName, String rxPortDeviceName, String rxPortName) {
145         return new ServiceAEndBuilder()
146             .setClli(clli)
147             .setNodeId(new NodeIdType(nodeid))
148             .setOpticType(OpticTypes.Gray)
149             .setServiceFormat(ServiceFormat.Ethernet)
150             .setServiceRate(Uint32.valueOf(100))
151             .setTxDirection(new TxDirectionBuilder()
152                 .setPort(new PortBuilder()
153                     .setPortDeviceName(txPortDeviceName)
154                     .setPortName(txPortName)
155                     .setPortRack(PORT_RACK_VALUE)
156                     .setPortShelf("00")
157                     .setPortType(PORT_TYPE)
158                     .build())
159                 .setLgx(new LgxBuilder()
160                     .setLgxDeviceName(LGX_DEVICE_NAME)
161                     .setLgxPortName(LGX_PORT_NAME)
162                     .setLgxPortRack(PORT_RACK_VALUE)
163                     .setLgxPortShelf("00")
164                     .build())
165                 .build())
166             .setRxDirection(new RxDirectionBuilder()
167                 .setPort(new PortBuilder()
168                     .setPortDeviceName(rxPortDeviceName)
169                     .setPortName(rxPortName)
170                     .setPortRack(PORT_RACK_VALUE)
171                     .setPortShelf("00")
172                     .setPortType(PORT_TYPE)
173                     .build())
174                 .setLgx(new LgxBuilder()
175                     .setLgxDeviceName(LGX_DEVICE_NAME)
176                     .setLgxPortName(LGX_PORT_NAME)
177                     .setLgxPortRack(PORT_RACK_VALUE)
178                     .setLgxPortShelf("00")
179                     .build())
180                 .build())
181             .build();
182     }
183
184     public static ServiceZEnd buildServiceZEnd(String nodeid, String clli, String txPortDeviceName,
185                                                String txPortName, String rxPortDeviceName, String rxPortName) {
186         return  new ServiceZEndBuilder().setClli(clli).setNodeId(new NodeIdType(nodeid))
187             .setOpticType(OpticTypes.Gray)
188             .setServiceFormat(ServiceFormat.Ethernet)
189             .setServiceRate(Uint32.valueOf(100))
190             .setTxDirection(new TxDirectionBuilder()
191                 .setPort(new PortBuilder()
192                     .setPortDeviceName(txPortDeviceName)
193                     .setPortName(txPortName)
194                     .setPortRack(PORT_RACK_VALUE)
195                     .setPortShelf("00")
196                     .setPortType(PORT_TYPE)
197                     .build())
198                 .setLgx(new LgxBuilder()
199                     .setLgxDeviceName(LGX_DEVICE_NAME)
200                     .setLgxPortName(LGX_PORT_NAME)
201                     .setLgxPortRack(PORT_RACK_VALUE)
202                     .setLgxPortShelf("00")
203                     .build())
204                 .build())
205             .setRxDirection(new RxDirectionBuilder()
206                 .setPort(new PortBuilder()
207                     .setPortDeviceName(rxPortDeviceName)
208                     .setPortName(rxPortName)
209                     .setPortRack(PORT_RACK_VALUE)
210                     .setPortShelf("00")
211                     .setPortType(PORT_TYPE)
212                     .build())
213                 .setLgx(new LgxBuilder()
214                     .setLgxDeviceName(LGX_DEVICE_NAME)
215                     .setLgxPortName(LGX_PORT_NAME)
216                     .setLgxPortRack(PORT_RACK_VALUE)
217                     .setLgxPortShelf("00")
218                     .build())
219                 .build())
220             .build();
221     }
222
223     private static ServiceAEnd getServiceAEnd(GenericServiceEndpoint sepA, GenericServiceEndpoint sepZ) {
224         if (sepA.getType().equals(ServiceEndpointType.SERVICEAEND)) {
225             return new ServiceAEndBuilder(sepA.getValue()).build();
226         } else if (sepZ.getType().equals(ServiceEndpointType.SERVICEAEND)) {
227             return new ServiceAEndBuilder(sepZ.getValue()).build();
228         } else {
229             return null;
230         }
231     }
232
233     private static ServiceZEnd getServiceZEnd(GenericServiceEndpoint sepA, GenericServiceEndpoint sepZ) {
234         if (sepA.getType().equals(ServiceEndpointType.SERVICEZEND)) {
235             return new ServiceZEndBuilder(sepA.getValue()).build();
236         } else if (sepZ.getType().equals(ServiceEndpointType.SERVICEZEND)) {
237             return new ServiceZEndBuilder(sepZ.getValue()).build();
238         } else {
239             return null;
240         }
241     }
242
243     public void setSipMap(Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips) {
244         this.sipMap = sips;
245     }
246
247     public ConnectivityService mapORServiceToTapiConnectivity(Service service) {
248         // Get service path with the description in OR based models.
249         LOG.info("Service = {}", service);
250         Optional<ServicePaths> optServicePaths =
251             this.serviceDataStoreOperations.getServicePath(service.getServiceName());
252         if (!optServicePaths.isPresent()) {
253             LOG.error("No service path found for service {}", service.getServiceName());
254             return null;
255         }
256         ServicePaths servicePaths = optServicePaths.get();
257         PathDescription pathDescription = servicePaths.getPathDescription();
258         LOG.info("Path description of service = {}", pathDescription);
259         org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceAEnd serviceAEnd
260             = service.getServiceAEnd();
261         // Endpoint creation
262         EndPoint endPoint1 = mapServiceAEndPoint(serviceAEnd, pathDescription);
263         org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceZEnd serviceZEnd
264             = service.getServiceZEnd();
265         EndPoint endPoint2 = mapServiceZEndPoint(serviceZEnd, pathDescription);
266         Map<EndPointKey, EndPoint> endPointMap = new HashMap<>();
267         endPointMap.put(endPoint1.key(), endPoint1);
268         endPointMap.put(endPoint2.key(), endPoint2);
269         LOG.info("EndPoints of connectivity services = {}", endPointMap);
270         // Services Names
271         Name name = new NameBuilder().setValueName("Connectivity Service Name").setValue(service.getServiceName())
272             .build();
273         // Connection creation
274         Map<ConnectionKey, Connection> connMap =
275             createConnectionsFromService(serviceAEnd, serviceZEnd, pathDescription);
276         // TODO: full connectivity service?? With constraints and the rest of fields...
277         return new ConnectivityServiceBuilder()
278             .setAdministrativeState(AdministrativeState.UNLOCKED)
279             .setOperationalState(OperationalState.ENABLED)
280             .setLifecycleState(LifecycleState.INSTALLED)
281             .setUuid(new Uuid(UUID.nameUUIDFromBytes(service.getServiceName().getBytes(Charset.forName("UTF-8")))
282                 .toString()))
283             .setConnectivityDirection(ForwardingDirection.BIDIRECTIONAL)
284             .setName(Map.of(name.key(), name))
285             .setConnection(connMap)
286             .setEndPoint(endPointMap)
287             .build();
288     }
289
290     private Map<ConnectionKey, Connection> createConnectionsFromService(
291             org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceAEnd
292                 serviceAEnd,
293             org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceZEnd
294                 serviceZEnd,
295         PathDescription pathDescription) {
296         Map<ConnectionKey, Connection> connectionServMap = new HashMap<>();
297         // build lists with ROADM nodes, XPDR/MUX/SWITCH nodes, ROADM DEG TTPs, ROADM SRG TTPs, XPDR CLIENT TTPs
298         //  and XPDR NETWORK TTPs (if any). From the path description. This will help to build the uuid of the CEPs
299         //  and the connections
300         String resourceType;
301         List<String> xpdrClientTplist = new ArrayList<>();
302         List<String> xpdrNetworkTplist = new ArrayList<>();
303         List<String> rdmAddDropTplist = new ArrayList<>();
304         List<String> rdmDegTplist = new ArrayList<>();
305         List<String> rdmNodelist = new ArrayList<>();
306         List<String> xpdrNodelist = new ArrayList<>();
307         for (AToZ elem:pathDescription.getAToZDirection().getAToZ().values().stream()
308             .sorted(Comparator.comparing(AToZ::getId)).collect(Collectors.toList())) {
309             resourceType = elem.getResource().getResource().implementedInterface().getSimpleName();
310             switch (resourceType) {
311                 case TP:
312                     TerminationPoint tp = (TerminationPoint) elem.getResource().getResource();
313                     String tpID = tp.getTpId();
314                     String tpNode;
315                     if (tpID.contains("CLIENT")) {
316                         tpNode = tp.getTpNodeId();
317                         if (!xpdrClientTplist.contains(String.join("+", tpNode, tpID))) {
318                             xpdrClientTplist.add(String.join("+", tpNode, tpID));
319                         }
320                     }
321                     if (tpID.contains("NETWORK")) {
322                         tpNode = tp.getTpNodeId();
323                         if (!xpdrNetworkTplist.contains(String.join("+", tpNode, tpID))) {
324                             xpdrNetworkTplist.add(String.join("+", tpNode, tpID));
325                         }
326                     }
327                     if (tpID.contains("PP")) {
328                         tpNode = getIdBasedOnModelVersion(tp.getTpNodeId());
329                         LOG.info("ROADM Node of tp = {}", tpNode);
330                         if (!rdmAddDropTplist.contains(String.join("+", tpNode, tpID))) {
331                             rdmAddDropTplist.add(String.join("+", tpNode, tpID));
332                         }
333                     }
334                     if (tpID.contains("TTP")) {
335                         tpNode = getIdBasedOnModelVersion(tp.getTpNodeId());
336                         LOG.info("ROADM Node of tp = {}", tpNode);
337                         if (!rdmDegTplist.contains(String.join("+", tpNode, tpID))) {
338                             rdmDegTplist.add(String.join("+", tpNode, tpID));
339                         }
340                     }
341                     break;
342                 case NODE:
343                     Node node = (Node) elem.getResource().getResource();
344                     String nodeId = node.getNodeId();
345                     if (nodeId.contains("XPDR") || nodeId.contains("SPDR") || nodeId.contains("MXPDR")) {
346                         LOG.info("Node id = {}", nodeId);
347                         if (!xpdrNodelist.contains(nodeId)) {
348                             xpdrNodelist.add(nodeId); // should contain only 2
349                         }
350                     }
351                     if (nodeId.contains("ROADM")) {
352                         nodeId = getIdBasedOnModelVersion(nodeId);
353                         LOG.info("Node id = {}", nodeId);
354                         if (!rdmNodelist.contains(nodeId)) {
355                             rdmNodelist.add(nodeId);
356                         }
357                     }
358                     break;
359                 default:
360                     LOG.warn("Resource is a {}", resourceType);
361             }
362         }
363         LOG.info("ROADM node list = {}", rdmNodelist);
364         LOG.info("ROADM degree list = {}", rdmDegTplist);
365         LOG.info("ROADM addrop list = {}", rdmAddDropTplist);
366         LOG.info("XPDR node list = {}", xpdrNodelist);
367         LOG.info("XPDR network list = {}", xpdrNetworkTplist);
368         LOG.info("XPDR client list = {}", xpdrClientTplist);
369         // TODO -> for 10GB eth and ODU services there are no ROADMs in path description as they use the OTU link,
370         //  but for 100GB eth all is created at once. Check if the roadm list is empty to determine whether we need
371         //  to trigger all the steps or not
372         String edgeRoadm1 = "";
373         String edgeRoadm2 = "";
374         if (!rdmNodelist.isEmpty()) {
375             edgeRoadm1 = rdmNodelist.get(0);
376             edgeRoadm2 = rdmNodelist.get(rdmNodelist.size() - 1);
377             LOG.info("edgeRoadm1 = {}", edgeRoadm1);
378             LOG.info("edgeRoadm2 = {}", edgeRoadm2);
379         }
380         // create corresponding CEPs and Connections. Connections should be added to the corresponding context
381         // CEPs must be included in the topology context as an augmentation for each ONEP!!
382         ServiceFormat serviceFormat = serviceAEnd.getServiceFormat(); // should be equal to serviceZEnd
383         // TODO -> better mechanism to map services???
384         switch (serviceFormat) {
385             case OC:
386                 // Identify number of ROADMs
387                 // - XC Connection between MC CEPs mapped from MC NEPs (within a roadm)
388                 // - XC Connection between OTSiMC CEPs mapped from OTSiMC NEPs (within a roadm)
389                 // - Top Connection MC betwwen MC CEPs of different roadms
390                 // - Top Connection OTSiMC betwwen OTSiMC CEPs of extreme roadms
391                 connectionServMap.putAll(createRoadmCepsAndConnections(rdmAddDropTplist, rdmDegTplist, rdmNodelist,
392                     edgeRoadm1, edgeRoadm2));
393                 break;
394             case OTU:
395                 // Identify number of ROADMs between XPDRs and check if OC is created
396                 // - XC Connection between MC CEPs mapped from MC NEPs (within a roadm)
397                 // - Top Connection MC betwwen MC CEPs of different roadms
398                 // - XC Connection between OTSiMC CEPs mapped from OTSiMC NEPs (within a roadm)
399                 // - Top Connection OTSiMC betwwen OTSiMC CEPs of different roadms
400                 connectionServMap.putAll(createRoadmCepsAndConnections(rdmAddDropTplist, rdmDegTplist, rdmNodelist,
401                     edgeRoadm1, edgeRoadm2));
402                 // - XC Connection OTSi betwwen iOTSi y eOTSi of xpdr
403                 // - Top connection OTSi between network ports of xpdrs in the Photonic media layer -> i_OTSi
404                 connectionServMap.putAll(createXpdrCepsAndConnectionsPht(xpdrNetworkTplist, xpdrNodelist));
405                 break;
406             case ODU:
407                 // Check if OC and OTU are created
408                 if (!rdmNodelist.isEmpty()) {
409                     connectionServMap.putAll(createRoadmCepsAndConnections(rdmAddDropTplist, rdmDegTplist, rdmNodelist,
410                         edgeRoadm1, edgeRoadm2));
411                     connectionServMap.putAll(createXpdrCepsAndConnectionsPht(xpdrNetworkTplist, xpdrNodelist));
412                 }
413                 // - XC Connection OTSi betwwen iODU and eODU of xpdr
414                 // - Top connection in the ODU layer, between xpdr eODU ports (?)
415                 connectionServMap.putAll(createXpdrCepsAndConnectionsOdu(xpdrNetworkTplist, xpdrNodelist));
416                 break;
417             case Ethernet:
418                 // Check if OC, OTU and ODU are created
419                 if (!rdmNodelist.isEmpty()) {
420                     connectionServMap.putAll(createRoadmCepsAndConnections(rdmAddDropTplist, rdmDegTplist, rdmNodelist,
421                         edgeRoadm1, edgeRoadm2));
422                     connectionServMap.putAll(createXpdrCepsAndConnectionsPht(xpdrNetworkTplist, xpdrNodelist));
423                     connectionServMap.putAll(createXpdrCepsAndConnectionsOdu(xpdrNetworkTplist, xpdrNodelist));
424                 }
425                 // Top connection in the DSR layer, between client ports of the xpdrs
426                 connectionServMap.putAll(createXpdrCepsAndConnectionsDsr(xpdrClientTplist, xpdrNodelist));
427                 break;
428             default:
429                 LOG.error("Service type format not supported");
430         }
431         return connectionServMap;
432     }
433
434     private Map<ConnectionKey,Connection> createXpdrCepsAndConnectionsDsr(List<String> xpdrClientTplist,
435                                                                           List<String> xpdrNodelist) {
436         Map<ConnectionKey, Connection> connServMap = new HashMap<>();
437         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointKey,
438             ConnectionEndPoint> cepMap = new HashMap<>();
439
440         // Create 1 cep per Xpdr in the CLIENT and a top connection DSR between the CLIENT xpdrs
441         for (String xpdr:xpdrNodelist) {
442             LOG.info("Creating ceps and xc for xpdr {}", xpdr);
443             String spcXpdrClient = xpdrClientTplist.stream().filter(netp -> netp.contains(xpdr)).findFirst().get();
444
445             ConnectionEndPoint netCep1 = createCepXpdr(spcXpdrClient, DSR, DSR, LayerProtocolName.DSR);
446             putXpdrCepInTopologyContext(xpdr, spcXpdrClient, DSR, DSR, netCep1);
447
448             cepMap.put(netCep1.key(), netCep1);
449         }
450
451         // DSR top connection between edge xpdr CLIENT DSR
452         String spcXpdr1 = xpdrClientTplist.stream().filter(adp -> adp.contains(xpdrNodelist
453             .get(0))).findFirst().get();
454         String spcXpdr2 = xpdrClientTplist.stream().filter(adp -> adp.contains(xpdrNodelist
455             .get(xpdrNodelist.size() - 1))).findFirst().get();
456         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
457             connection = createTopConnection(spcXpdr1, spcXpdr2, cepMap, DSR, LayerProtocolName.DSR);
458         this.connectionFullMap.put(connection.key(), connection);
459
460         // ODU top connection that will be added to the service object
461         Connection conn = new ConnectionBuilder().setConnectionUuid(connection.getUuid()).build();
462         connServMap.put(conn.key(), conn);
463
464         return connServMap;
465     }
466
467     private Map<ConnectionKey, Connection> createXpdrCepsAndConnectionsOdu(List<String> xpdrNetworkTplist,
468                                                                            List<String> xpdrNodelist) {
469         Map<ConnectionKey, Connection> connServMap = new HashMap<>();
470         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointKey,
471             ConnectionEndPoint> cepMap = new HashMap<>();
472         // Create 1 cep per Xpdr in the I_ODU and E_ODU, X connection between iODU and eODU and a top
473         // connection iODU between the xpdrs
474         for (String xpdr:xpdrNodelist) {
475             LOG.info("Creating ceps and xc for xpdr {}", xpdr);
476             String spcXpdrNetwork = xpdrNetworkTplist.stream().filter(netp -> netp.contains(xpdr)).findFirst().get();
477
478             ConnectionEndPoint netCep1 = createCepXpdr(spcXpdrNetwork, E_ODU, DSR, LayerProtocolName.ODU);
479             putXpdrCepInTopologyContext(xpdr, spcXpdrNetwork, E_ODU, DSR, netCep1);
480             ConnectionEndPoint netCep2 = createCepXpdr(spcXpdrNetwork, I_ODU, DSR, LayerProtocolName.ODU);
481             putXpdrCepInTopologyContext(xpdr, spcXpdrNetwork, I_ODU, DSR, netCep2);
482
483             cepMap.put(netCep1.key(), netCep1);
484             cepMap.put(netCep2.key(), netCep2);
485
486             // Create x connection between I_ODU and E_ODU within xpdr
487             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
488                 connection = createXCBetweenCeps(netCep1, netCep2, spcXpdrNetwork, spcXpdrNetwork, ODU,
489                 LayerProtocolName.ODU);
490             this.connectionFullMap.put(connection.key(), connection);
491
492             // Create X connection that will be added to the service object
493             Connection conn = new ConnectionBuilder().setConnectionUuid(connection.getUuid()).build();
494             connServMap.put(conn.key(), conn);
495         }
496
497         // ODU top connection between edge xpdr e_ODU
498         String spcXpdr1 = xpdrNetworkTplist.stream().filter(adp -> adp.contains(xpdrNodelist
499             .get(0))).findFirst().get();
500         String spcXpdr2 = xpdrNetworkTplist.stream().filter(adp -> adp.contains(xpdrNodelist
501             .get(xpdrNodelist.size() - 1))).findFirst().get();
502         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
503             connection = createTopConnection(spcXpdr1, spcXpdr2, cepMap, E_ODU, LayerProtocolName.ODU);
504         this.connectionFullMap.put(connection.key(), connection);
505
506         // ODU top connection that will be added to the service object
507         Connection conn = new ConnectionBuilder().setConnectionUuid(connection.getUuid()).build();
508         connServMap.put(conn.key(), conn);
509
510         return connServMap;
511     }
512
513     private Map<ConnectionKey, Connection> createXpdrCepsAndConnectionsPht(List<String> xpdrNetworkTplist,
514                                                                            List<String> xpdrNodelist) {
515         Map<ConnectionKey, Connection> connServMap = new HashMap<>();
516         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointKey,
517             ConnectionEndPoint> cepMap = new HashMap<>();
518
519         // create ceps and x connections within xpdr
520         for (String xpdr:xpdrNodelist) {
521             LOG.info("Creating ceps and xc for xpdr {}", xpdr);
522             String spcXpdrNetwork = xpdrNetworkTplist.stream().filter(netp -> netp.contains(xpdr)).findFirst().get();
523             // There should be 1 network tp per xpdr
524             // TODO photonic media model should be updated to have the corresponding CEPs. I will just create
525             //  3 different MC CEPs giving different IDs to show that they are different
526             // Create 3 CEPs for each xpdr otsi node and the corresponding cross connection matchin the NEPs
527             ConnectionEndPoint netCep1 = createCepXpdr(spcXpdrNetwork, PHTNC_MEDIA, OTSI,
528                 LayerProtocolName.PHOTONICMEDIA);
529             putXpdrCepInTopologyContext(xpdr, spcXpdrNetwork, PHTNC_MEDIA, OTSI, netCep1);
530             ConnectionEndPoint netCep2 = createCepXpdr(spcXpdrNetwork, E_OTSI, OTSI, LayerProtocolName.PHOTONICMEDIA);
531             putXpdrCepInTopologyContext(xpdr, spcXpdrNetwork, E_OTSI, OTSI, netCep2);
532             ConnectionEndPoint netCep3 = createCepXpdr(spcXpdrNetwork, I_OTSI, OTSI, LayerProtocolName.PHOTONICMEDIA);
533             putXpdrCepInTopologyContext(xpdr, spcXpdrNetwork, I_OTSI, OTSI, netCep3);
534             cepMap.put(netCep1.key(), netCep1);
535             cepMap.put(netCep2.key(), netCep2);
536             cepMap.put(netCep3.key(), netCep3);
537
538             // Create x connection between I_OTSi and E_OTSi within xpdr
539             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
540                 connection = createXCBetweenCeps(netCep2, netCep3, spcXpdrNetwork, spcXpdrNetwork, OTSI,
541                 LayerProtocolName.PHOTONICMEDIA);
542             this.connectionFullMap.put(connection.key(), connection);
543
544             // Create X connection that will be added to the service object
545             Connection conn = new ConnectionBuilder().setConnectionUuid(connection.getUuid()).build();
546             connServMap.put(conn.key(), conn);
547         }
548         // OTSi top connection between edge I_OTSI Xpdr
549         String spcXpdr1 = xpdrNetworkTplist.stream().filter(adp -> adp.contains(xpdrNodelist
550             .get(0))).findFirst().get();
551         String spcXpdr2 = xpdrNetworkTplist.stream().filter(adp -> adp.contains(xpdrNodelist
552             .get(xpdrNodelist.size() - 1))).findFirst().get();
553         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
554             connection = createTopConnection(spcXpdr1, spcXpdr2, cepMap, I_OTSI, LayerProtocolName.PHOTONICMEDIA);
555         this.connectionFullMap.put(connection.key(), connection);
556
557         // OTSi top connection that will be added to the service object
558         Connection conn = new ConnectionBuilder().setConnectionUuid(connection.getUuid()).build();
559         connServMap.put(conn.key(), conn);
560
561
562         return connServMap;
563     }
564
565     private Map<ConnectionKey, Connection> createRoadmCepsAndConnections(List<String> rdmAddDropTplist,
566                                                                          List<String> rdmDegTplist,
567                                                                          List<String> rdmNodelist,
568                                                                          String edgeRoadm1, String edgeRoadm2) {
569         // TODO: will need to check if things exist already or not
570         Map<ConnectionKey, Connection> connServMap = new HashMap<>();
571         Map<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointKey,
572             ConnectionEndPoint> cepMap = new HashMap<>();
573         // create ceps and x connections within roadm
574         for (String roadm : rdmNodelist) {
575             LOG.info("Creating ceps and xc for roadm {}", roadm);
576             String spcRdmAD = rdmAddDropTplist.stream().filter(adp -> adp.contains(roadm)).findFirst().get();
577             LOG.info("AD port of ROADm {} = {}", roadm, spcRdmAD);
578             // There should be only 1 AD and 1 DEG per roadm
579             // TODO photonic media model should be updated to have the corresponding CEPs. I will just create
580             //  3 different MC CEPs giving different IDs to show that they are different
581             // Create 3 CEPs for each AD and DEG and the corresponding cross connections, matching the NEPs
582             // created in the topology creation
583             // add CEPs to the topology to the corresponding ONEP
584             ConnectionEndPoint adCep1 = createCepRoadm(spcRdmAD, PHTNC_MEDIA);
585             putRdmCepInTopologyContext(roadm, spcRdmAD, PHTNC_MEDIA, adCep1);
586             ConnectionEndPoint adCep2 = createCepRoadm(spcRdmAD, MC);
587             putRdmCepInTopologyContext(roadm, spcRdmAD, MC, adCep2);
588             ConnectionEndPoint adCep3 = createCepRoadm(spcRdmAD, OTSI_MC);
589             putRdmCepInTopologyContext(roadm, spcRdmAD, OTSI_MC, adCep3);
590             cepMap.put(adCep1.key(), adCep1);
591             cepMap.put(adCep2.key(), adCep2);
592             cepMap.put(adCep3.key(), adCep3);
593
594             String spcRdmDEG = rdmDegTplist.stream().filter(adp -> adp.contains(roadm)).findFirst().get();
595             LOG.info("Degree port of ROADm {} = {}", roadm, spcRdmDEG);
596
597             ConnectionEndPoint degCep1 = createCepRoadm(spcRdmDEG, PHTNC_MEDIA);
598             putRdmCepInTopologyContext(roadm, spcRdmDEG, PHTNC_MEDIA, degCep1);
599             ConnectionEndPoint degCep2 = createCepRoadm(spcRdmDEG, MC);
600             putRdmCepInTopologyContext(roadm, spcRdmDEG, MC, degCep2);
601             ConnectionEndPoint degCep3 = createCepRoadm(spcRdmDEG, OTSI_MC);
602             putRdmCepInTopologyContext(roadm, spcRdmDEG, OTSI_MC, degCep3);
603             cepMap.put(degCep1.key(), degCep1);
604             cepMap.put(degCep2.key(), degCep2);
605             cepMap.put(degCep3.key(), degCep3);
606
607             LOG.info("Going to create cross connections for ROADM {}", roadm);
608             // Create X connections between MC and OTSi_MC for full map
609             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
610                 connection1 = createXCBetweenCeps(adCep2, degCep2, spcRdmAD, spcRdmDEG, MC,
611                 LayerProtocolName.PHOTONICMEDIA);
612             LOG.info("Cross connection 1 created = {}", connection1);
613             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
614                 connection2 = createXCBetweenCeps(adCep3, degCep3, spcRdmAD, spcRdmDEG, OTSI_MC,
615                 LayerProtocolName.PHOTONICMEDIA);
616             LOG.info("Cross connection 2 created = {}", connection2);
617             this.connectionFullMap.put(connection1.key(), connection1);
618             this.connectionFullMap.put(connection2.key(), connection2);
619
620             // Create X connections that will be added to the service object
621             Connection conn1 = new ConnectionBuilder().setConnectionUuid(connection1.getUuid()).build();
622             Connection conn2 = new ConnectionBuilder().setConnectionUuid(connection2.getUuid()).build();
623             connServMap.put(conn1.key(), conn1);
624             connServMap.put(conn2.key(), conn2);
625         }
626         LOG.info("Going to create top connections betwee roadms");
627         // create top connections between roadms: MC connections between AD MC CEPs of roadms
628         for (int i = 0; i < rdmNodelist.size(); i++) {
629             if (rdmNodelist.size() <= (i + 1)) {
630                 LOG.info("Reached last roadm. No more MC connections");
631                 break;
632             }
633             // Current roadm with roadm i + 1 --> MC
634             String roadm1 = rdmNodelist.get(i);
635             String spcRdmAD1 = rdmAddDropTplist.stream().filter(adp -> adp.contains(roadm1)).findFirst().get();
636             String roadm2 = rdmNodelist.get(i + 1);
637             String spcRdmAD2 = rdmAddDropTplist.stream().filter(adp -> adp.contains(roadm2)).findFirst().get();
638             LOG.info("Creating top connection from {} to {} between tps: {}-{}", roadm1, roadm2, spcRdmAD1, spcRdmAD2);
639
640             // Create top connections between MC for full map
641             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
642                 connection = createTopConnection(spcRdmAD1, spcRdmAD2, cepMap, MC, LayerProtocolName.PHOTONICMEDIA);
643             this.connectionFullMap.put(connection.key(), connection);
644             LOG.info("Top connection created = {}", connection);
645
646             // Create top connections that will be added to the service object
647             Connection conn = new ConnectionBuilder().setConnectionUuid(connection.getUuid()).build();
648             connServMap.put(conn.key(), conn);
649         }
650
651         // OTSiMC top connection between edge roadms
652         LOG.info("Going to created top connection between OTSiMC");
653         String spcRdmAD1 = rdmAddDropTplist.stream().filter(adp -> adp.contains(edgeRoadm1)).findFirst().get();
654         String spcRdmAD2 = rdmAddDropTplist.stream().filter(adp -> adp.contains(edgeRoadm2)).findFirst().get();
655         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
656             connection = createTopConnection(spcRdmAD1, spcRdmAD2, cepMap, OTSI_MC,
657             LayerProtocolName.PHOTONICMEDIA);
658         this.connectionFullMap.put(connection.key(), connection);
659         LOG.info("Top connection created = {}", connection);
660
661         // OTSiMC top connections that will be added to the service object
662         Connection conn = new ConnectionBuilder().setConnectionUuid(connection.getUuid()).build();
663         connServMap.put(conn.key(), conn);
664         return connServMap;
665     }
666
667     private org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
668             createTopConnection(String tp1, String tp2,
669                         Map<org.opendaylight.yang.gen.v1.urn
670                             .onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointKey,
671                             ConnectionEndPoint> cepMap, String qual, LayerProtocolName topPortocol) {
672         // find cep for each AD MC of roadm 1 and 2
673         LOG.info("Top connection name = {}", String.join("+", "TOP", tp1, tp2, qual));
674         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.ConnectionEndPoint adCep1 =
675             cepMap.get(new org.opendaylight.yang.gen.v1.urn
676                 .onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointKey(
677                 new Uuid(UUID.nameUUIDFromBytes((String.join("+", "CEP", tp1.split("\\+")[0],
678                     qual, tp1.split("\\+")[1])).getBytes(Charset.forName("UTF-8")))
679                     .toString())));
680         LOG.info("ADCEP1 = {}", adCep1);
681         org.opendaylight.yang.gen.v1.urn
682             .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPoint cep1 =
683             new org.opendaylight.yang.gen.v1.urn
684                 .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPointBuilder()
685                 .setNodeEdgePointUuid(adCep1.getClientNodeEdgePoint()
686                     .values().stream().findFirst().get().getNodeEdgePointUuid())
687                 .setTopologyUuid(adCep1.getClientNodeEdgePoint()
688                     .values().stream().findFirst().get().getTopologyUuid())
689                 .setNodeUuid(adCep1.getClientNodeEdgePoint()
690                     .values().stream().findFirst().get().getNodeUuid())
691                 .setConnectionEndPointUuid(adCep1.getUuid())
692                 .build();
693         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.ConnectionEndPoint adCep2 =
694             cepMap.get(new org.opendaylight.yang.gen.v1.urn
695                 .onf.otcc.yang.tapi.connectivity.rev181210.cep.list.ConnectionEndPointKey(
696                 new Uuid(UUID.nameUUIDFromBytes((String.join("+", "CEP", tp2.split("\\+")[0],
697                     qual, tp2.split("\\+")[1])).getBytes(Charset.forName("UTF-8")))
698                     .toString())));
699         LOG.info("ADCEP2 = {}", adCep2);
700         org.opendaylight.yang.gen.v1.urn
701             .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPoint cep2 =
702             new org.opendaylight.yang.gen.v1.urn
703                 .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPointBuilder()
704                 .setNodeEdgePointUuid(adCep2.getClientNodeEdgePoint()
705                     .values().stream().findFirst().get().getNodeEdgePointUuid())
706                 .setTopologyUuid(adCep2.getClientNodeEdgePoint()
707                     .values().stream().findFirst().get().getTopologyUuid())
708                 .setNodeUuid(adCep2.getClientNodeEdgePoint()
709                     .values().stream().findFirst().get().getNodeUuid())
710                 .setConnectionEndPointUuid(adCep1.getUuid())
711                 .build();
712         Map<ConnectionEndPointKey, org.opendaylight.yang.gen.v1.urn
713             .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPoint> ceps = new HashMap<>();
714         ceps.put(cep1.key(), cep1);
715         ceps.put(cep2.key(), cep2);
716         Name connName = new NameBuilder()
717             .setValueName("Connection name")
718             .setValue(String.join("+", "TOP", tp1, tp2, qual))
719             .build();
720         // TODO: lower connection, supported link.......
721         return new org.opendaylight.yang.gen.v1.urn
722             .onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectionBuilder()
723             .setUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", "TOP", tp1, tp2, qual))
724                 .getBytes(Charset.forName("UTF-8"))).toString()))
725             .setName(Map.of(connName.key(), connName))
726             .setConnectionEndPoint(ceps)
727             .setOperationalState(OperationalState.ENABLED)
728             .setLayerProtocolName(topPortocol)
729             .setLifecycleState(LifecycleState.INSTALLED)
730             .setDirection(ForwardingDirection.BIDIRECTIONAL)
731             .build();
732     }
733
734     private org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection
735             createXCBetweenCeps(ConnectionEndPoint cep1, ConnectionEndPoint cep2, String tp1, String tp2, String qual,
736                         LayerProtocolName xcProtocol) {
737         LOG.info("Creation cross connection between: {} and {}", tp1, tp2);
738         LOG.info("Cross connection name = {}", String.join("+", "XC", tp1, tp2, qual));
739         LOG.info("CEP1 = {}", cep1.getClientNodeEdgePoint());
740         LOG.info("CEP2 = {}", cep2.getClientNodeEdgePoint());
741         org.opendaylight.yang.gen.v1.urn
742             .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPoint cepServ1 =
743             new org.opendaylight.yang.gen.v1.urn
744                 .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPointBuilder()
745                 .setNodeEdgePointUuid(cep1.getClientNodeEdgePoint()
746                     .values().stream().findFirst().get().getNodeEdgePointUuid())
747                 .setTopologyUuid(cep1.getClientNodeEdgePoint()
748                     .values().stream().findFirst().get().getTopologyUuid())
749                 .setNodeUuid(cep1.getClientNodeEdgePoint()
750                     .values().stream().findFirst().get().getNodeUuid())
751                 .setConnectionEndPointUuid(cep1.getUuid())
752                 .build();
753         org.opendaylight.yang.gen.v1.urn
754             .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPoint cepServ2 =
755             new org.opendaylight.yang.gen.v1.urn
756                 .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPointBuilder()
757                 .setNodeEdgePointUuid(cep2.getClientNodeEdgePoint()
758                     .values().stream().findFirst().get().getNodeEdgePointUuid())
759                 .setTopologyUuid(cep2.getClientNodeEdgePoint()
760                     .values().stream().findFirst().get().getTopologyUuid())
761                 .setNodeUuid(cep2.getClientNodeEdgePoint()
762                     .values().stream().findFirst().get().getNodeUuid())
763                 .setConnectionEndPointUuid(cep2.getUuid())
764                 .build();
765         Map<ConnectionEndPointKey, org.opendaylight.yang.gen.v1.urn
766             .onf.otcc.yang.tapi.connectivity.rev181210.connection.ConnectionEndPoint> ceps = new HashMap<>();
767         ceps.put(cepServ1.key(), cepServ1);
768         ceps.put(cepServ2.key(), cepServ2);
769         Name connName = new NameBuilder()
770             .setValueName("Connection name")
771             .setValue(String.join("+", "XC", tp1, tp2, qual))
772             .build();
773         // TODO: lower connection, supported link.......
774         return new org.opendaylight.yang.gen.v1.urn
775             .onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectionBuilder()
776             .setUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", "XC", tp1, tp2, qual))
777                 .getBytes(Charset.forName("UTF-8"))).toString()))
778             .setName(Map.of(connName.key(), connName))
779             .setConnectionEndPoint(ceps)
780             .setOperationalState(OperationalState.ENABLED)
781             .setLayerProtocolName(xcProtocol)
782             .setLifecycleState(LifecycleState.INSTALLED)
783             .setDirection(ForwardingDirection.BIDIRECTIONAL)
784             .build();
785     }
786
787     private ConnectionEndPoint createCepRoadm(String id, String qualifier) {
788         LOG.info("NEP = {}", String.join("+", id.split("\\+")[0], qualifier, id.split("\\+")[1]));
789         Name cepName = new NameBuilder()
790             .setValueName("ConnectionEndPoint name")
791             .setValue(String.join("+", id.split("\\+")[0], qualifier,
792                 id.split("\\+")[1]))
793             .build();
794         ClientNodeEdgePoint cnep = new ClientNodeEdgePointBuilder()
795             .setNodeEdgePointUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", id.split("\\+")[0],
796                 qualifier, id.split("\\+")[1])).getBytes(Charset.forName("UTF-8")))
797                 .toString()))
798             .setNodeUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+",id.split("\\+")[0],
799                 qualifier)).getBytes(Charset.forName("UTF-8")))
800                 .toString()))
801             .setTopologyUuid(new Uuid(UUID.nameUUIDFromBytes(TopologyUtils.T0_FULL_MULTILAYER
802                 .getBytes(Charset.forName("UTF-8"))).toString()))
803             .build();
804         // TODO: add augmentation with the corresponding cep-spec (i.e. MC, OTSiMC...)
805         // TODO: add parent ONEP??
806         ConnectionEndPointBuilder cepBldr = new ConnectionEndPointBuilder()
807             .setUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", "CEP", id.split("\\+")[0],
808                 qualifier, id.split("\\+")[1])).getBytes(Charset.forName("UTF-8")))
809                 .toString()))
810             .setClientNodeEdgePoint(Map.of(cnep.key(), cnep))
811             .setName(Map.of(cepName.key(), cepName))
812             .setConnectionPortRole(PortRole.SYMMETRIC)
813             .setConnectionPortDirection(PortDirection.BIDIRECTIONAL)
814             .setOperationalState(OperationalState.ENABLED)
815             .setLifecycleState(LifecycleState.INSTALLED)
816             .setLayerProtocolName(LayerProtocolName.PHOTONICMEDIA);
817         return cepBldr.build();
818     }
819
820     private ConnectionEndPoint createCepXpdr(String id, String qualifier, String nodeLayer,
821                                              LayerProtocolName cepProtocol) {
822         Name cepName = new NameBuilder()
823             .setValueName("ConnectionEndPoint name")
824             .setValue(String.join("+", id.split("\\+")[0], qualifier,
825                 id.split("\\+")[1]))
826             .build();
827         ClientNodeEdgePoint cnep = new ClientNodeEdgePointBuilder()
828             .setNodeEdgePointUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", id.split("\\+")[0],
829                 qualifier, id.split("\\+")[1])).getBytes(Charset.forName("UTF-8")))
830                 .toString()))
831             .setNodeUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+",id.split("\\+")[0],
832                 nodeLayer)).getBytes(Charset.forName("UTF-8")))
833                 .toString()))
834             .setTopologyUuid(new Uuid(UUID.nameUUIDFromBytes(TopologyUtils.T0_FULL_MULTILAYER
835                 .getBytes(Charset.forName("UTF-8"))).toString()))
836             .build();
837         // TODO: add augmentation with the corresponding cep-spec (i.e. MC, OTSiMC...)
838         // TODO: add parent ONEP??
839         ConnectionEndPointBuilder cepBldr = new ConnectionEndPointBuilder()
840             .setUuid(new Uuid(UUID.nameUUIDFromBytes((String.join("+", "CEP", id.split("\\+")[0],
841                 qualifier, id.split("\\+")[1])).getBytes(Charset.forName("UTF-8")))
842                 .toString()))
843             .setClientNodeEdgePoint(Map.of(cnep.key(), cnep))
844             .setName(Map.of(cepName.key(), cepName))
845             .setConnectionPortRole(PortRole.SYMMETRIC)
846             .setConnectionPortDirection(PortDirection.BIDIRECTIONAL)
847             .setOperationalState(OperationalState.ENABLED)
848             .setLifecycleState(LifecycleState.INSTALLED)
849             .setLayerProtocolName(cepProtocol);
850         return cepBldr.build();
851     }
852
853     private EndPoint mapServiceZEndPoint(
854             org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceZEnd
855                 serviceZEnd, PathDescription pathDescription) {
856         EndPointBuilder endPointBuilder = new EndPointBuilder();
857         // 1. Service Format: ODU, OTU, ETH
858         ServiceFormat serviceFormat = serviceZEnd.getServiceFormat();
859         String serviceNodeId = serviceZEnd.getNodeId().getValue();
860         // Identify SIP name
861         Uuid sipUuid = getSipIdFromZend(pathDescription.getZToADirection().getZToA(), serviceNodeId, serviceFormat);
862         LOG.info("Uuid of z end {}", sipUuid);
863         LayerProtocolName layerProtocols = null;
864         // Layer protocol name
865         switch (serviceFormat) {
866             case Ethernet:
867                 layerProtocols = LayerProtocolName.DSR;
868                 break;
869             case OTU:
870             case OC:
871                 layerProtocols = LayerProtocolName.PHOTONICMEDIA;
872                 break;
873             case ODU:
874                 layerProtocols = LayerProtocolName.ODU;
875                 break;
876             default:
877                 LOG.error("Service Format not supported");
878         }
879         return endPointBuilder
880             .setServiceInterfacePoint(new ServiceInterfacePointBuilder()
881                 .setServiceInterfacePointUuid(sipUuid)
882                 .build())
883             .setAdministrativeState(AdministrativeState.UNLOCKED)
884             .setDirection(PortDirection.BIDIRECTIONAL)
885             .setLifecycleState(LifecycleState.INSTALLED)
886             .setOperationalState(OperationalState.ENABLED)
887             .setLayerProtocolName(layerProtocols)
888             .setCapacity(new CapacityBuilder()
889                 .setTotalSize(new TotalSizeBuilder()
890                     .setValue(Uint64.valueOf(serviceZEnd.getServiceRate()))
891                     .setUnit(CapacityUnit.GBPS)
892                     .build())
893                 .setBandwidthProfile(new BandwidthProfileBuilder().build()) // TODO: implement bandwidth profile
894                 .build())
895             .setProtectionRole(ProtectionRole.WORK)
896             .setRole(PortRole.SYMMETRIC)
897             .setLocalId(serviceNodeId)
898             .build();
899     }
900
901     private EndPoint mapServiceAEndPoint(
902             org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceAEnd
903                 serviceAEnd, PathDescription pathDescription) {
904         EndPointBuilder endPointBuilder = new EndPointBuilder();
905         // 1. Service Format: ODU, OTU, ETH
906         ServiceFormat serviceFormat = serviceAEnd.getServiceFormat();
907         String serviceNodeId = serviceAEnd.getNodeId().getValue();
908         // Identify SIP name
909         Uuid sipUuid = getSipIdFromAend(pathDescription.getAToZDirection().getAToZ(), serviceNodeId, serviceFormat);
910         LOG.info("Uuid of a end {}", sipUuid);
911         LayerProtocolName layerProtocols = null;
912         // Layer protocol name
913         switch (serviceFormat) {
914             case Ethernet:
915                 layerProtocols = LayerProtocolName.DSR;
916                 break;
917             case OTU:
918             case OC:
919                 layerProtocols = LayerProtocolName.PHOTONICMEDIA;
920                 break;
921             case ODU:
922                 layerProtocols = LayerProtocolName.ODU;
923                 break;
924             default:
925                 LOG.error("Service Format not supported");
926         }
927         return endPointBuilder
928             .setServiceInterfacePoint(new ServiceInterfacePointBuilder()
929                 .setServiceInterfacePointUuid(sipUuid)
930                 .build())
931             .setAdministrativeState(AdministrativeState.UNLOCKED)
932             .setDirection(PortDirection.BIDIRECTIONAL)
933             .setLifecycleState(LifecycleState.INSTALLED)
934             .setOperationalState(OperationalState.ENABLED)
935             .setLayerProtocolName(layerProtocols)
936             .setCapacity(new CapacityBuilder()
937                 .setTotalSize(new TotalSizeBuilder()
938                     .setValue(Uint64.valueOf(serviceAEnd.getServiceRate()))
939                     .setUnit(CapacityUnit.GBPS)
940                     .build())
941                 .setBandwidthProfile(new BandwidthProfileBuilder().build()) // TODO: implement bandwidth profile
942                 .build())
943             .setProtectionRole(ProtectionRole.WORK)
944             .setRole(PortRole.SYMMETRIC)
945             .setLocalId(serviceNodeId)
946             .build();
947     }
948
949     private Uuid getSipIdFromZend(Map<ZToAKey, ZToA> mapztoa, String serviceNodeId, ServiceFormat serviceFormat) {
950         Uuid zendUuid = null;
951         if (serviceNodeId.contains("ROADM")) {
952             // Service from ROADM to ROADM
953             // AddDrop-AddDrop ports --> MC layer SIPs
954             ZToA firstElement = mapztoa.values().stream().filter(ztoa -> ztoa.getId().equals("0")).findFirst().get();
955             TerminationPoint tp = (TerminationPoint) firstElement.getResource().getResource();
956             Uuid sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
957                 tp.getTpNodeId(), MC, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
958                 .toString());
959             LOG.info("SIP name = {}", String.join("+", tp.getTpNodeId(), MC, tp.getTpId()));
960             for (ServiceInterfacePoint sip:this.sipMap.values()) {
961                 if (!sip.getUuid().equals(sipUuid)) {
962                     LOG.info("SIP {} doesn match sipname {}", sip.getUuid().getValue(), sipUuid.getValue());
963                     continue;
964                 }
965                 zendUuid = sip.getUuid();
966                 break;
967             }
968         } else {
969             // Service from XPDR to XPDR
970             ZToA firstElement;
971             TerminationPoint tp;
972             Uuid sipUuid;
973             switch (serviceFormat) {
974                 case ODU:
975                     firstElement = mapztoa.values().stream().filter(ztoa -> ztoa.getId().equals("2")).findFirst().get();
976                     tp = (TerminationPoint) firstElement.getResource().getResource();
977                     // Network-Network ports --> iODU layer SIPs TODO --> updated to E_ODU
978                     sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
979                         tp.getTpNodeId(), E_ODU, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
980                         .toString());
981                     LOG.info("SIP name = {}", String.join("+", tp.getTpNodeId(), E_ODU, tp.getTpId()));
982                     break;
983                 case OTU:
984                     firstElement = mapztoa.values().stream().filter(ztoa -> ztoa.getId().equals("2")).findFirst().get();
985                     tp = (TerminationPoint) firstElement.getResource().getResource();
986                     // Network-Network ports --> iOTSi layer SIPs
987                     sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
988                         tp.getTpNodeId(), I_OTSI, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
989                         .toString());
990                     LOG.info("SIP name = {}", String.join("+", tp.getTpNodeId(), I_OTSI, tp.getTpId()));
991                     break;
992                 case Ethernet:
993                     firstElement = mapztoa.values().stream().filter(ztoa -> ztoa.getId().equals("0")).findFirst().get();
994                     tp = (TerminationPoint) firstElement.getResource().getResource();
995                     // Client-client ports --> DSR layer SIPs
996                     sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
997                         tp.getTpNodeId(), DSR, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
998                         .toString());
999                     LOG.info("SIP name = {}", String.join("+", tp.getTpNodeId(), DSR, tp.getTpId()));
1000                     break;
1001                 default:
1002                     sipUuid = null;
1003                     LOG.warn("Service format {} not supported (?)", serviceFormat.getName());
1004             }
1005             for (ServiceInterfacePoint sip:this.sipMap.values()) {
1006                 if (!sip.getUuid().equals(sipUuid)) {
1007                     LOG.info("SIP {} doesn match sipname {}", sip.getUuid().getValue(), sipUuid.getValue());
1008                     continue;
1009                 }
1010                 zendUuid = sip.getUuid();
1011                 break;
1012             }
1013         }
1014         return zendUuid;
1015     }
1016
1017     private Uuid getSipIdFromAend(Map<AToZKey, AToZ> mapatoz, String serviceNodeId, ServiceFormat serviceFormat) {
1018         Uuid aendUuid = null;
1019         LOG.info("ServiceNode = {} and ServiceFormat = {}", serviceNodeId, serviceFormat.getName());
1020         LOG.info("Map a to z = {}", mapatoz);
1021         if (serviceNodeId.contains("ROADM")) {
1022             // Service from ROADM to ROADM
1023             // AddDrop-AddDrop ports --> MC layer SIPs
1024             AToZ firstElement = mapatoz.values().stream().filter(atoz -> atoz.getId().equals("0")).findFirst().get();
1025             LOG.info("First element of service path = {}", firstElement.getResource().getResource());
1026             TerminationPoint tp = (TerminationPoint) firstElement.getResource().getResource();
1027             Uuid sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
1028                 tp.getTpNodeId(), MC, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
1029                 .toString());
1030             LOG.info("ROADM SIP name = {}", String.join("+", tp.getTpNodeId(), MC, tp.getTpId()));
1031             for (ServiceInterfacePoint sip:this.sipMap.values()) {
1032                 if (!sip.getUuid().equals(sipUuid)) {
1033                     LOG.info("SIP {} doesn match sipname {}", sip.getUuid().getValue(), sipUuid.getValue());
1034                     continue;
1035                 }
1036                 aendUuid = sip.getUuid();
1037                 break;
1038             }
1039         } else {
1040             // Service from XPDR to XPDR
1041             AToZ firstElement;
1042             TerminationPoint tp;
1043             Uuid sipUuid;
1044             switch (serviceFormat) {
1045                 case ODU:
1046                     firstElement = mapatoz.values().stream().filter(atoz -> atoz.getId().equals("2")).findFirst().get();
1047                     tp = (TerminationPoint) firstElement.getResource().getResource();
1048                     // Network-Network ports --> iODU layer SIPs. TODO -> updated to eODU
1049                     sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
1050                         tp.getTpNodeId(), E_ODU, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
1051                         .toString());
1052                     LOG.info("ODU XPDR SIP name = {}", String.join("+", tp.getTpNodeId(), E_ODU, tp.getTpId()));
1053                     break;
1054                 case OTU:
1055                     firstElement = mapatoz.values().stream().filter(atoz -> atoz.getId().equals("2")).findFirst().get();
1056                     tp = (TerminationPoint) firstElement.getResource().getResource();
1057                     // Network-Network ports --> iOTSi layer SIPs
1058                     sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
1059                         tp.getTpNodeId(), I_OTSI, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
1060                         .toString());
1061                     LOG.info("OTU XPDR SIP name = {}", String.join("+", tp.getTpNodeId(), I_OTSI, tp.getTpId()));
1062                     break;
1063                 case Ethernet:
1064                     firstElement = mapatoz.values().stream().filter(atoz -> atoz.getId().equals("0")).findFirst().get();
1065                     tp = (TerminationPoint) firstElement.getResource().getResource();
1066                     // Client-client ports --> DSR layer SIPs
1067                     sipUuid = new Uuid(UUID.nameUUIDFromBytes((String.join("+", "SIP",
1068                         tp.getTpNodeId(), DSR, tp.getTpId())).getBytes(Charset.forName("UTF-8")))
1069                         .toString());
1070                     LOG.info("DSR XPDR SIP name = {}", String.join("+", tp.getTpNodeId(), DSR, tp.getTpId()));
1071                     break;
1072                 default:
1073                     sipUuid = null;
1074                     LOG.warn("Service format {} not supported (?)", serviceFormat.getName());
1075             }
1076             for (ServiceInterfacePoint sip:this.sipMap.values()) {
1077                 if (!sip.getUuid().equals(sipUuid)) {
1078                     LOG.info("SIP {} doesn match sipname {}", sip.getUuid().getValue(), sipUuid.getValue());
1079                     continue;
1080                 }
1081                 aendUuid = sip.getUuid();
1082                 break;
1083             }
1084         }
1085         return aendUuid;
1086     }
1087
1088     private void putRdmCepInTopologyContext(String node, String spcRdmAD, String qual, ConnectionEndPoint cep) {
1089         LOG.info("NEP id before Merge = {}", String.join("+", node, qual, spcRdmAD.split("\\+")[1]));
1090         LOG.info("Node of NEP id before Merge = {}", String.join("+", node, PHTNC_MEDIA));
1091         // Give uuids so that it is easier to look for things: topology uuid, node uuid, nep uuid, cep
1092         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(TopologyUtils.T0_FULL_MULTILAYER
1093             .getBytes(Charset.forName("UTF-8"))).toString());
1094         Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes(String.join("+", node, PHTNC_MEDIA)
1095             .getBytes(Charset.forName("UTF-8"))).toString());
1096         Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes(String.join("+", node, qual, spcRdmAD.split("\\+")[1])
1097             .getBytes(Charset.forName("UTF-8"))).toString());
1098         this.tapiContext.updateTopologyWithCep(topoUuid, nodeUuid, nepUuid, cep);
1099     }
1100
1101     private void putXpdrCepInTopologyContext(String node, String spcXpdrNet, String qual, String nodeLayer,
1102                                              ConnectionEndPoint cep) {
1103         // Give uuids so that it is easier to look for things: topology uuid, node uuid, nep uuid, cep
1104         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(TopologyUtils.T0_FULL_MULTILAYER
1105             .getBytes(Charset.forName("UTF-8"))).toString());
1106         Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes(String.join("+", node, nodeLayer)
1107             .getBytes(Charset.forName("UTF-8"))).toString());
1108         Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes(String.join("+", node, qual, spcXpdrNet.split("\\+")[1])
1109             .getBytes(Charset.forName("UTF-8"))).toString());
1110         this.tapiContext.updateTopologyWithCep(topoUuid, nodeUuid, nepUuid, cep);
1111     }
1112
1113     public Map<org.opendaylight.yang.gen.v1.urn
1114             .onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectionKey,
1115             org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.Connection>
1116             getConnectionFullMap() {
1117         return this.connectionFullMap;
1118     }
1119
1120     private String getIdBasedOnModelVersion(String nodeid) {
1121         return nodeid.matches("[A-Z]{5}-[A-Z0-9]{2}-.*") ? String.join("-", nodeid.split("-")[0], nodeid.split("-")[1])
1122                                                         : nodeid.split("-")[0];
1123     }
1124 }