Bump to Magnesium dependencies
[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.getLogger(OpenRoadmOtnInterface221.class);
47
48     public  OpenRoadmOtnInterface221(PortMapping portMapping, OpenRoadmInterfaces openRoadmInterfaces) {
49         this.portMapping = portMapping;
50         this.openRoadmInterfaces = openRoadmInterfaces;
51     }
52
53     public String createOpenRoadmEth1GInterface(String nodeId, String logicalConnPoint)
54             throws OpenRoadmInterfaceException {
55         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
56         if (portMap == null) {
57             throw new OpenRoadmInterfaceException(String.format(
58                 "Unable to get mapping from PortMapping for node % and logical connection port %s",
59                 nodeId, logicalConnPoint));
60         }
61
62         // Ethernet interface specific data
63         EthernetBuilder ethIfBuilder = new EthernetBuilder();
64         //ethIfBuilder.setAutoNegotiation(EthAttributes.AutoNegotiation.Disabled);
65         ethIfBuilder.setSpeed(1000L);
66         InterfaceBuilder ethInterfaceBldr =
67             createGenericInterfaceBuilder(portMap, EthernetCsmacd.class, logicalConnPoint + "-ETHERNET1G");
68         // Create Interface1 type object required for adding as augmentation
69         Interface1Builder ethIf1Builder = new  Interface1Builder();
70         ethInterfaceBldr.addAugmentation(Interface1.class, ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
71         // Post interface on the device
72         this.openRoadmInterfaces.postOTNInterface(nodeId, ethInterfaceBldr);
73         // Post the equipment-state change on the device circuit-pack
74         this.openRoadmInterfaces.postOTNEquipmentState(nodeId, portMap.getSupportingCircuitPackName(), true);
75         this.portMapping.updateMapping(nodeId,portMap);
76         String ethernetInterfaceName = ethInterfaceBldr.getName();
77
78         return ethernetInterfaceName;
79     }
80
81     private InterfaceBuilder createGenericInterfaceBuilder(Mapping portMap,
82             Class<? extends InterfaceType> type, String key) {
83         InterfaceBuilder interfaceBuilder = new InterfaceBuilder()
84             //.setDescription("  TBD   ")
85             //.setCircuitId("   TBD    ")
86             .setSupportingCircuitPackName(portMap.getSupportingCircuitPackName())
87             .setSupportingPort(portMap.getSupportingPort())
88             .setAdministrativeState(AdminStates.InService)
89             //TODO get rid of unchecked cast warning
90             .setType(
91                 (Class<? extends InterfaceType>)
92                 type
93             )
94             .setName(key)
95             .withKey(new InterfaceKey(key));
96         return interfaceBuilder;
97     }
98
99     public String createOpenRoadmEth10GInterface(String nodeId, String logicalConnPoint)
100             throws OpenRoadmInterfaceException {
101         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
102         if (portMap == null) {
103             throw new OpenRoadmInterfaceException(
104                 String.format(
105                     "Unable to get mapping from PortMapping for node % and logical connection port %s",
106                     nodeId, logicalConnPoint));
107         }
108
109         // Ethernet interface specific data
110         EthernetBuilder ethIfBuilder = new EthernetBuilder()
111             //.setAutoNegotiation(EthAttributes.AutoNegotiation.Disabled)
112             .setSpeed(10000L);
113         // Create Interface1 type object required for adding as augmentation
114         Interface1Builder ethIf1Builder = new  Interface1Builder();
115         InterfaceBuilder ethInterfaceBldr =
116             createGenericInterfaceBuilder(portMap, EthernetCsmacd.class, logicalConnPoint + "-ETHERNET10G")
117             .addAugmentation(Interface1.class, ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
118         // Post interface on the device
119         this.openRoadmInterfaces.postOTNInterface(nodeId, ethInterfaceBldr);
120         // Post the equipment-state change on the device circuit-pack
121         this.openRoadmInterfaces.postOTNEquipmentState(nodeId, portMap.getSupportingCircuitPackName(), true);
122         this.portMapping.updateMapping(nodeId,portMap);
123         String ethernetInterfaceName = ethInterfaceBldr.getName();
124
125         return ethernetInterfaceName;
126     }
127
128
129     public String createOpenRoadmOdu2eInterface(
130             String nodeId, String logicalConnPoint, String serviceName,
131             String payLoad, boolean isNetworkPort, int tribPortNumber, int tribSlotIndex)
132             throws OpenRoadmInterfaceException {
133         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
134         if (portMap == null) {
135             throw new OpenRoadmInterfaceException(String.format(
136                 "Unable to get mapping from PortMapping for node % and logical connection port %s",
137                 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 ODU4 on network end or Eth iface on client");
150         }
151
152         InterfaceBuilder oduInterfaceBldr =
153             createGenericInterfaceBuilder(portMap, OtnOdu.class, logicalConnPoint + "-ODU2e-" + serviceName)
154                 .setSupportingInterface(supportingInterface);
155
156         // ODU interface specific data
157         OduBuilder oduIfBuilder = new OduBuilder()
158             .setRate(ODU2e.class)
159             .setOduFunction(ODUTTPCTP.class)
160             .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
161         LOG.debug("Inside the ODU2e creation {} {} {}",isNetworkPort,tribPortNumber,tribSlotIndex);
162         if (isNetworkPort) {
163             List<Uint16> tribSlots = new ArrayList<>();
164             Uint16 newIdx = Uint16.valueOf(tribSlotIndex);
165             tribSlots.add(newIdx);
166             IntStream.range(tribSlotIndex, tribSlotIndex + 8).forEach(
167                 nbr -> tribSlots.add(Uint16.valueOf(nbr))
168             );
169             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
170                 .setTribPortNumber(tribPortNumber)
171                 .setTribSlots(tribSlots);
172             oduIfBuilder.setOduFunction(ODUCTP.class)
173                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
174                     .setParentOduAllocation(parentOduAllocationBuilder.build());
175         }
176         else {
177             // Set Opu attributes
178             OpuBuilder opuBldr = new OpuBuilder()
179                     .setPayloadType(new PayloadTypeDef(payLoad))
180                     .setExpPayloadType(new PayloadTypeDef(payLoad));
181             oduIfBuilder.setOpu(opuBldr.build());
182         }
183         // Create Interface1 type object required for adding as augmentation
184         // TODO look at imports of different versions of class
185         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder oduIf1Builder =
186                 new 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, String logicalConnPoint,
198             String serviceName, String payLoad, boolean isNetworkPort, int tribPortNumber, int tribSlot)
199             throws OpenRoadmInterfaceException {
200         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
201         String supportingInterface = null;
202
203         if (isNetworkPort) {
204             supportingInterface = portMap.getSupportingOdu4();
205         } else {
206             supportingInterface = logicalConnPoint + "-ETHERNET1G";
207         }
208         if (portMap == null) {
209             throw new OpenRoadmInterfaceException(String.format(
210                 "Unable to get mapping from PortMapping for node % and logical connection port %s",
211                 nodeId, logicalConnPoint));
212         }
213         InterfaceBuilder oduInterfaceBldr =
214             createGenericInterfaceBuilder(portMap, OtnOdu.class, logicalConnPoint + "-ODU0-" + serviceName);
215         oduInterfaceBldr.setSupportingInterface(supportingInterface);
216
217         // ODU interface specific data
218         OduBuilder oduIfBuilder = new OduBuilder()
219             .setRate(ODU0.class)
220             .setOduFunction(ODUTTPCTP.class)
221             .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
222         if (isNetworkPort) {
223             LOG.debug("Network port is true");
224             List<Uint16> tribSlots = new ArrayList<>();
225             tribSlots.add(Uint16.valueOf(tribSlot)); //add trib slots
226             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
227                     //set trib port numbers
228                     .setTribPortNumber(tribPortNumber)
229                     .setTribSlots(tribSlots);
230             oduIfBuilder.setOduFunction(ODUCTP.class)
231                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
232                     .setParentOduAllocation(parentOduAllocationBuilder.build());
233             LOG.debug("Network port is true {} {} {}",
234                     oduIfBuilder.getParentOduAllocation().getTribSlots(),
235                     oduIfBuilder.getRate(),
236                     oduIfBuilder.getParentOduAllocation().getTribPortNumber());
237         }
238         else {
239             LOG.debug("Current port is a client port");
240             OpuBuilder opuBldr = new OpuBuilder()
241                     .setPayloadType(new PayloadTypeDef(payLoad))
242                     .setExpPayloadType(new PayloadTypeDef(payLoad));
243             oduIfBuilder.setOpu(opuBldr.build());
244         }
245         // Create Interface1 type object required for adding as augmentation
246         // TODO look at imports of different versions of class
247         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder oduIf1Builder =
248                 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
249         oduInterfaceBldr.addAugmentation(
250                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
251                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
252
253         // Post interface on the device
254         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
255         LOG.info("returning the ODU0 inteface {}", oduInterfaceBldr.getName());
256         return oduInterfaceBldr.getName();
257     }
258
259
260     public String createOpenRoadmOdu2Interface(
261             String nodeId, String logicalConnPoint, String serviceName,
262             String payLoad, boolean isNetworkPort, int tribPortNumber, int tribSlotIndex)
263             throws OpenRoadmInterfaceException {
264         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
265         String supportingInterface = null;
266
267         if (isNetworkPort) {
268             supportingInterface = portMap.getSupportingOdu4();
269         } else {
270             supportingInterface = portMap.getSupportingEthernet();
271         }
272         if (portMap == null) {
273             throw new OpenRoadmInterfaceException(
274                 String.format("Unable to get mapping from PortMapping for node % and logical connection port %s",
275                         nodeId, logicalConnPoint));
276         }
277         InterfaceBuilder oduInterfaceBldr =
278             createGenericInterfaceBuilder(portMap, OtnOdu.class, logicalConnPoint + "-ODU2-" + serviceName)
279                 .setSupportingInterface(supportingInterface);
280
281         OduBuilder oduIfBuilder = new OduBuilder()
282             .setRate(ODU2.class)
283             .setOduFunction(ODUTTPCTP.class)
284             .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
285         if (isNetworkPort) {
286             List<Uint16> tribSlots = new ArrayList<>();
287             IntStream.range(tribSlotIndex, tribSlotIndex + 8).forEach(
288                 nbr -> tribSlots.add(Uint16.valueOf(nbr))
289             );
290             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
291                     //set trib port numbers
292                     .setTribPortNumber(tribPortNumber)
293                     .setTribSlots(tribSlots);
294             oduIfBuilder.setOduFunction(ODUCTP.class)
295                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
296                     .setParentOduAllocation(parentOduAllocationBuilder.build());
297         }
298         else {
299             // Set Opu attributes
300             OpuBuilder opuBldr = new OpuBuilder()
301                     .setPayloadType(new PayloadTypeDef(payLoad))
302                     .setExpPayloadType(new PayloadTypeDef(payLoad));
303             oduIfBuilder.setOpu(opuBldr.build());
304         }
305
306         // Create Interface1 type object required for adding as augmentation
307         // TODO look at imports of different versions of class
308         // Create Interface1 type object required for adding as augmentation
309         // TODO look at imports of different versions of class
310         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder oduIf1Builder =
311                 new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
312         oduInterfaceBldr.addAugmentation(
313                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
314                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
315
316         // Post interface on the device
317         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
318         LOG.info("returning the ODU2 inteface {}", oduInterfaceBldr.getName());
319         return oduInterfaceBldr.getName();
320     }
321 }