Merge "Bug 5174: Support for AutoAttach Table in OVSDB"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / UcastMacsRemoteRemoveCommand.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.UcastMacsRemote;
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.RemoteUcastMacs;
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 UcastMacsRemoteRemoveCommand extends AbstractTransactCommand {
36     private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
37
38     public UcastMacsRemoteRemoveCommand(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<RemoteUcastMacs>> removeds =
46                 extractRemoved(getChanges(),RemoteUcastMacs.class);
47         if (!removeds.isEmpty()) {
48             for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> removed:
49                 removeds.entrySet()) {
50                 removeUcastMacRemote(transaction,  removed.getKey(), removed.getValue());
51             }
52         }
53     }
54
55     private void removeUcastMacRemote(TransactionBuilder transaction,
56             InstanceIdentifier<Node> instanceIdentifier, List<RemoteUcastMacs> macList) {
57         for (RemoteUcastMacs mac: macList) {
58             LOG.debug("Removing remoteUcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
59             Optional<RemoteUcastMacs> operationalMacOptional =
60                     getOperationalState().getRemoteUcastMacs(instanceIdentifier, mac.getKey());
61             UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
62                     UcastMacsRemote.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 need to be removed explicitly by user.
66                 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
67                 transaction.add(op.delete(ucastMacsRemote.getSchema()).
68                         where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
69                 transaction.add(op.comment("UcastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
70             } else {
71                 LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
72                         mac.getMacEntryKey().getValue());
73             }
74         }
75     }
76
77     private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractRemoved(
78             Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
79         Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
80             = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
81         if (changes != null && !changes.isEmpty()) {
82             for (DataTreeModification<Node> change : changes) {
83                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
84                 final DataObjectModification<Node> mod = change.getRootNode();
85                 //If the node which remoteUcastMacs belong to is removed, all remoteUcastMacs should be removed too.
86                 Node removed = TransactUtils.getRemoved(mod);
87                 if (removed != null) {
88                     List<RemoteUcastMacs> macListRemoved = null;
89                     if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
90                         macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteUcastMacs();
91                     }
92                     if (macListRemoved != null) {
93                         result.put(key, macListRemoved);
94                     }
95                 }
96                 //If the node which remoteUcastMacs belong to is updated, and remoteUcastMacs may
97                 //be created or updated or deleted, we need to get deleted ones.
98                 Node updated = TransactUtils.getUpdated(mod);
99                 Node before = mod.getDataBefore();
100                 if (updated != null && before != null) {
101                     List<RemoteUcastMacs> macListUpdated = null;
102                     List<RemoteUcastMacs> macListBefore = null;
103                     HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
104                     if (hgUpdated != null) {
105                         macListUpdated = hgUpdated.getRemoteUcastMacs();
106                     }
107                     HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
108                     if (hgBefore != null) {
109                         macListBefore = hgBefore.getRemoteUcastMacs();
110                     }
111                     if (macListBefore != null) {
112                         List<RemoteUcastMacs> macListRemoved = new ArrayList<RemoteUcastMacs>();
113                         if (macListUpdated != null) {
114                             macListBefore.removeAll(macListUpdated);
115                         }
116                         //then exclude updated remoteUcastMacs
117                         for (RemoteUcastMacs macBefore: macListBefore) {
118                             int i = 0;
119                             for(; i < macListUpdated.size(); i++) {
120                                 if (macBefore.getMacEntryKey().equals(macListUpdated.get(i).getMacEntryKey())) {
121                                     break;
122                                 }
123                             }
124                             if (i == macListUpdated.size()) {
125                                 macListRemoved.add(macBefore);
126                             }
127                         }
128                         if (!macListRemoved.isEmpty()) {
129                             result.put(key, macListRemoved);
130                         }
131                     }
132                 }
133             }
134         }
135         return result;
136     }
137 }