Fail build on checkstyle errors
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / SouthboundMapper.java
1 /*
2  * Copyright (c) 2015 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.ovsdb.southbound;
9
10 import java.net.Inet4Address;
11 import java.net.Inet6Address;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.ArrayList;
15 import java.util.HashMap;
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.ovsdb.lib.OvsdbClient;
22 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
27 import org.opendaylight.ovsdb.schema.openvswitch.Controller;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 import com.google.common.base.Joiner;
59 import com.google.common.base.Preconditions;
60 import com.google.common.base.Splitter;
61 import com.google.common.collect.ImmutableBiMap;
62
63 public class SouthboundMapper {
64     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
65
66     public static Node createNode(OvsdbClient client) {
67         NodeBuilder nodeBuilder = new NodeBuilder();
68         nodeBuilder.setNodeId(createNodeId(client.getConnectionInfo()));
69         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(client));
70         return nodeBuilder.build();
71     }
72     public static Node createNode(OvsdbClientKey key) {
73         NodeBuilder nodeBuilder = new NodeBuilder();
74         nodeBuilder.setNodeId(createNodeId(key.getIp(),key.getPort()));
75         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
76         return nodeBuilder.build();
77     }
78
79     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
80         return createOvsdbAugmentation(new OvsdbClientKey(client));
81     }
82
83     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClientKey key) {
84         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
85         ovsdbNodeBuilder.setIp(key.getIp());
86         ovsdbNodeBuilder.setPort(key.getPort());
87         return ovsdbNodeBuilder.build();
88     }
89
90     public static IpAddress createIpAddress(InetAddress address) {
91         IpAddress ip = null;
92         if (address instanceof Inet4Address) {
93             ip = createIpAddress((Inet4Address)address);
94         } else if (address instanceof Inet6Address) {
95             ip = createIpAddress((Inet6Address)address);
96         }
97         return ip;
98     }
99
100     public static IpAddress createIpAddress(Inet4Address address) {
101         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
102         return new IpAddress(ipv4);
103     }
104
105     public static IpAddress createIpAddress(Inet6Address address) {
106         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
107         return new IpAddress(ipv6);
108     }
109
110     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
111         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
112                 new PortNumber(client.getConnectionInfo().getRemotePort()));
113     }
114
115     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
116         InstanceIdentifier<Node> nodePath = InstanceIdentifier
117                 .create(NetworkTopology.class)
118                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
119                 .child(Node.class,new NodeKey(nodeId));
120         return nodePath;
121     }
122
123     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key,OvsdbBridgeName bridgeName) {
124         return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
125     }
126
127     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key) {
128         return createInstanceIdentifier(key.getIp(), key.getPort());
129     }
130
131     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
132         InstanceIdentifier<Node> path = InstanceIdentifier
133                 .create(NetworkTopology.class)
134                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
135                 .child(Node.class,createNodeKey(ip,port));
136         LOG.info("Created ovsdb path: {}",path);
137         return path;
138     }
139
140     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
141         return new NodeKey(createNodeId(ip,port));
142     }
143
144     public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
145         return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
146                 new PortNumber(connectionInfo.getRemotePort()));
147     }
148
149     public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName) {
150         return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
151                 new PortNumber(connectionInfo.getRemotePort()),
152                 bridgeName);
153     }
154
155     public static NodeId createManagedNodeId(OvsdbClientKey key, OvsdbBridgeName bridgeName) {
156         return createManagedNodeId(key.getIp(),key.getPort(),bridgeName);
157     }
158
159     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
160         return new NodeId(createNodeId(ip,port).getValue()
161                 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue());
162     }
163
164     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
165         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://"
166                 + new String(ip.getValue()) + ":" + port.getValue();
167         Uri uri = new Uri(uriString);
168         NodeId nodeId = new NodeId(uri);
169         return nodeId;
170     }
171
172     public static TpId createTerminationPointId(OvsdbConnectionInfo connectionInfo,
173                                                 OvsdbBridgeName bridgeName, String tpName) {
174         return createTerminationPointId(createIpAddress(connectionInfo.getRemoteAddress()),
175                 new PortNumber(connectionInfo.getRemotePort()),
176                 bridgeName, tpName);
177     }
178
179     public static TpId createTerminationPointId(OvsdbClientKey key, OvsdbBridgeName bridgeName, String tpName) {
180         return createTerminationPointId(key.getIp(),key.getPort(),bridgeName, tpName);
181     }
182
183     public static TpId createTerminationPointId(IpAddress ip, PortNumber port,
184                                                 OvsdbBridgeName bridgeName, String tpName) {
185         return new TpId(createNodeId(ip,port).getValue()
186                 + "/" + SouthboundConstants.BRIDGE_URI_PREFIX + "/" + bridgeName.getValue()
187                 + "/" + SouthboundConstants.TP_URI_PREFIX + "/" + tpName);
188     }
189
190     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
191         if (ip.getIpv4Address() != null) {
192             return InetAddress.getByName(ip.getIpv4Address().getValue());
193         } else if (ip.getIpv6Address() != null) {
194             return InetAddress.getByName(ip.getIpv6Address().getValue());
195         } else {
196             throw new UnknownHostException("IP Address has no value");
197         }
198     }
199
200     public static DatapathId createDatapathId(Bridge bridge) {
201         Preconditions.checkNotNull(bridge);
202         if (bridge.getDatapathIdColumn() == null) {
203             return null;
204         } else {
205             return createDatapathId(bridge.getDatapathIdColumn().getData());
206         }
207     }
208
209     public static DatapathId createDatapathId(Set<String> dpids) {
210         Preconditions.checkNotNull(dpids);
211         if (dpids.isEmpty()) {
212             return null;
213         } else {
214             String[] dpidArray = new String[dpids.size()];
215             dpids.toArray(dpidArray);
216             return createDatapathId(dpidArray[0]);
217         }
218     }
219
220     public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
221         String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
222
223         if (mdsalbridge.getDatapathType() != null) {
224             if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
225                 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
226             } else {
227                 throw new IllegalArgumentException("Unknown datapath type "
228                         + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
229             }
230         }
231         return datapathtype;
232     }
233
234     public static  Class<? extends DatapathTypeBase> createDatapathType(String type) {
235         Preconditions.checkNotNull(type);
236         if (type.isEmpty()) {
237             return DatapathTypeSystem.class;
238         } else {
239             ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
240                     SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
241             return mapper.get(type);
242         }
243     }
244
245     public static DatapathId createDatapathId(String dpid) {
246         Preconditions.checkNotNull(dpid);
247         DatapathId datapath;
248         if (dpid.matches("^[0-9a-fA-F]{16}")) {
249             Splitter splitter = Splitter.fixedLength(2);
250             Joiner joiner = Joiner.on(":");
251             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
252         } else {
253             datapath = new DatapathId(dpid);
254         }
255         return datapath;
256     }
257
258     public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
259         Set<String> protocols = new HashSet<String>();
260         if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
261             for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
262                 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
263                     protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
264                 } else {
265                     throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
266                 }
267             }
268         }
269         return protocols;
270     }
271
272     public static  Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
273         Preconditions.checkNotNull(type);
274         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
275     }
276
277     public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
278         Preconditions.checkNotNull(mdsaltype);
279         ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
280                 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
281         return mapper.get(mdsaltype);
282     }
283
284     public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
285         Set<String> protocols = bridge.getProtocolsColumn().getData();
286         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
287         if (protocols != null && protocols.size() > 0) {
288             ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
289                     SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
290             for (String protocol : protocols) {
291                 if (protocol != null && mapper.get(protocol) != null) {
292                     protocolList.add(new ProtocolEntryBuilder().
293                             setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get(protocol)).build());
294                 }
295             }
296         }
297         return protocolList;
298     }
299
300     public static List<ControllerEntry> createControllerEntries(Bridge bridge,Map<UUID,
301             Controller> updatedControllerRows) {
302         LOG.debug("Bridge: {}, updatedControllerRows: {}",bridge,updatedControllerRows);
303         Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
304         List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
305         for (UUID controllerUUID : controllerUUIDs ) {
306             Controller controller = updatedControllerRows.get(controllerUUID);
307             if (controller != null && controller.getTargetColumn() != null
308                     && controller.getTargetColumn() != null) {
309                 String targetString = controller.getTargetColumn().getData();
310                 controllerEntries.add(new ControllerEntryBuilder().setTarget(new Uri(targetString)).build());
311             }
312         }
313         return controllerEntries;
314     }
315
316     public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
317         List<ControllerEntry> controllerEntries = omn.getControllerEntry();
318         Map<UUID,Controller> controllerMap = new HashMap<UUID,Controller>();
319         if (controllerEntries != null && !controllerEntries.isEmpty()) {
320             int index = 0;
321             for (ControllerEntry controllerEntry : controllerEntries) {
322                 String controllerNamedUUID = "Controller_" + omn.getBridgeName().getValue() + index++;
323                 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
324                 controller.setTarget(controllerEntry.getTarget().getValue());
325                 controllerMap.put(new UUID(controllerNamedUUID), controller);
326             }
327         }
328         return controllerMap;
329     }
330 }