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