Change hwvtep.yang and fix related code
[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.HwvtepNodeName;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesBuilder;
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         return null;
83 /*        NodeBuilder lsNodeBuilder = new NodeBuilder();
84         NodeId psNodeId = getNodeId(lSwitch);
85         lsNodeBuilder.setNodeId(psNodeId);
86         LogicalSwitchesBuilder lsAugBuilder = new LogicalSwitchesBuilder();
87         setLogicalSwitchId(lsAugBuilder, lSwitch);
88         lsNodeBuilder.addAugmentation(LogicalSwitches.class, lsAugBuilder.build());
89
90         LOG.trace("Built with the intent to store PhysicalSwitch data {}",
91                 lsAugBuilder.build());
92         return lsNodeBuilder.build();*/
93     }
94
95     private void setLogicalSwitchId(LogicalSwitchesBuilder lsAugBuilder, LogicalSwitch lSwitch) {
96         lsAugBuilder.setHwvtepNodeName(new HwvtepNodeName(lSwitch.getName()));
97         if(lSwitch.getDescription() != null) {
98             lsAugBuilder.setHwvtepNodeDescription(lSwitch.getDescription());
99         }
100     }
101
102     private Node buildConnectionNode(LogicalSwitch lSwitch) {
103         //Update node with PhysicalSwitch reference
104         NodeBuilder connectionNode = new NodeBuilder();
105         connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
106
107         HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
108         List<Switches> switches = new ArrayList<>();
109         InstanceIdentifier<Node> switchIid = HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
110                         lSwitch);
111         hgAugmentationBuilder.setSwitches(switches);
112         /* FIXME:
113          * TODO: This need to be revisited after fix in yang file.
114          * It should ideally be HwvtepSwitchRef
115          */
116         Switches logicalSwitch = new SwitchesBuilder().setSwitchRef(
117                         new HwvtepPhysicalSwitchRef(switchIid)).build(); 
118         switches.add(logicalSwitch);
119
120         connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
121
122         LOG.debug("Update node with logicalswitch ref {}",
123                 hgAugmentationBuilder.getSwitches().iterator().next());
124         return connectionNode.build();
125     }
126
127     private InstanceIdentifier<Node> getInstanceIdentifier(LogicalSwitch lSwitch) {
128         return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
129                 lSwitch);
130     }
131
132     private NodeId getNodeId(LogicalSwitch lSwitch) {
133         NodeKey nodeKey = getInstanceIdentifier(lSwitch).firstKeyOf(Node.class, NodeKey.class);
134         return nodeKey.getNodeId();
135     }
136 }