b6fb026a93e0258b388c794c4b64e5343ce7242b
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteRemoveCommand.java
1 /*
2  * Copyright (c) 2015, 2017 China Telecom Beijing Research Institute 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.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import com.google.common.collect.Lists;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Objects;
18
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
25 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class UcastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
35     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
36
37     public UcastMacsRemoteRemoveCommand(final HwvtepOperationalState state,
38             final Collection<DataTreeModification<Node>> changes) {
39         super(state, changes);
40     }
41
42     @Override
43     public void execute(final TransactionBuilder transaction) {
44         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removeds =
45                 extractRemoved(getChanges(),RemoteUcastMacs.class);
46         if (!removeds.isEmpty()) {
47             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removed:
48                     removeds.entrySet()) {
49                 onConfigUpdate(transaction, removed.getKey(), removed.getValue());
50             }
51         }
52     }
53
54     public void onConfigUpdate(TransactionBuilder transaction,
55                                InstanceIdentifier<Node> nodeIid,
56                                List<RemoteUcastMacs> macs) {
57         for (RemoteUcastMacs mac : macs) {
58             InstanceIdentifier<RemoteUcastMacs> macKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
59                     .child(RemoteUcastMacs.class, mac.key());
60             getDeviceInfo().clearConfigData(RemoteUcastMacs.class, macKey);
61             onConfigUpdate(transaction, nodeIid, mac, macKey);
62         }
63     }
64
65     @Override
66     public void onConfigUpdate(TransactionBuilder transaction,
67                                InstanceIdentifier<Node> nodeIid,
68                                RemoteUcastMacs remoteMcastMac,
69                                InstanceIdentifier macKey,
70                                Object... extraData) {
71         processDependencies(EmptyDependencyGetter.INSTANCE, transaction, nodeIid, macKey, remoteMcastMac);
72     }
73
74     @Override
75     public void doDeviceTransaction(TransactionBuilder transaction,
76                                     InstanceIdentifier<Node> instanceIdentifier,
77                                     RemoteUcastMacs mac,
78                                     InstanceIdentifier macKey,
79                                     Object... extraData) {
80         removeUcastMacRemote(transaction, instanceIdentifier, Lists.newArrayList(mac));
81     }
82
83     private void removeUcastMacRemote(final TransactionBuilder transaction,
84                                       final InstanceIdentifier<Node> instanceIdentifier,
85                                       final List<RemoteUcastMacs> macList) {
86         for (RemoteUcastMacs mac: macList) {
87             final InstanceIdentifier<RemoteUcastMacs> macIid =
88                     instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class)
89                             .child(RemoteUcastMacs.class, mac.key());
90             HwvtepDeviceInfo.DeviceData deviceData = getDeviceOpData(RemoteUcastMacs.class, macIid);
91             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
92                     UcastMacsRemote.class, null);
93             LOG.debug("Remove received for remoteUcastMacs, key: {} txId: {}", macIid,
94                     getOperationalState().getTransactionId());
95             boolean deleted = false;
96             if (deviceData != null && deviceData.getUuid() != null) {
97                 if (deviceData.getData() != null && deviceData.getData() instanceof UcastMacsRemote
98                         && ((UcastMacsRemote)deviceData.getData()).getLogicalSwitchColumn() != null) {
99                     UUID logicalSwitchUid = ((UcastMacsRemote) deviceData.getData()).getLogicalSwitchColumn().getData();
100                     if (logicalSwitchUid != null) {
101                         deleted = true;
102                         updateCurrentTxDeleteData(RemoteUcastMacs.class, macIid, mac);
103                         transaction.add(op.delete(ucastMacsRemote.getSchema())
104                                 .where(ucastMacsRemote.getLogicalSwitchColumn().getSchema()
105                                         .opEqual(logicalSwitchUid))
106                                 .and(ucastMacsRemote.getMacColumn().getSchema().opEqual(mac.getMacEntryKey()
107                                         .getValue())).build());
108                         LOG.info("CONTROLLER - {} {}", TransactionType.DELETE, ucastMacsRemote);
109                     }
110                 }
111             }
112             if (!deleted && deviceData != null && deviceData.getUuid() != null) {
113                 updateCurrentTxDeleteData(RemoteUcastMacs.class, macIid, mac);
114                 UUID macEntryUUID = deviceData.getUuid();
115                 ucastMacsRemote.getUuidColumn().setData(macEntryUUID);
116                 transaction.add(op.delete(ucastMacsRemote.getSchema())
117                         .where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
118                 LOG.info("CONTROLLER - {} {}", TransactionType.DELETE, ucastMacsRemote);
119             } else {
120                 LOG.trace("Remove failed to find in op datastore key:{} txId:{}", macIid, getOperationalState()
121                         .getTransactionId());
122             }
123             getDeviceInfo().clearConfigData(RemoteUcastMacs.class, macIid);
124         }
125     }
126
127     @Override
128     protected List<RemoteUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
129         return augmentation.getRemoteUcastMacs();
130     }
131
132     @Override
133     protected boolean areEqual(RemoteUcastMacs remoteUcastMacs1, RemoteUcastMacs remoteUcastMacs2) {
134         return Objects.equals(remoteUcastMacs1.key(), remoteUcastMacs2.key());
135     }
136
137     @Override
138     public void onSuccess(TransactionBuilder tx) {
139         for (MdsalUpdate mdsalUpdate : updates) {
140             RemoteUcastMacs mac = (RemoteUcastMacs) mdsalUpdate.getNewData();
141             InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
142             getDeviceInfo().removeRemoteUcast(
143                     (InstanceIdentifier<LogicalSwitches>) mac.getLogicalSwitchRef().getValue(), macIid);
144         }
145         getDeviceInfo().onOperDataAvailable();
146     }
147
148     @Override
149     protected boolean isDeleteCmd() {
150         return true;
151     }
152
153 }