bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsLocalRemoveCommand.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.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18
19 import com.google.common.collect.Lists;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.McastMacsLocal;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
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 import com.google.common.base.Optional;
33
34 public class McastMacsLocalRemoveCommand extends AbstractTransactCommand<LocalMcastMacs, HwvtepGlobalAugmentation> {
35     private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalRemoveCommand.class);
36
37     public McastMacsLocalRemoveCommand(HwvtepOperationalState state,
38             Collection<DataTreeModification<Node>> changes) {
39         super(state, changes);
40     }
41
42     @Override
43     public void execute(TransactionBuilder transaction) {
44         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> removeds =
45                 extractRemoved(getChanges(),LocalMcastMacs.class);
46         if (!removeds.isEmpty()) {
47             for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> removed:
48                 removeds.entrySet()) {
49                 removeMcastMacLocal(transaction,  removed.getKey(), removed.getValue());
50             }
51         }
52     }
53
54     private void removeMcastMacLocal(TransactionBuilder transaction,
55             InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> macList) {
56         for (LocalMcastMacs mac: macList) {
57             LOG.debug("Removing localMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
58             Optional<LocalMcastMacs> operationalMacOptional =
59                     getOperationalState().getLocalMcastMacs(instanceIdentifier, mac.getKey());
60             McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
61                     McastMacsLocal.class, null);
62             if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
63                 //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
64                 //TODO: locator in config DS is not deleted
65                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
66                 mcastMacsLocal.getUuidColumn().setData(macEntryUUID);
67                 transaction.add(op.delete(mcastMacsLocal.getSchema()).
68                         where(mcastMacsLocal.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
69                 transaction.add(op.comment("McastMacLocal: Deleting " + mac.getMacEntryKey().getValue()));
70             } else {
71                 LOG.warn("Unable to delete localMcastMacs {} because it was not found in the operational store",
72                         mac.getMacEntryKey().getValue());
73             }
74         }
75     }
76
77     @Override
78     protected List<LocalMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
79         return augmentation.getLocalMcastMacs();
80     }
81
82     @Override
83     protected boolean cascadeDelete() {
84         return true;
85     }
86
87     @Override
88     protected UnMetDependencyGetter getDependencyGetter() {
89         return MAC_DEPENDENCY_GETTER;
90     }
91
92     static UnMetDependencyGetter MAC_DEPENDENCY_GETTER = new MacDependencyGetter();
93
94     public static class MacDependencyGetter extends UnMetDependencyGetter<LocalMcastMacs> {
95         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(LocalMcastMacs data) {
96             return Lists.newArrayList(data.getLogicalSwitchRef().getValue());
97         }
98
99         @Override
100         public List<InstanceIdentifier<?>> getTerminationPointDependencies(LocalMcastMacs data) {
101             return Collections.EMPTY_LIST;
102         }
103     }
104 }