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