TSC-101: Fixup Augmentable and Identifiable methods change
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / test / java / org / opendaylight / ovsdb / hwvtepsouthbound / TestBuilders.java
1 /*
2  * Copyright © 2016, 2017 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.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.UUID;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSetBuilder;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public final class TestBuilders {
43
44     public static final String VXLAN_OVER_IPV4 = "vxlan_over_ipv4";
45
46     private TestBuilders() {
47     }
48
49     public static List<LogicalSwitches> addLogicalSwitches(HwvtepGlobalAugmentationBuilder augmentationBuilder,
50             String[]... data) {
51         List<LogicalSwitches> logicalSwitcheses = new ArrayList<>();
52         for (String[] row : data) {
53             logicalSwitcheses.add(TestBuilders.buildLogicalSwitch(row));
54         }
55         augmentationBuilder.setLogicalSwitches(logicalSwitcheses);
56         return logicalSwitcheses;
57     }
58
59     public static List<RemoteMcastMacs> addRemoteMcastMacs(InstanceIdentifier<Node> iid,
60                                           HwvtepGlobalAugmentationBuilder augmentationBuilder, String[]... data) {
61         List<RemoteMcastMacs> remoteMcastMacses = new ArrayList<>();
62         for (String[] row : data) {
63             String[] teps = Arrays.copyOfRange(row, 2, row.length);
64             remoteMcastMacses.add(TestBuilders.buildRemoteMcastMacs(iid, row[0], row[1], teps));
65         }
66         augmentationBuilder.setRemoteMcastMacs(remoteMcastMacses);
67         return remoteMcastMacses;
68     }
69
70     public static List<RemoteUcastMacs> addRemoteUcastMacs(InstanceIdentifier<Node> iid,
71                                                            HwvtepGlobalAugmentationBuilder augmentationBuilder,
72                                                            String[]... data) {
73         List<RemoteUcastMacs> remoteUcastMacses = new ArrayList<>();
74         for (String[] row : data) {
75             remoteUcastMacses.add(TestBuilders.buildRemoteUcastMacs(iid, row[0], row[1], row[2], row[3]));
76         }
77         augmentationBuilder.setRemoteUcastMacs(remoteUcastMacses);
78         return remoteUcastMacses;
79     }
80
81     public static void addGlobalTerminationPoints(NodeBuilder nodeBuilder, InstanceIdentifier<Node> nodeIid,
82                                                   String[]... data) {
83         List<TerminationPoint> terminationPoints = new ArrayList<>();
84         for (String[] row : data) {
85             terminationPoints.add(TestBuilders.buildTerminationPoint(nodeIid, row[0]));
86         }
87         nodeBuilder.setTerminationPoint(terminationPoints);
88     }
89
90     public static HwvtepLogicalSwitchRef buildLogicalSwitchesRef(InstanceIdentifier<Node> nodeIid,
91                                                                  String logicalSwitchName) {
92         InstanceIdentifier<LogicalSwitches> switchIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
93                 .child(LogicalSwitches.class, new LogicalSwitchesKey(new HwvtepNodeName(logicalSwitchName)));
94         return new HwvtepLogicalSwitchRef(switchIid);
95     }
96
97     public static RemoteUcastMacs buildRemoteUcastMacs(InstanceIdentifier<Node> nodeIid, String vmMac,
98                                                        String vmip, String tepIp, String logicalSwitchName) {
99         RemoteUcastMacsBuilder ucmlBuilder = new RemoteUcastMacsBuilder();
100         ucmlBuilder.setIpaddr(new IpAddress(vmip.toCharArray()));
101         ucmlBuilder.setMacEntryKey(new MacAddress(vmMac));
102         ucmlBuilder.setMacEntryUuid(getUUid(vmMac));
103         ucmlBuilder.setLocatorRef(buildLocatorRef(nodeIid, tepIp));
104         ucmlBuilder.setLogicalSwitchRef(buildLogicalSwitchesRef(nodeIid, logicalSwitchName));
105         return ucmlBuilder.build();
106     }
107
108     public static TerminationPoint buildTerminationPoint(InstanceIdentifier<Node> nodeIid, String ip) {
109         TerminationPointKey tpKey = new TerminationPointKey(new TpId("vxlan_over_ipv4:" + ip));
110         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
111         if (nodeIid != null) {
112             tpBuilder.withKey(tpKey);
113             tpBuilder.setTpId(tpKey.getTpId());
114             HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder =
115                     new HwvtepPhysicalLocatorAugmentationBuilder();
116             tpAugmentationBuilder.setPhysicalLocatorUuid(getUUid(ip));
117             tpAugmentationBuilder.setEncapsulationType(HwvtepSouthboundMapper.createEncapsulationType(VXLAN_OVER_IPV4));
118             tpAugmentationBuilder.setDstIp(new IpAddress(ip.toCharArray()));
119             tpBuilder.addAugmentation(HwvtepPhysicalLocatorAugmentation.class, tpAugmentationBuilder.build());
120         }
121         return tpBuilder.build();
122     }
123
124     public static LogicalSwitches buildLogicalSwitch(String... keys) {
125         String logicalSwitch = keys[0];
126         String tunnelKey = keys[1];
127         LogicalSwitchesBuilder logicalSwitchesBuilder = new LogicalSwitchesBuilder();
128         logicalSwitchesBuilder.withKey(new LogicalSwitchesKey(new HwvtepNodeName(logicalSwitch)));
129         logicalSwitchesBuilder.setHwvtepNodeName(new HwvtepNodeName(logicalSwitch));
130         logicalSwitchesBuilder.setTunnelKey(tunnelKey);
131         Uuid uuid = getUUid(logicalSwitch);
132         logicalSwitchesBuilder.setLogicalSwitchUuid(uuid);
133         return logicalSwitchesBuilder.build();
134     }
135
136     public static RemoteMcastMacs buildRemoteMcastMacs(InstanceIdentifier<Node> iid, String mac,
137                                                        String logicalSwitchName, String[] tepIps) {
138
139         RemoteMcastMacsBuilder macLocalBuilder = new RemoteMcastMacsBuilder();
140         if (mac.equals(HwvtepSouthboundConstants.UNKNOWN_DST_STRING)) {
141             macLocalBuilder.setMacEntryKey(HwvtepSouthboundConstants.UNKNOWN_DST_MAC);
142         } else {
143             macLocalBuilder.setMacEntryKey(new MacAddress(mac));
144         }
145         macLocalBuilder.setMacEntryUuid(getUUid(mac));
146         macLocalBuilder.setLogicalSwitchRef(buildLogicalSwitchesRef(iid, logicalSwitchName));
147         List<LocatorSet> locatorSets = new ArrayList<>();
148         for (String tepIp : tepIps) {
149             locatorSets.add(new LocatorSetBuilder().setLocatorRef(
150                     buildLocatorRef(iid, tepIp)).build());
151         }
152         macLocalBuilder.setLocatorSet(locatorSets);
153         return macLocalBuilder.build();
154     }
155
156     public static HwvtepPhysicalLocatorRef buildLocatorRef(InstanceIdentifier<Node> nodeIid,String tepIp) {
157         InstanceIdentifier<TerminationPoint> tepId = buildTpId(nodeIid, tepIp);
158         return new HwvtepPhysicalLocatorRef(tepId);
159     }
160
161     public static Uuid getUUid(String key) {
162         return new Uuid(UUID.nameUUIDFromBytes(key.getBytes()).toString());
163     }
164
165     public static InstanceIdentifier<TerminationPoint> buildTpId(InstanceIdentifier<Node> nodeIid,String tepIp) {
166         String tpKeyStr = VXLAN_OVER_IPV4 + ':' + tepIp;
167         TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpKeyStr));
168         return nodeIid.child(TerminationPoint.class, tpKey);
169     }
170 }