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 Mapping portMap;
39 protected final String logicalConnPoint;
40 private static final Logger LOG = LoggerFactory.getLogger(OpenRoadmInterfaces.class);
42 public OpenRoadmInterfaces(DataBroker db, MountPointService mps, String nodeId, String logicalConnPoint) {
44 this.logicalConnPoint = logicalConnPoint;
46 this.portMap = PortMapping.getMapping(nodeId, logicalConnPoint, db);
47 netconfNodeDataBroker = PortMapping.getDeviceDataBroker(nodeId, mps);
51 * This methods creates a generic interface builder object to set the value
52 * that are common irrespective of the interface type.
55 * Mapping object containing attributes required to create
56 * interface on the device.
58 * @return InterfaceBuilder object with the data.
60 public InterfaceBuilder getIntfBuilder(Mapping portMap) {
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);
72 * This methods does an edit-config operation on the openROADM device in
73 * order to create the given interface.
76 * Before posting the interface it checks if:
79 * 1. Interface with same name does not exist
82 * 2. If exists then admin state of interface is outOfState/Maintenance
85 * Builder object containing the data to post.
87 * @return Result of operation true/false based on success/failure.
89 public boolean postInterface(InterfaceBuilder ifBuilder) {
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");
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();
108 LOG.info("Successfully posted interface " + ifBuilder.getName());
110 } catch (TransactionCommitFailedException ex) {
111 LOG.warn("Failed to post {} ", ifBuilder.getName(),ex);
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
125 * @param interfaceName
126 * Name of the interface
128 * @return true/false based on status of operation
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));
136 Optional<Interface> interfaceObject = rtx.read(LogicalDatastoreType.CONFIGURATION, interfacesIID).get();
137 if (interfaceObject.isPresent()) {
138 return interfaceObject.get();
140 LOG.info("Interface subtree is not present for " + interfaceName);
142 } catch (InterruptedException | ExecutionException ex) {
143 LOG.info("Read failed on interface subtree for",ex);
150 * This methods does an edit-config operation on the openROADM device in
151 * order to delete the given interface.
154 * Before deleting the method:
157 * 1. Checks if interface exists
160 * 2. If exists then changes the state of interface to outOfService
162 * @param interfaceName
163 * Name of the interface to delete.
165 * @return Result of operation true/false based on success/failure.
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();
185 LOG.info("Successfully deleted " + interfaceName);
188 } catch (TransactionCommitFailedException ex) {
189 LOG.error("Failed to delete interface {} ", interfaceName, ex);
195 LOG.error("Error changing the state of interface " + interfaceName);
199 LOG.info("Interface does not exist, cannot delete");
205 LOG.info("Device databroker not found");