2 * Copyright (c) 2015, 2016 China Telecom Beijing Research Institute and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Map.Entry;
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;
33 import com.google.common.base.Optional;
35 public class UcastMacsRemoteRemoveCommand extends AbstractTransactCommand {
36 private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteRemoveCommand.class);
38 public UcastMacsRemoteRemoveCommand(HwvtepOperationalState state,
39 Collection<DataTreeModification<Node>> changes) {
40 super(state, changes);
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());
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 ucastMacsRemote.getUuidColumn().setData(macEntryUUID);
68 transaction.add(op.delete(ucastMacsRemote.getSchema()).
69 where(ucastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
70 transaction.add(op.comment("UcastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
72 LOG.warn("Unable to delete remoteUcastMacs {} because it was not found in the operational store",
73 mac.getMacEntryKey().getValue());
78 private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractRemoved(
79 Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
80 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
81 = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
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<RemoteUcastMacs> macListRemoved = null;
90 if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
91 macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteUcastMacs();
93 if (macListRemoved != null) {
94 result.put(key, macListRemoved);
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<RemoteUcastMacs> macListUpdated = null;
103 List<RemoteUcastMacs> macListBefore = null;
104 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
105 if (hgUpdated != null) {
106 macListUpdated = hgUpdated.getRemoteUcastMacs();
108 HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
109 if (hgBefore != null) {
110 macListBefore = hgBefore.getRemoteUcastMacs();
112 if (macListBefore != null) {
113 List<RemoteUcastMacs> macListRemoved = new ArrayList<RemoteUcastMacs>();
114 if (macListUpdated != null) {
115 macListBefore.removeAll(macListUpdated);
117 //then exclude updated remoteUcastMacs
118 for (RemoteUcastMacs macBefore: macListBefore) {
120 for(; i < macListUpdated.size(); i++) {
121 if (macBefore.getMacEntryKey().equals(macListUpdated.get(i).getMacEntryKey())) {
125 if (i == macListUpdated.size()) {
126 macListRemoved.add(macBefore);
129 if (!macListRemoved.isEmpty()) {
130 result.put(key, macListRemoved);