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