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