56decc1cc31201e320cdefd15d49c5a75180b25b
[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.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.rev170206.circuit.packs.CircuitPacks;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.AdminStates;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev161014.States;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOdu;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev161014.OtnOtu;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.loopback.rev161014.maint.loopback.MaintLoopbackBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.testsignal.rev161014.maint.testsignal.MaintTestsignalBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1Builder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.container.OduBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.otu.container.OtuBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class OpenRoadmInterfacesImpl121 {
44
45     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl121.class);
46
47     private final DeviceTransactionManager deviceTransactionManager;
48
49     public OpenRoadmInterfacesImpl121(DeviceTransactionManager deviceTransactionManager) {
50         this.deviceTransactionManager = deviceTransactionManager;
51     }
52
53     public void postInterface(String nodeId, InterfaceBuilder ifBuilder) throws OpenRoadmInterfaceException {
54         Future<Optional<DeviceTransaction>> deviceTxFuture = deviceTransactionManager.getDeviceTransaction(nodeId);
55         DeviceTransaction deviceTx;
56         try {
57             Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
58             if (deviceTxOpt.isPresent()) {
59                 deviceTx = deviceTxOpt.get();
60             } else {
61                 throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
62                     nodeId));
63             }
64         } catch (InterruptedException | ExecutionException e) {
65             throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
66                 nodeId), e);
67         }
68
69         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
70             Interface.class, new InterfaceKey(ifBuilder.getName()));
71         deviceTx.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
72         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
73             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, 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         Optional<Interface> intf2DeleteOpt;
94         try {
95             intf2DeleteOpt = getInterface(nodeId, interfaceName);
96         } catch (OpenRoadmInterfaceException e) {
97             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
98                 interfaceName, nodeId), e);
99         }
100         if (intf2DeleteOpt.isPresent()) {
101             Interface intf2Delete = intf2DeleteOpt.get();
102             // State admin state to out of service
103             InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
104             if (ifBuilder.getType() == OtnOdu.class) {
105                 Interface1Builder oduBuilder = new Interface1Builder(intf2Delete.augmentation(Interface1.class));
106                 OduBuilder odu = new OduBuilder(oduBuilder.getOdu());
107                 if (odu.getMaintTestsignal() != null) {
108                     MaintTestsignalBuilder maintSignalBuilder = new MaintTestsignalBuilder();
109                     maintSignalBuilder.setEnabled(false);
110                     odu.setMaintTestsignal(maintSignalBuilder.build());
111                 }
112                 oduBuilder.setOdu(odu.build());
113                 ifBuilder.addAugmentation(Interface1.class, oduBuilder.build());
114             } else if (ifBuilder.getType() == OtnOtu.class) {
115                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder
116                     otuBuilder =
117                     new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1Builder(
118                         intf2Delete.augmentation(
119                             org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1
120                                 .class));
121                 OtuBuilder otu = new OtuBuilder(otuBuilder.getOtu());
122                 if (otu.getMaintLoopback() != null) {
123                     MaintLoopbackBuilder maintLoopBackBuilder = new MaintLoopbackBuilder();
124                     maintLoopBackBuilder.setEnabled(false);
125                     otu.setMaintLoopback(maintLoopBackBuilder.build());
126                 }
127                 otuBuilder.setOtu(otu.build());
128                 ifBuilder.addAugmentation(
129                     org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1.class,
130                     otuBuilder.build());
131             }
132             ifBuilder.setAdministrativeState(AdminStates.OutOfService);
133             // post interface with updated admin state
134             try {
135                 postInterface(nodeId, ifBuilder);
136             } catch (OpenRoadmInterfaceException ex) {
137                 throw new OpenRoadmInterfaceException(String.format("Failed to set state of interface %s to %s while"
138                     + " deleting it!", interfaceName, AdminStates.OutOfService), ex);
139             }
140
141             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
142                 Interface.class, new InterfaceKey(interfaceName));
143             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
144                 nodeId);
145             DeviceTransaction deviceTx;
146             try {
147                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
148                 if (deviceTxOpt.isPresent()) {
149                     deviceTx = deviceTxOpt.get();
150                 } else {
151                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
152                         nodeId));
153                 }
154             } catch (InterruptedException | ExecutionException e) {
155                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
156                     nodeId), e);
157             }
158
159             deviceTx.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
160             FluentFuture<? extends @NonNull CommitInfo> commit =
161                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
162
163             try {
164                 commit.get();
165                 LOG.info("Successfully deleted {} on node {}", interfaceName, nodeId);
166             } catch (InterruptedException | ExecutionException e) {
167                 throw new OpenRoadmInterfaceException(String.format("Failed to delete interface %s on " + "node %s",
168                     interfaceName, nodeId), e);
169             }
170             // change the equipment state on circuit pack if xpdr node
171             if (intf2Delete.getName().contains(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
172                 StringConstants.NETWORK_TOKEN)) {
173                 postEquipmentState(nodeId, intf2Delete.getSupportingCircuitPackName(), false);
174             }
175
176         } else {
177             LOG.info("Interface does not exist, cannot delete on node {}", nodeId);
178         }
179     }
180
181     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
182         throws OpenRoadmInterfaceException {
183
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().equals(States.NotReservedInuse)) {
200                 cpBldr.setEquipmentState(States.NotReservedInuse);
201                 change = true;
202             }
203         } else if (
204             (cpBldr.getEquipmentState() != null && !cpBldr.getEquipmentState().equals(States.NotReservedAvailable))) {
205             cpBldr.setEquipmentState(States.NotReservedAvailable);
206             change = true;
207         }
208         if (change) {
209             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
210                 nodeId);
211             DeviceTransaction deviceTx;
212             try {
213                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
214                 if (deviceTxOpt.isPresent()) {
215                     deviceTx = deviceTxOpt.get();
216                 } else {
217                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
218                         nodeId));
219                 }
220             } catch (InterruptedException | ExecutionException e) {
221                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
222                     nodeId), e);
223             }
224             deviceTx.put(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
225             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
226                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
227             try {
228                 txSubmitFuture.get();
229                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
230             } catch (InterruptedException | ExecutionException e) {
231                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
232                     nodeId), e);
233             }
234         }
235     }
236
237 }