c757f6abbde614237037d16ea1fd88e6624eb86b
[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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class UcastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
32     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
33
34     public UcastMacsRemoteRemoveCommand(HwvtepOperationalState state,
35             Collection<DataTreeModification<Node>> changes) {
36         super(state, changes);
37     }
38
39     @Override
40     public void execute(TransactionBuilder transaction) {
41         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removeds =
42                 extractRemoved(getChanges(),RemoteUcastMacs.class);
43         if (!removeds.isEmpty()) {
44             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removed:
45                 removeds.entrySet()) {
46                 removeUcastMacRemote(transaction,  removed.getKey(), removed.getValue());
47             }
48         }
49     }
50
51     private void removeUcastMacRemote(TransactionBuilder transaction,
52             InstanceIdentifier<Node> instanceIdentifier, List<RemoteUcastMacs> macList) {
53         for (RemoteUcastMacs mac: macList) {
54             LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
55             InstanceIdentifier<RemoteUcastMacs> macIid = instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class).
56                     child(RemoteUcastMacs.class, mac.getKey());
57             HwvtepDeviceInfo.DeviceData deviceData =
58                     getOperationalState().getDeviceInfo().getDeviceOperData(RemoteUcastMacs.class, macIid);
59             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
60                     UcastMacsRemote.class, null);
61             if (deviceData != null && deviceData.getUuid() != null) {
62                 //when mac entry is deleted, its referenced locators are deleted automatically.
63                 //locators in config DS is not deleted and need to be removed explicitly by user.
64                 UUID macEntryUUID = deviceData.getUuid();
65                 ucastMacsRemote.getUuidColumn().setData(macEntryUUID);
66                 transaction.add(op.delete(ucastMacsRemote.getSchema()).
67                         where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
68                 transaction.add(op.comment("UcastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
69             } else {
70                 LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
71                         mac.getMacEntryKey().getValue());
72             }
73             updateCurrentTxDeleteData(RemoteUcastMacs.class, macIid, mac);
74         }
75     }
76
77     protected List<RemoteUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
78         return augmentation.getRemoteUcastMacs();
79     }
80
81 }