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