Merge "Initial bundle creation for HWTEP SB"
[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.error.SchemaVersionMismatchException;
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.DatapathTypeSystem;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.InterfaceTypeBase;
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.OvsdbBridgeProtocolBase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntryBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfoBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import com.google.common.base.Joiner;
56 import com.google.common.base.Preconditions;
57 import com.google.common.base.Splitter;
58 import com.google.common.collect.ImmutableBiMap;
59
60 public class SouthboundMapper {
61     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
62
63     private static NodeId createNodeId(OvsdbConnectionInstance client) {
64         NodeKey key = client.getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
65         return key.getNodeId();
66
67     }
68
69     public static IpAddress createIpAddress(InetAddress address) {
70         IpAddress ip = null;
71         if (address instanceof Inet4Address) {
72             ip = createIpAddress((Inet4Address)address);
73         } else if (address instanceof Inet6Address) {
74             ip = createIpAddress((Inet6Address)address);
75         }
76         return ip;
77     }
78
79     public static IpAddress createIpAddress(Inet4Address address) {
80         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
81         return new IpAddress(ipv4);
82     }
83
84     public static IpAddress createIpAddress(Inet6Address address) {
85         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
86         return new IpAddress(ipv6);
87     }
88
89     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
90         InstanceIdentifier<Node> nodePath = InstanceIdentifier
91                 .create(NetworkTopology.class)
92                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
93                 .child(Node.class,new NodeKey(nodeId));
94         return nodePath;
95     }
96
97     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbConnectionInstance client,Bridge bridge) {
98         InstanceIdentifier<Node> iid;
99         if (bridge.getExternalIdsColumn() != null
100                 && bridge.getExternalIdsColumn().getData() != null
101                 && bridge.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
102             String iidString = bridge.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
103             iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
104         } else {
105             String nodeString = client.getNodeKey().getNodeId().getValue()
106                     + "/bridge/" + bridge.getName();
107             NodeId nodeId = new NodeId(new Uri(nodeString));
108             NodeKey nodeKey = new NodeKey(nodeId);
109             iid = InstanceIdentifier.builder(NetworkTopology.class)
110                     .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
111                     .child(Node.class,nodeKey)
112                     .build();
113         }
114         return iid;
115     }
116
117     public static InstanceIdentifier<Node> createInstanceIdentifier(
118             OvsdbConnectionInstance client, Controller controller, String bridgeName) {
119         InstanceIdentifier<Node> iid;
120         if (controller.getExternalIdsColumn() != null
121                 && controller.getExternalIdsColumn().getData() != null
122                 && controller.getExternalIdsColumn().getData().containsKey(SouthboundConstants.IID_EXTERNAL_ID_KEY)) {
123             String iidString = controller.getExternalIdsColumn().getData().get(SouthboundConstants.IID_EXTERNAL_ID_KEY);
124             iid = (InstanceIdentifier<Node>) SouthboundUtil.deserializeInstanceIdentifier(iidString);
125         } else {
126             // TODO retrieve bridge name
127             String nodeString = client.getNodeKey().getNodeId().getValue()
128                     + "/bridge/" + bridgeName;
129             NodeId nodeId = new NodeId(new Uri(nodeString));
130             NodeKey nodeKey = new NodeKey(nodeId);
131             iid = InstanceIdentifier.builder(NetworkTopology.class)
132                     .child(Topology.class,new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
133                     .child(Node.class,nodeKey)
134                     .build();
135         }
136         return iid;
137     }
138
139     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
140         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
141         return nodeKey.getNodeId();
142     }
143
144     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
145         if (ip.getIpv4Address() != null) {
146             return InetAddress.getByName(ip.getIpv4Address().getValue());
147         } else if (ip.getIpv6Address() != null) {
148             return InetAddress.getByName(ip.getIpv6Address().getValue());
149         } else {
150             throw new UnknownHostException("IP Address has no value");
151         }
152     }
153
154     public static DatapathId createDatapathId(Bridge bridge) {
155         Preconditions.checkNotNull(bridge);
156         if (bridge.getDatapathIdColumn() == null) {
157             return null;
158         } else {
159             return createDatapathId(bridge.getDatapathIdColumn().getData());
160         }
161     }
162
163     public static DatapathId createDatapathId(Set<String> dpids) {
164         Preconditions.checkNotNull(dpids);
165         if (dpids.isEmpty()) {
166             return null;
167         } else {
168             String[] dpidArray = new String[dpids.size()];
169             dpids.toArray(dpidArray);
170             return createDatapathId(dpidArray[0]);
171         }
172     }
173
174     public static String createDatapathType(OvsdbBridgeAugmentation mdsalbridge) {
175         String datapathtype = new String(SouthboundConstants.DATAPATH_TYPE_MAP.get(DatapathTypeSystem.class));
176
177         if (mdsalbridge.getDatapathType() != null) {
178             if (SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()) != null) {
179                 datapathtype = SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType());
180             } else {
181                 throw new IllegalArgumentException("Unknown datapath type "
182                         + SouthboundConstants.DATAPATH_TYPE_MAP.get(mdsalbridge.getDatapathType()));
183             }
184         }
185         return datapathtype;
186     }
187
188     public static  Class<? extends DatapathTypeBase> createDatapathType(String type) {
189         Preconditions.checkNotNull(type);
190         if (type.isEmpty()) {
191             return DatapathTypeSystem.class;
192         } else {
193             ImmutableBiMap<String, Class<? extends DatapathTypeBase>> mapper =
194                     SouthboundConstants.DATAPATH_TYPE_MAP.inverse();
195             return mapper.get(type);
196         }
197     }
198
199     public static DatapathId createDatapathId(String dpid) {
200         Preconditions.checkNotNull(dpid);
201         DatapathId datapath;
202         if (dpid.matches("^[0-9a-fA-F]{16}")) {
203             Splitter splitter = Splitter.fixedLength(2);
204             Joiner joiner = Joiner.on(":");
205             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
206         } else {
207             datapath = new DatapathId(dpid);
208         }
209         return datapath;
210     }
211
212     public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
213         Set<String> protocols = new HashSet<>();
214         if (ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
215             for (ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
216                 if (SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
217                     protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
218                 } else {
219                     throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
220                 }
221             }
222         }
223         return protocols;
224     }
225
226     public static  Class<? extends InterfaceTypeBase> createInterfaceType(String type) {
227         Preconditions.checkNotNull(type);
228         return SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(type);
229     }
230
231     public static String createOvsdbInterfaceType(Class<? extends InterfaceTypeBase> mdsaltype) {
232         Preconditions.checkNotNull(mdsaltype);
233         ImmutableBiMap<Class<? extends InterfaceTypeBase>, String> mapper =
234                 SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.inverse();
235         return mapper.get(mdsaltype);
236     }
237
238     public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
239         Set<String> protocols = null;
240         try {
241             protocols = bridge.getProtocolsColumn().getData();
242         } catch (SchemaVersionMismatchException e) {
243             LOG.warn("protocols not supported by this version of ovsdb", e);
244         }
245         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
246         if (protocols != null && protocols.size() > 0) {
247             ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper =
248                     SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
249             for (String protocol : protocols) {
250                 if (protocol != null && mapper.get(protocol) != null) {
251                     protocolList.add(new ProtocolEntryBuilder().
252                             setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get(protocol)).build());
253                 }
254             }
255         }
256         return protocolList;
257     }
258
259     /**
260      * Create the {@link ControllerEntry} list given an OVSDB {@link Bridge}
261      * and {@link Controller} rows.
262      *
263      * @param bridge the {@link Bridge} to update
264      * @param updatedControllerRows the list of {@link Controller} controllers with updates
265      * @return list of {@link ControllerEntry} entries
266      */
267     public static List<ControllerEntry> createControllerEntries(Bridge bridge,
268                                                                 Map<UUID, Controller> updatedControllerRows) {
269
270         LOG.debug("createControllerEntries Bridge: {}\n, updatedControllerRows: {}",
271                 bridge, updatedControllerRows);
272         final Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
273         final List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
274         for (UUID controllerUUID : controllerUUIDs ) {
275             final Controller controller = updatedControllerRows.get(controllerUUID);
276             addControllerEntries(controllerEntries, controller);
277         }
278         LOG.debug("controllerEntries: {}", controllerEntries);
279         return controllerEntries;
280     }
281
282     /**
283      * Create the {@link ControllerEntry} list given an MDSAL {@link Node} bridge
284      * and {@link Controller} rows.
285      *
286      * @param bridgeNode the {@link Node} to update
287      * @param updatedControllerRows the list of {@link Controller} controllers with updates
288      * @return list of {@link ControllerEntry} entries
289      */
290     public static List<ControllerEntry> createControllerEntries(Node bridgeNode,
291                                                                 Map<UUID, Controller> updatedControllerRows) {
292
293         LOG.debug("createControllerEntries Bridge 2: {}\n, updatedControllerRows: {}",
294                 bridgeNode, updatedControllerRows);
295         final List<ControllerEntry> controllerEntriesCreated = new ArrayList<ControllerEntry>();
296         final OvsdbBridgeAugmentation ovsdbBridgeAugmentation =
297                 bridgeNode.getAugmentation(OvsdbBridgeAugmentation.class);
298         if (ovsdbBridgeAugmentation == null) {
299             return controllerEntriesCreated;
300         }
301
302         final List<ControllerEntry> controllerEntries = ovsdbBridgeAugmentation.getControllerEntry();
303         if (controllerEntries != null) {
304             for (ControllerEntry controllerEntry : controllerEntries) {
305                 final Controller controller = updatedControllerRows.get(
306                         new UUID(controllerEntry.getControllerUuid().getValue()));
307                 addControllerEntries(controllerEntriesCreated, controller);
308             }
309         }
310         LOG.debug("controllerEntries: {}", controllerEntriesCreated);
311         return controllerEntriesCreated;
312     }
313
314     /**
315      * Add the OVSDB {@link Controller} updates to the MDSAL {@link ControllerEntry} list.
316      *
317      * @param controllerEntries the list of {@link ControllerEntry} to update
318      * @param controller the updated OVSDB {@link Controller}
319      */
320     public static void addControllerEntries(List<ControllerEntry> controllerEntries,
321                                             final Controller controller) {
322
323         if (controller != null && controller.getTargetColumn() != null) {
324             final String targetString = controller.getTargetColumn().getData();
325             final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid uuid =
326                     new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
327                             .ietf.yang.types.rev130715.Uuid(controller.getUuid().toString());
328
329             controllerEntries.add(new ControllerEntryBuilder()
330                     .setTarget(new Uri(targetString))
331                     .setIsConnected(controller.getIsConnectedColumn().getData())
332                     .setControllerUuid(uuid).build());
333         }
334     }
335
336     public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
337         List<ControllerEntry> controllerEntries = omn.getControllerEntry();
338         Map<UUID,Controller> controllerMap = new HashMap<>();
339         if (controllerEntries != null && !controllerEntries.isEmpty()) {
340             for (ControllerEntry controllerEntry : controllerEntries) {
341                 String controllerNamedUUID = "Controller_" + getRandomUUID();
342                 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
343                 controller.setTarget(controllerEntry.getTarget().getValue());
344                 controllerMap.put(new UUID(controllerNamedUUID), controller);
345             }
346         }
347         return controllerMap;
348     }
349
350     public static String getRandomUUID() {
351         return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
352     }
353     public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
354         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
355         connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
356         connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
357         connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
358         connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
359         return connectionInfoBuilder.build();
360     }
361
362     public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
363         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
364         connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
365         connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
366         return connectionInfoBuilder.build();
367     }
368 }