2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md;
11 import java.util.ArrayList;
12 import java.util.List;
14 import java.util.Map.Entry;
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;
42 import com.google.common.base.Optional;
44 public class LogicalSwitchUpdateCommand extends AbstractTransactionCommand {
46 private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
47 private Map<UUID, LogicalSwitch> updatedLSRows;
48 private Map<UUID, LogicalSwitch> oldLSRows;
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());
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());
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
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);
89 lsNodeBuilder.addAugmentation(HwvtepLogicalSwitchAugmentation.class, lsAugBuilder.build());
91 LOG.trace("Built with the intent to store PhysicalSwitch data {}",
92 lsAugBuilder.build());
93 return lsNodeBuilder.build();
96 private void setManagedBy(HwvtepLogicalSwitchAugmentationBuilder lsAugBuilder, LogicalSwitch lSwitch) {
97 // TODO This requires change to yang file
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());
108 private Node buildConnectionNode(LogicalSwitch lSwitch) {
109 //Update node with PhysicalSwitch reference
110 NodeBuilder connectionNode = new NodeBuilder();
111 connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
113 HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
114 List<Switches> switches = new ArrayList<>();
115 InstanceIdentifier<Node> switchIid = HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
117 hgAugmentationBuilder.setSwitches(switches);
119 * TODO: This need to be revisited after fix in yang file.
120 * It should ideally be HwvtepSwitchRef
122 Switches logicalSwitch = new SwitchesBuilder().setSwitchRef(
123 new HwvtepPhysicalSwitchRef(switchIid)).build();
124 switches.add(logicalSwitch);
126 connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
128 LOG.debug("Update node with logicalswitch ref {}",
129 hgAugmentationBuilder.getSwitches().iterator().next());
130 return connectionNode.build();
133 private InstanceIdentifier<Node> getInstanceIdentifier(LogicalSwitch lSwitch) {
134 return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
138 private NodeId getNodeId(LogicalSwitch lSwitch) {
139 NodeKey nodeKey = getInstanceIdentifier(lSwitch).firstKeyOf(Node.class, NodeKey.class);
140 return nodeKey.getNodeId();