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