Fix OLM bugs to make olm functional tests running
[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.ListenableFuture;
12 import java.util.Optional;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.transportpce.common.StringConstants;
17 import org.opendaylight.transportpce.common.Timeouts;
18 import org.opendaylight.transportpce.common.device.DeviceTransaction;
19 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacks;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.States;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOdu;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOtu;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.loopback.rev171215.maint.loopback.MaintLoopbackBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.testsignal.rev171215.maint.testsignal.MaintTestsignalBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.odu.container.OduBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.otu.container.OtuBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class OpenRoadmInterfacesImpl221 {
42
43     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl221.class);
44
45     private final DeviceTransactionManager deviceTransactionManager;
46
47     public OpenRoadmInterfacesImpl221(DeviceTransactionManager deviceTransactionManager) {
48         this.deviceTransactionManager = deviceTransactionManager;
49     }
50
51     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
52         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
53         DeviceTransaction deviceTx;
54         try {
55             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
56             if (deviceTxOpt.isPresent()) {
57                 deviceTx = deviceTxOpt.get();
58             } else {
59                 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
60                     nodeId));
61             }
62         } catch (InterruptedException | ExecutionException e) {
63             throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
64                 nodeId), e);
65         }
66
67         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
68             Interface.class, new InterfaceKey(ifBuilder.getName()));
69         LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
70             ifBuilder.getType().toString());
71         deviceTx.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
72         ListenableFuture<Void> txSubmitFuture = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
73             Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
74         try {
75             txSubmitFuture.get();
76             LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
77         } catch (InterruptedException | ExecutionException e) {
78             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
79                 .getName(), nodeId), e);
80         }
81     }
82
83
84     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
85         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
86             .child(Interface.class, new InterfaceKey(interfaceName));
87         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
88             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
89     }
90
91
92     public void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
93         LOG.info("deleting interface {} on device221 {}", interfaceName, nodeId);
94         Optional<Interface> intf2DeleteOpt;
95         try {
96             intf2DeleteOpt = getInterface(nodeId, interfaceName);
97         } catch (OpenRoadmInterfaceException e) {
98             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
99                 interfaceName, nodeId), e);
100         }
101         if (intf2DeleteOpt.isPresent()) {
102             Interface intf2Delete = intf2DeleteOpt.get();
103             // State admin state to out of service
104             InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
105             if (ifBuilder.getType() == OtnOdu.class) {
106                 Interface1Builder oduBuilder = new Interface1Builder(intf2Delete.augmentation(Interface1.class));
107                 OduBuilder odu = new OduBuilder(oduBuilder.getOdu());
108                 if (odu.getMaintTestsignal() != null) {
109                     MaintTestsignalBuilder maintSignalBuilder = new MaintTestsignalBuilder();
110                     maintSignalBuilder.setEnabled(false);
111                     odu.setMaintTestsignal(maintSignalBuilder.build());
112                 }
113                 oduBuilder.setOdu(odu.build());
114                 ifBuilder.addAugmentation(Interface1.class, oduBuilder.build());
115             } else if (ifBuilder.getType() == OtnOtu.class) {
116                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder
117                     otuBuilder =
118                     new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder(
119                         intf2Delete.augmentation(
120                             org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1
121                             .class));
122                 OtuBuilder otu = new OtuBuilder(otuBuilder.getOtu());
123                 if (otu.getMaintLoopback() != null) {
124                     MaintLoopbackBuilder maintLoopBackBuilder = new MaintLoopbackBuilder();
125                     maintLoopBackBuilder.setEnabled(false);
126                     otu.setMaintLoopback(maintLoopBackBuilder.build());
127                 }
128                 otuBuilder.setOtu(otu.build());
129                 ifBuilder.addAugmentation(
130                     org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1.class,
131                     otuBuilder.build());
132             }
133             ifBuilder.setAdministrativeState(AdminStates.OutOfService);
134             // post interface with updated admin state
135             try {
136                 postInterface(nodeId, ifBuilder);
137             } catch (OpenRoadmInterfaceException ex) {
138                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
139                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
140             }
141
142             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
143                 Interface.class, new InterfaceKey(interfaceName));
144             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
145                 nodeId);
146             DeviceTransaction deviceTx;
147             try {
148                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
149                 if (deviceTxOpt.isPresent()) {
150                     deviceTx = deviceTxOpt.get();
151                 } else {
152                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
153                         nodeId));
154                 }
155             } catch (InterruptedException | ExecutionException e) {
156                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
157                     nodeId), e);
158             }
159
160             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
161             ListenableFuture<Void> submit = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
162                 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
163
164             try {
165                 submit.get();
166                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
167             } catch (InterruptedException | ExecutionException e) {
168                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
169                     interfaceName, nodeId), e);
170             }
171             // change the equipment state on circuit pack if xpdr node
172             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
173                 StringConstants.NETWORK_TOKEN)) {
174                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
175             }
176
177         } else {
178             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
179         }
180     }
181
182     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
183         throws OpenRoadmInterfaceException {
184         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
185             CircuitPacks.class, new CircuitPacksKey(circuitPackName));
186         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
187             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
188             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
189         CircuitPacks cp = null;
190         if (cpOpt.isPresent()) {
191             cp = cpOpt.get();
192         } else {
193             throw new OpenRoadmInterfaceException(String.format(
194                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
195         }
196         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
197         boolean change = false;
198         if (activate) {
199             if (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().getName()
200                 .equals(States.NotReservedInuse)) {
201                 cpBldr.setEquipmentState(States.NotReservedInuse);
202                 change = true;
203             }
204         } else if ((cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().getName()
205             .equals(States.NotReservedAvailable))) {
206             cpBldr.setEquipmentState(States.NotReservedAvailable);
207             change = true;
208         }
209         if (change) {
210             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
211                 nodeId);
212             DeviceTransaction deviceTx;
213             try {
214                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
215                 if (deviceTxOpt.isPresent()) {
216                     deviceTx = deviceTxOpt.get();
217                 } else {
218                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
219                         nodeId));
220                 }
221             } catch (InterruptedException | ExecutionException e) {
222                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
223                     nodeId), e);
224             }
225             deviceTx.put(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
226             ListenableFuture<Void> txSubmitFuture = deviceTx.submit(Timeouts.DEVICE_WRITE_TIMEOUT,
227                 Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
228             try {
229                 txSubmitFuture.get();
230                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
231             } catch (InterruptedException | ExecutionException e) {
232                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
233                     nodeId), e);
234             }
235         }
236     }
237
238 }