Adapt TransportPCE code to Sulfur
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / openroadminterface / OpenRoadmInterface221.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.transportpce.renderer.openroadminterface;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.transportpce.common.StringConstants;
16 import org.opendaylight.transportpce.common.Timeouts;
17 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
18 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
19 import org.opendaylight.transportpce.common.fixedflex.SpectrumInformation;
20 import org.opendaylight.transportpce.common.mapping.PortMapping;
21 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
22 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.AEndApiInfo;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.ZEndApiInfo;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.mapping.Mapping;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyGHz;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.PowerDBm;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.R100G;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.OrgOpenroadmDeviceData;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnection;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.OduConnectionKey;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.types.rev191129.XpdrNodeTypes;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.EthAttributes;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.Interface1Builder;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.ethernet.container.EthernetBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.EthernetCsmacd;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.InterfaceType;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.MediaChannelTrailTerminationPoint;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.NetworkMediaChannelConnectionTerminationPoint;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OpenROADMOpticalMultiplex;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OpticalChannel;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OpticalTransport;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOdu;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOtu;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.media.channel.interfaces.rev181019.mc.ttp.container.McTtpBuilder;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.media.channel.interfaces.rev181019.nmc.ctp.container.NmcCtpBuilder;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev181019.och.container.OchBuilder;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev181019.OtsAttributes;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev181019.ots.container.OtsBuilder;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODU4;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODUCTP;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODUTTP;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODUTTPCTP;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.OTU4;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.OduFunctionIdentity;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.PayloadTypeDef;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.OduAttributes;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.OduAttributes.MonitoringMode;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.odu.container.OduBuilder;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.opu.Opu;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.opu.OpuBuilder;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.OtuAttributes;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.otu.container.OtuBuilder;
72 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
73 import org.opendaylight.yangtools.yang.common.Decimal64;
74 import org.opendaylight.yangtools.yang.common.Uint32;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78
79 public class OpenRoadmInterface221 {
80     private static final String MAPPING_ERROR_EXCEPTION_MESSAGE =
81             "Unable to get mapping from PortMapping for node % and logical connection port %s";
82     private final PortMapping portMapping;
83     private final OpenRoadmInterfaces openRoadmInterfaces;
84     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterface221.class);
85
86
87     public OpenRoadmInterface221(PortMapping portMapping, OpenRoadmInterfaces openRoadmInterfaces) {
88         this.portMapping = portMapping;
89         this.openRoadmInterfaces = openRoadmInterfaces;
90     }
91
92     public String createOpenRoadmEthInterface(String nodeId, String logicalConnPoint)
93         throws OpenRoadmInterfaceException {
94         Mapping mapping = portMapping.getMapping(nodeId, logicalConnPoint);
95         if (mapping == null) {
96             throw new OpenRoadmInterfaceException(
97                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, nodeId, logicalConnPoint));
98         }
99
100         // Ethernet interface specific data
101         EthernetBuilder ethIfBuilder = new EthernetBuilder()
102             .setFec(EthAttributes.Fec.Off)
103             .setSpeed(Uint32.valueOf(100000));
104
105         InterfaceBuilder ethInterfaceBldr = createGenericInterfaceBuilder(mapping, EthernetCsmacd.class,
106             logicalConnPoint + "-ETHERNET");
107         // Create Interface1 type object required for adding as augmentation
108         Interface1Builder ethIf1Builder = new Interface1Builder();
109         ethInterfaceBldr.addAugmentation(ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
110
111         // Post interface on the device
112         openRoadmInterfaces.postInterface(nodeId, ethInterfaceBldr);
113
114         // Post the equipment-state change on the device circuit-pack
115         openRoadmInterfaces.postEquipmentState(nodeId, mapping.getSupportingCircuitPackName(), true);
116         this.portMapping.updateMapping(nodeId, mapping);
117         return ethInterfaceBldr.getName();
118     }
119
120     public List<String> createFlexOCH(String nodeId, String logicalConnPoint, SpectrumInformation spectrumInformation)
121         throws OpenRoadmInterfaceException {
122         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
123         if (portMap == null) {
124             throw new OpenRoadmInterfaceException(
125                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, nodeId, logicalConnPoint));
126         }
127
128         List<String> interfacesCreated = new ArrayList<>();
129         String mcInterfaceCreated = "";
130         if (logicalConnPoint.contains("DEG")) {
131             mcInterfaceCreated = createMCInterface(nodeId, logicalConnPoint, spectrumInformation);
132             interfacesCreated.add(mcInterfaceCreated);
133         }
134         String nmcInterfaceCreated = createNMCInterface(nodeId, logicalConnPoint, spectrumInformation,
135                 mcInterfaceCreated);
136         interfacesCreated.add(nmcInterfaceCreated);
137         return interfacesCreated;
138     }
139
140     public String createMCInterface(String nodeId, String logicalConnPoint,
141         SpectrumInformation spectrumInformation)
142         throws OpenRoadmInterfaceException {
143         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
144         if (portMap == null) {
145             throw new OpenRoadmInterfaceException(
146                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, nodeId, logicalConnPoint));
147         }
148         // TODO : Check this method
149         LOG.info("MC interface Freq Start {} and Freq End {} and center-Freq {}",
150                 spectrumInformation.getMinFrequency(), spectrumInformation.getMaxFrequency(),
151                 spectrumInformation.getCenterFrequency());
152         InterfaceBuilder mcInterfaceBldr = createGenericInterfaceBuilder(portMap,
153             MediaChannelTrailTerminationPoint.class,
154             spectrumInformation.getIdentifierFromParams(logicalConnPoint, "mc"))
155                 .setSupportingInterface(portMap.getSupportingOms());
156         McTtpBuilder mcTtpBuilder = new McTtpBuilder()
157             .setMinFreq(new FrequencyTHz(Decimal64.valueOf(spectrumInformation.getMinFrequency())))
158             .setMaxFreq(new FrequencyTHz(Decimal64.valueOf(spectrumInformation.getMaxFrequency())));
159
160         // Create Interface1 type object required for adding as augmentation
161         org.opendaylight.yang.gen.v1.http.org.openroadm.media.channel.interfaces.rev181019.Interface1Builder
162             interface1Builder =
163             new org.opendaylight.yang.gen.v1.http.org.openroadm.media.channel.interfaces.rev181019.Interface1Builder()
164                 .setMcTtp(mcTtpBuilder.build());
165
166         mcInterfaceBldr.addAugmentation(interface1Builder.build());
167
168         // Post interface on the device
169         openRoadmInterfaces.postInterface(nodeId, mcInterfaceBldr);
170         return mcInterfaceBldr.getName();
171     }
172
173     public String createNMCInterface(String nodeId, String logicalConnPoint,
174         SpectrumInformation spectrumInformation, String mcName)
175         throws OpenRoadmInterfaceException {
176         LOG.info("This is the central frequency {}", spectrumInformation.getCenterFrequency());
177         LOG.info("This is the nmc width {}", spectrumInformation.getWidth());
178         // TODO : Check this method
179         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
180         if (portMap == null) {
181             throw new OpenRoadmInterfaceException(
182                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, nodeId, logicalConnPoint));
183         }
184         //TODO : Check this method
185         String nmcName = spectrumInformation.getIdentifierFromParams(logicalConnPoint, "nmc");
186         InterfaceBuilder nmcInterfaceBldr = createGenericInterfaceBuilder(portMap,
187             NetworkMediaChannelConnectionTerminationPoint.class, nmcName);
188         if (logicalConnPoint.contains("DEG")) {
189             nmcInterfaceBldr.setSupportingInterface(mcName);
190         }
191
192         NmcCtpBuilder nmcCtpIfBuilder = new NmcCtpBuilder()
193                 .setFrequency(new FrequencyTHz(Decimal64.valueOf(spectrumInformation.getCenterFrequency())))
194                 .setWidth(new FrequencyGHz(Decimal64.valueOf(spectrumInformation.getWidth())));
195
196         // Create Interface1 type object required for adding as augmentation
197         org.opendaylight.yang.gen.v1.http.org.openroadm.network.media.channel.interfaces.rev181019.Interface1Builder
198             nmcCtpI1fBuilder =
199             new org.opendaylight.yang.gen.v1.http.org.openroadm.network.media.channel.interfaces.rev181019
200                  .Interface1Builder().setNmcCtp(nmcCtpIfBuilder.build());
201         nmcInterfaceBldr.addAugmentation(nmcCtpI1fBuilder.build());
202         // Post interface on the device
203         openRoadmInterfaces.postInterface(nodeId, nmcInterfaceBldr);
204         return nmcInterfaceBldr.getName();
205     }
206
207     public String createOpenRoadmOchInterface(String nodeId, String logicalConnPoint,
208         SpectrumInformation spectrumInformation)
209         throws OpenRoadmInterfaceException {
210         // TODO : Check this method
211         ModulationFormat modulationFormat = ModulationFormat.DpQpsk;
212         Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
213                 .forName(spectrumInformation.getModulationFormat());
214         if (optionalModulationFormat.isPresent()) {
215             modulationFormat =  optionalModulationFormat.get();
216         }
217         // OCH interface specific data
218         OchBuilder ocIfBuilder = new OchBuilder()
219             .setFrequency(new FrequencyTHz(Decimal64.valueOf(spectrumInformation.getCenterFrequency())))
220             .setRate(R100G.class)
221             .setTransmitPower(new PowerDBm(Decimal64.valueOf("-5")))
222             .setModulationFormat(modulationFormat);
223         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
224         if (portMap == null) {
225             throw new OpenRoadmInterfaceException(
226                 String.format("Unable to get mapping from PortMapping for node %s and logical connection port %s",
227                     nodeId, logicalConnPoint));
228         }
229         // Create generic interface
230         InterfaceBuilder ochInterfaceBldr = createGenericInterfaceBuilder(portMap, OpticalChannel.class,
231             spectrumInformation.getIdentifierFromParams(logicalConnPoint));
232         // Create Interface1 type object required for adding as augmentation
233         // TODO look at imports of different versions of class
234         org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev181019.Interface1Builder
235             ochIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev181019
236             .Interface1Builder();
237         ochInterfaceBldr.addAugmentation(ochIf1Builder.setOch(ocIfBuilder.build()).build());
238
239         // Post interface on the device
240         openRoadmInterfaces.postInterface(nodeId, ochInterfaceBldr);
241
242         // Post the equipment-state change on the device circuit-pack if xpdr node
243         if (portMap.getLogicalConnectionPoint().contains(StringConstants.NETWORK_TOKEN)) {
244             this.openRoadmInterfaces.postEquipmentState(nodeId, portMap.getSupportingCircuitPackName(), true);
245         }
246
247         return ochInterfaceBldr.getName();
248     }
249
250     public String createOpenRoadmOdu4HOInterface(String nodeId, String logicalConnPoint, boolean isCTP,
251             AEndApiInfo apiInfoA, ZEndApiInfo apiInfoZ, String payloadType) throws OpenRoadmInterfaceException {
252         Mapping mapping = portMapping.getMapping(nodeId, logicalConnPoint);
253         if (mapping == null) {
254             throw new OpenRoadmInterfaceException(
255                     String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, nodeId, logicalConnPoint));
256         }
257
258         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(mapping, OtnOdu.class,
259             logicalConnPoint + "-ODU4");
260         if (mapping.getSupportingOtu4() != null) {
261             oduInterfaceBldr.setSupportingInterface(mapping.getSupportingOtu4());
262         }
263         if (mapping.getSupportingEthernet() != null) {
264             oduInterfaceBldr.setSupportingInterface(mapping.getSupportingEthernet());
265         }
266         // ODU interface specific data
267         Class<? extends OduFunctionIdentity> oduFunction;
268         MonitoringMode monitoringMode;
269         Opu opu = null;
270         if (isCTP) {
271             oduFunction = ODUCTP.class;
272             monitoringMode = MonitoringMode.Monitored;
273         } else {
274             // For TPDR it can be both CTP and TTP
275             if (mapping.getXponderType() == XpdrNodeTypes.Tpdr) {
276                 oduFunction = ODUTTPCTP.class;
277                 // For switch-ponder we still use TTP
278             } else {
279                 oduFunction = ODUTTP.class;
280             }
281             monitoringMode = MonitoringMode.Terminated;
282             opu = new OpuBuilder()
283                 .setPayloadType(PayloadTypeDef.getDefaultInstance(payloadType))
284                 .setExpPayloadType(PayloadTypeDef.getDefaultInstance(payloadType))
285                 .build();
286         }
287         OduBuilder oduIfBuilder = new OduBuilder()
288                 .setRate(ODU4.class)
289                 .setOduFunction(oduFunction)
290                 .setMonitoringMode(monitoringMode)
291                 .setOpu(opu);
292         if (apiInfoA != null) {
293             oduIfBuilder.setTxSapi(apiInfoA.getSapi())
294                 .setTxDapi(apiInfoA.getDapi())
295                 .setExpectedSapi(apiInfoA.getExpectedSapi())
296                 .setExpectedDapi(apiInfoA.getExpectedDapi());
297         }
298         if (apiInfoZ != null) {
299             oduIfBuilder.setTxSapi(apiInfoZ.getSapi())
300                 .setTxDapi(apiInfoZ.getDapi())
301                 .setExpectedSapi(apiInfoZ.getExpectedSapi())
302                 .setExpectedDapi(apiInfoZ.getExpectedDapi());
303         }
304         // Create Interface1 type object required for adding as augmentation
305         // TODO look at imports of different versions of class
306         oduInterfaceBldr.addAugmentation(
307             new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder()
308                 .setOdu(oduIfBuilder.build())
309                 .build());
310
311         // Post interface on the device
312         openRoadmInterfaces.postInterface(nodeId, oduInterfaceBldr);
313         if (!isCTP) {
314             LOG.info("{}-{} updating mapping with interface {}", nodeId, logicalConnPoint, oduInterfaceBldr.getName());
315             this.portMapping.updateMapping(nodeId, mapping);
316         }
317         return oduInterfaceBldr.getName();
318     }
319
320     public String createOpenRoadmOdu4Interface(String nodeId, String logicalConnPoint, String supportingOtuInterface)
321         throws OpenRoadmInterfaceException {
322         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
323         if (portMap == null) {
324             throw new OpenRoadmInterfaceException(
325                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE,
326                     nodeId, logicalConnPoint));
327         }
328         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(portMap, OtnOdu.class,
329             logicalConnPoint + "-ODU");
330         oduInterfaceBldr.setSupportingInterface(supportingOtuInterface);
331
332         // ODU interface specific data
333         // Set Opu attributes
334         OpuBuilder opuBldr = new OpuBuilder()
335                 .setPayloadType(PayloadTypeDef.getDefaultInstance("07"))
336                 .setExpPayloadType(PayloadTypeDef.getDefaultInstance("07"));
337         OduBuilder oduIfBuilder = new OduBuilder()
338                 .setRate(ODU4.class)
339                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated)
340                 .setOpu(opuBldr.build());
341
342         // Create Interface1 type object required for adding as augmentation
343         // TODO look at imports of different versions of class
344         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder oduIf1Builder =
345             new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
346         oduInterfaceBldr.addAugmentation(oduIf1Builder.setOdu(oduIfBuilder.build()).build());
347
348         // Post interface on the device
349         openRoadmInterfaces.postInterface(nodeId, oduInterfaceBldr);
350         return oduInterfaceBldr.getName();
351     }
352
353     public String createOpenRoadmOdu4Interface(String anodeId, String alogicalConnPoint, String supportingOtuInterface,
354         String znodeId, String zlogicalConnPoint)
355         throws OpenRoadmInterfaceException {
356         Mapping portMapA = portMapping.getMapping(anodeId, alogicalConnPoint);
357         Mapping portMapZ = portMapping.getMapping(znodeId, zlogicalConnPoint);
358         if (portMapA == null) {
359             throw new OpenRoadmInterfaceException(
360                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, anodeId, alogicalConnPoint));
361         }
362         if (portMapZ == null) {
363             throw new OpenRoadmInterfaceException(
364                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, znodeId, zlogicalConnPoint));
365         }
366         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(portMapA, OtnOdu.class,
367             alogicalConnPoint + "-ODU");
368         oduInterfaceBldr.setSupportingInterface(supportingOtuInterface);
369
370         // ODU interface specific data
371         // Set Opu attributes
372         OpuBuilder opuBldr = new OpuBuilder()
373             .setPayloadType(PayloadTypeDef.getDefaultInstance("07"))
374             .setExpPayloadType(PayloadTypeDef.getDefaultInstance("07"));
375         OduBuilder oduIfBuilder = new OduBuilder()
376             .setRate(ODU4.class)
377             .setMonitoringMode(OduAttributes.MonitoringMode.Terminated)
378             .setOpu(opuBldr.build())
379             .setTxSapi(portMapA.getLcpHashVal())
380             .setTxDapi(portMapZ.getLcpHashVal())
381             // Setting the expected Dapi and Sapi values
382             .setExpectedDapi(portMapA.getLcpHashVal())
383             .setExpectedSapi(portMapZ.getLcpHashVal());
384
385         // Create Interface1 type object required for adding as augmentation
386         // TODO look at imports of different versions of class
387         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder oduIf1Builder =
388             new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
389         oduInterfaceBldr.addAugmentation(oduIf1Builder.setOdu(oduIfBuilder.build()).build());
390
391         // Post interface on the device
392         openRoadmInterfaces.postInterface(anodeId, oduInterfaceBldr);
393         return oduInterfaceBldr.getName();
394     }
395
396     public String createOpenRoadmOtu4Interface(String nodeId, String logicalConnPoint, String supportOchInterface,
397             AEndApiInfo apiInfoA, ZEndApiInfo apiInfoZ) throws OpenRoadmInterfaceException {
398
399         Mapping mapping = this.portMapping.getMapping(nodeId, logicalConnPoint);
400         if (mapping == null) {
401             throw new OpenRoadmInterfaceException(
402                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, nodeId, logicalConnPoint));
403         }
404         InterfaceBuilder otuInterfaceBldr = createGenericInterfaceBuilder(mapping, OtnOtu.class,
405             logicalConnPoint + "-OTU");
406         otuInterfaceBldr.setSupportingInterface(supportOchInterface);
407
408         // OTU interface specific data
409         OtuBuilder otuIfBuilder = new OtuBuilder()
410             .setFec(OtuAttributes.Fec.Scfec)
411             .setRate(OTU4.class);
412         if (apiInfoA != null) {
413             otuIfBuilder.setTxSapi(apiInfoA.getSapi())
414                 .setTxDapi(apiInfoA.getDapi())
415                 .setExpectedSapi(apiInfoA.getExpectedSapi())
416                 .setExpectedDapi(apiInfoA.getExpectedDapi());
417         }
418         if (apiInfoZ != null) {
419             otuIfBuilder.setTxSapi(apiInfoZ.getSapi())
420                 .setTxDapi(apiInfoZ.getDapi())
421                 .setExpectedSapi(apiInfoZ.getExpectedSapi())
422                 .setExpectedDapi(apiInfoZ.getExpectedDapi());
423         }
424
425         // Create Interface1 type object required for adding as augmentation
426         // TODO look at imports of different versions of class
427         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder otuIf1Builder =
428             new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder();
429         otuInterfaceBldr.addAugmentation(otuIf1Builder.setOtu(otuIfBuilder.build()).build());
430
431         // Post interface on the device
432         openRoadmInterfaces.postInterface(nodeId, otuInterfaceBldr);
433         this.portMapping.updateMapping(nodeId, mapping);
434         return otuInterfaceBldr.getName();
435     }
436
437     public String createOpenRoadmOchInterfaceName(String logicalConnectionPoint, String spectralSlotName) {
438         return String.join(GridConstant.NAME_PARAMETERS_SEPARATOR,logicalConnectionPoint, spectralSlotName);
439     }
440
441     private InterfaceBuilder createGenericInterfaceBuilder(Mapping portMap, Class<? extends InterfaceType> type,
442         String key) {
443         return new InterfaceBuilder()
444                 .setDescription("  TBD   ")
445                 .setCircuitId("   TBD    ")
446                 .setSupportingCircuitPackName(portMap.getSupportingCircuitPackName())
447                 .setSupportingPort(portMap.getSupportingPort())
448                 .setAdministrativeState(AdminStates.InService)
449                 .setType(type)
450                 .setName(key)
451                 .withKey(new InterfaceKey(key));
452     }
453
454     public String createOpenRoadmOmsInterface(String nodeId, Mapping mapping) throws OpenRoadmInterfaceException {
455         if (mapping.getSupportingOms() == null) {
456             // Create generic interface
457             InterfaceBuilder omsInterfaceBldr = createGenericInterfaceBuilder(mapping, OpenROADMOpticalMultiplex.class,
458                 "OMS-" + mapping.getLogicalConnectionPoint());
459             if (mapping.getSupportingOts() != null) {
460                 omsInterfaceBldr.setSupportingInterface(mapping.getSupportingOts());
461             } else {
462                 LOG.error("Unable to get ots interface from mapping {} - {}", nodeId,
463                     mapping.getLogicalConnectionPoint());
464                 return null;
465             }
466             this.openRoadmInterfaces.postInterface(nodeId, omsInterfaceBldr);
467             this.portMapping.updateMapping(nodeId, mapping);
468             return omsInterfaceBldr.build().getName();
469         } else {
470             return mapping.getSupportingOms();
471         }
472     }
473
474     public String createOpenRoadmOtsInterface(String nodeId, Mapping mapping) throws OpenRoadmInterfaceException {
475         if (mapping.getSupportingOts() == null) {
476             // Create generic interface
477             InterfaceBuilder otsInterfaceBldr = createGenericInterfaceBuilder(mapping, OpticalTransport.class, "OTS-"
478                 + mapping.getLogicalConnectionPoint());
479             // OTS interface augmentation specific data
480             OtsBuilder otsIfBuilder = new OtsBuilder();
481             otsIfBuilder.setFiberType(OtsAttributes.FiberType.Smf);
482
483             // Create Interface1 type object required for adding as
484             // augmentation
485             org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev181019.Interface1Builder
486                 otsIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces
487                 .rev181019.Interface1Builder();
488             otsInterfaceBldr.addAugmentation(otsIf1Builder.setOts(otsIfBuilder.build()).build());
489             this.openRoadmInterfaces.postInterface(nodeId, otsInterfaceBldr);
490             this.portMapping.updateMapping(nodeId, mapping);
491             return otsInterfaceBldr.build().getName();
492         } else {
493             return mapping.getSupportingOts();
494         }
495     }
496
497     public boolean isUsedByXc(String nodeId, String interfaceName, String xc,
498         DeviceTransactionManager deviceTransactionManager) {
499         InstanceIdentifier<RoadmConnections> xciid = InstanceIdentifier
500             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
501             .child(RoadmConnections.class, new RoadmConnectionsKey(xc))
502             .build();
503         LOG.info("reading xc {} in node {}", xc, nodeId);
504         Optional<RoadmConnections> crossconnection = deviceTransactionManager.getDataFromDevice(nodeId,
505             LogicalDatastoreType.CONFIGURATION, xciid, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
506         if (crossconnection.isPresent()) {
507             RoadmConnections rc = crossconnection.get();
508             LOG.info("xd {} found", xc);
509             String supportedinter = null;
510             if (!interfaceName.contains("nmc")) {
511                 supportedinter = interfaceName.replace("mc", "nmc");
512             }
513             if (rc.getSource().getSrcIf().equals(interfaceName)
514                 || rc.getDestination().getDstIf().equals(interfaceName)
515                 || rc.getSource().getSrcIf().equals(supportedinter)
516                 || rc.getDestination().getDstIf().equals(supportedinter)) {
517                 return true;
518             }
519         } else {
520             LOG.info("xd {} not found !", xc);
521         }
522         return false;
523     }
524
525     public boolean isUsedByOtnXc(String nodeId, String interfaceName, String xc,
526         DeviceTransactionManager deviceTransactionManager) {
527         InstanceIdentifier<OduConnection> xciid = InstanceIdentifier
528             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
529             .child(OduConnection.class, new OduConnectionKey(xc))
530             .build();
531         LOG.info("reading xc {} in node {}", xc, nodeId);
532         Optional<OduConnection> oduConnectionOpt = deviceTransactionManager.getDataFromDevice(nodeId,
533             LogicalDatastoreType.CONFIGURATION, xciid, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
534         if (oduConnectionOpt.isPresent()) {
535             OduConnection oduXc = oduConnectionOpt.get();
536             LOG.info("xc {} found", xc);
537             if (oduXc.getSource().getSrcIf().equals(interfaceName)
538                 || oduXc.getDestination().getDstIf().equals(interfaceName)) {
539                 return true;
540             }
541         } else {
542             LOG.info("xc {} not found !", xc);
543         }
544         return false;
545     }
546
547     public String createOpenRoadmOtnOdu4Interface(String nodeId, String logicalConnPoint, String supportingOtuInterface)
548             throws OpenRoadmInterfaceException {
549         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
550         if (portMap == null) {
551             throw new OpenRoadmInterfaceException(
552                     String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, nodeId, logicalConnPoint));
553         }
554         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(portMap, OtnOdu.class,
555                 logicalConnPoint + "-ODU4");
556         oduInterfaceBldr.setSupportingInterface(supportingOtuInterface);
557
558         // ODU interface specific data
559         OduBuilder oduIfBuilder = new OduBuilder()
560                 .setRate(ODU4.class)
561                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
562
563         // Set Opu attributes
564         OpuBuilder opuBldr = new OpuBuilder()
565                 .setPayloadType(PayloadTypeDef.getDefaultInstance("21"))
566                 .setExpPayloadType(PayloadTypeDef.getDefaultInstance("21"));
567         oduIfBuilder.setOduFunction(ODUTTP.class)
568                 .setOpu(opuBldr.build());
569
570         // Create Interface1 type object required for adding as augmentation
571         // TODO look at imports of different versions of class
572         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder oduIf1Builder =
573                 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
574         oduInterfaceBldr.addAugmentation(oduIf1Builder.setOdu(oduIfBuilder.build()).build());
575
576         // Post interface on the device
577         openRoadmInterfaces.postInterface(nodeId, oduInterfaceBldr);
578         this.portMapping.updateMapping(nodeId, portMap);
579         return oduInterfaceBldr.getName();
580     }
581
582     public String createOpenRoadmOtnOdu4Interface(String anodeId, String alogicalConnPoint,
583         String asupportingOtuInterface, String znodeId, String zlogicalConnPoint)
584         throws OpenRoadmInterfaceException {
585         Mapping portMapA = portMapping.getMapping(anodeId, alogicalConnPoint);
586         if (portMapA == null) {
587             throw new OpenRoadmInterfaceException(
588                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, anodeId, alogicalConnPoint));
589         }
590         Mapping portMapZ = portMapping.getMapping(znodeId, zlogicalConnPoint);
591         if (portMapZ == null) {
592             throw new OpenRoadmInterfaceException(
593                 String.format(MAPPING_ERROR_EXCEPTION_MESSAGE, znodeId, zlogicalConnPoint));
594         }
595         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(portMapA, OtnOdu.class,
596             alogicalConnPoint + "-ODU4");
597         oduInterfaceBldr.setSupportingInterface(asupportingOtuInterface);
598
599         // ODU interface specific data
600         OduBuilder oduIfBuilder = new OduBuilder()
601             .setRate(ODU4.class)
602             .setMonitoringMode(OduAttributes.MonitoringMode.Terminated)
603             .setTxSapi(portMapA.getLcpHashVal())
604             .setTxDapi(portMapZ.getLcpHashVal())
605             .setExpectedSapi(portMapZ.getLcpHashVal())
606             .setExpectedDapi(portMapA.getLcpHashVal());
607
608
609         // Set Opu attributes
610         OpuBuilder opuBldr = new OpuBuilder()
611             .setPayloadType(PayloadTypeDef.getDefaultInstance("21"))
612             .setExpPayloadType(PayloadTypeDef.getDefaultInstance("21"));
613         oduIfBuilder.setOduFunction(ODUTTP.class)
614             .setOpu(opuBldr.build());
615
616         // Create Interface1 type object required for adding as augmentation
617         // TODO look at imports of different versions of class
618         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder oduIf1Builder =
619             new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
620         oduInterfaceBldr.addAugmentation(oduIf1Builder.setOdu(oduIfBuilder.build()).build());
621
622         // Post interface on the device
623         openRoadmInterfaces.postInterface(anodeId, oduInterfaceBldr);
624         this.portMapping.updateMapping(anodeId, portMapA);
625         return oduInterfaceBldr.getName();
626     }
627 }