Turn PortMappingImpl into a component
[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, PortMapping portMapping) {
51         this.deviceTransactionManager = deviceTransactionManager;
52         this.portMapping = portMapping;
53         this.portMapping221 = portMapping.getPortMappingVersion221();
54     }
55
56     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
57         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
58         DeviceTransaction deviceTx;
59         try {
60             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
61             if (deviceTxOpt.isPresent()) {
62                 deviceTx = deviceTxOpt.get();
63             } else {
64                 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
65                     nodeId));
66             }
67         } catch (InterruptedException | ExecutionException e) {
68             throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
69                 nodeId), e);
70         }
71
72         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
73             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
74             .child(Interface.class, new InterfaceKey(ifBuilder.getName()))
75             .build();
76         LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
77             ifBuilder.getType().toString());
78         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
79         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
80             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
81         // TODO: instead of using this infinite loop coupled with this timeout,
82         // it would be better to use a notification mechanism from the device to be advertised
83         // that the new created interface is present in the device circuit-pack/port
84         final Thread current = Thread.currentThread();
85         Thread timer = new Thread() {
86             public void run() {
87                 try {
88                     Thread.sleep(3000);
89                     current.interrupt();
90                 } catch (InterruptedException e) {
91                     LOG.error("Timeout before the new created interface appears on the deivce circuit-pack port", e);
92                 }
93             }
94         };
95         try {
96             txSubmitFuture.get();
97             LOG.info("Successfully posted/deleted interface {} on node {}", ifBuilder.getName(), nodeId);
98             // this check is not needed during the delete operation
99             // during the delete operation, ifBuilder does not contain supporting-cp and supporting-port
100             if (ifBuilder.getSupportingCircuitPackName() != null && ifBuilder.getSupportingPort() != null) {
101                 boolean devicePortIsUptodated = false;
102                 while (!devicePortIsUptodated) {
103                     devicePortIsUptodated = checkIfDevicePortIsUpdatedWithInterface(nodeId, ifBuilder);
104                 }
105                 LOG.info("{} - {} - interface {} updated on port {}", nodeId, ifBuilder.getSupportingCircuitPackName(),
106                     ifBuilder.getName(), ifBuilder.getSupportingPort());
107             }
108             timer.interrupt();
109         } catch (InterruptedException | ExecutionException e) {
110             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
111                 .getName(), nodeId), e);
112         }
113     }
114
115
116     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
117         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
118             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
119             .child(Interface.class, new InterfaceKey(interfaceName))
120             .build();
121         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
122             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
123     }
124
125
126     public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
127         LOG.info("deleting interface {} on device221 {}", interfaceName, nodeId);
128         Optional<Interface> intf2DeleteOpt;
129         try {
130             intf2DeleteOpt = getInterface(nodeId, interfaceName);
131         } catch (OpenRoadmInterfaceException e) {
132             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
133                 interfaceName, nodeId), e);
134         }
135         if (intf2DeleteOpt.isPresent()) {
136             Interface intf2Delete = intf2DeleteOpt.get();
137             // State admin state to out of service
138             InterfaceBuilder ifBuilder = new InterfaceBuilder()
139                 .setAdministrativeState(AdminStates.OutOfService)
140                 .setName(intf2Delete.getName())
141                 .setType(intf2Delete.getType());
142             // post interface with updated admin state
143             try {
144                 postInterface(nodeId, ifBuilder);
145             } catch (OpenRoadmInterfaceException ex) {
146                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
147                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
148             }
149
150             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier
151                 .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
152                 .child(Interface.class, new InterfaceKey(interfaceName))
153                 .build();
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                 Mapping oldmapping = this.portMapping.getMapping(nodeId, intf2Delete.getSupportingCircuitPackName(),
186                     intf2Delete.getSupportingPort());
187                 this.portMapping.deleteMapping(nodeId, oldmapping.getLogicalConnectionPoint());
188                 this.portMapping221.updateMapping(nodeId, oldmapping);
189             }
190
191         } else {
192             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
193         }
194     }
195
196     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
197         throws OpenRoadmInterfaceException {
198         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier
199             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
200             .child(CircuitPacks.class, new CircuitPacksKey(circuitPackName))
201             .build();
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().getSupportingInterface();
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         InstanceIdentifier<Ports> portIID = InstanceIdentifier
271             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
272             .child(CircuitPacks.class, new CircuitPacksKey(ifBuilder.getSupportingCircuitPackName()))
273             .child(Ports.class, new PortsKey(ifBuilder.getSupportingPort()))
274             .build();
275         Ports port = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL,
276             portIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT).get();
277         if (port.getInterfaces() == null) {
278             return false;
279         }
280         for (Interfaces interf : port.getInterfaces()) {
281             if (interf.getInterfaceName().equals(ifBuilder.getName())) {
282                 return true;
283             }
284         }
285         return false;
286     }
287 }