5444db03e94e0cb6f3c66ae83c9b63bc53555552
[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.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacks;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.circuit.packs.CircuitPacksKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.Interface;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.interfaces.grp.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev200529.org.openroadm.device.container.OrgOpenroadmDevice;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.States;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class OpenRoadmInterfacesImpl710 {
36
37     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl710.class);
38
39     private final DeviceTransactionManager deviceTransactionManager;
40
41     public OpenRoadmInterfacesImpl710(DeviceTransactionManager deviceTransactionManager) {
42         this.deviceTransactionManager = deviceTransactionManager;
43     }
44
45     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
46         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
47         DeviceTransaction deviceTx;
48         try {
49             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
50             if (deviceTxOpt.isPresent()) {
51                 deviceTx = deviceTxOpt.get();
52             } else {
53                 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
54                     nodeId));
55             }
56         } catch (InterruptedException | ExecutionException e) {
57             throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
58                 nodeId), e);
59         }
60
61         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
62             Interface.class, new InterfaceKey(ifBuilder.getName()));
63         LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
64             ifBuilder.getType().toString());
65         deviceTx.merge(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
66         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
67             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
68         try {
69             txSubmitFuture.get();
70             LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
71         } catch (InterruptedException | ExecutionException e) {
72             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
73                 .getName(), nodeId), e);
74         }
75     }
76
77
78     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
79         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
80             .child(Interface.class, new InterfaceKey(interfaceName));
81         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
82             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
83     }
84
85
86     public synchronized void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
87         LOG.info("deleting interface {} on device71 {}", interfaceName, nodeId);
88         Optional<Interface> intf2DeleteOpt;
89         try {
90             intf2DeleteOpt = getInterface(nodeId, interfaceName);
91         } catch (OpenRoadmInterfaceException e) {
92             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
93                 interfaceName, nodeId), e);
94         }
95         if (intf2DeleteOpt.isPresent()) {
96             Interface intf2Delete = intf2DeleteOpt.get();
97             // State admin state to out of service
98             InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
99             ifBuilder.setAdministrativeState(AdminStates.OutOfService);
100             // post interface with updated admin state
101             try {
102                 postInterface(nodeId, ifBuilder);
103             } catch (OpenRoadmInterfaceException ex) {
104                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
105                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
106             }
107
108             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
109                 Interface.class, new InterfaceKey(interfaceName));
110             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
111                 nodeId);
112             DeviceTransaction deviceTx;
113             try {
114                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
115                 if (deviceTxOpt.isPresent()) {
116                     deviceTx = deviceTxOpt.get();
117                 } else {
118                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
119                         nodeId));
120                 }
121             } catch (InterruptedException | ExecutionException e) {
122                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
123                     nodeId), e);
124             }
125
126             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
127             FluentFuture<? extends @NonNull CommitInfo> commit =
128                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
129
130             try {
131                 commit.get();
132                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
133             } catch (InterruptedException | ExecutionException e) {
134                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
135                     interfaceName, nodeId), e);
136             }
137             // change the equipment state on circuit pack if xpdr node
138             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
139                 StringConstants.NETWORK_TOKEN)) {
140                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
141             }
142
143         } else {
144             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
145         }
146     }
147
148     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
149         throws OpenRoadmInterfaceException {
150         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
151             CircuitPacks.class, new CircuitPacksKey(circuitPackName));
152         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
153             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
154             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
155         CircuitPacks cp = null;
156         if (cpOpt.isPresent()) {
157             cp = cpOpt.get();
158         } else {
159             throw new OpenRoadmInterfaceException(String.format(
160                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
161         }
162         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
163         boolean change = false;
164         if (activate) {
165             if (cpBldr.getEquipmentState() != null
166                 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
167                 cpBldr.setEquipmentState(States.NotReservedInuse);
168                 change = true;
169             }
170         } else if ((cpBldr.getEquipmentState() != null
171             && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
172             cpBldr.setEquipmentState(States.NotReservedAvailable);
173             change = true;
174         }
175         if (change) {
176             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
177                 nodeId);
178             DeviceTransaction deviceTx;
179             try {
180                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
181                 if (deviceTxOpt.isPresent()) {
182                     deviceTx = deviceTxOpt.get();
183                 } else {
184                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
185                         nodeId));
186                 }
187             } catch (InterruptedException | ExecutionException e) {
188                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
189                     nodeId), e);
190             }
191             deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
192             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
193                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
194             try {
195                 txSubmitFuture.get();
196                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
197             } catch (InterruptedException | ExecutionException e) {
198                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
199                     nodeId), e);
200             }
201         }
202     }
203
204     public String getSupportedInterface(String nodeId, String interf) {
205         Optional<Interface> supInterfOpt;
206         try {
207             supInterfOpt = getInterface(nodeId, interf);
208             if (supInterfOpt.isPresent()) {
209                 return supInterfOpt.get().getSupportingInterfaceList().get(0);
210             } else {
211                 return null;
212             }
213         } catch (OpenRoadmInterfaceException e) {
214             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
215             return null;
216         }
217     }
218
219 }