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