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