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.Collection;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.Optional;
18 import org.opendaylight.mdsal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacsKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
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 public class McastMacsLocalUpdateCommand
34 extends AbstractTransactCommand<LocalMcastMacs, LocalMcastMacsKey, HwvtepGlobalAugmentation> {
35 private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalUpdateCommand.class);
37 public McastMacsLocalUpdateCommand(final HwvtepOperationalState state,
38 final Collection<DataTreeModification<Node>> changes) {
39 super(state, changes);
43 public void execute(final TransactionBuilder transaction) {
44 Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> updateds =
45 extractUpdated(getChanges(),LocalMcastMacs.class);
46 if (!updateds.isEmpty()) {
47 for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
48 updateds.entrySet()) {
49 updateMcastMacsLocal(transaction, updated.getKey(), updated.getValue());
54 private void updateMcastMacsLocal(final TransactionBuilder transaction,
55 final InstanceIdentifier<Node> instanceIdentifier, final List<LocalMcastMacs> localMcastMacs) {
56 for (LocalMcastMacs localMcastMac: localMcastMacs) {
57 LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
58 final Optional<LocalMcastMacs> operationalMacOptional =
59 getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.key());
60 McastMacsLocal mcastMacsLocal = transaction.getTypedRowWrapper(McastMacsLocal.class);
61 setIpAddress(mcastMacsLocal, localMcastMac);
62 setLocatorSet(transaction, mcastMacsLocal, localMcastMac);
63 setLogicalSwitch(mcastMacsLocal, localMcastMac);
64 if (!operationalMacOptional.isPresent()) {
65 setMac(mcastMacsLocal, localMcastMac, operationalMacOptional);
66 LOG.trace("execute: create LocalMcastMac entry: {}", mcastMacsLocal);
67 transaction.add(op.insert(mcastMacsLocal));
68 transaction.add(op.comment("McastMacLocal: Creating " + localMcastMac.getMacEntryKey().getValue()));
69 } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
70 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
71 McastMacsLocal extraMac = transaction.getTypedRowSchema(McastMacsLocal.class);
72 extraMac.getUuidColumn().setData(macEntryUUID);
73 LOG.trace("execute: update LocalMcastMac entry: {}", mcastMacsLocal);
74 transaction.add(op.update(mcastMacsLocal)
75 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
77 transaction.add(op.comment("McastMacLocal: Updating " + macEntryUUID));
79 LOG.warn("Unable to update localMcastMacs {} because uuid not found in the operational store",
80 localMcastMac.getMacEntryKey().getValue());
85 private void setLogicalSwitch(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac) {
86 if (inputMac.getLogicalSwitchRef() != null) {
87 @SuppressWarnings("unchecked")
88 InstanceIdentifier<LogicalSwitches> lswitchIid =
89 (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
90 Optional<LogicalSwitches> operationalSwitchOptional =
91 getOperationalState().getLogicalSwitches(lswitchIid);
92 if (operationalSwitchOptional.isPresent()) {
93 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
94 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
95 mcastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
98 "Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
104 private void setLocatorSet(final TransactionBuilder transaction, final McastMacsLocal mcastMacsLocal,
105 final LocalMcastMacs inputMac) {
106 if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
107 UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction,
108 inputMac.getLocatorSet());
109 mcastMacsLocal.setLocatorSet(locatorSetUuid);
113 private static void setIpAddress(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac) {
114 if (inputMac.getIpaddr() != null) {
115 mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
119 private static void setMac(final McastMacsLocal mcastMacsLocal, final LocalMcastMacs inputMac,
120 final Optional<LocalMcastMacs> inputSwitchOptional) {
121 if (inputMac.getMacEntryKey() != null) {
122 if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
123 mcastMacsLocal.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
125 mcastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
127 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
128 mcastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
133 protected Map<LocalMcastMacsKey, LocalMcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
134 return augmentation.getLocalMcastMacs();
138 protected String getKeyStr(InstanceIdentifier<LocalMcastMacs> iid) {
139 return getLsKeyStr(iid.firstKeyOf(LocalMcastMacs.class).getLogicalSwitchRef().getValue());