Merge changes If75e58ba,I48f074cd
[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 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();
99             ifBuilder.setName(intf2Delete.getName());
100             ifBuilder.setType(intf2Delete.getType());
101             ifBuilder.setAdministrativeState(AdminStates.OutOfService);
102             // post interface with updated admin state
103             try {
104                 postInterface(nodeId, ifBuilder);
105             } catch (OpenRoadmInterfaceException ex) {
106                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
107                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
108             }
109
110             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
111                 Interface.class, new InterfaceKey(interfaceName));
112             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
113                 nodeId);
114             DeviceTransaction deviceTx;
115             try {
116                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
117                 if (deviceTxOpt.isPresent()) {
118                     deviceTx = deviceTxOpt.get();
119                 } else {
120                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
121                         nodeId));
122                 }
123             } catch (InterruptedException | ExecutionException e) {
124                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
125                     nodeId), e);
126             }
127
128             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
129             FluentFuture<? extends @NonNull CommitInfo> commit =
130                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
131
132             try {
133                 commit.get();
134                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
135             } catch (InterruptedException | ExecutionException e) {
136                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
137                     interfaceName, nodeId), e);
138             }
139             // change the equipment state on circuit pack if xpdr node
140             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
141                 StringConstants.NETWORK_TOKEN)) {
142                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
143             }
144
145         } else {
146             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
147         }
148     }
149
150     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
151         throws OpenRoadmInterfaceException {
152         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
153             CircuitPacks.class, new CircuitPacksKey(circuitPackName));
154         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
155             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
156             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
157         CircuitPacks cp = null;
158         if (cpOpt.isPresent()) {
159             cp = cpOpt.get();
160         } else {
161             throw new OpenRoadmInterfaceException(String.format(
162                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
163         }
164         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
165         boolean change = false;
166         if (activate) {
167             if (cpBldr.getEquipmentState() != null
168                 && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
169                 cpBldr.setEquipmentState(States.NotReservedInuse);
170                 change = true;
171             }
172         } else if ((cpBldr.getEquipmentState() != null
173             && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
174             cpBldr.setEquipmentState(States.NotReservedAvailable);
175             change = true;
176         }
177         if (change) {
178             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
179                 nodeId);
180             DeviceTransaction deviceTx;
181             try {
182                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
183                 if (deviceTxOpt.isPresent()) {
184                     deviceTx = deviceTxOpt.get();
185                 } else {
186                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
187                         nodeId));
188                 }
189             } catch (InterruptedException | ExecutionException e) {
190                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
191                     nodeId), e);
192             }
193             deviceTx.merge(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
194             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
195                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
196             try {
197                 txSubmitFuture.get();
198                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
199             } catch (InterruptedException | ExecutionException e) {
200                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
201                     nodeId), e);
202             }
203         }
204     }
205
206 }