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