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