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;
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 import com.google.common.base.Optional;
35 public class McastMacsLocalUpdateCommand extends AbstractTransactCommand<LocalMcastMacs, HwvtepGlobalAugmentation> {
36 private static final Logger LOG = LoggerFactory.getLogger(McastMacsLocalUpdateCommand.class);
38 public McastMacsLocalUpdateCommand(HwvtepOperationalState state,
39 Collection<DataTreeModification<Node>> changes) {
40 super(state, changes);
44 public void execute(TransactionBuilder transaction) {
45 Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> updateds =
46 extractUpdated(getChanges(),LocalMcastMacs.class);
47 if (!updateds.isEmpty()) {
48 for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
49 updateds.entrySet()) {
50 updateMcastMacsLocal(transaction, updated.getKey(), updated.getValue());
55 private void updateMcastMacsLocal(TransactionBuilder transaction,
56 InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> localMcastMacs) {
57 for (LocalMcastMacs localMcastMac: localMcastMacs) {
58 LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
59 Optional<LocalMcastMacs> operationalMacOptional =
60 getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.getKey());
61 McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsLocal.class);
62 setIpAddress(mcastMacsLocal, localMcastMac);
63 setLocatorSet(transaction, mcastMacsLocal, localMcastMac);
64 setLogicalSwitch(mcastMacsLocal, localMcastMac);
65 if (!operationalMacOptional.isPresent()) {
66 setMac(mcastMacsLocal, localMcastMac, operationalMacOptional);
67 LOG.trace("execute: create LocalMcastMac entry: {}", mcastMacsLocal);
68 transaction.add(op.insert(mcastMacsLocal));
69 transaction.add(op.comment("McastMacLocal: Creating " + localMcastMac.getMacEntryKey().getValue()));
70 } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
71 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
72 McastMacsLocal extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
73 McastMacsLocal.class, null);
74 extraMac.getUuidColumn().setData(macEntryUUID);
75 LOG.trace("execute: update LocalMcastMac entry: {}", mcastMacsLocal);
76 transaction.add(op.update(mcastMacsLocal)
77 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
79 transaction.add(op.comment("McastMacLocal: Updating " + macEntryUUID));
81 LOG.warn("Unable to update localMcastMacs {} because uuid not found in the operational store",
82 localMcastMac.getMacEntryKey().getValue());
87 private void setLogicalSwitch(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
88 if (inputMac.getLogicalSwitchRef() != null) {
89 @SuppressWarnings("unchecked")
90 InstanceIdentifier<LogicalSwitches> lswitchIid = (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);
98 LOG.warn("Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
104 private void setLocatorSet(TransactionBuilder transaction, McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
105 if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
106 UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction, inputMac.getLocatorSet());
107 mcastMacsLocal.setLocatorSet(locatorSetUuid);
111 private void setIpAddress(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
112 if (inputMac.getIpaddr() != null) {
113 mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
117 private void setMac(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac,
118 Optional<LocalMcastMacs> inputSwitchOptional) {
119 if (inputMac.getMacEntryKey() != null) {
120 if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
121 mcastMacsLocal.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
123 mcastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
125 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
126 mcastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
131 protected List<LocalMcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
132 return augmentation.getLocalMcastMacs();