Update MRI projects for Aluminium
[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 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 import java.util.Optional;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
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.LocalUcastMacs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
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
32         extends AbstractTransactCommand<LocalUcastMacs, LocalUcastMacsKey, HwvtepGlobalAugmentation> {
33     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsLocalRemoveCommand.class);
34
35     public UcastMacsLocalRemoveCommand(final HwvtepOperationalState state,
36             final Collection<DataTreeModification<Node>> changes) {
37         super(state, changes);
38     }
39
40     @Override
41     public void execute(final TransactionBuilder transaction) {
42         Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> removeds =
43                 extractRemoved(getChanges(),LocalUcastMacs.class);
44         if (!removeds.isEmpty()) {
45             for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> removed:
46                 removeds.entrySet()) {
47                 removeUcastMacLocal(transaction,  removed.getKey(), removed.getValue());
48             }
49         }
50     }
51
52     private void removeUcastMacLocal(final TransactionBuilder transaction,
53             final InstanceIdentifier<Node> instanceIdentifier, final List<LocalUcastMacs> macList) {
54         for (LocalUcastMacs mac: macList) {
55             LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
56             Optional<LocalUcastMacs> operationalMacOptional =
57                     getOperationalState().getLocalUcastMacs(instanceIdentifier, mac.key());
58             UcastMacsLocal ucastMacsLocal = transaction.getTypedRowSchema(UcastMacsLocal.class);
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 Map<LocalUcastMacsKey, LocalUcastMacs> getData(final 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(final LocalUcastMacs data) {
94             return Collections.singletonList(data.getLogicalSwitchRef().getValue());
95         }
96
97         @Override
98         public List<InstanceIdentifier<?>> getTerminationPointDependencies(final LocalUcastMacs data) {
99             return Collections.emptyList();
100         }
101     }
102
103     @Override
104     protected boolean isDeleteCmd() {
105         return true;
106     }
107 }