150b8fd6317679ecf796358d644c77a71b417767
[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.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.rev181019.circuit.packs.CircuitPacks;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.circuit.packs.CircuitPacksKey;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.AdminStates;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev171215.States;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOdu;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.interfaces.rev170626.OtnOtu;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.loopback.rev171215.maint.loopback.MaintLoopbackBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.maintenance.testsignal.rev171215.maint.testsignal.MaintTestsignalBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.Interface1Builder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev181019.odu.container.OduBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.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 OpenRoadmInterfacesImpl221 {
44
45     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfacesImpl221.class);
46
47     private final DeviceTransactionManager deviceTransactionManager;
48
49     public OpenRoadmInterfacesImpl221(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         LOG.info("POST INTERF for {} : InterfaceBuilder : name = {} \t type = {}", nodeId, ifBuilder.getName(),
72             ifBuilder.getType().toString());
73         deviceTx.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
74         FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
75             deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
76         try {
77             txSubmitFuture.get();
78             LOG.info("Successfully posted interface {} on node {}", ifBuilder.getName(), nodeId);
79         } catch (InterruptedException | ExecutionException e) {
80             throw new OpenRoadmInterfaceException(String.format("Failed to post interface %s on node %s!", ifBuilder
81                 .getName(), nodeId), e);
82         }
83     }
84
85
86     public Optional<Interface> getInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
87         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
88             .child(Interface.class, new InterfaceKey(interfaceName));
89         return deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
90             interfacesIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
91     }
92
93
94     public void deleteInterface(String nodeId, String interfaceName) throws OpenRoadmInterfaceException {
95         LOG.info("deleting interface {} on device221 {}", interfaceName, nodeId);
96         Optional<Interface> intf2DeleteOpt;
97         try {
98             intf2DeleteOpt = getInterface(nodeId, interfaceName);
99         } catch (OpenRoadmInterfaceException e) {
100             throw new OpenRoadmInterfaceException(String.format("Failed to check if interface %s exists on node %s!",
101                 interfaceName, nodeId), e);
102         }
103         if (intf2DeleteOpt.isPresent()) {
104             Interface intf2Delete = intf2DeleteOpt.get();
105             // State admin state to out of service
106             InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
107             if (ifBuilder.getType() == OtnOdu.class) {
108                 Interface1Builder oduBuilder = new Interface1Builder(intf2Delete.augmentation(Interface1.class));
109                 OduBuilder odu = new OduBuilder(oduBuilder.getOdu());
110                 if (odu.getMaintTestsignal() != null) {
111                     MaintTestsignalBuilder maintSignalBuilder = new MaintTestsignalBuilder();
112                     maintSignalBuilder.setEnabled(false);
113                     odu.setMaintTestsignal(maintSignalBuilder.build());
114                 }
115                 oduBuilder.setOdu(odu.build());
116                 ifBuilder.addAugmentation(Interface1.class, oduBuilder.build());
117             } else if (ifBuilder.getType() == OtnOtu.class) {
118                 org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder
119                     otuBuilder =
120                     new org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1Builder(
121                         intf2Delete.augmentation(
122                             org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev181019.Interface1
123                             .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.rev181019.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             FluentFuture<? extends @NonNull CommitInfo> commit =
164                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
165
166             try {
167                 commit.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(StringConstants.CLIENT_TOKEN) || intf2Delete.getName().contains(
175                 StringConstants.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     public void postEquipmentState(String nodeId, String circuitPackName, boolean activate)
185         throws OpenRoadmInterfaceException {
186         InstanceIdentifier<CircuitPacks> circuitPackIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(
187             CircuitPacks.class, new CircuitPacksKey(circuitPackName));
188         Optional<CircuitPacks> cpOpt = this.deviceTransactionManager.getDataFromDevice(nodeId,
189             LogicalDatastoreType.CONFIGURATION, circuitPackIID, Timeouts.DEVICE_READ_TIMEOUT,
190             Timeouts.DEVICE_READ_TIMEOUT_UNIT);
191         CircuitPacks cp = null;
192         if (cpOpt.isPresent()) {
193             cp = cpOpt.get();
194         } else {
195             throw new OpenRoadmInterfaceException(String.format(
196                 "Could not find CircuitPack %s in equipment config datastore for node %s", circuitPackName, nodeId));
197         }
198         CircuitPacksBuilder cpBldr = new CircuitPacksBuilder(cp);
199         boolean change = false;
200         if (activate) {
201             if (cpBldr.getEquipmentState() != null
202                     && !States.NotReservedInuse.equals(cpBldr.getEquipmentState())) {
203                 cpBldr.setEquipmentState(States.NotReservedInuse);
204                 change = true;
205             }
206         } else if ((cpBldr.getEquipmentState() != null
207                 && !States.NotReservedAvailable.equals(cpBldr.getEquipmentState()))) {
208             cpBldr.setEquipmentState(States.NotReservedAvailable);
209             change = true;
210         }
211         if (change) {
212             Future<Optional<DeviceTransaction>> deviceTxFuture = this.deviceTransactionManager.getDeviceTransaction(
213                 nodeId);
214             DeviceTransaction deviceTx;
215             try {
216                 Optional<DeviceTransaction> deviceTxOpt = deviceTxFuture.get();
217                 if (deviceTxOpt.isPresent()) {
218                     deviceTx = deviceTxOpt.get();
219                 } else {
220                     throw new OpenRoadmInterfaceException(String.format("Device transaction was not found for node %s!",
221                         nodeId));
222                 }
223             } catch (InterruptedException | ExecutionException e) {
224                 throw new OpenRoadmInterfaceException(String.format("Failed to obtain device transaction for node %s!",
225                     nodeId), e);
226             }
227             deviceTx.put(LogicalDatastoreType.CONFIGURATION, circuitPackIID, cpBldr.build());
228             FluentFuture<? extends @NonNull CommitInfo> txSubmitFuture =
229                 deviceTx.commit(Timeouts.DEVICE_WRITE_TIMEOUT, Timeouts.DEVICE_WRITE_TIMEOUT_UNIT);
230             try {
231                 txSubmitFuture.get();
232                 LOG.info("Successfully posted equipment state change on node {}", nodeId);
233             } catch (InterruptedException | ExecutionException e) {
234                 throw new OpenRoadmInterfaceException(String.format("Failed to post equipment state on node %s!",
235                     nodeId), e);
236             }
237         }
238     }
239
240 }