BUG-1089: migrate CompatibleSwitchManager
[controller.git] / opendaylight / md-sal / compatibility / inventory-topology-compatibility / src / main / java / org / opendaylight / controller / md / compatibility / switchmanager / CompatibleSwitchManager.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.controller.md.compatibility.switchmanager;
9
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;
18 import java.util.Map;
19 import java.util.Set;
20
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;
46
47 public class CompatibleSwitchManager extends ConfigurableSwitchManager implements ISwitchManager {
48     private static final  Logger LOG = LoggerFactory.getLogger(CompatibleSwitchManager.class);
49
50     private DataBrokerService _dataService;
51
52     public DataBrokerService getDataService() {
53         return this._dataService;
54     }
55
56     public void setDataService(final DataBrokerService dataService) {
57         this._dataService = dataService;
58     }
59
60     @Override
61     public Status addNodeConnectorProp(final NodeConnector nodeConnector, final Property prop) {
62         final DataModificationTransaction it = getDataService().beginTransaction();
63         final NodeConnectorRef path = NodeMapping.toNodeConnectorRef(nodeConnector);
64         return null;
65     }
66
67     @Override
68     public Property createProperty(final String propName, final String propValue) {
69         try {
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);
80             } else {
81                 LOG.debug("Not able to create {} property", propName);
82             }
83         } catch (Exception e) {
84             LOG.debug("createProperty caught exception {}", e.getMessage());
85         }
86
87         return null;
88     }
89
90     @Override
91     public boolean doesNodeConnectorExist(final NodeConnector nc) {
92         return (getDataService().readOperationalData(NodeMapping.toNodeConnectorRef(nc).getValue()) != null);
93     }
94
95     @Override
96     public byte[] getControllerMAC() {
97         final Enumeration<NetworkInterface> nis;
98         try {
99             nis = NetworkInterface.getNetworkInterfaces();
100         } catch (SocketException e) {
101             LOG.error("Failed to acquire list of interfaces, cannot determine controller MAC", e);
102             return null;
103         }
104
105         while (nis.hasMoreElements()) {
106             final NetworkInterface ni = nis.nextElement();
107             try {
108                 return ni.getHardwareAddress();
109             } catch (SocketException e) {
110                 LOG.error("Failed to acquire controller MAC from interface {}", ni, e);
111             }
112         }
113
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");
117         return null;
118     }
119
120     @Override
121     public Map<String,Property> getControllerProperties() {
122         return Collections.<String, Property>emptyMap();
123     }
124
125     @Override
126     public Property getControllerProperty(final String propertyName) {
127         return null;
128     }
129
130     @Override
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()) {
136             try {
137                 ret.add(toSwitch(node));
138             } catch (ConstructionException e) {
139                 throw new IllegalStateException(String.format("Failed to create switch {}", node), e);
140             }
141         }
142         return ret;
143     }
144
145     @Override
146     public NodeConnector getNodeConnector(final org.opendaylight.controller.sal.core.Node node, final String nodeConnectorName) {
147         final NodeConnectorKey key = new NodeConnectorKey(new NodeConnectorId(nodeConnectorName));
148         try {
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);
152         }
153     }
154
155     @Override
156     public Property getNodeConnectorProp(final NodeConnector nodeConnector, final String propName) {
157         return getNodeConnectorProps(nodeConnector).get(propName);
158     }
159
160     @Override
161     public Map<String,Property> getNodeConnectorProps(final NodeConnector nodeConnector) {
162         final NodeConnectorRef ref = NodeMapping.toNodeConnectorRef(nodeConnector);
163         return toAdProperties(readNodeConnector(ref.getValue()));
164     }
165
166     @Override
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()) {
171             try {
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);
175             }
176         }
177         return ret;
178     }
179
180     @Override
181     public String getNodeDescription(final org.opendaylight.controller.sal.core.Node node) {
182         return ((Description) getNodeProps(node).get(Description.propertyName)).getValue();
183     }
184
185     @Override
186     public byte[] getNodeMAC(final org.opendaylight.controller.sal.core.Node node) {
187         return ((MacAddress) getNodeProps(node).get(MacAddress.name)).getMacAddress();
188     }
189
190     @Override
191     public Property getNodeProp(final org.opendaylight.controller.sal.core.Node node, final String propName) {
192         return getNodeProps(node).get(propName);
193     }
194
195     @Override
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())));
199     }
200
201     @Override
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()) {
207             try {
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);
211             }
212         }
213         return ret;
214     }
215
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()));
218     }
219
220     @Override
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);
227             try {
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);
231             }
232         }
233         return ret;
234     }
235
236     private static Map<String,Property> toAdProperties(final org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector connector) {
237         return Collections.emptyMap();
238     }
239
240     private static Map<String,Property> toAdProperties(final Node connector) {
241         return Collections.emptyMap();
242     }
243
244     private Node readNode(final InstanceIdentifier<? extends Object> ref) {
245         return (Node) getDataService().readOperationalData((ref));
246     }
247
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));
250     }
251
252     @Override
253     public List<NodeConnector> getSpanPorts(final org.opendaylight.controller.sal.core.Node node) {
254         throw new UnsupportedOperationException("TODO: auto-generated method stub");
255     }
256
257     @Override
258     public Subnet getSubnetByNetworkAddress(final InetAddress networkAddress) {
259         throw new UnsupportedOperationException("TODO: auto-generated method stub");
260     }
261
262     @Override
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()) {
269                 try {
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);
273                 }
274             }
275         }
276         return ret;
277     }
278
279     @Override
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());
283         return true;
284     }
285
286     @Override
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());
290         return true;
291     }
292
293     @Override
294     public Status removeControllerProperty(final String propertyName) {
295         return null;
296     }
297
298     @Override
299     public Status removeNodeAllProps(final org.opendaylight.controller.sal.core.Node node) {
300         return null;
301     }
302
303     @Override
304     public Status removeNodeConnectorAllProps(final NodeConnector nodeConnector) {
305         return null;
306     }
307
308     @Override
309     public Status removeNodeConnectorProp(final NodeConnector nc, final String propName) {
310         return null;
311     }
312
313     @Override
314     public Status removeNodeProp(final org.opendaylight.controller.sal.core.Node node, final String propName) {
315         return null;
316     }
317
318     @Override
319     public Status removePortsFromSubnet(final String name, final List<String> nodeConnectors) {
320         return null;
321     }
322
323     @Override
324     public Status removeSubnet(final String name) {
325         return null;
326     }
327
328     @Override
329     public Status setControllerProperty(final Property property) {
330         return null;
331     }
332
333     @Override
334     public void setNodeProp(final org.opendaylight.controller.sal.core.Node node, final Property prop) {
335         throw new UnsupportedOperationException("TODO: auto-generated method stub");
336     }
337
338     @Override
339     public Status addPortsToSubnet(final String name, final List<String> nodeConnectors) {
340         throw new UnsupportedOperationException("TODO: auto-generated method stub");
341     }
342
343     @Override
344     public Set<Switch> getConfiguredNotConnectedSwitches() {
345         return null;
346     }
347 }