Update port in operational datastore.
[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.Set;
15
16 import org.opendaylight.ovsdb.lib.OvsdbClient;
17 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
18 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.base.Joiner;
41 import com.google.common.base.Preconditions;
42 import com.google.common.base.Splitter;
43
44 public class SouthboundMapper {
45     private static final Logger LOG = LoggerFactory.getLogger(SouthboundMapper.class);
46
47     public static Node createNode(OvsdbClient client) {
48         NodeBuilder nodeBuilder = new NodeBuilder();
49         nodeBuilder.setNodeId(createNodeId(client.getConnectionInfo()));
50         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(client));
51         return nodeBuilder.build();
52     }
53     public static Node createNode(OvsdbClientKey key) {
54         NodeBuilder nodeBuilder = new NodeBuilder();
55         nodeBuilder.setNodeId(createNodeId(key.getIp(),key.getPort()));
56         nodeBuilder.addAugmentation(OvsdbNodeAugmentation.class, createOvsdbAugmentation(key));
57         return nodeBuilder.build();
58     }
59
60     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClient client) {
61         return createOvsdbAugmentation(new OvsdbClientKey(client));
62     }
63
64     public static OvsdbNodeAugmentation createOvsdbAugmentation(OvsdbClientKey key) {
65         OvsdbNodeAugmentationBuilder ovsdbNodeBuilder = new OvsdbNodeAugmentationBuilder();
66         ovsdbNodeBuilder.setIp(key.getIp());
67         ovsdbNodeBuilder.setPort(key.getPort());
68         return ovsdbNodeBuilder.build();
69     }
70
71     public static IpAddress createIpAddress(InetAddress address) {
72         IpAddress ip = null;
73         if(address instanceof Inet4Address) {
74             ip = createIpAddress((Inet4Address)address);
75         } else if (address instanceof Inet6Address) {
76             ip = createIpAddress((Inet6Address)address);
77         }
78         return ip;
79     }
80
81     public static IpAddress createIpAddress(Inet4Address address) {
82         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
83         return new IpAddress(ipv4);
84     }
85
86     public static IpAddress createIpAddress(Inet6Address address) {
87         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
88         return new IpAddress(ipv6);
89     }
90
91     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClient client) {
92         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
93                 new PortNumber(client.getConnectionInfo().getRemotePort()));
94     }
95
96     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
97         InstanceIdentifier<Node> nodePath = InstanceIdentifier
98                 .create(NetworkTopology.class)
99                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
100                 .child(Node.class,new NodeKey(nodeId));
101         return nodePath;
102     }
103
104     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key,OvsdbBridgeName bridgeName) {
105         return createInstanceIdentifier(createManagedNodeId(key, bridgeName));
106     }
107
108     public static InstanceIdentifier<Node> createInstanceIdentifier(OvsdbClientKey key) {
109         return createInstanceIdentifier(key.getIp(), key.getPort());
110     }
111
112     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
113         InstanceIdentifier<Node> path = InstanceIdentifier
114                 .create(NetworkTopology.class)
115                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
116                 .child(Node.class,createNodeKey(ip,port));
117         LOG.info("Created ovsdb path: {}",path);
118         return path;
119     }
120
121     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
122         return new NodeKey(createNodeId(ip,port));
123     }
124
125     public static NodeId createNodeId(OvsdbConnectionInfo connectionInfo) {
126         return createNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
127                 new PortNumber(connectionInfo.getRemotePort()));
128     }
129
130     public static NodeId createManagedNodeId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName) {
131         return createManagedNodeId(createIpAddress(connectionInfo.getRemoteAddress()),
132                 new PortNumber(connectionInfo.getRemotePort()),
133                 bridgeName);
134     }
135
136     public static NodeId createManagedNodeId(OvsdbClientKey key, OvsdbBridgeName bridgeName) {
137         return createManagedNodeId(key.getIp(),key.getPort(),bridgeName);
138     }
139
140     public static NodeId createManagedNodeId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName) {
141         return new NodeId(createNodeId(ip,port).getValue()
142                 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+"/"+ bridgeName.getValue());
143     }
144
145     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
146         String uriString = SouthboundConstants.OVSDB_URI_PREFIX + "://" + new String(ip.getValue()) +
147                    ":" + port.getValue();
148         Uri uri = new Uri(uriString);
149         NodeId nodeId = new NodeId(uri);
150         return nodeId;
151     }
152
153     public static TpId createTerminationPointId(OvsdbConnectionInfo connectionInfo, OvsdbBridgeName bridgeName, String tpName) {
154         return createTerminationPointId(createIpAddress(connectionInfo.getRemoteAddress()),
155                 new PortNumber(connectionInfo.getRemotePort()),
156                 bridgeName, tpName);
157     }
158
159     public static TpId createTerminationPointId(OvsdbClientKey key, OvsdbBridgeName bridgeName, String tpName) {
160         return createTerminationPointId(key.getIp(),key.getPort(),bridgeName, tpName);
161     }
162
163     public static TpId createTerminationPointId(IpAddress ip, PortNumber port, OvsdbBridgeName bridgeName, String tpName) {
164         return new TpId(createNodeId(ip,port).getValue()
165                 + "/"+SouthboundConstants.BRIDGE_URI_PREFIX+"/"+ bridgeName.getValue()
166                 + "/"+SouthboundConstants.TP_URI_PREFIX+"/"+ tpName);
167     }
168
169     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
170         if(ip.getIpv4Address() != null) {
171             return InetAddress.getByName(ip.getIpv4Address().getValue());
172         } else if(ip.getIpv6Address() != null) {
173             return InetAddress.getByName(ip.getIpv6Address().getValue());
174         } else {
175             throw new UnknownHostException("IP Address has no value");
176         }
177     }
178
179     public static DatapathId createDatapathId(Bridge bridge) {
180         Preconditions.checkNotNull(bridge);
181         if(bridge.getDatapathIdColumn() == null) {
182             return null;
183         } else {
184             return createDatapathId(bridge.getDatapathIdColumn().getData());
185         }
186     }
187
188     public static DatapathId createDatapathId(Set<String> dpids) {
189         Preconditions.checkNotNull(dpids);
190         if(dpids.isEmpty()) {
191             return null;
192         } else {
193             String[] dpidArray = new String[dpids.size()];
194             dpids.toArray(dpidArray);
195             return createDatapathId(dpidArray[0]);
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 }