d7142d01cc023d027d60106658c411ede81d7910
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsLocalRemoveCommand.java
1 /*
2  * Copyright © 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 com.google.common.base.Optional;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.UcastMacsLocal;
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.LocalUcastMacs;
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 UcastMacsLocalRemoveCommand extends AbstractTransactCommand<LocalUcastMacs, HwvtepGlobalAugmentation> {
32     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsLocalRemoveCommand.class);
33
34     public UcastMacsLocalRemoveCommand(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<LocalUcastMacs>> removeds =
42                 extractRemoved(getChanges(),LocalUcastMacs.class);
43         if (!removeds.isEmpty()) {
44             for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> removed:
45                 removeds.entrySet()) {
46                 removeUcastMacLocal(transaction,  removed.getKey(), removed.getValue());
47             }
48         }
49     }
50
51     private void removeUcastMacLocal(TransactionBuilder transaction,
52             InstanceIdentifier<Node> instanceIdentifier, List<LocalUcastMacs> macList) {
53         for (LocalUcastMacs mac: macList) {
54             LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
55             Optional<LocalUcastMacs> operationalMacOptional =
56                     getOperationalState().getLocalUcastMacs(instanceIdentifier, mac.key());
57             UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
58                     UcastMacsLocal.class, null);
59             if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
60                 //when mac entry is deleted, its referenced locators are deleted automatically.
61                 //locators in config DS is not deleted and user need to be removed explicitly by user.
62                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
63                 ucastMacsLocal.getUuidColumn().setData(macEntryUUID);
64                 transaction.add(op.delete(ucastMacsLocal.getSchema())
65                         .where(ucastMacsLocal.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
66                 transaction.add(op.comment("UcastMacLocal: Deleting " + mac.getMacEntryKey().getValue()));
67             } else {
68                 LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
69                         mac.getMacEntryKey().getValue());
70             }
71         }
72     }
73
74     @Override
75     protected List<LocalUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
76         return augmentation.getLocalUcastMacs();
77     }
78
79     @Override
80     protected boolean cascadeDelete() {
81         return true;
82     }
83
84     @Override
85     protected UnMetDependencyGetter getDependencyGetter() {
86         return MAC_DEPENDENCY_GETTER;
87     }
88
89     static UnMetDependencyGetter MAC_DEPENDENCY_GETTER = new MacDependencyGetter();
90
91     public static class MacDependencyGetter extends UnMetDependencyGetter<LocalUcastMacs> {
92         @Override
93         public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(LocalUcastMacs data) {
94             return Collections.singletonList(data.getLogicalSwitchRef().getValue());
95         }
96
97         @Override
98         public List<InstanceIdentifier<?>> getTerminationPointDependencies(LocalUcastMacs data) {
99             return Collections.emptyList();
100         }
101     }
102
103     @Override
104     protected boolean isRemoveCommand() {
105         return true;
106     }
107 }