Update testtool version in tests folder
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / openroadminterface / OpenRoadmInterfaces.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.renderer.openroadminterface;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13
14 import java.util.concurrent.ExecutionException;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.transportpce.renderer.mapping.PortMapping;
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.urn.opendaylight.params.xml.ns.yang.portmapping.rev170228.network.nodes.Mapping;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class OpenRoadmInterfaces {
34
35     protected final DataBroker db;
36     protected final DataBroker netconfNodeDataBroker;
37     protected final String nodeId;
38     protected final MountPointService mps;
39     protected final Mapping portMap;
40     protected final String logicalConnPoint;
41     private final String serviceName;
42     private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfaces.class);
43
44     public OpenRoadmInterfaces(DataBroker db, MountPointService mps, String nodeId, String logicalConnPoint) {
45         this.db = db;
46         this.mps = mps;
47         this.logicalConnPoint = logicalConnPoint;
48         this.nodeId = nodeId;
49         if (logicalConnPoint != null) {
50             this.portMap = PortMapping.getMapping(nodeId, logicalConnPoint, db);
51         } else {
52             this.portMap = null;
53         }
54         this.serviceName = null;
55         netconfNodeDataBroker = PortMapping.getDeviceDataBroker(nodeId, mps);
56     }
57
58     public OpenRoadmInterfaces(DataBroker db, MountPointService mps, String nodeId, String logicalConnPoint,
59             String serviceName) {
60         this.db = db;
61         this.mps = mps;
62         this.logicalConnPoint = logicalConnPoint;
63         this.nodeId = nodeId;
64         if (logicalConnPoint != null) {
65             this.portMap = PortMapping.getMapping(nodeId, logicalConnPoint, db);
66         } else {
67             this.portMap = null;
68         }
69         this.serviceName = serviceName;
70         netconfNodeDataBroker = PortMapping.getDeviceDataBroker(nodeId, mps);
71     }
72
73     /**
74      * This methods creates a generic interface builder object to set the value that
75      * are common irrespective of the interface type.
76      *
77      * @param portMap
78      *            Mapping object containing attributes required to create interface
79      *            on the device.
80      *
81      * @return InterfaceBuilder object with the data.
82      */
83     public InterfaceBuilder getIntfBuilder(Mapping portMap) {
84
85         InterfaceBuilder ifBuilder = new InterfaceBuilder();
86         ifBuilder.setDescription("  TBD   ");
87         ifBuilder.setCircuitId("   TBD    ");
88         ifBuilder.setSupportingCircuitPackName(portMap.getSupportingCircuitPackName());
89         ifBuilder.setSupportingPort(portMap.getSupportingPort());
90         ifBuilder.setAdministrativeState(AdminStates.InService);
91         return ifBuilder;
92     }
93
94     /**
95      * This methods does an edit-config operation on the openROADM device in order
96      * to create the given interface.
97      *
98      * <p>
99      * Before posting the interface it checks if:
100      *
101      * <p>
102      * 1. Interface with same name does not exist
103      *
104      * <p>
105      * 2. If exists then admin state of interface is outOfState/Maintenance
106      *
107      * @param ifBuilder
108      *            Builder object containing the data to post.
109      *
110      * @return Result of operation true/false based on success/failure.
111      */
112     public boolean postInterface(InterfaceBuilder ifBuilder) {
113         String intf2Post = ifBuilder.getName();
114         Interface intf2PostCheck = getInterface(intf2Post);
115         if (intf2PostCheck != null && intf2PostCheck.getAdministrativeState() == AdminStates.InService) {
116             LOG.info("Interface with same name in service already exists on node " + nodeId);
117             return true;
118         }
119
120         // Post interface with its specific augmentation to the device
121         if (netconfNodeDataBroker != null) {
122             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
123                     .child(Interface.class, new InterfaceKey(ifBuilder.getName()));
124             final WriteTransaction writeTransaction = netconfNodeDataBroker.newWriteOnlyTransaction();
125             writeTransaction.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
126             final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
127             try {
128                 submit.checkedGet();
129                 LOG.info("Successfully posted interface " + ifBuilder.getName() + " on node " + nodeId);
130                 return true;
131             } catch (TransactionCommitFailedException ex) {
132                 LOG.warn("Failed to post {} ", ifBuilder.getName() + " on node " + nodeId, ex);
133                 return false;
134             }
135
136         } else {
137             return false;
138         }
139     }
140
141     /**
142      * This private does a get on the interface subtree of the device with the
143      * interface name as the key and return the class corresponding to the interface
144      * type.
145      *
146      * @param interfaceName
147      *            Name of the interface
148      *
149      * @return true/false based on status of operation
150      */
151
152     public Interface getInterface(String interfaceName) {
153         ReadOnlyTransaction rtx = netconfNodeDataBroker.newReadOnlyTransaction();
154         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
155                 .child(Interface.class, new InterfaceKey(interfaceName));
156         try {
157             Optional<Interface> interfaceObject = rtx.read(LogicalDatastoreType.CONFIGURATION, interfacesIID).get();
158             if (interfaceObject.isPresent()) {
159                 return interfaceObject.get();
160             } else {
161                 LOG.info("Interface subtree is not present for " + interfaceName + " on node " + nodeId);
162             }
163         } catch (InterruptedException | ExecutionException ex) {
164             LOG.info("Read failed on interface subtree for" + interfaceName + " on node " + nodeId, ex);
165             return null;
166         }
167         return null;
168     }
169
170     /**
171      * This methods does an edit-config operation on the openROADM device in order
172      * to delete the given interface.
173      *
174      * <p>
175      * Before deleting the method:
176      *
177      * <p>
178      * 1. Checks if interface exists
179      *
180      * <p>
181      * 2. If exists then changes the state of interface to outOfService
182      *
183      * @param interfaceName
184      *            Name of the interface to delete.
185      *
186      * @return Result of operation true/false based on success/failure.
187      */
188     public boolean deleteInterface(String interfaceName) {
189         // Post interface with its specific augmentation to the device
190         if (netconfNodeDataBroker != null) {
191             Interface intf2Delete = getInterface(interfaceName);
192             if (intf2Delete != null) {
193                 // State admin state to out of service
194                 InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
195                 ifBuilder.setAdministrativeState(AdminStates.OutOfService);
196                 // post interface with updated admin state
197                 if (postInterface(ifBuilder)) {
198                     InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
199                             .child(Interface.class, new InterfaceKey(interfaceName));
200                     final WriteTransaction writeTransaction = netconfNodeDataBroker.newWriteOnlyTransaction();
201                     writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
202                     final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
203
204                     try {
205                         submit.checkedGet();
206                         LOG.info("Successfully deleted " + interfaceName + " on node " + nodeId);
207                         return true;
208
209                     } catch (TransactionCommitFailedException ex) {
210                         LOG.error("Failed to delete interface " + interfaceName + " on node " + nodeId);
211                         return false;
212                     }
213
214                 } else {
215
216                     LOG.error("Error changing the state of interface " + interfaceName + " on node " + nodeId);
217                     return false;
218                 }
219             } else {
220                 LOG.info("Interface does not exist, cannot delete on node " + nodeId);
221                 return false;
222             }
223
224         } else {
225
226             LOG.info("Device databroker not found on node " + nodeId);
227             return false;
228         }
229     }
230 }