Adapt TransportPCE code to Sulfur
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / openroadminterfaces / OpenRoadmInterfacesImpl710.java
1 /*
2  * Copyright © 2020 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
9 package org.opendaylight.transportpce.common.openroadminterfaces;
10
11 import com.google.common.util.concurrent.FluentFuture;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.CommitInfo;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.transportpce.common.StringConstants;
19 import org.opendaylight.transportpce.common.Timeouts;
20 import org.opendaylight.transportpce.common.device.DeviceTransaction;
21 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
22 import org.opendaylight.transportpce.common.mapping.PortMapping;
23 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.mapping.Mapping;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.OrgOpenroadmDeviceData;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.Ports;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.pack.PortsKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.Interface;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.port.Interfaces;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.States;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class OpenRoadmInterfacesImpl710 {
43
44     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl710.class);
45
46     private final DeviceTransactionManager deviceTransactionManager;
47     private final PortMapping portMapping;
48     private final PortMappingVersion710 portMapping710;
49
50     public OpenRoadmInterfacesImpl710(DeviceTransactionManager deviceTransactionManager,
51             PortMapping portMapping, PortMappingVersion710 portMapping710) {
52         this.deviceTransactionManager = deviceTransactionManager;
53         this.portMapping = portMapping;
54         this.portMapping710 = portMapping710;
55     }
56
57     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
58         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
59         DeviceTransaction deviceTx;
60         try {
61             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
62             if (deviceTxOpt.isPresent()) {
63                 deviceTx = deviceTxOpt.get();
64             } else {
65                 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
66                     nodeId));
67             }
68         } catch (InterruptedException | ExecutionException e) {
69             throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
70                 nodeId), e);
71         }
72
73         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
74             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
75             .child(Interface.class, new InterfaceKey(ifBuilder.getName()))
76             .build();
77         LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
78             ifBuilder.getType().toString());
79         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
80         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
81             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
82         // TODO: instead of using this infinite loop coupled with this timeout,
83         // it would be better to use a notification mechanism from the device to be advertised
84         // that the new created interface is present in the device circuit-pack/port
85         final Thread current = Thread.currentThread();
86         Thread timer = new Thread() {
87             public void run() {
88                 try {
89                     Thread.sleep(3000);
90                     current.interrupt();
91                 } catch (InterruptedException e) {
92                     LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
93                 }
94             }
95         };
96         try {
97             txSubmitFuture.get();
98             LOG.info("Successfully posted/deleted interface {} on node {}", ifBuilder.getName(), nodeId);
99             // this check is not needed during the delete operation
100             // during the delete operation, ifBuilder does not contain supporting-cp and supporting-port
101             if (ifBuilder.getSupportingCircuitPackName() != null && ifBuilder.getSupportingPort() != null) {
102                 boolean devicePortIsUptodated = false;
103                 while (!devicePortIsUptodated) {
104                     devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
105                 }
106                 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
107                     ifBuilder.getName(), ifBuilder.getSupportingPort());
108             }
109             timer.interrupt();
110         } catch (InterruptedException | ExecutionException e) {
111             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
112                 .getName(), nodeId), e);
113         }
114     }
115
116
117     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
118         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
119             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
120             .child(Interface.class, new InterfaceKey(interfaceName))
121             .build();
122         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
123             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
124     }
125
126
127     public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
128         LOG.info("deleting interface {} on device71 {}", interfaceName, nodeId);
129         Optional<Interface> intf2DeleteOpt;
130         try {
131             intf2DeleteOpt = getInterface(nodeId, interfaceName);
132         } catch (OpenRoadmInterfaceException e) {
133             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
134                 interfaceName, nodeId), e);
135         }
136         if (intf2DeleteOpt.isPresent()) {
137             Interface intf2Delete = intf2DeleteOpt.get();
138             // set the name and set the type. Having these two lines will post the interface with just
139             // name, type and admin-state, without all the default values such as maint-testsignal
140             //  delete the interfaces successfully
141             // just build a new Interface builder without the arguments for inter2Delete
142             InterfaceBuilder ifBuilder = new InterfaceBuilder()
143                 .setAdministrativeState(AdminStates.OutOfService)
144                 // Though these could be redundant, but 'when' statements are causing problem,
145                 // when deleting the interfaces trying to be deleted
146                 .setName(intf2Delete.getName())
147                 .setType(intf2Delete.getType());
148
149             // post interface with updated admin state
150             try {
151                 postInterface(nodeId, ifBuilder);
152             } catch (OpenRoadmInterfaceException ex) {
153                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
154                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
155             }
156
157             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
158                 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
159                 .child(Interface.class, new InterfaceKey(interfaceName))
160                 .build();
161             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
162                 nodeId);
163             DeviceTransaction deviceTx;
164             try {
165                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
166                 if (deviceTxOpt.isPresent()) {
167                     deviceTx = deviceTxOpt.get();
168                 } else {
169                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
170                         nodeId));
171                 }
172             } catch (InterruptedException | ExecutionException e) {
173                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
174                     nodeId), e);
175             }
176
177             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
178             FluentFuture<? extends @NonNull CommitInfo> commit =
179                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
180
181             try {
182                 commit.get();
183                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
184             } catch (InterruptedException | ExecutionException e) {
185                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
186                     interfaceName, nodeId), e);
187             }
188             // change the equipment state on circuit pack if xpdr node
189             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
190                 StringConstants.NETWORK_TOKEN)) {
191                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
192                 // Here we update the port-mapping data after the interface delete
193                 Mapping oldMapping = this.portMapping.getMapping(
194                     nodeId, intf2Delete.getSupportingCircuitPackName(), intf2Delete.getSupportingPort());
195                 this.portMapping.deleteMapping(nodeId, oldMapping.getLogicalConnectionPoint());
196                 this.portMapping710.updateMapping(nodeId, oldMapping);
197             }
198
199         } else {
200             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
201         }
202     }
203
204     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
205         throws OpenRoadmInterfaceException {
206         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier
207             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
208             .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName))
209             .build();
210         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
211             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
212             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
213         CircuitPacks cp = null;
214         if (cpOpt.isPresent()) {
215             cp = cpOpt.get();
216         } else {
217             throw new OpenRoadmInterfaceException(String.format(
218                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
219         }
220         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
221         boolean change = false;
222         if (activate) {
223             if (cpBldr.getEquipmentState() != null
224                 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
225                 cpBldr.setEquipmentState(States.NotReservedInuse);
226                 change = true;
227             }
228         } else if ((cpBldr.getEquipmentState() != null
229             && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
230             cpBldr.setEquipmentState(States.NotReservedAvailable);
231             change = true;
232         }
233         if (change) {
234             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
235                 nodeId);
236             DeviceTransaction deviceTx;
237             try {
238                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
239                 if (deviceTxOpt.isPresent()) {
240                     deviceTx = deviceTxOpt.get();
241                 } else {
242                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
243                         nodeId));
244                 }
245             } catch (InterruptedException | ExecutionException e) {
246                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
247                     nodeId), e);
248             }
249             deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
250             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
251                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
252             try {
253                 txSubmitFuture.get();
254                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
255             } catch (InterruptedException | ExecutionException e) {
256                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
257                     nodeId), e);
258             }
259         }
260     }
261
262     public String getSupportedInterface(String nodeId, String interf) {
263         Optional<Interface> supInterfOpt;
264         try {
265             supInterfOpt = getInterface(nodeId, interf);
266             if (supInterfOpt.isPresent()) {
267                 return supInterfOpt.get().getSupportingInterfaceList().stream().findFirst().get();
268             } else {
269                 return null;
270             }
271         } catch (OpenRoadmInterfaceException e) {
272             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
273             return null;
274         }
275     }
276
277     private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
278         InstanceIdentifier<Ports> portIID = InstanceIdentifier
279             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
280             .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
281             .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()))
282             .build();
283         Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
284             portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
285         if (port.getInterfaces() == null) {
286             return false;
287         }
288         for (Interfaces interf : port.getInterfaces()) {
289             if (interf.getInterfaceName().equals(ifBuilder.getName())) {
290                 return true;
291             }
292         }
293         return false;
294     }
295 }