7039df857322b8208a17f35c8ef60f4f97433ace
[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) {
116             if (intf2PostCheck.getAdministrativeState() == AdminStates.InService) {
117                 LOG.info("Interface with same name in service already exists on node " + nodeId);
118                 return true;
119             }
120         }
121         // Post interface with its specific augmentation to the device
122         if (netconfNodeDataBroker != null) {
123             InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
124                     .child(Interface.class, new InterfaceKey(ifBuilder.getName()));
125             final WriteTransaction writeTransaction = netconfNodeDataBroker.newWriteOnlyTransaction();
126             writeTransaction.put(LogicalDatastoreType.CONFIGURATION, interfacesIID, ifBuilder.build());
127             final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
128             try {
129                 submit.checkedGet();
130                 LOG.info("Successfully posted interface " + ifBuilder.getName() + " on node " + nodeId);
131                 return true;
132             } catch (TransactionCommitFailedException ex) {
133                 LOG.warn("Failed to post {} ", ifBuilder.getName() + " on node " + nodeId, ex);
134                 return false;
135             }
136
137         } else {
138             return false;
139         }
140     }
141
142     /**
143      * This private does a get on the interface subtree of the device with the
144      * interface name as the key and return the class corresponding to the interface
145      * type.
146      *
147      * @param interfaceName
148      *            Name of the interface
149      *
150      * @return true/false based on status of operation
151      */
152
153     public Interface getInterface(String interfaceName) {
154         ReadOnlyTransaction rtx = netconfNodeDataBroker.newReadOnlyTransaction();
155         InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
156                 .child(Interface.class, new InterfaceKey(interfaceName));
157         try {
158             Optional<Interface> interfaceObject = rtx.read(LogicalDatastoreType.CONFIGURATION, interfacesIID).get();
159             if (interfaceObject.isPresent()) {
160                 return interfaceObject.get();
161             } else {
162                 LOG.info("Interface subtree is not present for " + interfaceName + " on node " + nodeId);
163             }
164         } catch (InterruptedException | ExecutionException ex) {
165             LOG.info("Read failed on interface subtree for" + interfaceName + " on node " + nodeId, ex);
166             return null;
167         }
168         return null;
169     }
170
171     /**
172      * This methods does an edit-config operation on the openROADM device in order
173      * to delete the given interface.
174      *
175      * <p>
176      * Before deleting the method:
177      *
178      * <p>
179      * 1. Checks if interface exists
180      *
181      * <p>
182      * 2. If exists then changes the state of interface to outOfService
183      *
184      * @param interfaceName
185      *            Name of the interface to delete.
186      *
187      * @return Result of operation true/false based on success/failure.
188      */
189     public boolean deleteInterface(String interfaceName) {
190         // Post interface with its specific augmentation to the device
191         if (netconfNodeDataBroker != null) {
192             Interface intf2Delete = getInterface(interfaceName);
193             if (intf2Delete != null) {
194                 // State admin state to out of service
195                 InterfaceBuilder ifBuilder = new InterfaceBuilder(intf2Delete);
196                 ifBuilder.setAdministrativeState(AdminStates.OutOfService);
197                 // post interface with updated admin state
198                 if (postInterface(ifBuilder)) {
199                     InstanceIdentifier<Interface> interfacesIID = InstanceIdentifier.create(OrgOpenroadmDevice.class)
200                             .child(Interface.class, new InterfaceKey(interfaceName));
201                     final WriteTransaction writeTransaction = netconfNodeDataBroker.newWriteOnlyTransaction();
202                     writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, interfacesIID);
203                     final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
204
205                     try {
206                         submit.checkedGet();
207                         LOG.info("Successfully deleted " + interfaceName + " on node " + nodeId);
208                         return true;
209
210                     } catch (TransactionCommitFailedException ex) {
211                         LOG.error("Failed to delete interface " + interfaceName + " on node " + nodeId);
212                         return false;
213                     }
214
215                 } else {
216
217                     LOG.error("Error changing the state of interface " + interfaceName + " on node " + nodeId);
218                     return false;
219                 }
220             } else {
221                 LOG.info("Interface does not exist, cannot delete on node " + nodeId);
222                 return false;
223             }
224
225         } else {
226
227             LOG.info("Device databroker not found on node " + nodeId);
228             return false;
229         }
230     }
231 }