fb940790463ee51a357f763352b159ba41cdbec5
[transportpce.git] / common / src / main / java / org / opendaylight / transportpce / common / openroadminterfaces / OpenRoadmInterfacesImpl121.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.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.port.Interfaces;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.AdminStates;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.States;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOdu;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOtu;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.loopback.rev161014.maint.loopback.MaintLoopbackBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.testsignal.rev161014.maint.testsignal.MaintTestsignalBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.container.OduBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.otu.container.OtuBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class OpenRoadmInterfacesImpl121 {
48
49     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl121.class);
50
51     private final DeviceTransactionManager deviceTransactionManager;
52
53     public OpenRoadmInterfacesImpl121(DeviceTransactionManager deviceTransactionManager) {
54         this.deviceTransactionManager = deviceTransactionManager;
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         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
76         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
77             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
78         // TODO: instead of using this infinite loop coupled with this timeout,
79         // it would be better to use a notification mechanism from the device to be advertised
80         // that the new created interface is present in the device circuit-pack/port
81         final Thread current = Thread.currentThread();
82         Thread timer = new Thread() {
83             public void run() {
84                 try {
85                     Thread.sleep(3000);
86                     current.interrupt();
87                 } catch (InterruptedException e) {
88                     LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
89                 }
90             }
91         };
92         try {
93             txSubmitFuture.get();
94             LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
95             boolean devicePortIsUptodated = false;
96             while (!devicePortIsUptodated) {
97                 devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
98             }
99             LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
100                 ifBuilder.getName(), ifBuilder.getSupportingPort());
101             timer.interrupt();
102         } catch (InterruptedException | ExecutionException e) {
103             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
104                 .getName(), nodeId), e);
105         }
106     }
107
108
109     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
110         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
111             .child(Interface.class, new InterfaceKey(interfaceName));
112         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
113             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
114     }
115
116
117     public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
118         Optional<Interface> intf2DeleteOpt;
119         try {
120             intf2DeleteOpt = getInterface(nodeId, interfaceName);
121         } catch (OpenRoadmInterfaceException e) {
122             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
123                 interfaceName, nodeId), e);
124         }
125         if (intf2DeleteOpt.isPresent()) {
126             Interface intf2Delete = intf2DeleteOpt.get();
127             // State admin state to out of service
128             InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
129             if (ifBuilder.getType() == OtnOdu.class) {
130                 Interface1Builder oduBuilder = new Interface1Builder(intf2Delete.augmentation(Interface1.class));
131                 OduBuilder odu = new OduBuilder(oduBuilder.getOdu());
132                 if (odu.getMaintTestsignal() != null) {
133                     MaintTestsignalBuilder maintSignalBuilder = new MaintTestsignalBuilder();
134                     maintSignalBuilder.setEnabled(false);
135                     odu.setMaintTestsignal(maintSignalBuilder.build());
136                 }
137                 oduBuilder.setOdu(odu.build());
138                 ifBuilder.addAugmentation(oduBuilder.build());
139             } else if (ifBuilder.getType() == OtnOtu.class) {
140                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder
141                     otuBuilder =
142                     new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder(
143                         intf2Delete.augmentation(
144                             org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1
145                                 .class));
146                 OtuBuilder otu = new OtuBuilder(otuBuilder.getOtu());
147                 if (otu.getMaintLoopback() != null) {
148                     MaintLoopbackBuilder maintLoopBackBuilder = new MaintLoopbackBuilder();
149                     maintLoopBackBuilder.setEnabled(false);
150                     otu.setMaintLoopback(maintLoopBackBuilder.build());
151                 }
152                 otuBuilder.setOtu(otu.build());
153                 ifBuilder.addAugmentation(otuBuilder.build());
154             }
155             ifBuilder.setAdministrativeState(AdminStates.OutOfService);
156             // post interface with updated admin state
157             try {
158                 postInterface(nodeId, ifBuilder);
159             } catch (OpenRoadmInterfaceException ex) {
160                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
161                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
162             }
163
164             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
165                 Interface.class, new InterfaceKey(interfaceName));
166             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
167                 nodeId);
168             DeviceTransaction deviceTx;
169             try {
170                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
171                 if (deviceTxOpt.isPresent()) {
172                     deviceTx = deviceTxOpt.get();
173                 } else {
174                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
175                         nodeId));
176                 }
177             } catch (InterruptedException | ExecutionException e) {
178                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
179                     nodeId), e);
180             }
181
182             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
183             FluentFuture<? extends @NonNull CommitInfo> commit =
184                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
185
186             try {
187                 commit.get();
188                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
189             } catch (InterruptedException | ExecutionException e) {
190                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
191                     interfaceName, nodeId), e);
192             }
193             // change the equipment state on circuit pack if xpdr node
194             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
195                 StringConstants.NETWORK_TOKEN)) {
196                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
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
207         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
208             CircuitPacks.class, new CircuitPacksKey(circuitPackName));
209         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
210             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
211             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
212         CircuitPacks cp = null;
213         if (cpOpt.isPresent()) {
214             cp = cpOpt.get();
215         } else {
216             throw new OpenRoadmInterfaceException(String.format(
217                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
218         }
219         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
220         boolean change = false;
221         if (activate) {
222             if (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedInuse)) {
223                 cpBldr.setEquipmentState(States.NotReservedInuse);
224                 change = true;
225             }
226         } else if (
227             (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedAvailable))) {
228             cpBldr.setEquipmentState(States.NotReservedAvailable);
229             change = true;
230         }
231         if (change) {
232             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
233                 nodeId);
234             DeviceTransaction deviceTx;
235             try {
236                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
237                 if (deviceTxOpt.isPresent()) {
238                     deviceTx = deviceTxOpt.get();
239                 } else {
240                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
241                         nodeId));
242                 }
243             } catch (InterruptedException | ExecutionException e) {
244                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
245                     nodeId), e);
246             }
247             deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
248             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
249                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
250             try {
251                 txSubmitFuture.get();
252                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
253             } catch (InterruptedException | ExecutionException e) {
254                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
255                     nodeId), e);
256             }
257         }
258     }
259
260     public String getSupportedInterface(String nodeId, String interf) {
261         Optional<Interface> supInterfOpt;
262         try {
263             supInterfOpt = getInterface(nodeId, interf);
264             if (supInterfOpt.isPresent()) {
265                 return supInterfOpt.get().getSupportingInterface();
266             } else {
267                 return null;
268             }
269         } catch (OpenRoadmInterfaceException e) {
270             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
271             return null;
272         }
273     }
274
275     private boolean checkIfDevicePortIsUpdatedWithInterface(String nodeId, InterfaceBuilder ifBuilder) {
276         KeyedInstanceIdentifier<Ports, PortsKey> portIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
277             .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
278             .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()));
279         Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
280             portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
281         if (port.getInterfaces() == null) {
282             return false;
283         }
284         for (Interfaces interf : port.getInterfaces()) {
285             if (interf.getInterfaceName().equals(ifBuilder.getName())) {
286                 return true;
287             }
288         }
289         return false;
290     }
291 }