Make methods static
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepPhysicalSwitchUpdateCommand.java
1 /*
2  * Copyright (c) 2015, 2017 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 package org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Sets;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.Set;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
25 import org.opendaylight.ovsdb.lib.message.TableUpdates;
26 import org.opendaylight.ovsdb.lib.notation.UUID;
27 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
28 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
29 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
30 import org.opendaylight.ovsdb.schema.hardwarevtep.Tunnel;
31 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
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.inet.types.rev130715.IpAddressBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
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;
60
61 public class HwvtepPhysicalSwitchUpdateCommand extends AbstractTransactionCommand {
62
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;
67
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());
73         try {
74             updatedTunnelRows = TyperUtils.extractRowsUpdated(Tunnel.class, getUpdates(), getDbSchema());
75         } catch (IllegalArgumentException e) {
76             LOG.debug("Tunnel Table not supported on this HWVTEP device", e);
77         }
78     }
79
80     @Override
81     public void execute(ReadWriteTransaction transaction) {
82         for (Map.Entry<UUID, PhysicalSwitch> entry : updatedPSRows.entrySet()) {
83             updatePhysicalSwitch(transaction, entry.getKey(), entry.getValue());
84         }
85     }
86
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
94             // Physical Switch
95             Node connectionNode = buildConnectionNode(phySwitch);
96             transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
97
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);
102             addToDeviceUpdate(TransactionType.ADD, phySwitch);
103             LOG.info("DEVICE - {} {}", TransactionType.ADD, phySwitch);
104
105             PhysicalSwitch oldPSwitch = oldPSRows.get(uuid);
106             updateTunnelIps(phySwitch, oldPSwitch, transaction);
107
108             getOvsdbConnectionInstance().getDeviceInfo().putPhysicalSwitch(phySwitch.getUuid(), phySwitch);
109             getDeviceInfo().updateDeviceOperData(Node.class, psIid, phySwitch.getUuid(), phySwitch);
110             // TODO: Delete entries that are no longer needed
111             // TODO: Deletion of tunnels
112             // TODO: Deletion of Tunnel BFD config and params
113             //Deleting old switch fault status entries
114             deleteEntries(transaction, getSwitchFaultStatusToRemove(psIid,phySwitch));
115         }
116     }
117
118     private static InstanceIdentifier<TunnelIps> getTunnelIpIid(final String tunnelIp,
119             final InstanceIdentifier<Node> psIid) {
120         IpAddress ip = IpAddressBuilder.getDefaultInstance(tunnelIp);
121         TunnelIps tunnelIps = new TunnelIpsBuilder().withKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build();
122         return psIid.augmentation(PhysicalSwitchAugmentation.class).child(TunnelIps.class, tunnelIps.key());
123     }
124
125     private void updateTunnelIps(@NonNull final PhysicalSwitch newPSwitch, @Nullable final PhysicalSwitch oldPSwitch,
126                                  final ReadWriteTransaction transaction) {
127         Set<String> oldTunnelIps = oldPSwitch != null && oldPSwitch.getTunnelIpsColumn() != null ? oldPSwitch
128                 .getTunnelIpsColumn().getData() : Collections.emptySet();
129         Set<String> newTunelIps = newPSwitch.getTunnelIpsColumn() != null ? newPSwitch
130                 .getTunnelIpsColumn().getData() : Collections.emptySet();
131
132         Set<String> addedTunnelIps = Sets.difference(newTunelIps, oldTunnelIps);
133         Set<String> removedTunnelIps = Sets.difference(oldTunnelIps, newTunelIps);
134
135         InstanceIdentifier<Node> psIid = getInstanceIdentifier(newPSwitch);
136         for (String tunnelIp : removedTunnelIps) {
137             InstanceIdentifier<TunnelIps> tunnelIpsInstanceIdentifier = getTunnelIpIid(tunnelIp, psIid);
138             transaction.delete(LogicalDatastoreType.OPERATIONAL, tunnelIpsInstanceIdentifier);
139         }
140         for (String tunnelIp : addedTunnelIps) {
141             IpAddress ip = IpAddressBuilder.getDefaultInstance(tunnelIp);
142             InstanceIdentifier<TunnelIps> tunnelIpsInstanceIdentifier = getTunnelIpIid(tunnelIp, psIid);
143             TunnelIps tunnelIps = new TunnelIpsBuilder().withKey(new TunnelIpsKey(ip)).setTunnelIpsKey(ip).build();
144             transaction.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, tunnelIpsInstanceIdentifier,
145                 tunnelIps);
146         }
147     }
148
149     private Node buildPhysicalSwitchNode(Node node, PhysicalSwitch phySwitch) {
150         NodeBuilder psNodeBuilder = new NodeBuilder();
151         NodeId psNodeId = getNodeId(phySwitch);
152         psNodeBuilder.setNodeId(psNodeId);
153         PhysicalSwitchAugmentationBuilder psAugmentationBuilder = new PhysicalSwitchAugmentationBuilder();
154         psAugmentationBuilder.setPhysicalSwitchUuid(new Uuid(phySwitch.getUuid().toString()));
155         setManagedBy(psAugmentationBuilder);
156         setPhysicalSwitchId(psAugmentationBuilder, phySwitch);
157         setManagementIps(psAugmentationBuilder, phySwitch);
158         setTunnels(psAugmentationBuilder, phySwitch);
159         setSwitchFaultStatus(psAugmentationBuilder, phySwitch);
160
161         psNodeBuilder.addAugmentation(psAugmentationBuilder.build());
162
163         LOG.trace("Built with the intent to store PhysicalSwitch data {}", psAugmentationBuilder.build());
164         return psNodeBuilder.build();
165     }
166
167     private void setTunnels(PhysicalSwitchAugmentationBuilder psAugmentationBuilder, PhysicalSwitch phySwitch) {
168         if (updatedTunnelRows != null && phySwitch.getTunnels() != null && phySwitch.getTunnels().getData() != null
169                 && !phySwitch.getTunnels().getData().isEmpty()) {
170             // Nothing to do but update deviceInfo cache
171             for (UUID uuid: phySwitch.getTunnels().getData()) {
172                 getOvsdbConnectionInstance().getDeviceInfo().putPhysicalSwitchForTunnel(uuid, phySwitch.getUuid());
173             }
174         }
175     }
176
177     private void setManagedBy(PhysicalSwitchAugmentationBuilder psAugmentationBuilder) {
178         InstanceIdentifier<Node> connectionNodePath = getOvsdbConnectionInstance().getInstanceIdentifier();
179         psAugmentationBuilder.setManagedBy(new HwvtepGlobalRef(connectionNodePath));
180     }
181
182     private static void setPhysicalSwitchId(PhysicalSwitchAugmentationBuilder psAugmentationBuilder,
183             PhysicalSwitch phySwitch) {
184         if (phySwitch.getName() != null) {
185             psAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName(phySwitch.getName()));
186         }
187         if (phySwitch.getDescription() != null) {
188             psAugmentationBuilder.setHwvtepNodeDescription(phySwitch.getDescription());
189         }
190     }
191
192     private static void setManagementIps(PhysicalSwitchAugmentationBuilder psAugmentationBuilder,
193             PhysicalSwitch phySwitch) {
194         if (phySwitch.getManagementIpsColumn() != null && phySwitch.getManagementIpsColumn().getData() != null
195                 && !phySwitch.getManagementIpsColumn().getData().isEmpty()) {
196             List<ManagementIps> mgmtIps = new ArrayList<>();
197             for (String mgmtIp : phySwitch.getManagementIpsColumn().getData()) {
198                 IpAddress ip = IpAddressBuilder.getDefaultInstance(mgmtIp);
199                 mgmtIps.add(
200                         new ManagementIpsBuilder().withKey(new ManagementIpsKey(ip)).setManagementIpsKey(ip).build());
201             }
202             psAugmentationBuilder.setManagementIps(mgmtIps);
203         }
204     }
205
206     private Node buildConnectionNode(PhysicalSwitch phySwitch) {
207         // Update node with PhysicalSwitch reference
208         NodeBuilder connectionNode = new NodeBuilder();
209         connectionNode.setNodeId(getOvsdbConnectionInstance().getNodeId());
210
211         InstanceIdentifier<Node> switchIid =
212                 HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), phySwitch);
213         Switches physicalSwitch = new SwitchesBuilder().setSwitchRef(new HwvtepPhysicalSwitchRef(switchIid)).build();
214
215         connectionNode.addAugmentation(new HwvtepGlobalAugmentationBuilder()
216             .setSwitches(Map.of(physicalSwitch.key(), physicalSwitch))
217             .build());
218
219         LOG.debug("Update node with physicalswitch ref {}", physicalSwitch);
220         return connectionNode.build();
221     }
222
223     private InstanceIdentifier<Node> getInstanceIdentifier(PhysicalSwitch phySwitch) {
224         return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), phySwitch);
225     }
226
227     private NodeId getNodeId(PhysicalSwitch phySwitch) {
228         NodeKey nodeKey = getInstanceIdentifier(phySwitch).firstKeyOf(Node.class);
229         return nodeKey.getNodeId();
230     }
231
232     private static <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
233             List<InstanceIdentifier<T>> entryIids) {
234         for (InstanceIdentifier<T> entryIid : entryIids) {
235             transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
236         }
237     }
238
239     private List<InstanceIdentifier<SwitchFaultStatus>> getSwitchFaultStatusToRemove(InstanceIdentifier<Node> psIid,
240             PhysicalSwitch phySwitch) {
241         Preconditions.checkNotNull(psIid);
242         Preconditions.checkNotNull(phySwitch);
243         List<InstanceIdentifier<SwitchFaultStatus>> result = new ArrayList<>();
244         PhysicalSwitch oldSwitch = oldPSRows.get(phySwitch.getUuid());
245         if (oldSwitch != null && oldSwitch.getSwitchFaultStatusColumn() != null) {
246             for (String switchFltStat : oldSwitch.getSwitchFaultStatusColumn().getData()) {
247                 if (phySwitch.getSwitchFaultStatusColumn() == null
248                         || !phySwitch.getSwitchFaultStatusColumn().getData().contains(switchFltStat)) {
249                     InstanceIdentifier<SwitchFaultStatus> iid = psIid.augmentation(PhysicalSwitchAugmentation.class)
250                             .child(SwitchFaultStatus.class, new SwitchFaultStatusKey(switchFltStat));
251                     result.add(iid);
252                 }
253             }
254         }
255         return result;
256     }
257
258     private static void setSwitchFaultStatus(PhysicalSwitchAugmentationBuilder psAugmentationBuilder,
259             PhysicalSwitch phySwitch) {
260         if (phySwitch.getSwitchFaultStatusColumn() != null && phySwitch.getSwitchFaultStatusColumn().getData() != null
261                 && !phySwitch.getSwitchFaultStatusColumn().getData().isEmpty()) {
262             List<SwitchFaultStatus> switchFaultStatusLst = new ArrayList<>();
263             for (String switchFaultStatus : phySwitch.getSwitchFaultStatusColumn().getData()) {
264                 switchFaultStatusLst
265                         .add(new SwitchFaultStatusBuilder().withKey(new SwitchFaultStatusKey(switchFaultStatus))
266                                 .setSwitchFaultStatusKey(switchFaultStatus).build());
267             }
268             psAugmentationBuilder.setSwitchFaultStatus(switchFaultStatusLst);
269         }
270     }
271 }