1b9670188f9ef2e9914ccf25a9dda86b05606cdc
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / commands / SwitchesCmd.java
1 /*
2  * Copyright (c) 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.ha.commands;
9
10 import java.util.List;
11 import javax.annotation.Nullable;
12 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchRef;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.SwitchesBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.SwitchesKey;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
20 import org.opendaylight.yangtools.yang.binding.Identifier;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22
23 public class SwitchesCmd extends MergeCommand<Switches, HwvtepGlobalAugmentationBuilder, HwvtepGlobalAugmentation> {
24     public SwitchesCmd() {
25     }
26
27     @Override
28     @Nullable
29     public List<Switches> getData(HwvtepGlobalAugmentation node) {
30         if (node != null) {
31             return node.getSwitches();
32         }
33         return null;
34     }
35
36     @Override
37     public void setData(HwvtepGlobalAugmentationBuilder builder, List<Switches> data) {
38         builder.setSwitches(data);
39     }
40
41     @Override
42     public InstanceIdentifier<Switches> generateId(InstanceIdentifier<Node> id, Switches node) {
43         SwitchesKey switchesKey = transform(id, node).key();
44         return id.augmentation(HwvtepGlobalAugmentation.class).child(Switches.class, switchesKey);
45     }
46
47     @Override
48     public Switches transform(InstanceIdentifier<Node> nodePath, Switches src) {
49         SwitchesBuilder builder = new SwitchesBuilder(src);
50         InstanceIdentifier<Node> psNodeIid = (InstanceIdentifier<Node>) src.getSwitchRef().getValue();
51         String psNodeId = psNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
52         String nodeId = nodePath.firstKeyOf(Node.class).getNodeId().getValue();
53         int idx = nodeId.indexOf("/physicalswitch/");
54         if (idx > 0) {
55             nodeId = nodeId.substring(0, idx);
56         }
57         idx = psNodeId.indexOf("/physicalswitch/") + "/physicalswitch/".length();
58         String switchName = psNodeId.substring(idx);
59         String dstNodeId = nodeId + "/physicalswitch/" + switchName;
60
61         InstanceIdentifier<Node> id2 = HwvtepHAUtil.convertToInstanceIdentifier(dstNodeId);
62
63         builder.setSwitchRef(new HwvtepPhysicalSwitchRef(id2));
64         builder.withKey(new SwitchesKey(new HwvtepPhysicalSwitchRef(id2)));
65         return builder.build();
66     }
67
68     @Override
69     public Identifier getKey(Switches data) {
70         InstanceIdentifier<?> id = data.getSwitchRef().getValue();
71         return id.firstKeyOf(Node.class);
72     }
73
74     @Override
75     public String getDescription() {
76         return "Switches";
77     }
78
79     @Override
80     public boolean areEqual(Switches switchA, Switches switchB) {
81         return true;
82     }
83
84     @Override
85     public Switches withoutUuid(Switches data) {
86         return data;
87     }
88 }