Fix spotbugs issues of the renderer module
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / openroadminterface / OpenRoadmOtnInterface221.java
1 /*
2  * Copyright © 2019 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 package org.opendaylight.transportpce.renderer.openroadminterface;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.stream.IntStream;
13
14 import org.opendaylight.transportpce.common.mapping.PortMapping;
15 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
16 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200128.network.nodes.Mapping;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.Interface1;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.Interface1Builder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.ethernet.container.EthernetBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.EthernetCsmacd;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.InterfaceType;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOdu;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODU0;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODU2;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODU2e;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODUCTP;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODUTTPCTP;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.PayloadTypeDef;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.OduAttributes;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.odu.container.OduBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.opu.OpuBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.parent.odu.allocation.ParentOduAllocationBuilder;
37 import org.opendaylight.yangtools.yang.common.Uint16;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 public class OpenRoadmOtnInterface221 {
43
44     private final PortMapping portMapping;
45     private final OpenRoadmInterfaces openRoadmInterfaces;
46     private static final Logger LOG = LoggerFactory
47             .getLogger(OpenRoadmOtnInterface221.class);
48
49     public OpenRoadmOtnInterface221(PortMapping portMapping,
50             OpenRoadmInterfaces openRoadmInterfaces) {
51         this.portMapping = portMapping;
52         this.openRoadmInterfaces = openRoadmInterfaces;
53     }
54
55     public String createOpenRoadmEth1GInterface(String nodeId,
56             String logicalConnPoint) throws OpenRoadmInterfaceException {
57         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
58         if (portMap == null) {
59             throwException(nodeId, logicalConnPoint);
60         }
61
62         // Ethernet interface specific data
63         EthernetBuilder ethIfBuilder = new EthernetBuilder();
64         ethIfBuilder.setSpeed(1000L);
65         InterfaceBuilder ethInterfaceBldr = createGenericInterfaceBuilder(
66                 portMap, EthernetCsmacd.class,
67                 logicalConnPoint + "-ETHERNET1G");
68         // Create Interface1 type object required for adding as augmentation
69         Interface1Builder ethIf1Builder = new Interface1Builder();
70         ethInterfaceBldr.addAugmentation(Interface1.class,
71                 ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
72         // Post interface on the device
73         this.openRoadmInterfaces.postOTNInterface(nodeId, ethInterfaceBldr);
74         // Post the equipment-state change on the device circuit-pack
75         this.openRoadmInterfaces.postOTNEquipmentState(nodeId,
76                 portMap.getSupportingCircuitPackName(), true);
77         this.portMapping.updateMapping(nodeId, portMap);
78         String ethernetInterfaceName = ethInterfaceBldr.getName();
79
80         return ethernetInterfaceName;
81     }
82
83     private void throwException(String nodeId, String logicalConnPoint)
84             throws OpenRoadmInterfaceException {
85         throw new OpenRoadmInterfaceException(String.format(
86                 "Unable to get mapping from PortMapping for node % and logical connection port %s",
87                 nodeId, logicalConnPoint));
88     }
89
90     private InterfaceBuilder createGenericInterfaceBuilder(Mapping portMap,
91             Class<? extends InterfaceType> type, String key) {
92         InterfaceBuilder interfaceBuilder = new InterfaceBuilder()
93                 // .setDescription(" TBD ")
94                 // .setCircuitId(" TBD ")
95                 .setSupportingCircuitPackName(
96                         portMap.getSupportingCircuitPackName())
97                 .setSupportingPort(portMap.getSupportingPort())
98                 .setAdministrativeState(AdminStates.InService)
99                 // TODO get rid of unchecked cast warning
100                 .setType(type).setName(key).withKey(new InterfaceKey(key));
101         return interfaceBuilder;
102     }
103
104     public String createOpenRoadmEth10GInterface(String nodeId,
105             String logicalConnPoint) throws OpenRoadmInterfaceException {
106         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
107         if (portMap == null) {
108             throwException(nodeId, logicalConnPoint);
109         }
110
111         // Ethernet interface specific data
112         EthernetBuilder ethIfBuilder = new EthernetBuilder()
113                 // .setAutoNegotiation(EthAttributes.AutoNegotiation.Disabled)
114                 .setSpeed(10000L);
115         // Create Interface1 type object required for adding as augmentation
116         Interface1Builder ethIf1Builder = new Interface1Builder();
117         InterfaceBuilder ethInterfaceBldr = createGenericInterfaceBuilder(portMap, EthernetCsmacd.class,
118                 logicalConnPoint + "-ETHERNET10G").addAugmentation(Interface1.class,
119                         ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
120         // Post interface on the device
121         this.openRoadmInterfaces.postOTNInterface(nodeId, ethInterfaceBldr);
122         // Post the equipment-state change on the device circuit-pack
123         this.openRoadmInterfaces.postOTNEquipmentState(nodeId,
124                 portMap.getSupportingCircuitPackName(), true);
125         this.portMapping.updateMapping(nodeId, portMap);
126         String ethernetInterfaceName = ethInterfaceBldr.getName();
127
128         return ethernetInterfaceName;
129     }
130
131     public String createOpenRoadmOdu2eInterface(String nodeId,
132             String logicalConnPoint, String serviceName, String payLoad,
133             boolean isNetworkPort, int tribPortNumber, int tribSlotIndex)
134             throws OpenRoadmInterfaceException {
135         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
136         if (portMap == null) {
137             throwException(nodeId, logicalConnPoint);
138         }
139         String supportingInterface = null;
140
141         if (isNetworkPort) {
142             supportingInterface = portMap.getSupportingOdu4();
143         } else {
144             supportingInterface = logicalConnPoint + "-ETHERNET10G";
145         }
146
147         if (supportingInterface == null) {
148             throw new OpenRoadmInterfaceException(
149                     "Interface Creation failed because of missing supported "
150                     + "ODU4 on network end or Eth iface on client");
151         }
152
153         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(
154                 portMap, OtnOdu.class,
155                 logicalConnPoint + "-ODU2e-" + serviceName)
156                         .setSupportingInterface(supportingInterface);
157
158         // ODU interface specific data
159         OduBuilder oduIfBuilder = new OduBuilder().setRate(ODU2e.class)
160                 .setOduFunction(ODUTTPCTP.class)
161                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
162         LOG.debug("Inside the ODU2e creation {} {} {}", isNetworkPort,
163                 tribPortNumber, tribSlotIndex);
164         if (isNetworkPort) {
165             List<Uint16> tribSlots = new ArrayList<>();
166             Uint16 newIdx = Uint16.valueOf(tribSlotIndex);
167             tribSlots.add(newIdx);
168             IntStream.range(tribSlotIndex, tribSlotIndex + 8)
169                     .forEach(nbr -> tribSlots.add(Uint16.valueOf(nbr)));
170             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
171                     .setTribPortNumber(tribPortNumber).setTribSlots(tribSlots);
172             oduIfBuilder.setOduFunction(ODUCTP.class)
173                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
174                     .setParentOduAllocation(parentOduAllocationBuilder.build());
175         } else {
176             // Set Opu attributes
177             OpuBuilder opuBldr = new OpuBuilder()
178                     .setPayloadType(new PayloadTypeDef(payLoad))
179                     .setExpPayloadType(new PayloadTypeDef(payLoad));
180             oduIfBuilder.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.rev181019.Interface1Builder
185             oduIf1Builder = new
186                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
187         oduInterfaceBldr.addAugmentation(
188                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
189                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
190
191         // Post interface on the device
192         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
193         LOG.info("returning the ODU2e inteface {}", oduInterfaceBldr.getName());
194         return oduInterfaceBldr.getName();
195     }
196
197     public String createOpenRoadmOdu0Interface(String nodeId,
198             String logicalConnPoint, String serviceName, String payLoad,
199             boolean isNetworkPort, int tribPortNumber, int tribSlot)
200             throws OpenRoadmInterfaceException {
201         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
202         String supportingInterface = null;
203
204         if (isNetworkPort) {
205             supportingInterface = portMap.getSupportingOdu4();
206         } else {
207             supportingInterface = logicalConnPoint + "-ETHERNET1G";
208         }
209         if (portMap == null) {
210             throwException(nodeId, logicalConnPoint);
211         }
212         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(
213                 portMap, OtnOdu.class,
214                 logicalConnPoint + "-ODU0-" + serviceName);
215         oduInterfaceBldr.setSupportingInterface(supportingInterface);
216
217         // ODU interface specific data
218         OduBuilder oduIfBuilder = new OduBuilder().setRate(ODU0.class)
219                 .setOduFunction(ODUTTPCTP.class)
220                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
221         if (isNetworkPort) {
222             LOG.debug("Network port is true");
223             List<Uint16> tribSlots = new ArrayList<>();
224             // add trib slots
225             tribSlots.add(Uint16.valueOf(tribSlot));
226             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
227                     // set trib port numbers
228                     .setTribPortNumber(tribPortNumber).setTribSlots(tribSlots);
229             oduIfBuilder.setOduFunction(ODUCTP.class)
230                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
231                     .setParentOduAllocation(parentOduAllocationBuilder.build());
232             LOG.debug("Network port is true {} {} {}",
233                     oduIfBuilder.getParentOduAllocation().getTribSlots(),
234                     oduIfBuilder.getRate(),
235                     oduIfBuilder.getParentOduAllocation().getTribPortNumber());
236         } else {
237             LOG.debug("Current port is a client port");
238             OpuBuilder opuBldr = new OpuBuilder()
239                     .setPayloadType(new PayloadTypeDef(payLoad))
240                     .setExpPayloadType(new PayloadTypeDef(payLoad));
241             oduIfBuilder.setOpu(opuBldr.build());
242         }
243         // Create Interface1 type object required for adding as augmentation
244         // TODO look at imports of different versions of class
245         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder
246             oduIf1Builder = new
247             org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
248         oduInterfaceBldr.addAugmentation(
249                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
250                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
251
252         // Post interface on the device
253         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
254         LOG.info("returning the ODU0 inteface {}", oduInterfaceBldr.getName());
255         return oduInterfaceBldr.getName();
256     }
257
258     public String createOpenRoadmOdu2Interface(String nodeId,
259             String logicalConnPoint, String serviceName, String payLoad,
260             boolean isNetworkPort, int tribPortNumber, int tribSlotIndex)
261             throws OpenRoadmInterfaceException {
262         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
263         String supportingInterface = null;
264
265         if (portMap != null) {
266             if (isNetworkPort) {
267                 supportingInterface = portMap.getSupportingOdu4();
268             } else {
269                 supportingInterface = portMap.getSupportingEthernet();
270             }
271         } else {
272             throwException(nodeId, logicalConnPoint);
273         }
274         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(
275                 portMap, OtnOdu.class,
276                 logicalConnPoint + "-ODU2-" + serviceName)
277                         .setSupportingInterface(supportingInterface);
278
279         OduBuilder oduIfBuilder = new OduBuilder().setRate(ODU2.class)
280                 .setOduFunction(ODUTTPCTP.class)
281                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
282         if (isNetworkPort) {
283             List<Uint16> tribSlots = new ArrayList<>();
284             IntStream.range(tribSlotIndex, tribSlotIndex + 8)
285                     .forEach(nbr -> tribSlots.add(Uint16.valueOf(nbr)));
286             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
287                     // set trib port numbers
288                     .setTribPortNumber(tribPortNumber).setTribSlots(tribSlots);
289             oduIfBuilder.setOduFunction(ODUCTP.class)
290                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
291                     .setParentOduAllocation(parentOduAllocationBuilder.build());
292         } else {
293             // Set Opu attributes
294             OpuBuilder opuBldr = new OpuBuilder()
295                     .setPayloadType(new PayloadTypeDef(payLoad))
296                     .setExpPayloadType(new PayloadTypeDef(payLoad));
297             oduIfBuilder.setOpu(opuBldr.build());
298         }
299
300         // Create Interface1 type object required for adding as augmentation
301         // TODO look at imports of different versions of class
302         // Create Interface1 type object required for adding as augmentation
303         // TODO look at imports of different versions of class
304         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder
305             oduIf1Builder = new
306                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
307         oduInterfaceBldr.addAugmentation(
308                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
309                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
310
311         // Post interface on the device
312         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
313         LOG.info("returning the ODU2 inteface {}", oduInterfaceBldr.getName());
314         return oduInterfaceBldr.getName();
315     }
316 }