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 com.google.common.base.Optional;
14 import java.util.Collection;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.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.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsLocal;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalMcastMacs;
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 extends AbstractTransactCommand<LocalMcastMacs, HwvtepGlobalAugmentation> {
34 private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalUpdateCommand.class);
36 public McastMacsLocalUpdateCommand(HwvtepOperationalState state,
37 Collection<DataTreeModification<Node>> changes) {
38 super(state, changes);
42 public void execute(TransactionBuilder transaction) {
43 Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> updateds =
44 extractUpdated(getChanges(),LocalMcastMacs.class);
45 if (!updateds.isEmpty()) {
46 for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
47 updateds.entrySet()) {
48 updateMcastMacsLocal(transaction, updated.getKey(), updated.getValue());
53 private void updateMcastMacsLocal(TransactionBuilder transaction,
54 InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> localMcastMacs) {
55 for (LocalMcastMacs localMcastMac: localMcastMacs) {
56 LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
57 final Optional<LocalMcastMacs> operationalMacOptional =
58 getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.key());
59 McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
60 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 = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
72 McastMacsLocal.class, null);
73 extraMac.getUuidColumn().setData(macEntryUUID);
74 LOG.trace("execute: update LocalMcastMac entry: {}", mcastMacsLocal);
75 transaction.add(op.update(mcastMacsLocal)
76 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
78 transaction.add(op.comment("McastMacLocal: Updating " + macEntryUUID));
80 LOG.warn("Unable to update localMcastMacs {} because uuid not found in the operational store",
81 localMcastMac.getMacEntryKey().getValue());
86 private void setLogicalSwitch(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
87 if (inputMac.getLogicalSwitchRef() != null) {
88 @SuppressWarnings("unchecked")
89 InstanceIdentifier<LogicalSwitches> lswitchIid =
90 (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
91 Optional<LogicalSwitches> operationalSwitchOptional =
92 getOperationalState().getLogicalSwitches(lswitchIid);
93 if (operationalSwitchOptional.isPresent()) {
94 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
95 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
96 mcastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
99 "Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
105 private void setLocatorSet(TransactionBuilder transaction, McastMacsLocal mcastMacsLocal, 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 void setIpAddress(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
114 if (inputMac.getIpaddr() != null) {
115 mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
119 private void setMac(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac,
120 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 List<LocalMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
134 return augmentation.getLocalMcastMacs();