Adapt TransportPCE code to Sulfur
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / openroadminterfaces / OpenRoadmInterfacesImpl221.java
1 /*
2  * Copyright © 2017 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.PortMappingVersion221;
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.rev181019.OrgOpenroadmDeviceData;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.Ports;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.pack.PortsKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.port.Interfaces;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.States;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class OpenRoadmInterfacesImpl221 {
43
44     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl221.class);
45
46     private final DeviceTransactionManager deviceTransactionManager;
47     private final PortMapping portMapping;
48     private final PortMappingVersion221 portMapping221;
49
50     public OpenRoadmInterfacesImpl221(DeviceTransactionManager deviceTransactionManager,
51             PortMapping portMapping, PortMappingVersion221 portMapping221) {
52         this.deviceTransactionManager = deviceTransactionManager;
53         this.portMapping = portMapping;
54         this.portMapping221 = portMapping221;
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 device221 {}", 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             // State admin state to out of service
139             InterfaceBuilder ifBuilder = new InterfaceBuilder()
140                 .setAdministrativeState(AdminStates.OutOfService)
141                 .setName(intf2Delete.getName())
142                 .setType(intf2Delete.getType());
143             // post interface with updated admin state
144             try {
145                 postInterface(nodeId, ifBuilder);
146             } catch (OpenRoadmInterfaceException ex) {
147                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
148                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
149             }
150
151             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
152                 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
153                 .child(Interface.class, new InterfaceKey(interfaceName))
154                 .build();
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                 Mapping oldmapping = this.portMapping.getMapping(nodeId, intf2Delete.getSupportingCircuitPackName(),
187                     intf2Delete.getSupportingPort());
188                 this.portMapping.deleteMapping(nodeId, oldmapping.getLogicalConnectionPoint());
189                 this.portMapping221.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
200             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
201             .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName))
202             .build();
203         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
204             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
205             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
206         CircuitPacks cp = null;
207         if (cpOpt.isPresent()) {
208             cp = cpOpt.get();
209         } else {
210             throw new OpenRoadmInterfaceException(String.format(
211                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
212         }
213         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
214         boolean change = false;
215         if (activate) {
216             if (cpBldr.getEquipmentState() != null
217                     && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
218                 cpBldr.setEquipmentState(States.NotReservedInuse);
219                 change = true;
220             }
221         } else if ((cpBldr.getEquipmentState() != null
222                 && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
223             cpBldr.setEquipmentState(States.NotReservedAvailable);
224             change = true;
225         }
226         if (change) {
227             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
228                 nodeId);
229             DeviceTransaction deviceTx;
230             try {
231                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
232                 if (deviceTxOpt.isPresent()) {
233                     deviceTx = deviceTxOpt.get();
234                 } else {
235                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
236                         nodeId));
237                 }
238             } catch (InterruptedException | ExecutionException e) {
239                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
240                     nodeId), e);
241             }
242             deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
243             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
244                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
245             try {
246                 txSubmitFuture.get();
247                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
248             } catch (InterruptedException | ExecutionException e) {
249                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
250                     nodeId), e);
251             }
252         }
253     }
254
255     public String getSupportedInterface(String nodeId, String interf) {
256         Optional<Interface> supInterfOpt;
257         try {
258             supInterfOpt = getInterface(nodeId, interf);
259             if (supInterfOpt.isPresent()) {
260                 return supInterfOpt.get().getSupportingInterface();
261             } else {
262                 return null;
263             }
264         } catch (OpenRoadmInterfaceException e) {
265             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
266             return null;
267         }
268     }
269
270     private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
271         InstanceIdentifier<Ports> portIID = InstanceIdentifier
272             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
273             .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
274             .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()))
275             .build();
276         Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
277             portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
278         if (port.getInterfaces() == null) {
279             return false;
280         }
281         for (Interfaces interf : port.getInterfaces()) {
282             if (interf.getInterfaceName().equals(ifBuilder.getName())) {
283                 return true;
284             }
285         }
286         return false;
287     }
288 }