Merge "Adding integration tests for Security Groups (fixed and default SG)"
[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.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.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.EncapsulationTypeBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfoBuilder;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import com.google.common.base.Preconditions;
48 import com.google.common.collect.ImmutableBiMap;
49
50 public class HwvtepSouthboundMapper {
51     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundMapper.class);
52     private static final String N_CONNECTIONS_STR = "n_connections";
53
54     public static InstanceIdentifier<Node> createInstanceIdentifier(NodeId nodeId) {
55         return InstanceIdentifier
56                 .create(NetworkTopology.class)
57                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
58                 .child(Node.class,new NodeKey(nodeId));
59     }
60
61     public static InstanceIdentifier<Node> createInstanceIdentifier (OvsdbClient client) {
62         return createInstanceIdentifier(createIpAddress(client.getConnectionInfo().getRemoteAddress()),
63                         new PortNumber(client.getConnectionInfo().getRemotePort()));
64     }
65
66     private static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
67         String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
68                 + String.valueOf(ip.getValue()) + ":" + port.getValue();
69         Uri uri = new Uri(uriString);
70         NodeId nodeId = new NodeId(uri);
71         InstanceIdentifier<Node> path = InstanceIdentifier.create(NetworkTopology.class)
72                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
73                         .child(Node.class,new NodeKey(nodeId));
74         LOG.debug("Created ovsdb path: {}",path);
75         return path;
76     }
77
78     public static NodeId createManagedNodeId(InstanceIdentifier<Node> iid) {
79         NodeKey nodeKey = iid.firstKeyOf(Node.class, NodeKey.class);
80         return nodeKey.getNodeId();
81     }
82
83     public static IpAddress createIpAddress(InetAddress address) {
84         IpAddress ip = null;
85         if (address instanceof Inet4Address) {
86             ip = createIpAddress((Inet4Address)address);
87         } else if (address instanceof Inet6Address) {
88             ip = createIpAddress((Inet6Address)address);
89         }
90         return ip;
91     }
92
93     public static IpAddress createIpAddress(Inet4Address address) {
94         Ipv4Address ipv4 = new Ipv4Address(address.getHostAddress());
95         return new IpAddress(ipv4);
96     }
97
98     public static IpAddress createIpAddress(Inet6Address address) {
99         Ipv6Address ipv6 = new Ipv6Address(address.getHostAddress());
100         return new IpAddress(ipv6);
101     }
102
103     public static ConnectionInfo createConnectionInfo(OvsdbClient client) {
104         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
105         connectionInfoBuilder.setRemoteIp(createIpAddress(client.getConnectionInfo().getRemoteAddress()));
106         connectionInfoBuilder.setRemotePort(new PortNumber(client.getConnectionInfo().getRemotePort()));
107         connectionInfoBuilder.setLocalIp(createIpAddress(client.getConnectionInfo().getLocalAddress()));
108         connectionInfoBuilder.setLocalPort(new PortNumber(client.getConnectionInfo().getLocalPort()));
109         return connectionInfoBuilder.build();
110     }
111
112     public static ConnectionInfo suppressLocalIpPort(ConnectionInfo connectionInfo) {
113         ConnectionInfoBuilder connectionInfoBuilder = new ConnectionInfoBuilder();
114         connectionInfoBuilder.setRemoteIp(connectionInfo.getRemoteIp());
115         connectionInfoBuilder.setRemotePort(connectionInfo.getRemotePort());
116         return connectionInfoBuilder.build();
117     }
118
119     public static InetAddress createInetAddress(IpAddress ip) throws UnknownHostException {
120         if (ip.getIpv4Address() != null) {
121             return InetAddress.getByName(ip.getIpv4Address().getValue());
122         } else if (ip.getIpv6Address() != null) {
123             return InetAddress.getByName(ip.getIpv6Address().getValue());
124         } else {
125             throw new UnknownHostException("IP Address has no value");
126         }
127     }
128
129     public static InstanceIdentifier<Node> getInstanceIdentifier(Global global) {
130         String nodeString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://" +
131                 HwvtepSouthboundConstants.UUID + "/" + global.getUuid().toString();
132         NodeId nodeId = new NodeId(new Uri(nodeString));
133         NodeKey nodeKey = new NodeKey(nodeId);
134         TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID);
135         return InstanceIdentifier.builder(NetworkTopology.class)
136                 .child(Topology.class, topoKey)
137                 .child(Node.class, nodeKey)
138                 .build();
139     }
140
141     public static InstanceIdentifier<Node> createInstanceIdentifier(HwvtepConnectionInstance client,
142                     PhysicalSwitch pSwitch) {
143         String nodeString = client.getNodeKey().getNodeId().getValue() + "/physicalswitch/" + pSwitch.getName();
144         NodeId nodeId = new NodeId(new Uri(nodeString));
145         NodeKey nodeKey = new NodeKey(nodeId);
146         return InstanceIdentifier.builder(NetworkTopology.class)
147                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
148                         .child(Node.class, nodeKey).build();
149     }
150
151     public static InstanceIdentifier<LogicalSwitches> createInstanceIdentifier(HwvtepConnectionInstance client,
152                     LogicalSwitch lSwitch) {
153         InstanceIdentifier<LogicalSwitches> iid = null;
154         iid = InstanceIdentifier.builder(NetworkTopology.class)
155                         .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
156                         .child(Node.class, client.getNodeKey()).augmentation(HwvtepGlobalAugmentation.class)
157                         .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))
158                         .build();
159         /* TODO: Will this work instead to make it simpler?
160         iid = client.getInstanceIdentifier().builder()
161             .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(lSwitch.getName())))).build()
162          */
163         return iid;
164     }
165
166     public static Class<? extends EncapsulationTypeBase> createEncapsulationType(String type) {
167         Preconditions.checkNotNull(type);
168         if (type.isEmpty()) {
169             return EncapsulationTypeVxlanOverIpv4.class;
170         } else {
171             ImmutableBiMap<String, Class<? extends EncapsulationTypeBase>> mapper =
172                     HwvtepSouthboundConstants.ENCAPS_TYPE_MAP.inverse();
173             return mapper.get(type);
174         }
175     }
176
177     public static InstanceIdentifier<TerminationPoint> createInstanceIdentifier(InstanceIdentifier<Node> nodeIid,
178                     PhysicalLocator physicalLocator) {
179         return nodeIid.child(TerminationPoint.class, getTerminationPointKey(physicalLocator));
180     }
181
182     public static TerminationPointKey getTerminationPointKey(PhysicalLocator pLoc) {
183         TerminationPointKey tpKey = null;
184         if(pLoc.getEncapsulationTypeColumn().getData() != null &&
185                         pLoc.getDstIpColumn().getData() != null) {
186             String tpKeyStr = pLoc.getEncapsulationTypeColumn().getData()+':'+pLoc.getDstIpColumn().getData();
187             tpKey = new TerminationPointKey(new TpId(tpKeyStr));
188         }
189         return tpKey;
190     }
191
192     public static String getRandomUUID() {
193         return "Random_" + java.util.UUID.randomUUID().toString().replace("-", "");
194     }
195 }