Merge "Maven cleanup and features-parent migration"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / LogicalSwitchUpdateCommand.java
1 /*
2  * Copyright (c) 2015 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.transactions.md;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
21 import org.opendaylight.ovsdb.lib.message.TableUpdates;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchAugmentationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.SwitchesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.google.common.base.Optional;
43
44 public class LogicalSwitchUpdateCommand extends AbstractTransactionCommand {
45
46     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
47     private Map<UUID, LogicalSwitch> updatedLSRows;
48     private Map<UUID, LogicalSwitch> oldLSRows;
49
50     public LogicalSwitchUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
51             DatabaseSchema dbSchema) {
52         super(key, updates, dbSchema);
53         updatedLSRows = TyperUtils.extractRowsUpdated(LogicalSwitch.class, getUpdates(),getDbSchema());
54         oldLSRows = TyperUtils.extractRowsOld(LogicalSwitch.class, getUpdates(),getDbSchema());
55     }
56
57     @Override
58     public void execute(ReadWriteTransaction transaction) {
59         if(updatedLSRows != null && !updatedLSRows.isEmpty()) {
60             for (Entry<UUID, LogicalSwitch> entry : updatedLSRows.entrySet()) {
61                 updateLogicalSwitch(transaction, entry.getValue());
62             }
63         }
64     }
65
66     private void updateLogicalSwitch(ReadWriteTransaction transaction, LogicalSwitch lSwitch) {
67         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
68         Optional<Node> connection = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
69         if (connection.isPresent()) {
70             LOG.debug("Connection {} is present",connection);
71             Node connectionNode = buildConnectionNode(lSwitch);
72             transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
73             // Update the Logical Switch with whatever data we are getting
74             InstanceIdentifier<Node> lsIid = getInstanceIdentifier(lSwitch);
75             Node lsNode = buildLogicalSwitchNode(lSwitch);
76             transaction.merge(LogicalDatastoreType.OPERATIONAL, lsIid, lsNode);
77 //            TODO: Delete entries that are no longer needed
78         }
79     }
80
81     private Node buildLogicalSwitchNode(LogicalSwitch lSwitch) {
82         NodeBuilder lsNodeBuilder = new NodeBuilder();
83         NodeId psNodeId = getNodeId(lSwitch);
84         lsNodeBuilder.setNodeId(psNodeId);
85         HwvtepLogicalSwitchAugmentationBuilder lsAugBuilder = new HwvtepLogicalSwitchAugmentationBuilder();
86         setManagedBy(lsAugBuilder, lSwitch);
87         setLogicalSwitchId(lsAugBuilder, lSwitch);
88
89         lsNodeBuilder.addAugmentation(HwvtepLogicalSwitchAugmentation.class, lsAugBuilder.build());
90
91         LOG.trace("Built with the intent to store PhysicalSwitch data {}",
92                 lsAugBuilder.build());
93         return lsNodeBuilder.build();
94     }
95
96     private void setManagedBy(HwvtepLogicalSwitchAugmentationBuilder lsAugBuilder, LogicalSwitch lSwitch) {
97         // TODO This requires change to yang file
98     }
99
100
101     private void setLogicalSwitchId(HwvtepLogicalSwitchAugmentationBuilder lsAugBuilder, LogicalSwitch lSwitch) {
102         lsAugBuilder.setHwvtepNodeName(new HwvtepNodeName(lSwitch.getName()));
103         if(lSwitch.getDescription() != null) {
104             lsAugBuilder.setHwvtepNodeDescription(lSwitch.getDescription());
105         }
106     }
107
108     private Node buildConnectionNode(LogicalSwitch lSwitch) {
109         //Update node with PhysicalSwitch reference
110         NodeBuilder connectionNode = new NodeBuilder();
111         connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
112
113         HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
114         List<Switches> switches = new ArrayList<>();
115         InstanceIdentifier<Node> switchIid = HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
116                         lSwitch);
117         hgAugmentationBuilder.setSwitches(switches);
118         /* FIXME:
119          * TODO: This need to be revisited after fix in yang file.
120          * It should ideally be HwvtepSwitchRef
121          */
122         Switches logicalSwitch = new SwitchesBuilder().setSwitchRef(
123                         new HwvtepPhysicalSwitchRef(switchIid)).build(); 
124         switches.add(logicalSwitch);
125
126         connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
127
128         LOG.debug("Update node with logicalswitch ref {}",
129                 hgAugmentationBuilder.getSwitches().iterator().next());
130         return connectionNode.build();
131     }
132
133     private InstanceIdentifier<Node> getInstanceIdentifier(LogicalSwitch lSwitch) {
134         return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
135                 lSwitch);
136     }
137
138     private NodeId getNodeId(LogicalSwitch lSwitch) {
139         NodeKey nodeKey = getInstanceIdentifier(lSwitch).firstKeyOf(Node.class, NodeKey.class);
140         return nodeKey.getNodeId();
141     }
142 }