Do not use char[]-based union access
[netvirt.git] / elanmanager / impl / src / test / java / org / opendaylight / netvirt / elan / l2gw / nodehandlertest / NodeConnectedHandlerUtils.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 package org.opendaylight.netvirt.elan.l2gw.nodehandlertest;
9
10 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
11
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.UUID;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIpsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIpsKey;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34
35 /**
36  * Created by eaksahu on 10/14/2016.
37  */
38 public class NodeConnectedHandlerUtils {
39
40     CheckedFuture<Void, TransactionCommitFailedException> addNode(InstanceIdentifier<Node> path,
41             InstanceIdentifier<Node> psPath, String logicalSwitchData, String localUcasMacData, String localMcastData,
42             String remoteMcastData, String remoteUcasteMacData, String globalTerminationPointIp,
43             WriteTransaction transaction) throws Exception {
44         NodeBuilder nodeBuilder = null;
45         HwvtepGlobalAugmentationBuilder augmentationBuilder = null;
46         nodeBuilder = prepareOperationalNode(path);
47         augmentationBuilder = prepareAugmentationBuilder();
48
49         GlobalAugmentationHelper.addLogicalSwitches(augmentationBuilder, getData(logicalSwitchData));
50
51         GlobalAugmentationHelper.addLocalUcastMacs(path, augmentationBuilder, getData(localUcasMacData));
52
53         GlobalAugmentationHelper.addLocalMcastMacs(path, augmentationBuilder, getData(localMcastData));
54
55         GlobalAugmentationHelper.addRemoteMcastMacs(path, augmentationBuilder, getData(remoteMcastData));
56
57         GlobalAugmentationHelper.addRemoteUcastMacs(path, augmentationBuilder, getData(remoteUcasteMacData));
58
59         GlobalAugmentationHelper.addGlobalTerminationPoints(nodeBuilder, path, getData(globalTerminationPointIp));
60
61         GlobalAugmentationHelper.addSwitches(augmentationBuilder, psPath);
62
63         nodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, augmentationBuilder.build());
64
65         return TestUtil.submitNode(OPERATIONAL, path, nodeBuilder.build(), transaction);
66     }
67
68     CheckedFuture<Void, TransactionCommitFailedException> addPsNode(InstanceIdentifier<Node> path,
69             InstanceIdentifier<Node> parentPath, List<String> portNameList, WriteTransaction transaction)
70             throws Exception {
71         NodeBuilder nodeBuilder = null;
72
73         nodeBuilder = prepareOperationalNode(path);
74         PhysicalSwitchAugmentationBuilder physicalSwitchAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
75         physicalSwitchAugmentationBuilder.setManagedBy(new HwvtepGlobalRef(parentPath));
76         physicalSwitchAugmentationBuilder.setPhysicalSwitchUuid(getUUid("d1s3"));
77         physicalSwitchAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName("s3"));
78         physicalSwitchAugmentationBuilder.setHwvtepNodeDescription("description");
79
80         List<TunnelIps> tunnelIps = new ArrayList<>();
81         IpAddress ip = new IpAddress(new Ipv4Address("192.168.122.30"));
82         tunnelIps.add(new TunnelIpsBuilder().withKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build());
83         physicalSwitchAugmentationBuilder.setTunnelIps(tunnelIps);
84
85         nodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, physicalSwitchAugmentationBuilder.build());
86         PhysicalSwitchHelper.dId = parentPath;
87         nodeBuilder.setTerminationPoint(PhysicalSwitchHelper
88                 .addPhysicalSwitchTerminationPoints(path, transaction, portNameList));
89
90         return TestUtil.submitNode(OPERATIONAL, path, nodeBuilder.build(), transaction);
91     }
92
93     NodeBuilder prepareOperationalNode(InstanceIdentifier<Node> iid) {
94         NodeBuilder nodeBuilder = new NodeBuilder();
95         nodeBuilder.setNodeId(iid.firstKeyOf(Node.class).getNodeId());
96         return nodeBuilder;
97     }
98
99     HwvtepGlobalAugmentationBuilder prepareAugmentationBuilder() {
100         HwvtepGlobalAugmentationBuilder builder = new HwvtepGlobalAugmentationBuilder();
101         builder.setManagers(TestBuilders.buildManagers());
102         return builder;
103     }
104
105     public List<String> getData(String data) {
106         return Arrays.asList(data.split(","));
107     }
108
109     public static Uuid getUUid(String key) {
110         return new Uuid(UUID.nameUUIDFromBytes(key.getBytes()).toString());
111     }
112 }