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