2 * Copyright © 2017 AT&T and others. All rights reserved.
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
9 package org.opendaylight.transportpce.renderer.openroadminterface;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.concurrent.ExecutionException;
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;
33 public class OpenRoadmInterfaces {
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);
44 public OpenRoadmInterfaces(DataBroker db, MountPointService mps, String nodeId, String logicalConnPoint) {
47 this.logicalConnPoint = logicalConnPoint;
49 if (logicalConnPoint != null) {
50 this.portMap = PortMapping.getMapping(nodeId, logicalConnPoint, db);
54 this.serviceName = null;
55 netconfNodeDataBroker = PortMapping.getDeviceDataBroker(nodeId, mps);
58 public OpenRoadmInterfaces(DataBroker db, MountPointService mps, String nodeId, String logicalConnPoint,
62 this.logicalConnPoint = logicalConnPoint;
64 if (logicalConnPoint != null) {
65 this.portMap = PortMapping.getMapping(nodeId, logicalConnPoint, db);
69 this.serviceName = serviceName;
70 netconfNodeDataBroker = PortMapping.getDeviceDataBroker(nodeId, mps);
74 * This methods creates a generic interface builder object to set the value that
75 * are common irrespective of the interface type.
78 * Mapping object containing attributes required to create interface
81 * @return InterfaceBuilder object with the data.
83 public InterfaceBuilder getIntfBuilder(Mapping portMap) {
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);
95 * This methods does an edit-config operation on the openROADM device in order
96 * to create the given interface.
99 * Before posting the interface it checks if:
102 * 1. Interface with same name does not exist
105 * 2. If exists then admin state of interface is outOfState/Maintenance
108 * Builder object containing the data to post.
110 * @return Result of operation true/false based on success/failure.
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);
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();
130 LOG.info("Successfully posted interface " + ifBuilder.getName() + " on node " + nodeId);
132 } catch (TransactionCommitFailedException ex) {
133 LOG.warn("Failed to post {} ", ifBuilder.getName() + " on node " + nodeId, ex);
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
147 * @param interfaceName
148 * Name of the interface
150 * @return true/false based on status of operation
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));
158 Optional<Interface> interfaceObject = rtx.read(LogicalDatastoreType.CONFIGURATION, interfacesIID).get();
159 if (interfaceObject.isPresent()) {
160 return interfaceObject.get();
162 LOG.info("Interface subtree is not present for " + interfaceName + " on node " + nodeId);
164 } catch (InterruptedException | ExecutionException ex) {
165 LOG.info("Read failed on interface subtree for" + interfaceName + " on node " + nodeId, ex);
172 * This methods does an edit-config operation on the openROADM device in order
173 * to delete the given interface.
176 * Before deleting the method:
179 * 1. Checks if interface exists
182 * 2. If exists then changes the state of interface to outOfService
184 * @param interfaceName
185 * Name of the interface to delete.
187 * @return Result of operation true/false based on success/failure.
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();
207 LOG.info("Successfully deleted " + interfaceName + " on node " + nodeId);
210 } catch (TransactionCommitFailedException ex) {
211 LOG.error("Failed to delete interface " + interfaceName + " on node " + nodeId);
217 LOG.error("Error changing the state of interface " + interfaceName + " on node " + nodeId);
221 LOG.info("Interface does not exist, cannot delete on node " + nodeId);
227 LOG.info("Device databroker not found on node " + nodeId);