update portmapping YANG to store SAPI/DAPI hashmap
[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         ethIfBuilder.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 Eth iface 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).setTribSlots(tribSlots);
171             oduIfBuilder.setOduFunction(ODUCTP.class)
172                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
173                     .setParentOduAllocation(parentOduAllocationBuilder.build());
174         } else {
175             // Set Opu attributes
176             OpuBuilder opuBldr = new OpuBuilder()
177                     .setPayloadType(new PayloadTypeDef(payLoad))
178                     .setExpPayloadType(new PayloadTypeDef(payLoad));
179             oduIfBuilder.setOpu(opuBldr.build());
180         }
181         // Create Interface1 type object required for adding as augmentation
182         // TODO look at imports of different versions of class
183         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder
184             oduIf1Builder = new
185                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
186         oduInterfaceBldr.addAugmentation(
187                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
188                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
189
190         // Post interface on the device
191         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
192         LOG.info("returning the ODU2e inteface {}", oduInterfaceBldr.getName());
193         return oduInterfaceBldr.getName();
194     }
195
196     public String createOpenRoadmOdu0Interface(String nodeId,
197             String logicalConnPoint, String serviceName, String payLoad,
198             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             throwException(nodeId, logicalConnPoint);
210         }
211         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(
212                 portMap, OtnOdu.class,
213                 logicalConnPoint + "-ODU0-" + serviceName);
214         oduInterfaceBldr.setSupportingInterface(supportingInterface);
215
216         // ODU interface specific data
217         OduBuilder oduIfBuilder = new OduBuilder().setRate(ODU0.class)
218                 .setOduFunction(ODUTTPCTP.class)
219                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
220         if (isNetworkPort) {
221             LOG.debug("Network port is true");
222             List<Uint16> tribSlots = new ArrayList<>();
223             // add trib slots
224             tribSlots.add(Uint16.valueOf(tribSlot));
225             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
226                     // set trib port numbers
227                     .setTribPortNumber(tribPortNumber).setTribSlots(tribSlots);
228             oduIfBuilder.setOduFunction(ODUCTP.class)
229                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
230                     .setParentOduAllocation(parentOduAllocationBuilder.build());
231             LOG.debug("Network port is true {} {} {}",
232                     oduIfBuilder.getParentOduAllocation().getTribSlots(),
233                     oduIfBuilder.getRate(),
234                     oduIfBuilder.getParentOduAllocation().getTribPortNumber());
235         } else {
236             LOG.debug("Current port is a client port");
237             OpuBuilder opuBldr = new OpuBuilder()
238                     .setPayloadType(new PayloadTypeDef(payLoad))
239                     .setExpPayloadType(new PayloadTypeDef(payLoad));
240             oduIfBuilder.setOpu(opuBldr.build());
241         }
242         // Create Interface1 type object required for adding as augmentation
243         // TODO look at imports of different versions of class
244         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder
245             oduIf1Builder = new
246             org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
247         oduInterfaceBldr.addAugmentation(
248                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
249                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
250
251         // Post interface on the device
252         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
253         LOG.info("returning the ODU0 inteface {}", oduInterfaceBldr.getName());
254         return oduInterfaceBldr.getName();
255     }
256
257     public String createOpenRoadmOdu2Interface(String nodeId,
258             String logicalConnPoint, String serviceName, String payLoad,
259             boolean isNetworkPort, int tribPortNumber, int tribSlotIndex)
260             throws OpenRoadmInterfaceException {
261         Mapping portMap = this.portMapping.getMapping(nodeId, logicalConnPoint);
262         String supportingInterface = null;
263
264         if (portMap != null) {
265             if (isNetworkPort) {
266                 supportingInterface = portMap.getSupportingOdu4();
267             } else {
268                 supportingInterface = portMap.getSupportingEthernet();
269             }
270         } else {
271             throwException(nodeId, logicalConnPoint);
272         }
273         InterfaceBuilder oduInterfaceBldr = createGenericInterfaceBuilder(
274                 portMap, OtnOdu.class,
275                 logicalConnPoint + "-ODU2-" + serviceName)
276                         .setSupportingInterface(supportingInterface);
277
278         OduBuilder oduIfBuilder = new OduBuilder().setRate(ODU2.class)
279                 .setOduFunction(ODUTTPCTP.class)
280                 .setMonitoringMode(OduAttributes.MonitoringMode.Terminated);
281         if (isNetworkPort) {
282             List<Uint16> tribSlots = new ArrayList<>();
283             IntStream.range(tribSlotIndex, tribSlotIndex + 8)
284                     .forEach(nbr -> tribSlots.add(Uint16.valueOf(nbr)));
285             ParentOduAllocationBuilder parentOduAllocationBuilder = new ParentOduAllocationBuilder()
286                     // set trib port numbers
287                     .setTribPortNumber(tribPortNumber).setTribSlots(tribSlots);
288             oduIfBuilder.setOduFunction(ODUCTP.class)
289                     .setMonitoringMode(OduAttributes.MonitoringMode.Monitored)
290                     .setParentOduAllocation(parentOduAllocationBuilder.build());
291         } else {
292             // Set Opu attributes
293             OpuBuilder opuBldr = new OpuBuilder()
294                     .setPayloadType(new PayloadTypeDef(payLoad))
295                     .setExpPayloadType(new PayloadTypeDef(payLoad));
296             oduIfBuilder.setOpu(opuBldr.build());
297         }
298
299         // Create Interface1 type object required for adding as augmentation
300         // TODO look at imports of different versions of class
301         // Create Interface1 type object required for adding as augmentation
302         // TODO look at imports of different versions of class
303         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder
304             oduIf1Builder = new
305                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder();
306         oduInterfaceBldr.addAugmentation(
307                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1.class,
308                 oduIf1Builder.setOdu(oduIfBuilder.build()).build());
309
310         // Post interface on the device
311         this.openRoadmInterfaces.postOTNInterface(nodeId, oduInterfaceBldr);
312         LOG.info("returning the ODU2 inteface {}", oduInterfaceBldr.getName());
313         return oduInterfaceBldr.getName();
314     }
315 }