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 && intf2PostCheck.getAdministrativeState() == AdminStates.InService) {
116 LOG.info("Interface with same name in service already exists on node " + nodeId);
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();
129 LOG.info("Successfully posted interface " + ifBuilder.getName() + " on node " + nodeId);
131 } catch (TransactionCommitFailedException ex) {
132 LOG.warn("Failed to post {} ", ifBuilder.getName() + " on node " + nodeId, ex);
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
146 * @param interfaceName
147 * Name of the interface
149 * @return true/false based on status of operation
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));
157 Optional<Interface> interfaceObject = rtx.read(LogicalDatastoreType.CONFIGURATION, interfacesIID).get();
158 if (interfaceObject.isPresent()) {
159 return interfaceObject.get();
161 LOG.info("Interface subtree is not present for " + interfaceName + " on node " + nodeId);
163 } catch (InterruptedException | ExecutionException ex) {
164 LOG.info("Read failed on interface subtree for" + interfaceName + " on node " + nodeId, ex);
171 * This methods does an edit-config operation on the openROADM device in order
172 * to delete the given interface.
175 * Before deleting the method:
178 * 1. Checks if interface exists
181 * 2. If exists then changes the state of interface to outOfService
183 * @param interfaceName
184 * Name of the interface to delete.
186 * @return Result of operation true/false based on success/failure.
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();
206 LOG.info("Successfully deleted " + interfaceName + " on node " + nodeId);
209 } catch (TransactionCommitFailedException ex) {
210 LOG.error("Failed to delete interface " + interfaceName + " on node " + nodeId);
216 LOG.error("Error changing the state of interface " + interfaceName + " on node " + nodeId);
220 LOG.info("Interface does not exist, cannot delete on node " + nodeId);
226 LOG.info("Device databroker not found on node " + nodeId);