Upgrade ietf-{inet,yang}-types to 2013-07-15
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundMapper.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
10
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15
16 import org.opendaylight.ovsdb.lib.OvsdbClient;
17 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
18 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
19 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
20 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeBase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 import com.google.common.base.Preconditions;
55 import com.google.common.collect.ImmutableBiMap;
56 import com.google.common.net.InetAddresses;
57
58 public class HwvtepSouthboundMapper {
59     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
60     private static final String N_CONNECTIONS_STR = "n_connections";
61
62     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
63         return InstanceIdentifier
64                 .create(NetworkTopology.class)
65                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
66                 .child(Node.class,new NodeKey(nodeId));
67     }
68
69     public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
70         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
71                         new PortNumber(client.getConnectionInfo().getRemotePort()));
72     }
73
74     private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
75         String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
76                 + String.valueOf(ip.getValue()) + ":" + port.getValue();
77         Uri uri = new Uri(uriString);
78         NodeId nodeId = new NodeId(uri);
79         InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
80                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
81                         .child(Node.class,new NodeKey(nodeId));
82         LOG.debug("Created ovsdb path: {}",path);
83         return path;
84     }
85
86     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
87         NodeKey nodeKey = iid.firstKeyOf(Node.class);
88         return nodeKey.getNodeId();
89     }
90
91     public static IpAddress createIpAddress(InetAddress address) {
92         IpAddress ip = null;
93         if (address instanceof Inet4Address) {
94             ip = createIpAddress((Inet4Address)address);
95         } else if (address instanceof Inet6Address) {
96             ip = createIpAddress((Inet6Address)address);
97         }
98         return ip;
99     }
100
101     public static IpAddress createIpAddress(Inet4Address address) {
102         return IetfInetUtil.INSTANCE.ipAddressFor(address);
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 ConnectionInfo createConnectionInfo(OvsdbClient client) {
111         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
112         connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
113         connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
114         connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
115         connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
116         return connectionInfoBuilder.build();
117     }
118
119     public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
120         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
121         connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
122         connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
123         return connectionInfoBuilder.build();
124     }
125
126     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
127         if (ip.getIpv4Address() != null) {
128             return InetAddresses.forString(ip.getIpv4Address().getValue());
129         } else if (ip.getIpv6Address() != null) {
130             return InetAddress.getByName(ip.getIpv6Address().getValue());
131         } else {
132             throw new UnknownHostException("IP Address has no value");
133         }
134     }
135
136     public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
137         String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
138                 HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
139         NodeId nodeId = new NodeId(new Uri(nodeString));
140         NodeKey nodeKey = new NodeKey(nodeId);
141         TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
142         return InstanceIdentifier.builder(NetworkTopology.class)
143                 .child(Topology.class, topoKey)
144                 .child(Node.class, nodeKey)
145                 .build();
146     }
147
148     public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
149                     PhysicalSwitch pSwitch) {
150         //TODO: Clean this up
151         return createInstanceIdentifier(client, new HwvtepNodeName(pSwitch.getName()));
152     }
153
154     public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
155                     HwvtepNodeName psName) {
156         NodeKey nodeKey = new NodeKey(createManagedNodeId(client, psName));
157         return InstanceIdentifier.builder(NetworkTopology.class)
158                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
159                         .child(Node.class, nodeKey).build();
160     }
161
162     public static NodeId createManagedNodeId(HwvtepConnectionInstance client, HwvtepNodeName psName) {
163         String nodeString = client.getNodeKey().getNodeId().getValue()
164                         + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/" + psName.getValue();
165         NodeId nodeId = new NodeId(new Uri(nodeString));
166         return nodeId;
167     }
168
169     public static InstanceIdentifier<LogicalSwitches> createInstanceIdentifier(HwvtepConnectionInstance client,
170                     LogicalSwitch lSwitch) {
171         InstanceIdentifier<LogicalSwitches> iid = null;
172         iid = InstanceIdentifier.builder(NetworkTopology.class)
173                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
174                         .child(Node.class, client.getNodeKey()).augmentation(HwvtepGlobalAugmentation.class)
175                         .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))
176                         .build();
177         /* TODO: Will this work instead to make it simpler?
178         iid = client.getInstanceIdentifier().builder()
179             .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))).build()
180          */
181         return iid;
182     }
183
184     public static InstanceIdentifier<Tunnels> createInstanceIdentifier( InstanceIdentifier<Node> nodeIid,
185                     InstanceIdentifier<TerminationPoint> localTpIid, InstanceIdentifier<TerminationPoint> remoteTpIid) {
186
187         TunnelsKey tunnelsKey = new TunnelsKey(new HwvtepPhysicalLocatorRef(localTpIid),
188                                                new HwvtepPhysicalLocatorRef(remoteTpIid));
189         InstanceIdentifier<Tunnels> tunnelInstanceId = nodeIid.builder().augmentation(PhysicalSwitchAugmentation.class)
190                                                        .child(Tunnels.class, tunnelsKey).build();
191         return tunnelInstanceId;
192     }
193
194     public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
195         Preconditions.checkNotNull(type);
196         if (type.isEmpty()) {
197             return EncapsulationTypeVxlanOverIpv4.class;
198         } else {
199             ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper =
200                     HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
201             return mapper.get(type);
202         }
203     }
204
205     public static InstanceIdentifier<TerminationPoint> createInstanceIdentifier(InstanceIdentifier<Node> nodeIid,
206                     PhysicalLocator physicalLocator) {
207         return nodeIid.child(TerminationPoint.class, getTerminationPointKey(physicalLocator));
208     }
209
210     public static TerminationPointKey getTerminationPointKey(PhysicalLocator pLoc) {
211         TerminationPointKey tpKey = null;
212         if(pLoc.getEncapsulationTypeColumn().getData() != null &&
213                         pLoc.getDstIpColumn().getData() != null) {
214             String tpKeyStr = pLoc.getEncapsulationTypeColumn().getData()+':'+pLoc.getDstIpColumn().getData();
215             tpKey = new TerminationPointKey(new TpId(tpKeyStr));
216         }
217         return tpKey;
218     }
219
220     public static String getRandomUUID() {
221         return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
222     }
223
224     public static InstanceIdentifier<VlanBindings> createInstanceIdentifier(HwvtepConnectionInstance client,
225             InstanceIdentifier<TerminationPoint> tpPath, VlanBindings vBindings) {
226                 return tpPath.augmentation(HwvtepPhysicalPortAugmentation.class) .child(VlanBindings.class,
227                         new VlanBindingsKey(vBindings.getKey()));
228
229     }
230 }