Update MRI projects for Aluminium
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbAutoAttachRemovedCommand.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.southbound.transactions.md;
10
11 import java.util.Map;
12 import java.util.Optional;
13 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.ovsdb.lib.message.TableUpdates;
16 import org.opendaylight.ovsdb.lib.notation.UUID;
17 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
18 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
19 import org.opendaylight.ovsdb.schema.openvswitch.AutoAttach;
20 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
21 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
22 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Autoattach;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.AutoattachKey;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class OvsdbAutoAttachRemovedCommand extends AbstractTransactionCommand {
33     private static final Logger LOG = LoggerFactory.getLogger(OvsdbAutoAttachRemovedCommand.class);
34
35     private final Map<UUID, AutoAttach> removedAutoAttachRows;
36
37     public OvsdbAutoAttachRemovedCommand(OvsdbConnectionInstance key,
38             TableUpdates updates, DatabaseSchema dbSchema) {
39         super(key, updates, dbSchema);
40         removedAutoAttachRows = TyperUtils.extractRowsRemoved(AutoAttach.class, getUpdates(), getDbSchema());
41     }
42
43     @Override
44     public void execute(ReadWriteTransaction transaction) {
45         if (removedAutoAttachRows == null || removedAutoAttachRows.isEmpty()) {
46             return;
47         }
48
49         final InstanceIdentifier<Node> nodeIId = getOvsdbConnectionInstance().getInstanceIdentifier();
50         final Optional<Node> ovsdbNode = SouthboundUtil.readNode(transaction, nodeIId);
51         if (ovsdbNode.isPresent()) {
52             final InstanceIdentifier<Node> ovsdbNodeIid =
53                     SouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance().getNodeId());
54             // FIXME: Iterate on external_ids instead of uuid when Open vSwitch supports external_ids column
55             for (final UUID autoAttachUuid : removedAutoAttachRows.keySet()) {
56                 final AutoattachKey autoAttachKey = getAutoAttachKeyToRemove(ovsdbNode.get(), autoAttachUuid);
57                 if (autoAttachKey != null) {
58                     final InstanceIdentifier<Autoattach> iid = ovsdbNodeIid
59                             .augmentation(OvsdbNodeAugmentation.class)
60                             .child(Autoattach.class, autoAttachKey);
61                     transaction.delete(LogicalDatastoreType.OPERATIONAL, iid);
62                     LOG.debug("AutoAttach table {} for Ovsdb Node {} is deleted", autoAttachUuid,
63                             ovsdbNode.get().getNodeId());
64                 } else {
65                     LOG.warn("AutoAttach table {} not found for Ovsdb Node {} to delete", autoAttachUuid,
66                             ovsdbNode.get().getNodeId());
67                 }
68             }
69         }
70     }
71
72     private AutoattachKey getAutoAttachKeyToRemove(Node node, UUID autoAttachUuid) {
73         final Map<AutoattachKey, Autoattach> autoAttachList =
74                 node.augmentation(OvsdbNodeAugmentation.class).getAutoattach();
75         if (autoAttachList == null || autoAttachList.isEmpty()) {
76             return null;
77         }
78         for (final Autoattach autoAttach : autoAttachList.values()) {
79             if (autoAttach.getAutoattachUuid().equals(new Uuid(autoAttachUuid.toString()))) {
80                 return autoAttach.key();
81             }
82         }
83         return null;
84     }
85 }