2 * Copyright (c) 2015, 2017 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
8 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import com.google.common.collect.Lists;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.Objects;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
21 import org.opendaylight.ovsdb.lib.notation.UUID;
22 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
23 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
24 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
25 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
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.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacsKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class UcastMacsRemoteUpdateCommand
37 extends AbstractTransactCommand<RemoteUcastMacs, RemoteUcastMacsKey, HwvtepGlobalAugmentation> {
38 private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
39 private static final UcastMacUnMetDependencyGetter UCAST_MAC_DATA_VALIDATOR = new UcastMacUnMetDependencyGetter();
41 public UcastMacsRemoteUpdateCommand(final HwvtepOperationalState state,
42 final Collection<DataTreeModification<Node>> changes) {
43 super(state, changes);
47 public void execute(final TransactionBuilder transaction) {
48 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
49 extractUpdated(getChanges(),RemoteUcastMacs.class);
50 if (!updateds.isEmpty()) {
51 for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
52 updateds.entrySet()) {
53 updateUcastMacsRemote(transaction, updated.getKey(), updated.getValue());
58 private void updateUcastMacsRemote(final TransactionBuilder transaction,
59 final InstanceIdentifier<Node> instanceIdentifier,
60 final List<RemoteUcastMacs> remoteUcastMacs) {
61 if (remoteUcastMacs == null) {
64 for (RemoteUcastMacs remoteUcastMac : remoteUcastMacs) {
65 InstanceIdentifier<RemoteUcastMacs> macIid =
66 instanceIdentifier.augmentation(HwvtepGlobalAugmentation.class)
67 .child(RemoteUcastMacs.class, remoteUcastMac.key());
68 getDeviceInfo().updateConfigData(RemoteUcastMacs.class, macIid, remoteUcastMac);
69 onConfigUpdate(transaction, instanceIdentifier, remoteUcastMac, null);
74 public void onConfigUpdate(final TransactionBuilder transaction,
75 final InstanceIdentifier<Node> nodeIid,
76 final RemoteUcastMacs remoteUcastMacs,
77 final InstanceIdentifier macKey,
78 final Object... extraData) {
79 InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
80 .child(RemoteUcastMacs.class, remoteUcastMacs.key());
81 processDependencies(UCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteUcastMacs);
85 public void doDeviceTransaction(final TransactionBuilder transaction,
86 final InstanceIdentifier<Node> instanceIdentifier,
87 final RemoteUcastMacs remoteUcastMac,
88 final InstanceIdentifier macKey,
89 final Object... extraData) {
90 LOG.debug("DoDeviceTransaction remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
91 updateConfigData(RemoteUcastMacs.class, macKey, remoteUcastMac);
92 HwvtepDeviceInfo.DeviceData deviceData = getDeviceOpData(RemoteUcastMacs.class, macKey);
93 UcastMacsRemote ucastMacsRemote = transaction.getTypedRowWrapper(UcastMacsRemote.class);
94 setIpAddress(ucastMacsRemote, remoteUcastMac);
95 setLogicalSwitch(transaction, ucastMacsRemote, remoteUcastMac);
96 //TODO handle multiple inserts
97 if (deviceData == null) {
98 setLocator(transaction, ucastMacsRemote, remoteUcastMac);
99 setMac(ucastMacsRemote, remoteUcastMac);
100 LOG.trace("DoDeviceTransaction: creating RemotUcastMac entry: {} txId: {}", macKey,
101 getOperationalState().getTransactionId());
102 transaction.add(op.insert(ucastMacsRemote));
103 updateCurrentTxData(RemoteUcastMacs.class, macKey, new UUID("uuid"), remoteUcastMac);
104 LOG.info("CONTROLLER - {} {}", TransactionType.ADD, ucastMacsRemote);
106 } else if (deviceData.getUuid() != null) {
107 UUID newLocator = setLocator(transaction, ucastMacsRemote, remoteUcastMac);
108 if (deviceData.getData() != null) {
109 UcastMacsRemote existing = (UcastMacsRemote) deviceData.getData();
110 if (existing.getLocatorColumn() != null) {
111 UUID oldLocatorUuid = existing.getLocatorColumn().getData();
112 if (Objects.equals(newLocator, oldLocatorUuid)) {
117 setMac(ucastMacsRemote, remoteUcastMac);
118 UUID macEntryUUID = deviceData.getUuid();
119 UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
120 UcastMacsRemote.class, null);
121 extraMac.getUuidColumn().setData(macEntryUUID);
122 LOG.trace("doDeviceTransaction: updating RemotUcastMac entry: {} txId: {}", macKey,
123 getOperationalState().getTransactionId());
124 transaction.add(op.update(ucastMacsRemote)
125 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
127 LOG.info("CONTROLLER - {} {}", TransactionType.UPDATE, ucastMacsRemote);
128 addToUpdates(macKey, remoteUcastMac);
130 LOG.warn("Unable to update remoteUcastMacs {} because uuid not found in the operational store txId: {}",
131 macKey, getOperationalState().getTransactionId());
136 private void setLogicalSwitch(final TransactionBuilder transaction, final UcastMacsRemote ucastMacsRemote,
137 final RemoteUcastMacs inputMac) {
138 if (inputMac.getLogicalSwitchRef() != null) {
139 @SuppressWarnings("unchecked")
140 InstanceIdentifier<LogicalSwitches> lswitchIid =
141 (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
142 UUID logicalSwitchUUID = TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(), lswitchIid);
143 if (logicalSwitchUUID != null) {
144 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(transaction, getOperationalState(),
150 private UUID setLocator(final TransactionBuilder transaction, final UcastMacsRemote ucastMacsRemote,
151 final RemoteUcastMacs inputMac) {
152 //get UUID by locatorRef
153 if (inputMac.getLocatorRef() != null) {
154 @SuppressWarnings("unchecked")
155 InstanceIdentifier<TerminationPoint> iid =
156 (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
157 UUID locatorUuid = TransactUtils.createPhysicalLocator(transaction, getOperationalState(), iid);
158 ucastMacsRemote.setLocator(locatorUuid);
164 private static void setIpAddress(final UcastMacsRemote ucastMacsRemote, final RemoteUcastMacs inputMac) {
165 if (inputMac.getIpaddr() != null) {
166 ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
170 private static void setMac(final UcastMacsRemote ucastMacsRemote, final RemoteUcastMacs inputMac) {
171 if (inputMac.getMacEntryKey() != null) {
172 ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
177 protected Map<RemoteUcastMacsKey, RemoteUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
178 return augmentation.getRemoteUcastMacs();
181 static class UcastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteUcastMacs> {
184 public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(final RemoteUcastMacs data) {
186 return Collections.EMPTY_LIST;
188 return Lists.newArrayList(data.getLogicalSwitchRef().getValue());
192 public List<InstanceIdentifier<?>> getTerminationPointDependencies(final RemoteUcastMacs data) {
194 return Collections.EMPTY_LIST;
196 return Lists.newArrayList(data.getLocatorRef().getValue());
201 protected boolean areEqual(final RemoteUcastMacs remoteUcastMacs1, final RemoteUcastMacs remoteUcastMacs2) {
202 return Objects.equals(remoteUcastMacs1.key(), remoteUcastMacs2.key())
203 && Objects.equals(remoteUcastMacs1.getLocatorRef(), remoteUcastMacs2.getLocatorRef());
207 public void onSuccess(final TransactionBuilder tx) {
208 for (MdsalUpdate mdsalUpdate : updates) {
209 RemoteUcastMacs mac = (RemoteUcastMacs) mdsalUpdate.getNewData();
210 InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
211 getDeviceInfo().updateRemoteUcast(
212 (InstanceIdentifier<LogicalSwitches>) mac.getLogicalSwitchRef().getValue(), macIid, mac);