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