2 * Copyright (c) 2014 Cisco Systems, Inc. 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
8 package org.opendaylight.controller.md.compatibility.switchmanager;
10 import java.net.InetAddress;
11 import java.net.NetworkInterface;
12 import java.net.SocketException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Enumeration;
16 import java.util.HashSet;
17 import java.util.List;
21 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
22 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
23 import org.opendaylight.controller.sal.compatibility.NodeMapping;
24 import org.opendaylight.controller.sal.core.Bandwidth;
25 import org.opendaylight.controller.sal.core.ConstructionException;
26 import org.opendaylight.controller.sal.core.Description;
27 import org.opendaylight.controller.sal.core.ForwardingMode;
28 import org.opendaylight.controller.sal.core.MacAddress;
29 import org.opendaylight.controller.sal.core.NodeConnector;
30 import org.opendaylight.controller.sal.core.Property;
31 import org.opendaylight.controller.sal.core.Tier;
32 import org.opendaylight.controller.sal.utils.Status;
33 import org.opendaylight.controller.switchmanager.ISwitchManager;
34 import org.opendaylight.controller.switchmanager.Subnet;
35 import org.opendaylight.controller.switchmanager.Switch;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class CompatibleSwitchManager extends ConfigurableSwitchManager implements ISwitchManager {
48 private static final Logger LOG = LoggerFactory.getLogger(CompatibleSwitchManager.class);
50 private DataBrokerService _dataService;
52 public DataBrokerService getDataService() {
53 return this._dataService;
56 public void setDataService(final DataBrokerService dataService) {
57 this._dataService = dataService;
61 public Status addNodeConnectorProp(final NodeConnector nodeConnector, final Property prop) {
62 final DataModificationTransaction it = getDataService().beginTransaction();
63 final NodeConnectorRef path = NodeMapping.toNodeConnectorRef(nodeConnector);
68 public Property createProperty(final String propName, final String propValue) {
70 if (propName.equalsIgnoreCase(Description.propertyName)) {
71 return new Description(propValue);
72 } else if (propName.equalsIgnoreCase(Tier.TierPropName)) {
73 return new Tier(Integer.parseInt(propValue));
74 } else if (propName.equalsIgnoreCase(Bandwidth.BandwidthPropName)) {
75 return new Bandwidth(Long.parseLong(propValue));
76 } else if (propName.equalsIgnoreCase(ForwardingMode.name)) {
77 return new ForwardingMode(Integer.parseInt(propValue));
78 } else if (propName.equalsIgnoreCase(MacAddress.name)) {
79 return new MacAddress(propValue);
81 LOG.debug("Not able to create {} property", propName);
83 } catch (Exception e) {
84 LOG.debug("createProperty caught exception {}", e.getMessage());
91 public boolean doesNodeConnectorExist(final NodeConnector nc) {
92 return (getDataService().readOperationalData(NodeMapping.toNodeConnectorRef(nc).getValue()) != null);
96 public byte[] getControllerMAC() {
97 final Enumeration<NetworkInterface> nis;
99 nis = NetworkInterface.getNetworkInterfaces();
100 } catch (SocketException e) {
101 LOG.error("Failed to acquire list of interfaces, cannot determine controller MAC", e);
105 while (nis.hasMoreElements()) {
106 final NetworkInterface ni = nis.nextElement();
108 return ni.getHardwareAddress();
109 } catch (SocketException e) {
110 LOG.error("Failed to acquire controller MAC from interface {}", ni, e);
114 // This happens when running controller on windows VM, for example
115 // Try parsing the OS command output
116 LOG.warn("Failed to acquire controller MAC: No physical interface found");
121 public Map<String,Property> getControllerProperties() {
122 return Collections.<String, Property>emptyMap();
126 public Property getControllerProperty(final String propertyName) {
131 public List<Switch> getNetworkDevices() {
132 final InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).toInstance();
133 final Nodes data = ((Nodes) getDataService().readOperationalData(path));
134 final ArrayList<Switch> ret = new ArrayList<>();
135 for (final Node node : data.getNode()) {
137 ret.add(toSwitch(node));
138 } catch (ConstructionException e) {
139 throw new IllegalStateException(String.format("Failed to create switch {}", node), e);
146 public NodeConnector getNodeConnector(final org.opendaylight.controller.sal.core.Node node, final String nodeConnectorName) {
147 final NodeConnectorKey key = new NodeConnectorKey(new NodeConnectorId(nodeConnectorName));
149 return new NodeConnector(NodeMapping.MD_SAL_TYPE, key, node);
150 } catch (ConstructionException e) {
151 throw new IllegalStateException(String.format("Failed to create node connector for {} {}", node, nodeConnectorName), e);
156 public Property getNodeConnectorProp(final NodeConnector nodeConnector, final String propName) {
157 return getNodeConnectorProps(nodeConnector).get(propName);
161 public Map<String,Property> getNodeConnectorProps(final NodeConnector nodeConnector) {
162 final NodeConnectorRef ref = NodeMapping.toNodeConnectorRef(nodeConnector);
163 return toAdProperties(readNodeConnector(ref.getValue()));
167 public Set<NodeConnector> getNodeConnectors(final org.opendaylight.controller.sal.core.Node node) {
168 final Node data = this.readNode(NodeMapping.toNodeRef(node).getValue());
169 final HashSet<NodeConnector> ret = new HashSet<>();
170 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc : data.getNodeConnector()) {
172 ret.add(new NodeConnector(NodeMapping.MD_SAL_TYPE, nc.getKey(), node));
173 } catch (ConstructionException e) {
174 throw new IllegalStateException(String.format("Failed to create node {} connector", node, nc.getKey()), e);
181 public String getNodeDescription(final org.opendaylight.controller.sal.core.Node node) {
182 return ((Description) getNodeProps(node).get(Description.propertyName)).getValue();
186 public byte[] getNodeMAC(final org.opendaylight.controller.sal.core.Node node) {
187 return ((MacAddress) getNodeProps(node).get(MacAddress.name)).getMacAddress();
191 public Property getNodeProp(final org.opendaylight.controller.sal.core.Node node, final String propName) {
192 return getNodeProps(node).get(propName);
196 public Map<String,Property> getNodeProps(final org.opendaylight.controller.sal.core.Node node) {
197 final NodeRef ref = NodeMapping.toNodeRef(node);
198 return toAdProperties(((Node) getDataService().readOperationalData(ref.getValue())));
202 public Set<org.opendaylight.controller.sal.core.Node> getNodes() {
203 final InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).toInstance();
204 final Nodes data = ((Nodes) getDataService().readOperationalData(path));
205 final HashSet<org.opendaylight.controller.sal.core.Node> ret = new HashSet<>();
206 for (final Node node : data.getNode()) {
208 ret.add(new org.opendaylight.controller.sal.core.Node(NodeMapping.MD_SAL_TYPE, node.getKey()));
209 } catch (ConstructionException e) {
210 throw new IllegalStateException(String.format("Failed to create node for {}", node), e);
216 private static Switch toSwitch(final Node node) throws ConstructionException {
217 return new Switch(new org.opendaylight.controller.sal.core.Node(NodeMapping.MD_SAL_TYPE, node.getKey()));
221 public Set<NodeConnector> getPhysicalNodeConnectors(final org.opendaylight.controller.sal.core.Node node) {
222 final NodeRef ref = NodeMapping.toNodeRef(node);
223 final Node data = readNode(ref.getValue());
224 final HashSet<NodeConnector> ret = new HashSet<>();
225 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc : data.getNodeConnector()) {
226 final FlowCapableNodeConnector flowConnector = nc.getAugmentation(FlowCapableNodeConnector.class);
228 ret.add(new NodeConnector(NodeMapping.MD_SAL_TYPE, nc.getKey(), node));
229 } catch (ConstructionException e) {
230 throw new IllegalStateException(String.format("Failed to create connector for {} on node {}", nc.getKey(), node), e);
236 private static Map<String,Property> toAdProperties(final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector connector) {
237 return Collections.emptyMap();
240 private static Map<String,Property> toAdProperties(final Node connector) {
241 return Collections.emptyMap();
244 private Node readNode(final InstanceIdentifier<? extends Object> ref) {
245 return (Node) getDataService().readOperationalData((ref));
248 private org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector readNodeConnector(final InstanceIdentifier<? extends Object> ref) {
249 return ((org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector) getDataService().readOperationalData(ref));
253 public List<NodeConnector> getSpanPorts(final org.opendaylight.controller.sal.core.Node node) {
254 throw new UnsupportedOperationException("TODO: auto-generated method stub");
258 public Subnet getSubnetByNetworkAddress(final InetAddress networkAddress) {
259 throw new UnsupportedOperationException("TODO: auto-generated method stub");
263 public Set<NodeConnector> getUpNodeConnectors(final org.opendaylight.controller.sal.core.Node node) {
264 final Node data = readNode(NodeMapping.toNodeRef(node).getValue());
265 final HashSet<NodeConnector> ret = new HashSet<>();
266 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc : data.getNodeConnector()) {
267 final FlowCapableNodeConnector flowConn = nc.<FlowCapableNodeConnector>getAugmentation(FlowCapableNodeConnector.class);
268 if (flowConn != null && flowConn.getState() != null && !flowConn.getState().isLinkDown()) {
270 ret.add(new NodeConnector(NodeMapping.MD_SAL_TYPE, nc.getKey(), node));
271 } catch (ConstructionException e) {
272 throw new IllegalStateException(String.format("Failed to create node connector for node {} connector {}", node, nc), e);
280 public Boolean isNodeConnectorEnabled(final NodeConnector nodeConnector) {
281 final NodeConnectorRef ref = NodeMapping.toNodeConnectorRef(nodeConnector);
282 final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector data = readNodeConnector(ref.getValue());
287 public boolean isSpecial(final NodeConnector p) {
288 final NodeConnectorRef ref = NodeMapping.toNodeConnectorRef(p);
289 final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector data = readNodeConnector(ref.getValue());
294 public Status removeControllerProperty(final String propertyName) {
299 public Status removeNodeAllProps(final org.opendaylight.controller.sal.core.Node node) {
304 public Status removeNodeConnectorAllProps(final NodeConnector nodeConnector) {
309 public Status removeNodeConnectorProp(final NodeConnector nc, final String propName) {
314 public Status removeNodeProp(final org.opendaylight.controller.sal.core.Node node, final String propName) {
319 public Status removePortsFromSubnet(final String name, final List<String> nodeConnectors) {
324 public Status removeSubnet(final String name) {
329 public Status setControllerProperty(final Property property) {
334 public void setNodeProp(final org.opendaylight.controller.sal.core.Node node, final Property prop) {
335 throw new UnsupportedOperationException("TODO: auto-generated method stub");
339 public Status addPortsToSubnet(final String name, final List<String> nodeConnectors) {
340 throw new UnsupportedOperationException("TODO: auto-generated method stub");
344 public Set<Switch> getConfiguredNotConnectedSwitches() {