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.List;
17 import java.util.Map.Entry;
18 import java.util.Objects;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
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.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;
33 import com.google.common.base.Optional;
35 public class McastMacsRemoteRemoveCommand extends AbstractTransactCommand<RemoteMcastMacs, HwvtepGlobalAugmentation> {
36 private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteRemoveCommand.class);
38 public McastMacsRemoteRemoveCommand(HwvtepOperationalState state,
39 Collection<DataTreeModification<Node>> changes) {
40 super(state, changes);
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());
53 //Remove the ones whose locator set got emptied
54 Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated =
55 extractUpdated(getChanges(),RemoteMcastMacs.class);
56 if (!HwvtepSouthboundUtil.isEmptyMap(updated)) {
57 for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> entry:
59 List<RemoteMcastMacs> updatedList = entry.getValue();
60 List<RemoteMcastMacs> tobeRemovedList = new ArrayList<>();
61 if (!HwvtepSouthboundUtil.isEmpty(updatedList)) {
62 for (RemoteMcastMacs mac: updatedList) {
63 if (HwvtepSouthboundUtil.isEmpty(mac.getLocatorSet())) {
64 tobeRemovedList.add(mac);
67 removeMcastMacRemote(transaction, entry.getKey(), tobeRemovedList);
73 private void removeMcastMacRemote(TransactionBuilder transaction,
74 InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
75 for (RemoteMcastMacs mac: macList) {
76 LOG.debug("Removing remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
77 Optional<RemoteMcastMacs> operationalMacOptional =
78 getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
79 McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
80 McastMacsRemote.class, null);
81 if (operationalMacOptional.isPresent() && operationalMacOptional.get().getMacEntryUuid() != null) {
82 //when mac entry is deleted, its referenced locator set and locators are deleted automatically.
83 //TODO: locator in config DS is not deleted
84 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
85 mcastMacsRemote.getUuidColumn().setData(macEntryUUID);
86 transaction.add(op.delete(mcastMacsRemote.getSchema()).
87 where(mcastMacsRemote.getUuidColumn().getSchema().opEqual(macEntryUUID)).build());
88 transaction.add(op.comment("McastMacRemote: Deleting " + mac.getMacEntryKey().getValue()));
90 LOG.warn("Unable to delete remoteMcastMacs {} because it was not found in the operational store",
91 mac.getMacEntryKey().getValue());
93 InstanceIdentifier<RemoteMcastMacs> macIid = instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class).
94 child(RemoteMcastMacs.class, mac.getKey());
95 updateCurrentTxDeleteData(RemoteMcastMacs.class, macIid, mac);
100 protected List<RemoteMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
101 return augmentation.getRemoteMcastMacs();
105 protected boolean areEqual(RemoteMcastMacs a, RemoteMcastMacs b) {
106 return a.getKey().equals(b.getKey()) && Objects.equals(a.getLocatorSet(), b.getLocatorSet());