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