Change logical-switch-ref to iid in hwvtep.yang
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / McastMacsRemoteRemoveCommand.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.hwvtepsouthbound.HwvtepSouthboundConstants;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
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.RemoteMcastMacs;
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 McastMacsRemoteRemoveCommand extends AbstractTransactCommand {
36     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
37
38     public McastMacsRemoteRemoveCommand(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<RemoteMcastMacs>> removeds =
46                 extractRemoved(getChanges(),RemoteMcastMacs.class);
47         if (!removeds.isEmpty()) {
48             for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> removed:
49                 removeds.entrySet()) {
50                 removeMcastMacRemote(transaction,  removed.getKey(), removed.getValue());
51             }
52         }
53     }
54
55     private void removeMcastMacRemote(TransactionBuilder transaction,
56             InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
57         for (RemoteMcastMacs mac: macList) {
58             LOG.debug("Removing remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
59             Optional<RemoteMcastMacs> operationalMacOptional =
60                     getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
61             McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
62                     McastMacsRemote.class, null);
63             if (operationalMacOptional.isPresent()) {
64                 //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
65                 //TODO: locator in config DS is not deleted
66                 String macString = null;
67                 if (mac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
68                     macString = HwvtepSouthboundConstants.UNKNOWN_DST_STRING;
69                 } else {
70                     macString = mac.getMacEntryKey().getValue();
71                 }
72                 transaction.add(op.delete(mcastMacsRemote.getSchema())
73                         .where(mcastMacsRemote.getMacColumn().getSchema().opEqual(macString)).build());
74                 transaction.add(op.comment("Remote McastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
75             } else {
76                 LOG.warn("Unable to delete remoteMcastMacs {} because it was not found in the operational store",
77                         mac.getMacEntryKey().getValue());
78             }
79         }
80     }
81
82     private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractRemoved(
83             Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
84         Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
85             = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
86         if (changes != null && !changes.isEmpty()) {
87             for (DataTreeModification<Node> change : changes) {
88                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
89                 final DataObjectModification<Node> mod = change.getRootNode();
90                 //If the node which remoteMcastMacs belong to is removed, all remoteMcastMacs should be removed too.
91                 Node removed = TransactUtils.getRemoved(mod);
92                 if (removed != null) {
93                     List<RemoteMcastMacs> macListRemoved = null;
94                     if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
95                         macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteMcastMacs();
96                     }
97                     if (macListRemoved != null) {
98                         result.put(key, macListRemoved);
99                     }
100                 }
101                 //If the node which remoteMcastMacs belong to is updated, and remoteMcastMacs may
102                 //be created or updated or deleted, we need to get deleted ones.
103                 Node updated = TransactUtils.getUpdated(mod);
104                 Node before = mod.getDataBefore();
105                 if (updated != null && before != null) {
106                     List<RemoteMcastMacs> macListUpdated = null;
107                     List<RemoteMcastMacs> macListBefore = null;
108                     HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
109                     if (hgUpdated != null) {
110                         macListUpdated = hgUpdated.getRemoteMcastMacs();
111                     }
112                     HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
113                     if (hgBefore != null) {
114                         macListBefore = hgBefore.getRemoteMcastMacs();
115                     }
116                     if (macListBefore != null) {
117                         List<RemoteMcastMacs> macListRemoved = new ArrayList<RemoteMcastMacs>();
118                         if (macListUpdated != null) {
119                             macListBefore.removeAll(macListUpdated);
120                         }
121                         //then exclude updated remoteMcastMacs
122                         for (RemoteMcastMacs macBefore: macListBefore) {
123                             int i = 0;
124                             for(; i < macListUpdated.size(); i++) {
125                                 if (macBefore.getMacEntryKey().equals(macListUpdated.get(i).getMacEntryKey())) {
126                                     break;
127                                 }
128                             }
129                             if (i == macListUpdated.size()) {
130                                 macListRemoved.add(macBefore);
131                             }
132                         }
133                         if (!macListRemoved.isEmpty()) {
134                             result.put(key, macListRemoved);
135                         }
136                     }
137                 }
138             }
139         }
140         return result;
141     }
142 }