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.collect.Sets;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
23 import org.opendaylight.ovsdb.lib.message.TableUpdates;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
26 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.Tunnel;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.SwitchesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIps;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIpsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIpsKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.SwitchFaultStatus;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.SwitchFaultStatusBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.SwitchFaultStatusKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIpsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIpsKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import com.google.common.base.Optional;
59 import com.google.common.base.Preconditions;
61 public class HwvtepPhysicalSwitchUpdateCommand extends AbstractTransactionCommand {
63 private static final Logger LOG = LoggerFactory.getLogger(HwvtepPhysicalSwitchUpdateCommand.class);
64 private Map<UUID, PhysicalSwitch> updatedPSRows;
65 private Map<UUID, Tunnel> updatedTunnelRows;
66 private Map<UUID, PhysicalSwitch> oldPSRows;
68 public HwvtepPhysicalSwitchUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
69 super(key, updates, dbSchema);
70 updatedPSRows = TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(), getDbSchema());
71 oldPSRows = TyperUtils.extractRowsOld(PhysicalSwitch.class, getUpdates(), getDbSchema());
73 updatedTunnelRows = TyperUtils.extractRowsUpdated(Tunnel.class, getUpdates(), getDbSchema());
74 } catch (IllegalArgumentException e) {
75 LOG.debug("Tunnel Table not supported on this HWVTEP device", e.getMessage());
80 public void execute(ReadWriteTransaction transaction) {
81 for (Map.Entry<UUID, PhysicalSwitch> entry : updatedPSRows.entrySet()) {
82 updatePhysicalSwitch(transaction, entry.getKey(), entry.getValue());
86 private void updatePhysicalSwitch(ReadWriteTransaction transaction, UUID uuid, PhysicalSwitch pSwitch) {
87 final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
88 Optional<Node> connection = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
89 if (connection.isPresent()) {
90 LOG.debug("Connection {} is present", connection);
91 // Update the connection node to let it know it manages this
93 Node connectionNode = buildConnectionNode(pSwitch);
94 transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
96 // Update the Physical Switch with whatever data we are getting
97 InstanceIdentifier<Node> psIid = getInstanceIdentifier(pSwitch);
98 Node psNode = buildPhysicalSwitchNode(connection.get(), pSwitch);
100 PhysicalSwitch oldPSwitch = oldPSRows.get(uuid);
101 updateTunnelIps(pSwitch, oldPSwitch, transaction);
103 getOvsdbConnectionInstance().getDeviceInfo().putPhysicalSwitch(pSwitch.getUuid(), pSwitch);
104 // TODO: Delete entries that are no longer needed
105 // TODO: Deletion of tunnels
106 // TODO: Deletion of Tunnel BFD config and params
107 //Deleting old switch fault status entries
108 deleteEntries(transaction, getSwitchFaultStatusToRemove(psIid,pSwitch));
112 private InstanceIdentifier<TunnelIps> getTunnelIpIid(final String tunnelIp, final InstanceIdentifier<Node> psIid) {
113 IpAddress ip = new IpAddress(tunnelIp.toCharArray());
114 TunnelIps tunnelIps = new TunnelIpsBuilder().setKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build();
115 return psIid.augmentation(PhysicalSwitchAugmentation.class).child(TunnelIps.class, tunnelIps.getKey());
118 private void updateTunnelIps(final PhysicalSwitch newPSwitch, final PhysicalSwitch oldPSwitch,
119 final ReadWriteTransaction transaction) {
120 Set<String> oldTunnelIps = oldPSwitch != null && oldPSwitch.getTunnelIpsColumn() != null ? oldPSwitch
121 .getTunnelIpsColumn().getData() : Collections.EMPTY_SET;
122 Set<String> newTunelIps = newPSwitch != null && newPSwitch.getTunnelIpsColumn() != null ? newPSwitch
123 .getTunnelIpsColumn().getData() : Collections.EMPTY_SET;
125 Set<String> addedTunnelIps = Sets.difference(newTunelIps, oldTunnelIps);
126 Set<String> removedTunnelIps = Sets.difference(oldTunnelIps, newTunelIps);
128 InstanceIdentifier<Node> psIid = getInstanceIdentifier(newPSwitch);
129 for (String tunnelIp : removedTunnelIps) {
130 InstanceIdentifier<TunnelIps> tunnelIpsInstanceIdentifier = getTunnelIpIid(tunnelIp, psIid);
131 transaction.delete(LogicalDatastoreType.OPERATIONAL, tunnelIpsInstanceIdentifier);
133 for (String tunnelIp : addedTunnelIps) {
134 IpAddress ip = new IpAddress(tunnelIp.toCharArray());
135 InstanceIdentifier<TunnelIps> tunnelIpsInstanceIdentifier = getTunnelIpIid(tunnelIp, psIid);
136 TunnelIps tunnelIps = new TunnelIpsBuilder().setKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build();
137 transaction.put(LogicalDatastoreType.OPERATIONAL, tunnelIpsInstanceIdentifier, tunnelIps, true);
141 private Node buildPhysicalSwitchNode(Node node, PhysicalSwitch pSwitch) {
142 NodeBuilder psNodeBuilder = new NodeBuilder();
143 NodeId psNodeId = getNodeId(pSwitch);
144 psNodeBuilder.setNodeId(psNodeId);
145 PhysicalSwitchAugmentationBuilder psAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
146 psAugmentationBuilder.setPhysicalSwitchUuid(new Uuid(pSwitch.getUuid().toString()));
147 setManagedBy(psAugmentationBuilder);
148 setPhysicalSwitchId(psAugmentationBuilder, pSwitch);
149 setManagementIps(psAugmentationBuilder, pSwitch);
150 setTunnels(psAugmentationBuilder, pSwitch);
151 setSwitchFaultStatus(psAugmentationBuilder, pSwitch);
153 psNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, psAugmentationBuilder.build());
155 LOG.trace("Built with the intent to store PhysicalSwitch data {}", psAugmentationBuilder.build());
156 return psNodeBuilder.build();
159 private void setTunnels(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch pSwitch) {
160 if (updatedTunnelRows != null && pSwitch.getTunnels() != null && pSwitch.getTunnels().getData() != null
161 && !pSwitch.getTunnels().getData().isEmpty()) {
162 // Nothing to do but update deviceInfo cache
163 for(UUID uuid: pSwitch.getTunnels().getData()) {
164 getOvsdbConnectionInstance().getDeviceInfo().putPhysicalSwitchForTunnel(uuid, pSwitch.getUuid());
169 private void setManagedBy(PhysicalSwitchAugmentationBuilder psAugmentationBuilder) {
170 InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
171 psAugmentationBuilder.setManagedBy(new HwvtepGlobalRef(connectionNodePath));
174 private void setPhysicalSwitchId(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch pSwitch) {
175 if (pSwitch.getName() != null) {
176 psAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName(pSwitch.getName()));
178 if (pSwitch.getDescription() != null) {
179 psAugmentationBuilder.setHwvtepNodeDescription(pSwitch.getDescription());
183 private void setManagementIps(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch pSwitch) {
184 if (pSwitch.getManagementIpsColumn() != null && pSwitch.getManagementIpsColumn().getData() != null
185 && !pSwitch.getManagementIpsColumn().getData().isEmpty()) {
186 List<ManagementIps> mgmtIps = new ArrayList<>();
187 for (String mgmtIp : pSwitch.getManagementIpsColumn().getData()) {
188 IpAddress ip = new IpAddress(mgmtIp.toCharArray());
190 new ManagementIpsBuilder().setKey(new ManagementIpsKey(ip)).setManagementIpsKey(ip).build());
192 psAugmentationBuilder.setManagementIps(mgmtIps);
196 private void setTunnelIps(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch pSwitch) {
197 if (pSwitch.getTunnelIpsColumn() != null && pSwitch.getTunnelIpsColumn().getData() != null
198 && !pSwitch.getTunnelIpsColumn().getData().isEmpty()) {
199 List<TunnelIps> tunnelIps = new ArrayList<>();
200 for (String tunnelIp : pSwitch.getTunnelIpsColumn().getData()) {
201 IpAddress ip = new IpAddress(tunnelIp.toCharArray());
202 tunnelIps.add(new TunnelIpsBuilder().setKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build());
204 psAugmentationBuilder.setTunnelIps(tunnelIps);
208 private Node buildConnectionNode(PhysicalSwitch pSwitch) {
209 // Update node with PhysicalSwitch reference
210 NodeBuilder connectionNode = new NodeBuilder();
211 connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
213 HwvtepGlobalAugmentationBuilder hgAugmentationBuilder = new HwvtepGlobalAugmentationBuilder();
214 List<Switches> switches = new ArrayList<>();
215 InstanceIdentifier<Node> switchIid =
216 HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), pSwitch);
217 hgAugmentationBuilder.setSwitches(switches);
218 Switches physicalSwitch = new SwitchesBuilder().setSwitchRef(new HwvtepPhysicalSwitchRef(switchIid)).build();
219 switches.add(physicalSwitch);
221 connectionNode.addAugmentation(HwvtepGlobalAugmentation.class, hgAugmentationBuilder.build());
223 LOG.debug("Update node with physicalswitch ref {}", hgAugmentationBuilder.getSwitches().iterator().next());
224 return connectionNode.build();
227 private InstanceIdentifier<Node> getInstanceIdentifier(PhysicalSwitch pSwitch) {
228 return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), pSwitch);
231 private NodeId getNodeId(PhysicalSwitch pSwitch) {
232 NodeKey nodeKey = getInstanceIdentifier(pSwitch).firstKeyOf(Node.class);
233 return nodeKey.getNodeId();
236 private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
237 List<InstanceIdentifier<T>> entryIids) {
238 for (InstanceIdentifier<T> entryIid : entryIids) {
239 transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
243 private List<InstanceIdentifier<SwitchFaultStatus>> getSwitchFaultStatusToRemove(InstanceIdentifier<Node> psIid,
244 PhysicalSwitch pSwitch) {
245 Preconditions.checkNotNull(psIid);
246 Preconditions.checkNotNull(pSwitch);
247 List<InstanceIdentifier<SwitchFaultStatus>> result = new ArrayList<>();
248 PhysicalSwitch oldSwitch = oldPSRows.get(pSwitch.getUuid());
249 if (oldSwitch != null && oldSwitch.getSwitchFaultStatusColumn() != null) {
250 for (String switchFltStat : oldSwitch.getSwitchFaultStatusColumn().getData()) {
251 if (pSwitch.getSwitchFaultStatusColumn() == null
252 || !pSwitch.getSwitchFaultStatusColumn().getData().contains(switchFltStat)) {
253 InstanceIdentifier<SwitchFaultStatus> iid = psIid.augmentation(PhysicalSwitchAugmentation.class)
254 .child(SwitchFaultStatus.class, new SwitchFaultStatusKey(switchFltStat));
262 private void setSwitchFaultStatus(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch pSwitch) {
263 if (pSwitch.getSwitchFaultStatusColumn() != null && pSwitch.getSwitchFaultStatusColumn().getData() != null
264 && !pSwitch.getSwitchFaultStatusColumn().getData().isEmpty()) {
265 List<SwitchFaultStatus> switchFaultStatusLst = new ArrayList<>();
266 for (String switchFaultStatus : pSwitch.getSwitchFaultStatusColumn().getData()) {
268 .add(new SwitchFaultStatusBuilder().setKey(new SwitchFaultStatusKey(switchFaultStatus))
269 .setSwitchFaultStatusKey(switchFaultStatus).build());
271 psAugmentationBuilder.setSwitchFaultStatus(switchFaultStatusLst);