/* * Copyright (c) 2015 China Telecom Beijing Research Institute and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.ovsdb.hwvtepsouthbound.transact; import static org.opendaylight.ovsdb.lib.operations.Operations.op; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants; import org.opendaylight.ovsdb.lib.notation.UUID; import org.opendaylight.ovsdb.lib.operations.TransactionBuilder; import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils; import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote; import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator; import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocatorSet; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.locator.set.attributes.LocatorSet; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Optional; public class McastMacsRemoteRemoveCommand extends AbstractTransactCommand { private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class); public McastMacsRemoteRemoveCommand(HwvtepOperationalState state, Collection> changes) { super(state, changes); } @Override public void execute(TransactionBuilder transaction) { Map, List> removeds = extractRemoved(getChanges(),RemoteMcastMacs.class); if (!removeds.isEmpty()) { for (Entry, List> removed: removeds.entrySet()) { removeMcastMacRemote(transaction, removed.getKey(), removed.getValue()); } } } private void removeMcastMacRemote(TransactionBuilder transaction, InstanceIdentifier instanceIdentifier, List macList) { for (RemoteMcastMacs mac: macList) { LOG.debug("Removing remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue()); Optional operationalMacOptional = getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey()); McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class, null); if (operationalMacOptional.isPresent()) { //when mac entry is deleted, its referenced locator set and locators are deleted automatically. //TODO: locator in config DS is not deleted String macString = null; if (mac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) { macString = HwvtepSouthboundConstants.UNKNOWN_DST_STRING; } else { macString = mac.getMacEntryKey().getValue(); } transaction.add(op.delete(mcastMacsRemote.getSchema()) .where(mcastMacsRemote.getMacColumn().getSchema().opEqual(macString)).build()); transaction.add(op.comment("Remote McastMacRemote: Deleting " + mac.getMacEntryKey().getValue())); } else { LOG.warn("Unable to delete remoteMcastMacs {} because it was not found in the operational store", mac.getMacEntryKey().getValue()); } } } private Map, List> extractRemoved( Collection> changes, Class class1) { Map, List> result = new HashMap, List>(); if (changes != null && !changes.isEmpty()) { for (DataTreeModification change : changes) { final InstanceIdentifier key = change.getRootPath().getRootIdentifier(); final DataObjectModification mod = change.getRootNode(); //If the node which remoteMcastMacs belong to is removed, all remoteMcastMacs should be removed too. Node removed = TransactUtils.getRemoved(mod); if (removed != null) { List macListRemoved = null; if (removed.getAugmentation(HwvtepGlobalAugmentation.class) != null) { macListRemoved = removed.getAugmentation(HwvtepGlobalAugmentation.class).getRemoteMcastMacs(); } if (macListRemoved != null) { result.put(key, macListRemoved); } } //If the node which remoteMcastMacs belong to is updated, and remoteMcastMacs may //be created or updated or deleted, we need to get deleted ones. Node updated = TransactUtils.getUpdated(mod); Node before = mod.getDataBefore(); if (updated != null && before != null) { List macListUpdated = null; List macListBefore = null; HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class); if (hgUpdated != null) { macListUpdated = hgUpdated.getRemoteMcastMacs(); } HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class); if (hgBefore != null) { macListBefore = hgBefore.getRemoteMcastMacs(); } if (macListBefore != null) { List macListRemoved = new ArrayList(); if (macListUpdated != null) { macListBefore.removeAll(macListUpdated); } //then exclude updated remoteMcastMacs for (RemoteMcastMacs macBefore: macListBefore) { int i = 0; for(; i < macListUpdated.size(); i++) { if (macBefore.getMacEntryKey().equals(macListUpdated.get(i).getMacEntryKey())) { break; } } if (i == macListUpdated.size()) { macListRemoved.add(macBefore); } } if (!macListRemoved.isEmpty()) { result.put(key, macListRemoved); } } } } } return result; } }