Migrate renderer module to Aluminium
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / openroadminterface / OpenRoadmInterface121.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.math.BigDecimal;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.transportpce.common.StringConstants;
17 import org.opendaylight.transportpce.common.Timeouts;
18 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
19 import org.opendaylight.transportpce.common.mapping.PortMapping;
20 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
21 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.Mapping;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.PowerDBm;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.AdminStates;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev161014.EthAttributes;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev161014.Interface1Builder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev161014.ethernet.container.EthernetBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.EthernetCsmacd;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.InterfaceType;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OpenROADMOpticalMultiplex;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OpticalChannel;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OpticalTransport;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOdu;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOtu;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.OchAttributes;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.R100G;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.och.container.OchBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev161014.OtsAttributes;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev161014.ots.container.OtsBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.ODU4;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.OduAttributes;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.container.OduBuilder;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.opu.OpuBuilder;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.OTU4;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.OtuAttributes;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.otu.container.OtuBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.Uint32;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58 public class OpenRoadmInterface121 {
59     private static final String MAPPING_MSG_ERROR =
60             "Unable to get mapping from PortMapping for node % and logical connection port %s";
61     private final PortMapping portMapping;
62     private final OpenRoadmInterfaces openRoadmInterfaces;
63     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterface121.class);
64
65     public  OpenRoadmInterface121(PortMapping portMapping, OpenRoadmInterfaces openRoadmInterfaces) {
66         this.portMapping = portMapping;
67         this.openRoadmInterfaces = openRoadmInterfaces;
68     }
69
70     public String createOpenRoadmEthInterface(String nodeId, String logicalConnPoint)
71             throws OpenRoadmInterfaceException {
72         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
73         if (portMap == null) {
74             throw new OpenRoadmInterfaceException(String.format(MAPPING_MSG_ERROR, nodeId, logicalConnPoint));
75         }
76
77         // Ethernet interface specific data
78         EthernetBuilder ethIfBuilder = new EthernetBuilder()
79                 .setAutoNegotiation(EthAttributes.AutoNegotiation.Enabled)
80                 .setDuplex(EthAttributes.Duplex.Full)
81                 .setFec(EthAttributes.Fec.Off)
82                 .setSpeed(Uint32.valueOf(100000))
83                 .setMtu(Uint32.valueOf(9000));
84
85         InterfaceBuilder ethInterfaceBldr = createGenericInterfaceBuilder(portMap, EthernetCsmacd.class,
86                                                                           logicalConnPoint + "-ETHERNET");
87
88         // Create Interface1 type object required for adding as augmentation
89         Interface1Builder ethIf1Builder = new Interface1Builder();
90         ethInterfaceBldr.addAugmentation(ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
91
92         // Post interface on the device
93         this.openRoadmInterfaces.postInterface(nodeId, ethInterfaceBldr);
94
95         // Post the equipment-state change on the device circuit-pack
96         this.openRoadmInterfaces.postEquipmentState(nodeId, portMap.getSupportingCircuitPackName(), true);
97
98         return ethInterfaceBldr.getName();
99     }
100
101     private InterfaceBuilder createGenericInterfaceBuilder(Mapping portMap, Class<? extends InterfaceType> type,
102                                                            String key) {
103         return new InterfaceBuilder()
104                 .setDescription("  TBD   ")
105                 .setCircuitId("   TBD    ")
106                 .setSupportingCircuitPackName(portMap.getSupportingCircuitPackName())
107                 .setSupportingPort(portMap.getSupportingPort())
108                 .setAdministrativeState(AdminStates.InService)
109                 .setType(type)
110                 .setName(key)
111                 .withKey(new InterfaceKey(key));
112     }
113     /**
114      * This methods creates an OTU interface on the given termination point.
115      *
116      * @param nodeId node ID
117      * @param logicalConnPoint logical Connection Point
118      * @param supportOchInterface support OCH Interface
119      *
120      * @return Name of the interface if successful, otherwise return null.
121      * @throws OpenRoadmInterfaceException OpenRoadmInterfaceException
122      */
123
124     public String createOpenRoadmOtu4Interface(String nodeId, String logicalConnPoint, String supportOchInterface)
125             throws OpenRoadmInterfaceException {
126         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
127         if (portMap == null) {
128             throw new OpenRoadmInterfaceException(String.format(MAPPING_MSG_ERROR, nodeId, logicalConnPoint));
129         }
130         // Create generic interface
131         InterfaceBuilder otuInterfaceBldr = createGenericInterfaceBuilder(portMap, OtnOtu.class, logicalConnPoint
132                 + "-OTU");
133         otuInterfaceBldr.setSupportingInterface(supportOchInterface);
134
135         // OTU interface specific data
136         OtuBuilder otuIfBuilder = new OtuBuilder()
137                 .setFec(OtuAttributes.Fec.Scfec)
138                 .setRate(OTU4.class);
139
140         // Create Interface1 type object required for adding as augmentation
141         // TODO look at imports of different versions of class
142         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder otuIf1Builder =
143                 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder();
144         otuInterfaceBldr.addAugmentation(otuIf1Builder.setOtu(otuIfBuilder.build()).build());
145
146         // Post interface on the device
147         this.openRoadmInterfaces.postInterface(nodeId, otuInterfaceBldr);
148         return otuInterfaceBldr.getName();
149     }
150
151     /**
152      * This methods creates an ODU interface on the given termination point.
153      *
154      * @param nodeId node ID
155      * @param logicalConnPoint logical Connection Point
156      * @param supportingOtuInterface supporting OTU Interface
157      *
158      * @return Name of the interface if successful, otherwise return null.
159      * @throws OpenRoadmInterfaceException OpenRoadmInterfaceException
160      */
161
162     public String createOpenRoadmOdu4Interface(String nodeId, String logicalConnPoint, String supportingOtuInterface)
163             throws OpenRoadmInterfaceException {
164         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
165         if (portMap == null) {
166             throw new OpenRoadmInterfaceException(String.format(MAPPING_MSG_ERROR, nodeId, logicalConnPoint));
167         }
168         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(portMap, OtnOdu.class, logicalConnPoint
169                 + "-ODU");
170         oduInterfaceBldr.setSupportingInterface(supportingOtuInterface);
171
172         // ODU interface specific data
173         // Set Opu attributes
174         OpuBuilder opuBldr = new OpuBuilder()
175                 .setPayloadType("07")
176                 .setExpPayloadType("07");
177         OduBuilder oduIfBuilder = new OduBuilder()
178                 .setRate(ODU4.class)
179                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated)
180                 .setOpu(opuBldr.build());
181
182         // Create Interface1 type object required for adding as augmentation
183         // TODO look at imports of different versions of class
184         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder oduIf1Builder =
185                 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder();
186         oduInterfaceBldr.addAugmentation(oduIf1Builder.setOdu(oduIfBuilder.build()).build());
187
188         // Post interface on the device
189         this.openRoadmInterfaces.postInterface(nodeId, oduInterfaceBldr);
190         return oduInterfaceBldr.getName();
191     }
192     /**
193      * This methods creates an OCH interface on the given termination point on
194      * Roadm.
195      *
196      * @param nodeId node ID
197      * @param logicalConnPoint logical connection point
198      * @param waveNumber wavelength number of the OCH interface.
199      *
200      * @return Name of the interface if successful, otherwise return null.
201      *
202      * @throws OpenRoadmInterfaceException OpenRoadm interface exception
203      */
204
205     public List<String> createOpenRoadmOchInterface(String nodeId, String logicalConnPoint, Long waveNumber)
206         throws OpenRoadmInterfaceException {
207
208         Mapping portMap = portMapping.getMapping(nodeId, logicalConnPoint);
209         if (portMap == null) {
210             throw new OpenRoadmInterfaceException(String.format(
211                 "Unable to get mapping from PortMapping for node %s and logical connection port %s",
212                     nodeId, logicalConnPoint));
213         }
214         // Create generic interface
215         InterfaceBuilder ochInterfaceBldr = createGenericInterfaceBuilder(portMap, OpticalChannel.class,
216             createOpenRoadmOchInterfaceName(logicalConnPoint, waveNumber));
217
218         // OCH interface specific data
219         OchBuilder ocIfBuilder = new OchBuilder().setWavelengthNumber(Uint32.valueOf(waveNumber));
220
221         // Add supporting OMS interface
222         if (portMap.getSupportingOms() != null) {
223             ochInterfaceBldr.setSupportingInterface(portMap.getSupportingOms());
224         }
225         // Create Interface1 type object required for adding as augmentation
226         // TODO look at imports of different versions of class
227         org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.Interface1Builder
228                 ochIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces
229                 .rev161014.Interface1Builder();
230         ochInterfaceBldr.addAugmentation(ochIf1Builder.setOch(ocIfBuilder.build()).build());
231
232         List<String> interfacesCreated = new ArrayList<>();
233         // Post interface on the device
234         openRoadmInterfaces.postInterface(nodeId, ochInterfaceBldr);
235         interfacesCreated.add(ochInterfaceBldr.getName());
236         return interfacesCreated;
237     }
238
239     public String createOpenRoadmOchInterface(String nodeId, String logicalConnPoint, Long waveNumber,
240         OchAttributes.ModulationFormat format) throws OpenRoadmInterfaceException {
241         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
242         if (portMap == null) {
243             throw new OpenRoadmInterfaceException(String.format(
244                 "Unable to get mapping from PortMapping for node %s and logical connection port %s",
245                     nodeId, logicalConnPoint));
246         }
247
248         // OCH interface specific data
249         OchBuilder ocIfBuilder = new OchBuilder()
250                 .setWavelengthNumber(Uint32.valueOf(waveNumber))
251                 .setModulationFormat(format)
252                 .setRate(R100G.class)
253                 .setTransmitPower(new PowerDBm(new BigDecimal("-5")));
254
255         // Create Interface1 type object required for adding as augmentation
256         // TODO look at imports of different versions of class
257         org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014
258                 .Interface1Builder ochIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel
259                 .interfaces.rev161014.Interface1Builder();
260         // Create generic interface
261         InterfaceBuilder ochInterfaceBldr = createGenericInterfaceBuilder(portMap, OpticalChannel.class,
262             createOpenRoadmOchInterfaceName(logicalConnPoint, waveNumber));
263         ochInterfaceBldr.addAugmentation(ochIf1Builder.setOch(ocIfBuilder.build()).build());
264
265         // Post interface on the device
266         this.openRoadmInterfaces.postInterface(nodeId, ochInterfaceBldr);
267
268         // Post the equipment-state change on the device circuit-pack if xpdr node
269         if (portMap.getLogicalConnectionPoint().contains(StringConstants.NETWORK_TOKEN)) {
270             this.openRoadmInterfaces.postEquipmentState(nodeId, portMap.getSupportingCircuitPackName(), true);
271         }
272         return ochInterfaceBldr.getName();
273     }
274
275     public String createOpenRoadmOchInterfaceName(String logicalConnectionPoint, Long waveNumber) {
276         return logicalConnectionPoint + "-" + waveNumber;
277     }
278
279     public String createOpenRoadmOmsInterface(String nodeId, Mapping mapping) throws OpenRoadmInterfaceException {
280         if (mapping.getSupportingOms() == null) {
281             // Create generic interface
282             InterfaceBuilder omsInterfaceBldr = createGenericInterfaceBuilder(mapping, OpenROADMOpticalMultiplex.class,
283                 "OMS-" + mapping.getLogicalConnectionPoint());
284             if (mapping.getSupportingOts() != null) {
285                 omsInterfaceBldr.setSupportingInterface(mapping.getSupportingOts());
286             } else {
287                 LOG.error("Unable to get ots interface from mapping {} - {}", nodeId,
288                           mapping.getLogicalConnectionPoint());
289                 return null;
290             }
291             this.openRoadmInterfaces.postInterface(nodeId, omsInterfaceBldr);
292             this.portMapping.updateMapping(nodeId, mapping);
293             return omsInterfaceBldr.build().getName();
294         } else {
295             return mapping.getSupportingOms();
296         }
297     }
298
299     public String createOpenRoadmOtsInterface(String nodeId, Mapping mapping) throws OpenRoadmInterfaceException {
300         if (mapping.getSupportingOts() == null) {
301             // Create generic interface
302             InterfaceBuilder otsInterfaceBldr = createGenericInterfaceBuilder(mapping, OpticalTransport.class, "OTS-"
303                     + mapping.getLogicalConnectionPoint());
304             // OTS interface augmentation specific data
305             OtsBuilder otsIfBuilder = new OtsBuilder();
306             otsIfBuilder.setFiberType(OtsAttributes.FiberType.Smf);
307
308             // Create Interface1 type object required for adding as
309             // augmentation
310             org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev161014
311                     .Interface1Builder otsIf1Builder = new org.opendaylight.yang.gen.v1.http.org.openroadm
312                     .optical.transport.interfaces.rev161014.Interface1Builder();
313             otsInterfaceBldr.addAugmentation(otsIf1Builder.setOts(otsIfBuilder.build()).build());
314             this.openRoadmInterfaces.postInterface(nodeId, otsInterfaceBldr);
315             this.portMapping.updateMapping(nodeId, mapping);
316             return otsInterfaceBldr.build().getName();
317         } else {
318             return mapping.getSupportingOts();
319         }
320     }
321
322     public boolean isUsedByXc(String nodeId, String interfaceName, String xc,
323         DeviceTransactionManager deviceTransactionManager) {
324         InstanceIdentifier<RoadmConnections> xciid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
325             .child(RoadmConnections.class, new RoadmConnectionsKey(xc));
326         LOG.info("reading xc {} in node {}", xc, nodeId);
327         Optional<RoadmConnections> crossconnection = deviceTransactionManager.getDataFromDevice(nodeId,
328             LogicalDatastoreType.CONFIGURATION, xciid, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
329         if (crossconnection.isPresent()) {
330             RoadmConnections rc = crossconnection.get();
331             LOG.info("xd {} found", xc);
332             if (rc.getSource().getSrcIf().equals(interfaceName)
333                 || rc.getDestination().getDstIf().equals(interfaceName)) {
334                 return true;
335             }
336         } else {
337             LOG.info("xd {} not found !", xc);
338         }
339         return false;
340     }
341
342 }