2 * Copyright (c) 2015, 2017 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 com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
26 import org.opendaylight.ovsdb.lib.message.TableUpdates;
27 import org.opendaylight.ovsdb.lib.notation.UUID;
28 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
29 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
30 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
31 import org.opendaylight.ovsdb.schema.hardwarevtep.Tunnel;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchRef;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.SwitchesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIps;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIpsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIpsKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.SwitchFaultStatus;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.SwitchFaultStatusBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.SwitchFaultStatusKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIpsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIpsKey;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
56 import org.opendaylight.yangtools.yang.binding.DataObject;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 public class HwvtepPhysicalSwitchUpdateCommand extends AbstractTransactionCommand {
63 private static final Logger LOG = LoggerFactory.getLogger(HwvtepPhysicalSwitchUpdateCommand.class);
64 private final Map<UUID, PhysicalSwitch> updatedPSRows;
65 private Map<UUID, Tunnel> updatedTunnelRows;
66 private final Map<UUID, PhysicalSwitch> oldPSRows;
68 public HwvtepPhysicalSwitchUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates,
69 DatabaseSchema dbSchema) {
70 super(key, updates, dbSchema);
71 updatedPSRows = TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(), getDbSchema());
72 oldPSRows = TyperUtils.extractRowsOld(PhysicalSwitch.class, getUpdates(), getDbSchema());
74 updatedTunnelRows = TyperUtils.extractRowsUpdated(Tunnel.class, getUpdates(), getDbSchema());
75 } catch (IllegalArgumentException e) {
76 LOG.debug("Tunnel Table not supported on this HWVTEP device", e.getMessage());
81 public void execute(ReadWriteTransaction transaction) {
82 for (Map.Entry<UUID, PhysicalSwitch> entry : updatedPSRows.entrySet()) {
83 updatePhysicalSwitch(transaction, entry.getKey(), entry.getValue());
87 private void updatePhysicalSwitch(ReadWriteTransaction transaction, UUID uuid, PhysicalSwitch phySwitch) {
88 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
89 //TODO remove this read
90 Optional<Node> connection = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
91 if (connection.isPresent()) {
92 LOG.debug("Connection {} is present", connection);
93 // Update the connection node to let it know it manages this
95 Node connectionNode = buildConnectionNode(phySwitch);
96 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
98 // Update the Physical Switch with whatever data we are getting
99 InstanceIdentifier<Node> psIid = getInstanceIdentifier(phySwitch);
100 Node psNode = buildPhysicalSwitchNode(connection.get(), phySwitch);
101 transaction.merge(LogicalDatastoreType.OPERATIONAL, psIid, psNode);
103 PhysicalSwitch oldPSwitch = oldPSRows.get(uuid);
104 updateTunnelIps(phySwitch, oldPSwitch, transaction);
106 getOvsdbConnectionInstance().getDeviceInfo().putPhysicalSwitch(phySwitch.getUuid(), phySwitch);
107 getDeviceInfo().updateDeviceOperData(Node.class, psIid, phySwitch.getUuid(), phySwitch);
108 // TODO: Delete entries that are no longer needed
109 // TODO: Deletion of tunnels
110 // TODO: Deletion of Tunnel BFD config and params
111 //Deleting old switch fault status entries
112 deleteEntries(transaction, getSwitchFaultStatusToRemove(psIid,phySwitch));
116 private InstanceIdentifier<TunnelIps> getTunnelIpIid(final String tunnelIp, final InstanceIdentifier<Node> psIid) {
117 IpAddress ip = new IpAddress(tunnelIp.toCharArray());
118 TunnelIps tunnelIps = new TunnelIpsBuilder().setKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build();
119 return psIid.augmentation(PhysicalSwitchAugmentation.class).child(TunnelIps.class, tunnelIps.getKey());
122 private void updateTunnelIps(@Nonnull final PhysicalSwitch newPSwitch, @Nullable final PhysicalSwitch oldPSwitch,
123 final ReadWriteTransaction transaction) {
124 Set<String> oldTunnelIps = oldPSwitch != null && oldPSwitch.getTunnelIpsColumn() != null ? oldPSwitch
125 .getTunnelIpsColumn().getData() : Collections.emptySet();
126 Set<String> newTunelIps = newPSwitch.getTunnelIpsColumn() != null ? newPSwitch
127 .getTunnelIpsColumn().getData() : Collections.emptySet();
129 Set<String> addedTunnelIps = Sets.difference(newTunelIps, oldTunnelIps);
130 Set<String> removedTunnelIps = Sets.difference(oldTunnelIps, newTunelIps);
132 InstanceIdentifier<Node> psIid = getInstanceIdentifier(newPSwitch);
133 for (String tunnelIp : removedTunnelIps) {
134 InstanceIdentifier<TunnelIps> tunnelIpsInstanceIdentifier = getTunnelIpIid(tunnelIp, psIid);
135 transaction.delete(LogicalDatastoreType.OPERATIONAL, tunnelIpsInstanceIdentifier);
137 for (String tunnelIp : addedTunnelIps) {
138 IpAddress ip = new IpAddress(tunnelIp.toCharArray());
139 InstanceIdentifier<TunnelIps> tunnelIpsInstanceIdentifier = getTunnelIpIid(tunnelIp, psIid);
140 TunnelIps tunnelIps = new TunnelIpsBuilder().setKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build();
141 transaction.put(LogicalDatastoreType.OPERATIONAL, tunnelIpsInstanceIdentifier, tunnelIps, true);
145 private Node buildPhysicalSwitchNode(Node node, PhysicalSwitch phySwitch) {
146 NodeBuilder psNodeBuilder = new NodeBuilder();
147 NodeId psNodeId = getNodeId(phySwitch);
148 psNodeBuilder.setNodeId(psNodeId);
149 PhysicalSwitchAugmentationBuilder psAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
150 psAugmentationBuilder.setPhysicalSwitchUuid(new Uuid(phySwitch.getUuid().toString()));
151 setManagedBy(psAugmentationBuilder);
152 setPhysicalSwitchId(psAugmentationBuilder, phySwitch);
153 setManagementIps(psAugmentationBuilder, phySwitch);
154 setTunnels(psAugmentationBuilder, phySwitch);
155 setSwitchFaultStatus(psAugmentationBuilder, phySwitch);
157 psNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, psAugmentationBuilder.build());
159 LOG.trace("Built with the intent to store PhysicalSwitch data {}", psAugmentationBuilder.build());
160 return psNodeBuilder.build();
163 private void setTunnels(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch phySwitch) {
164 if (updatedTunnelRows != null && phySwitch.getTunnels() != null && phySwitch.getTunnels().getData() != null
165 && !phySwitch.getTunnels().getData().isEmpty()) {
166 // Nothing to do but update deviceInfo cache
167 for (UUID uuid: phySwitch.getTunnels().getData()) {
168 getOvsdbConnectionInstance().getDeviceInfo().putPhysicalSwitchForTunnel(uuid, phySwitch.getUuid());
173 private void setManagedBy(PhysicalSwitchAugmentationBuilder psAugmentationBuilder) {
174 InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
175 psAugmentationBuilder.setManagedBy(new HwvtepGlobalRef(connectionNodePath));
178 private void setPhysicalSwitchId(PhysicalSwitchAugmentationBuilder psAugmentationBuilder,
179 PhysicalSwitch phySwitch) {
180 if (phySwitch.getName() != null) {
181 psAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName(phySwitch.getName()));
183 if (phySwitch.getDescription() != null) {
184 psAugmentationBuilder.setHwvtepNodeDescription(phySwitch.getDescription());
188 private void setManagementIps(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch phySwitch) {
189 if (phySwitch.getManagementIpsColumn() != null && phySwitch.getManagementIpsColumn().getData() != null
190 && !phySwitch.getManagementIpsColumn().getData().isEmpty()) {
191 List<ManagementIps> mgmtIps = new ArrayList<>();
192 for (String mgmtIp : phySwitch.getManagementIpsColumn().getData()) {
193 IpAddress ip = new IpAddress(mgmtIp.toCharArray());
195 new ManagementIpsBuilder().setKey(new ManagementIpsKey(ip)).setManagementIpsKey(ip).build());
197 psAugmentationBuilder.setManagementIps(mgmtIps);
201 private Node buildConnectionNode(PhysicalSwitch phySwitch) {
202 // Update node with PhysicalSwitch reference
203 NodeBuilder connectionNode = new NodeBuilder();
204 connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
206 HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
207 List<Switches> switches = new ArrayList<>();
208 InstanceIdentifier<Node> switchIid =
209 HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), phySwitch);
210 hgAugmentationBuilder.setSwitches(switches);
211 Switches physicalSwitch = new SwitchesBuilder().setSwitchRef(new HwvtepPhysicalSwitchRef(switchIid)).build();
212 switches.add(physicalSwitch);
214 connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
216 LOG.debug("Update node with physicalswitch ref {}", hgAugmentationBuilder.getSwitches().iterator().next());
217 return connectionNode.build();
220 private InstanceIdentifier<Node> getInstanceIdentifier(PhysicalSwitch phySwitch) {
221 return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), phySwitch);
224 private NodeId getNodeId(PhysicalSwitch phySwitch) {
225 NodeKey nodeKey = getInstanceIdentifier(phySwitch).firstKeyOf(Node.class);
226 return nodeKey.getNodeId();
229 private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
230 List<InstanceIdentifier<T>> entryIids) {
231 for (InstanceIdentifier<T> entryIid : entryIids) {
232 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
236 private List<InstanceIdentifier<SwitchFaultStatus>> getSwitchFaultStatusToRemove(InstanceIdentifier<Node> psIid,
237 PhysicalSwitch phySwitch) {
238 Preconditions.checkNotNull(psIid);
239 Preconditions.checkNotNull(phySwitch);
240 List<InstanceIdentifier<SwitchFaultStatus>> result = new ArrayList<>();
241 PhysicalSwitch oldSwitch = oldPSRows.get(phySwitch.getUuid());
242 if (oldSwitch != null && oldSwitch.getSwitchFaultStatusColumn() != null) {
243 for (String switchFltStat : oldSwitch.getSwitchFaultStatusColumn().getData()) {
244 if (phySwitch.getSwitchFaultStatusColumn() == null
245 || !phySwitch.getSwitchFaultStatusColumn().getData().contains(switchFltStat)) {
246 InstanceIdentifier<SwitchFaultStatus> iid = psIid.augmentation(PhysicalSwitchAugmentation.class)
247 .child(SwitchFaultStatus.class, new SwitchFaultStatusKey(switchFltStat));
255 private void setSwitchFaultStatus(PhysicalSwitchAugmentationBuilder psAugmentationBuilder,
256 PhysicalSwitch phySwitch) {
257 if (phySwitch.getSwitchFaultStatusColumn() != null && phySwitch.getSwitchFaultStatusColumn().getData() != null
258 && !phySwitch.getSwitchFaultStatusColumn().getData().isEmpty()) {
259 List<SwitchFaultStatus> switchFaultStatusLst = new ArrayList<>();
260 for (String switchFaultStatus : phySwitch.getSwitchFaultStatusColumn().getData()) {
262 .add(new SwitchFaultStatusBuilder().setKey(new SwitchFaultStatusKey(switchFaultStatus))
263 .setSwitchFaultStatusKey(switchFaultStatus).build());
265 psAugmentationBuilder.setSwitchFaultStatus(switchFaultStatusLst);