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