SH-Renderer API code to handle OTN Step 2
[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 import org.opendaylight.transportpce.common.mapping.PortMapping;
14 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
15 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.Mapping;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.Interface1;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.Interface1Builder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev181019.ethernet.container.EthernetBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.EthernetCsmacd;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.InterfaceType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOdu;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODU0;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODU2;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODU2e;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODUCTP;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.ODUTTPCTP;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev171215.PayloadTypeDef;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.OduAttributes;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.odu.container.OduBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.opu.OpuBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.parent.odu.allocation.ParentOduAllocationBuilder;
36 import org.opendaylight.yangtools.yang.common.Uint16;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 public class OpenRoadmOtnInterface221 {
42
43     private final PortMapping portMapping;
44     private final OpenRoadmInterfaces openRoadmInterfaces;
45     private static final Logger LOG = LoggerFactory
46             .getLogger(OpenRoadmOtnInterface221.class);
47
48     public OpenRoadmOtnInterface221(PortMapping portMapping,
49             OpenRoadmInterfaces openRoadmInterfaces) {
50         this.portMapping = portMapping;
51         this.openRoadmInterfaces = openRoadmInterfaces;
52     }
53
54     public String createOpenRoadmEth1GInterface(String nodeId,
55             String logicalConnPoint) throws OpenRoadmInterfaceException {
56         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
57         if (portMap == null) {
58             throwException(nodeId, logicalConnPoint);
59         }
60
61         // Ethernet interface specific data
62         EthernetBuilder ethIfBuilder = new EthernetBuilder()
63                 .setSpeed(1000L);
64         InterfaceBuilder ethInterfaceBldr = createGenericInterfaceBuilder(
65                 portMap, EthernetCsmacd.class,
66                 logicalConnPoint + "-ETHERNET1G");
67         // Create Interface1 type object required for adding as augmentation
68         Interface1Builder ethIf1Builder = new Interface1Builder();
69         ethInterfaceBldr.addAugmentation(Interface1.class,
70                 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,
75                 portMap.getSupportingCircuitPackName(), true);
76         this.portMapping.updateMapping(nodeId, portMap);
77         String ethernetInterfaceName = ethInterfaceBldr.getName();
78
79         return ethernetInterfaceName;
80     }
81
82     private void throwException(String nodeId, String logicalConnPoint)
83             throws OpenRoadmInterfaceException {
84         throw new OpenRoadmInterfaceException(String.format(
85                 "Unable to get mapping from PortMapping for node % and logical connection port %s",
86                 nodeId, logicalConnPoint));
87     }
88
89     private InterfaceBuilder createGenericInterfaceBuilder(Mapping portMap,
90             Class<? extends InterfaceType> type, String key) {
91         InterfaceBuilder interfaceBuilder = new InterfaceBuilder()
92                 // .setDescription(" TBD ")
93                 // .setCircuitId(" TBD ")
94                 .setSupportingCircuitPackName(
95                         portMap.getSupportingCircuitPackName())
96                 .setSupportingPort(portMap.getSupportingPort())
97                 .setAdministrativeState(AdminStates.InService)
98                 // TODO get rid of unchecked cast warning
99                 .setType(type).setName(key).withKey(new InterfaceKey(key));
100         return interfaceBuilder;
101     }
102
103     public String createOpenRoadmEth10GInterface(String nodeId,
104             String logicalConnPoint) throws OpenRoadmInterfaceException {
105         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
106         if (portMap == null) {
107             throwException(nodeId, logicalConnPoint);
108         }
109
110         // Ethernet interface specific data
111         EthernetBuilder ethIfBuilder = new EthernetBuilder()
112                 // .setAutoNegotiation(EthAttributes.AutoNegotiation.Disabled)
113                 .setSpeed(10000L);
114         // Create Interface1 type object required for adding as augmentation
115         Interface1Builder ethIf1Builder = new Interface1Builder();
116         InterfaceBuilder ethInterfaceBldr = createGenericInterfaceBuilder(portMap, EthernetCsmacd.class,
117                 logicalConnPoint + "-ETHERNET10G").addAugmentation(Interface1.class,
118                         ethIf1Builder.setEthernet(ethIfBuilder.build()).build());
119         // Post interface on the device
120         this.openRoadmInterfaces.postOTNInterface(nodeId, ethInterfaceBldr);
121         // Post the equipment-state change on the device circuit-pack
122         this.openRoadmInterfaces.postOTNEquipmentState(nodeId,
123                 portMap.getSupportingCircuitPackName(), true);
124         this.portMapping.updateMapping(nodeId, portMap);
125         String ethernetInterfaceName = ethInterfaceBldr.getName();
126
127         return ethernetInterfaceName;
128     }
129
130     public String createOpenRoadmOdu2eInterface(String nodeId,
131             String logicalConnPoint, String serviceName, String payLoad,
132             boolean isNetworkPort, int tribPortNumber, int tribSlotIndex)
133             throws OpenRoadmInterfaceException {
134         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
135         if (portMap == null) {
136             throwException(nodeId, logicalConnPoint);
137         }
138         String supportingInterface = null;
139
140         if (isNetworkPort) {
141             supportingInterface = portMap.getSupportingOdu4();
142         } else {
143             supportingInterface = logicalConnPoint + "-ETHERNET10G";
144         }
145
146         if (supportingInterface == null) {
147             throw new OpenRoadmInterfaceException(
148                     "Interface Creation failed because of missing supported "
149                     + "ODU4 on network end or Ethernet on client");
150         }
151
152         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(
153                 portMap, OtnOdu.class,
154                 logicalConnPoint + "-ODU2e-" + serviceName)
155                         .setSupportingInterface(supportingInterface);
156
157         // ODU interface specific data
158         OduBuilder oduIfBuilder = new OduBuilder().setRate(ODU2e.class)
159                 .setOduFunction(ODUTTPCTP.class)
160                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
161         LOG.debug("Inside the ODU2e creation {} {} {}", isNetworkPort,
162                 tribPortNumber, tribSlotIndex);
163         if (isNetworkPort) {
164             List<Uint16> tribSlots = new ArrayList<>();
165             Uint16 newIdx = Uint16.valueOf(tribSlotIndex);
166             tribSlots.add(newIdx);
167             IntStream.range(tribSlotIndex, tribSlotIndex + 8)
168                     .forEach(nbr -> tribSlots.add(Uint16.valueOf(nbr)));
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         } 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)
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         } else {
238             LOG.debug("Current port is a client port");
239             OpuBuilder opuBldr = new OpuBuilder()
240                     .setPayloadType(new PayloadTypeDef(payLoad))
241                     .setExpPayloadType(new PayloadTypeDef(payLoad));
242             oduIfBuilder.setOpu(opuBldr.build());
243         }
244         // Create Interface1 type object required for adding as augmentation
245         // TODO look at imports of different versions of class
246         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder
247             oduIf1Builder = new
248             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     public String createOpenRoadmOdu2Interface(String nodeId,
260             String logicalConnPoint, String serviceName, String payLoad,
261             boolean isNetworkPort, int tribPortNumber, int tribSlotIndex)
262             throws OpenRoadmInterfaceException {
263         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
264         String supportingInterface = null;
265
266         if (portMap != null) {
267             if (isNetworkPort) {
268                 supportingInterface = portMap.getSupportingOdu4();
269             } else {
270                 supportingInterface = portMap.getSupportingEthernet();
271             }
272         } else {
273             throwException(nodeId, logicalConnPoint);
274         }
275         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(
276                 portMap, OtnOdu.class,
277                 logicalConnPoint + "-ODU2-" + serviceName)
278                         .setSupportingInterface(supportingInterface);
279
280         OduBuilder oduIfBuilder = new OduBuilder().setRate(ODU2.class)
281                 .setOduFunction(ODUTTPCTP.class)
282                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
283         if (isNetworkPort) {
284             List<Uint16> tribSlots = new ArrayList<>();
285             IntStream.range(tribSlotIndex, tribSlotIndex + 8)
286                     .forEach(nbr -> tribSlots.add(Uint16.valueOf(nbr)));
287             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
288                     // set trib port numbers
289                     .setTribPortNumber(tribPortNumber)
290                     .setTribSlots(tribSlots);
291             oduIfBuilder.setOduFunction(ODUCTP.class)
292                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
293                     .setParentOduAllocation(parentOduAllocationBuilder.build());
294         } else {
295             // Set Opu attributes
296             OpuBuilder opuBldr = new OpuBuilder()
297                     .setPayloadType(new PayloadTypeDef(payLoad))
298                     .setExpPayloadType(new PayloadTypeDef(payLoad));
299             oduIfBuilder.setOpu(opuBldr.build());
300         }
301
302         // Create Interface1 type object required for adding as augmentation
303         // TODO look at imports of different versions of class
304         // Create Interface1 type object required for adding as augmentation
305         // TODO look at imports of different versions of class
306         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder
307             oduIf1Builder = new
308                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
309         oduInterfaceBldr.addAugmentation(
310                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
311                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
312
313         // Post interface on the device
314         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
315         LOG.info("returning the ODU2 inteface {}", oduInterfaceBldr.getName());
316         return oduInterfaceBldr.getName();
317     }
318 }