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