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