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