Changed the constant name logger to LOGGER to comply with the naming convention
[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.OvsdbBridgeAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeProtocolBase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
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.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
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     public static Node createNode(OvsdbClient client) {
64         NodeBuilder nodeBuilder = new NodeBuilder();
65         nodeBuilder.setNodeId(createNodeId(client.getConnectionInfo()));
66         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(client));
67         return nodeBuilder.build();
68     }
69     public static Node createNode(OvsdbClientKey key) {
70         NodeBuilder nodeBuilder = new NodeBuilder();
71         nodeBuilder.setNodeId(createNodeId(key.getIp(),key.getPort()));
72         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
73         return nodeBuilder.build();
74     }
75
76     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
77         return createOvsdbAugmentation(new OvsdbClientKey(client));
78     }
79
80     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClientKey key) {
81         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
82         ovsdbNodeBuilder.setIp(key.getIp());
83         ovsdbNodeBuilder.setPort(key.getPort());
84         return ovsdbNodeBuilder.build();
85     }
86
87     public static IpAddress createIpAddress(InetAddress address) {
88         IpAddress ip = null;
89         if(address instanceof Inet4Address) {
90             ip = createIpAddress((Inet4Address)address);
91         } else if (address instanceof Inet6Address) {
92             ip = createIpAddress((Inet6Address)address);
93         }
94         return ip;
95     }
96
97     public static IpAddress createIpAddress(Inet4Address address) {
98         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
99         return new IpAddress(ipv4);
100     }
101
102     public static IpAddress createIpAddress(Inet6Address address) {
103         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
104         return new IpAddress(ipv6);
105     }
106
107     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
108         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
109                 new PortNumber(client.getConnectionInfo().getRemotePort()));
110     }
111
112     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
113         InstanceIdentifier<Node> nodePath = InstanceIdentifier
114                 .create(NetworkTopology.class)
115                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
116                 .child(Node.class,new NodeKey(nodeId));
117         return nodePath;
118     }
119
120     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key,OvsdbBridgeName bridgeName) {
121         return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
122     }
123
124     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key) {
125         return createInstanceIdentifier(key.getIp(), key.getPort());
126     }
127
128     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
129         InstanceIdentifier<Node> path = InstanceIdentifier
130                 .create(NetworkTopology.class)
131                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
132                 .child(Node.class,createNodeKey(ip,port));
133         LOG.info("Created ovsdb path: {}",path);
134         return path;
135     }
136
137     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
138         return new NodeKey(createNodeId(ip,port));
139     }
140
141     public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
142         return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
143                 new PortNumber(connectionInfo.getRemotePort()));
144     }
145
146     public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName) {
147         return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
148                 new PortNumber(connectionInfo.getRemotePort()),
149                 bridgeName);
150     }
151
152     public static NodeId createManagedNodeId(OvsdbClientKey key, OvsdbBridgeName bridgeName) {
153         return createManagedNodeId(key.getIp(),key.getPort(),bridgeName);
154     }
155
156     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
157         return new NodeId(createNodeId(ip,port).getValue()
158                 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+"/"+ bridgeName.getValue());
159     }
160
161     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
162         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + new String(ip.getValue()) +
163                    ":" + port.getValue();
164         Uri uri = new Uri(uriString);
165         NodeId nodeId = new NodeId(uri);
166         return nodeId;
167     }
168
169     public static TpId createTerminationPointId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName, String tpName) {
170         return createTerminationPointId(createIpAddress(connectionInfo.getRemoteAddress()),
171                 new PortNumber(connectionInfo.getRemotePort()),
172                 bridgeName, tpName);
173     }
174
175     public static TpId createTerminationPointId(OvsdbClientKey key, OvsdbBridgeName bridgeName, String tpName) {
176         return createTerminationPointId(key.getIp(),key.getPort(),bridgeName, tpName);
177     }
178
179     public static TpId createTerminationPointId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName, String tpName) {
180         return new TpId(createNodeId(ip,port).getValue()
181                 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+"/"+ bridgeName.getValue()
182                 + "/"+SouthboundConstants.TP_URI_PREFIX+"/"+ tpName);
183     }
184
185     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
186         if(ip.getIpv4Address() != null) {
187             return InetAddress.getByName(ip.getIpv4Address().getValue());
188         } else if(ip.getIpv6Address() != null) {
189             return InetAddress.getByName(ip.getIpv6Address().getValue());
190         } else {
191             throw new UnknownHostException("IP Address has no value");
192         }
193     }
194
195     public static DatapathId createDatapathId(Bridge bridge) {
196         Preconditions.checkNotNull(bridge);
197         if(bridge.getDatapathIdColumn() == null) {
198             return null;
199         } else {
200             return createDatapathId(bridge.getDatapathIdColumn().getData());
201         }
202     }
203
204     public static DatapathId createDatapathId(Set<String> dpids) {
205         Preconditions.checkNotNull(dpids);
206         if(dpids.isEmpty()) {
207             return null;
208         } else {
209             String[] dpidArray = new String[dpids.size()];
210             dpids.toArray(dpidArray);
211             return createDatapathId(dpidArray[0]);
212         }
213     }
214
215     public static DatapathId createDatapathId(String dpid) {
216         Preconditions.checkNotNull(dpid);
217         DatapathId datapath;
218         if(dpid.matches("^[0-9a-fA-F]{16}")) {
219             Splitter splitter = Splitter.fixedLength(2);
220             Joiner joiner = Joiner.on(":");
221             datapath = new DatapathId(joiner.join(splitter.split(dpid)));
222         } else {
223             datapath = new DatapathId(dpid);
224         }
225         return datapath;
226     }
227
228     public static Set<String> createOvsdbBridgeProtocols(OvsdbBridgeAugmentation ovsdbBridgeNode) {
229         Set<String> protocols = new HashSet<String>();
230         if(ovsdbBridgeNode.getProtocolEntry() != null && ovsdbBridgeNode.getProtocolEntry().size() > 0) {
231             for(ProtocolEntry protocol : ovsdbBridgeNode.getProtocolEntry()) {
232                 if(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()) != null) {
233                     protocols.add(SouthboundConstants.OVSDB_PROTOCOL_MAP.get(protocol.getProtocol()));
234                 } else {
235                     throw new IllegalArgumentException("Unknown protocol " + protocol.getProtocol());
236                 }
237             }
238         }
239         return protocols;
240     }
241
242     public static List<ProtocolEntry> createMdsalProtocols(Bridge bridge) {
243         Set<String> protocols = bridge.getProtocolsColumn().getData();
244         List<ProtocolEntry> protocolList = new ArrayList<ProtocolEntry>();
245         if(protocols != null && protocols.size() >0) {
246             ImmutableBiMap<String, Class<? extends OvsdbBridgeProtocolBase>> mapper = SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse();
247             for(String protocol : protocols) {
248                 if(protocol != null && mapper.get(protocol) != null) {
249                     protocolList.add(new ProtocolEntryBuilder().setProtocol((Class<? extends OvsdbBridgeProtocolBase>) mapper.get(protocol)).build());
250                 }
251             }
252         }
253         return protocolList;
254     }
255
256     public static List<ControllerEntry> createControllerEntries(Bridge bridge,Map<UUID,Controller> updatedControllerRows) {
257         LOG.info("Bridge: {}, updatedControllerRows: {}",bridge,updatedControllerRows);
258         Set<UUID> controllerUUIDs = bridge.getControllerColumn().getData();
259         List<ControllerEntry> controllerEntries = new ArrayList<ControllerEntry>();
260         for(UUID controllerUUID : controllerUUIDs ) {
261             Controller controller = updatedControllerRows.get(controllerUUID);
262             if(controller != null && controller.getTargetColumn() != null
263                     && controller.getTargetColumn() != null) {
264                 String targetString = controller.getTargetColumn().getData();
265                 controllerEntries.add(new ControllerEntryBuilder().setTarget(new Uri(targetString)).build());
266             }
267         }
268         return controllerEntries;
269     }
270
271     public static Map<UUID, Controller> createOvsdbController(OvsdbBridgeAugmentation omn,DatabaseSchema dbSchema) {
272         List<ControllerEntry> controllerEntries = omn.getControllerEntry();
273         Map<UUID,Controller> controllerMap = new HashMap<UUID,Controller>();
274         if(controllerEntries != null && !controllerEntries.isEmpty()) {
275             int index = 0;
276             for(ControllerEntry controllerEntry : controllerEntries) {
277                 String controllerNamedUUID = "Controller_" + omn.getBridgeName().getValue() + index++;
278                 Controller controller = TyperUtils.getTypedRowWrapper(dbSchema, Controller.class);
279                 controller.setTarget(controllerEntry.getTarget().getValue());
280                 controllerMap.put(new UUID(controllerNamedUUID), controller);
281             }
282         }
283         return controllerMap;
284     }
285 }