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 org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
15 import org.opendaylight.ovsdb.lib.notation.UUID;
16 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
17 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
18 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsRemote;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.List;
32 import java.util.Map.Entry;
34 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand<RemoteUcastMacs, HwvtepGlobalAugmentation> {
35 private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
36 private static final UcastMacUnMetDependencyGetter UCAST_MAC_DATA_VALIDATOR = new UcastMacUnMetDependencyGetter();
38 public UcastMacsRemoteUpdateCommand(HwvtepOperationalState state,
39 Collection<DataTreeModification<Node>> changes) {
40 super(state, changes);
44 public void execute(TransactionBuilder transaction) {
45 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
46 extractUpdated(getChanges(),RemoteUcastMacs.class);
47 if (!updateds.isEmpty()) {
48 for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
49 updateds.entrySet()) {
50 updateUcastMacsRemote(transaction, updated.getKey(), updated.getValue());
55 private void updateUcastMacsRemote(TransactionBuilder transaction,
56 InstanceIdentifier<Node> instanceIdentifier,
57 List<RemoteUcastMacs> remoteUcastMacs) {
58 if (remoteUcastMacs == null) {
61 for (RemoteUcastMacs remoteUcastMac : remoteUcastMacs) {
62 onConfigUpdate(transaction, instanceIdentifier, remoteUcastMac, null);
67 public void onConfigUpdate(TransactionBuilder transaction,
68 InstanceIdentifier<Node> nodeIid,
69 RemoteUcastMacs remoteUcastMacs,
70 InstanceIdentifier macKey,
71 Object... extraData) {
72 InstanceIdentifier<RemoteUcastMacs> macIid = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
73 child(RemoteUcastMacs.class, remoteUcastMacs.getKey());
74 processDependencies(UCAST_MAC_DATA_VALIDATOR, transaction, nodeIid, macIid, remoteUcastMacs);
78 public void doDeviceTransaction(TransactionBuilder transaction,
79 InstanceIdentifier<Node> instanceIdentifier,
80 RemoteUcastMacs remoteUcastMac,
81 InstanceIdentifier macKey,
82 Object... extraData) {
83 LOG.debug("Creating remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
84 HwvtepDeviceInfo.DeviceData deviceData =
85 getOperationalState().getDeviceInfo().getDeviceOperData(RemoteUcastMacs.class, macKey);
87 UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsRemote.class);
88 setIpAddress(ucastMacsRemote, remoteUcastMac);
89 setLocator(transaction, ucastMacsRemote, remoteUcastMac);
90 setLogicalSwitch(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 transaction.add(op.comment("UcastMacRemote: Creating " + remoteUcastMac.getMacEntryKey().getValue()));
96 } else if (deviceData.getUuid() != null) {
97 UUID macEntryUUID = deviceData.getUuid();
98 UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
99 UcastMacsRemote.class, null);
100 extraMac.getUuidColumn().setData(macEntryUUID);
101 LOG.trace("doDeviceTransaction: updating RemotUcastMac entry: {}", ucastMacsRemote);
102 transaction.add(op.update(ucastMacsRemote)
103 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
105 transaction.add(op.comment("UcastMacRemote: Updating " + remoteUcastMac.getMacEntryKey().getValue()));
107 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
108 remoteUcastMac.getMacEntryKey().getValue());
112 private void setLogicalSwitch(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
113 if (inputMac.getLogicalSwitchRef() != null) {
114 @SuppressWarnings("unchecked")
115 InstanceIdentifier<LogicalSwitches> lswitchIid =
116 (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
117 HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo().getDeviceOperData(
118 LogicalSwitches.class, lswitchIid);
119 if (deviceData != null && deviceData.getUuid() != null) {
120 ucastMacsRemote.setLogicalSwitch(deviceData.getUuid());
122 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
127 private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
128 //get UUID by locatorRef
129 if (inputMac.getLocatorRef() != null) {
130 UUID locatorUuid = null;
131 @SuppressWarnings("unchecked")
132 InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
133 //try to find locator in operational DS
134 HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo().getDeviceOperData(TerminationPoint.class, iid);
135 if (deviceData != null) {
137 locatorUuid = deviceData.getUuid();
139 locatorUuid = getOperationalState().getUUIDFromCurrentTx(TerminationPoint.class, iid);
140 if (locatorUuid == null) {
141 locatorUuid = TransactUtils.createPhysicalLocator(transaction, getOperationalState(),
142 (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue());
143 updateCurrentTxData(TerminationPoint.class, iid, locatorUuid, null);
146 if (locatorUuid != null) {
147 ucastMacsRemote.setLocator(locatorUuid);
152 private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
153 if (inputMac.getIpaddr() != null) {
154 ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
158 private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
159 if (inputMac.getMacEntryKey() != null) {
160 ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
164 protected List<RemoteUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
165 return augmentation.getRemoteUcastMacs();
168 static class UcastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteUcastMacs> {
170 public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(RemoteUcastMacs data) {
172 return Collections.emptyList();
174 return Collections.singletonList(data.getLogicalSwitchRef().getValue());
177 public List<InstanceIdentifier<?>> getTerminationPointDependencies(RemoteUcastMacs data) {
179 return Collections.emptyList();
181 return Collections.singletonList(data.getLocatorRef().getValue());