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
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.Collections;
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.HwvtepDeviceInfo;
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.UcastMacsRemote;
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.LogicalSwitches;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
34 private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
35 private static final UcastMacUnMetDependencyGetter UCAST_MAC_DATA_VALIDATOR = new UcastMacUnMetDependencyGetter();
37 public UcastMacsRemoteUpdateCommand(HwvtepOperationalState state,
38 Collection<DataTreeModification<Node>> changes) {
39 super(state, changes);
43 public void execute(TransactionBuilder transaction) {
44 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
45 extractUpdated(getChanges(),RemoteUcastMacs.class);
46 if (!updateds.isEmpty()) {
47 for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
48 updateds.entrySet()) {
49 updateUcastMacsRemote(transaction, updated.getKey(), updated.getValue());
54 private void updateUcastMacsRemote(final TransactionBuilder transaction,
55 final InstanceIdentifier<Node> instanceIdentifier,
56 final List<RemoteUcastMacs> remoteUcastMacs) {
57 if (remoteUcastMacs == null) {
60 for (RemoteUcastMacs remoteUcastMac : remoteUcastMacs) {
61 onConfigUpdate(transaction, instanceIdentifier, remoteUcastMac, null);
66 public void onConfigUpdate(final TransactionBuilder transaction,
67 final InstanceIdentifier<Node> nodeIid,
68 final RemoteUcastMacs remoteUcastMacs,
69 final InstanceIdentifier macKey,
70 final Object... extraData) {
71 InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
72 .child(RemoteUcastMacs.class, remoteUcastMacs.key());
73 processDependencies(UCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteUcastMacs);
77 public void doDeviceTransaction(final TransactionBuilder transaction,
78 final InstanceIdentifier<Node> instanceIdentifier,
79 final RemoteUcastMacs remoteUcastMac,
80 final InstanceIdentifier macKey,
81 final Object... extraData) {
82 LOG.debug("Creating remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
83 final HwvtepDeviceInfo.DeviceData deviceData =
84 getOperationalState().getDeviceInfo().getDeviceOperData(RemoteUcastMacs.class, macKey);
86 UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
87 UcastMacsRemote.class);
88 setIpAddress(ucastMacsRemote, remoteUcastMac);
89 setLocator(transaction, ucastMacsRemote, remoteUcastMac);
90 setLogicalSwitch(transaction, ucastMacsRemote, remoteUcastMac);
91 if (deviceData == null) {
92 setMac(ucastMacsRemote, remoteUcastMac);
93 LOG.trace("doDeviceTransaction: creating RemotUcastMac entry: {}", ucastMacsRemote);
94 transaction.add(op.insert(ucastMacsRemote));
95 getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteUcastMacs.class, macKey);
96 updateCurrentTxData(RemoteUcastMacs.class, macKey, new UUID("uuid"), remoteUcastMac);
97 } else if (deviceData.getUuid() != null) {
98 UUID macEntryUUID = deviceData.getUuid();
99 UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
100 UcastMacsRemote.class, null);
101 extraMac.getUuidColumn().setData(macEntryUUID);
102 LOG.trace("doDeviceTransaction: updating RemotUcastMac entry: {}", ucastMacsRemote);
103 transaction.add(op.update(ucastMacsRemote)
104 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
107 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
108 remoteUcastMac.getMacEntryKey().getValue());
112 private void setLogicalSwitch(final TransactionBuilder transaction, final UcastMacsRemote ucastMacsRemote,
113 final RemoteUcastMacs inputMac) {
114 if (inputMac.getLogicalSwitchRef() != null) {
115 @SuppressWarnings("unchecked")
116 InstanceIdentifier<LogicalSwitches> lswitchIid =
117 (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
118 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(
119 transaction, getOperationalState(), lswitchIid));
123 private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
124 //get UUID by locatorRef
125 if (inputMac.getLocatorRef() != null) {
126 @SuppressWarnings("unchecked")
127 InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>)
128 inputMac.getLocatorRef().getValue();
129 UUID locatorUuid = TransactUtils.createPhysicalLocator(transaction, getOperationalState(), iid);
130 if (locatorUuid != null) {
131 ucastMacsRemote.setLocator(locatorUuid);
136 private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
137 if (inputMac.getIpaddr() != null) {
138 ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
142 private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
143 if (inputMac.getMacEntryKey() != null) {
144 ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
149 protected List<RemoteUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
150 return augmentation.getRemoteUcastMacs();
153 static class UcastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteUcastMacs> {
156 public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(RemoteUcastMacs data) {
158 return Collections.emptyList();
160 return Collections.singletonList(data.getLogicalSwitchRef().getValue());
164 public List<InstanceIdentifier<?>> getTerminationPointDependencies(RemoteUcastMacs data) {
166 return Collections.emptyList();
168 return Collections.singletonList(data.getLocatorRef().getValue());
173 public void onCommandSucceeded() {
174 for (MdsalUpdate mdsalUpdate : updates.get(getDeviceTransaction())) {
175 RemoteUcastMacs newMac = (RemoteUcastMacs) mdsalUpdate.getNewData();
176 InstanceIdentifier<RemoteUcastMacs> macIid = mdsalUpdate.getKey();
177 RemoteUcastMacs oldMac = (RemoteUcastMacs) mdsalUpdate.getOldData();
178 if (oldMac != null && !oldMac.equals(newMac)) {
179 getDeviceInfo().decRefCount(macIid, oldMac.getLocatorRef().getValue());
181 getDeviceInfo().updateRemoteUcast(
182 (InstanceIdentifier<LogicalSwitches>) newMac.getLogicalSwitchRef().getValue(), macIid, newMac);