Merge "Do not install or deploy the karaf artifact"
[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.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
19
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.base.Optional;
35
36 public class McastMacsLocalRemoveCommand extends AbstractTransactCommand {
37     private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalRemoveCommand.class);
38
39     public McastMacsLocalRemoveCommand(HwvtepOperationalState state,
40             Collection<DataTreeModification<Node>> changes) {
41         super(state, changes);
42     }
43
44     @Override
45     public void execute(TransactionBuilder transaction) {
46         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> removeds =
47                 extractRemoved(getChanges(),LocalMcastMacs.class);
48         if (!removeds.isEmpty()) {
49             for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> removed:
50                 removeds.entrySet()) {
51                 removeMcastMacLocal(transaction,  removed.getKey(), removed.getValue());
52             }
53         }
54     }
55
56     private void removeMcastMacLocal(TransactionBuilder transaction,
57             InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> macList) {
58         for (LocalMcastMacs mac: macList) {
59             LOG.debug("Removing localMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
60             Optional<LocalMcastMacs> operationalMacOptional =
61                     getOperationalState().getLocalMcastMacs(instanceIdentifier, mac.getKey());
62             McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
63                     McastMacsLocal.class, null);
64             if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
65                 //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
66                 //TODO: locator in config DS is not deleted
67                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
68                 mcastMacsLocal.getUuidColumn().setData(macEntryUUID);
69                 transaction.add(op.delete(mcastMacsLocal.getSchema()).
70                         where(mcastMacsLocal.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
71                 transaction.add(op.comment("McastMacLocal: Deleting " + mac.getMacEntryKey().getValue()));
72             } else {
73                 LOG.warn("Unable to delete localMcastMacs {} because it was not found in the operational store",
74                         mac.getMacEntryKey().getValue());
75             }
76         }
77     }
78
79     private Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> extractRemoved(
80             Collection<DataTreeModification<Node>> changes, Class<LocalMcastMacs> class1) {
81         Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> result
82             = new HashMap<InstanceIdentifier<Node>, List<LocalMcastMacs>>();
83         if (changes != null && !changes.isEmpty()) {
84             for (DataTreeModification<Node> change : changes) {
85                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
86                 final DataObjectModification<Node> mod = change.getRootNode();
87                 //If the node which localMcastMacs belong to is removed, all localMcastMacs should be removed too.
88                 Node removed = TransactUtils.getRemoved(mod);
89                 if (removed != null) {
90                     List<LocalMcastMacs> macListRemoved = null;
91                     if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
92                         macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getLocalMcastMacs();
93                     }
94                     if (macListRemoved != null) {
95                         result.put(key, macListRemoved);
96                     }
97                 }
98                 //If the node which localMcastMacs belong to is updated, and localMcastMacs may
99                 //be created or updated or deleted, we need to get deleted ones.
100                 Node updated = TransactUtils.getUpdated(mod);
101                 Node before = mod.getDataBefore();
102                 if (updated != null && before != null) {
103                     List<LocalMcastMacs> macListUpdated = null;
104                     List<LocalMcastMacs> macListBefore = null;
105                     HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
106                     if (hgUpdated != null) {
107                         macListUpdated = hgUpdated.getLocalMcastMacs();
108                     }
109                     HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
110                     if (hgBefore != null) {
111                         macListBefore = hgBefore.getLocalMcastMacs();
112                     }
113                     if (macListBefore != null) {
114                         List<LocalMcastMacs> macListRemoved = new ArrayList<LocalMcastMacs>();
115                         if (macListUpdated != null) {
116                             macListBefore.removeAll(macListUpdated);
117                         }
118                         //then exclude updated localMcastMacs
119                         for (LocalMcastMacs macBefore: macListBefore) {
120                             int i = 0;
121                             for(; i < macListUpdated.size(); i++) {
122                                 if (macBefore.getMacEntryKey().equals(macListUpdated.get(i).getMacEntryKey())) {
123                                     break;
124                                 }
125                             }
126                             if (i == macListUpdated.size()) {
127                                 macListRemoved.add(macBefore);
128                             }
129                         }
130                         if (!macListRemoved.isEmpty()) {
131                             result.put(key, macListRemoved);
132                         }
133                     }
134                 }
135             }
136         }
137         return result;
138     }
139 }