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(final TransactionBuilder transaction,
56 final InstanceIdentifier<Node> instanceIdentifier,
57 final List<RemoteUcastMacs> remoteUcastMacs) {
58 if (remoteUcastMacs == null) {
61 for (RemoteUcastMacs remoteUcastMac : remoteUcastMacs) {
62 onConfigUpdate(transaction, instanceIdentifier, remoteUcastMac, null);
67 public void onConfigUpdate(final TransactionBuilder transaction,
68 final InstanceIdentifier<Node> nodeIid,
69 final RemoteUcastMacs remoteUcastMacs,
70 final InstanceIdentifier macKey,
71 final 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(final TransactionBuilder transaction,
79 final InstanceIdentifier<Node> instanceIdentifier,
80 final RemoteUcastMacs remoteUcastMac,
81 final InstanceIdentifier macKey,
82 final 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 getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteUcastMacs.class, macKey);
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))
106 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
107 remoteUcastMac.getMacEntryKey().getValue());
111 private void setLogicalSwitch(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
112 if (inputMac.getLogicalSwitchRef() != null) {
113 @SuppressWarnings("unchecked")
114 InstanceIdentifier<LogicalSwitches> lswitchIid =
115 (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
116 HwvtepDeviceInfo.DeviceData deviceData = getOperationalState().getDeviceInfo().getDeviceOperData(
117 LogicalSwitches.class, lswitchIid);
118 if (deviceData != null && deviceData.getUuid() != null) {
119 ucastMacsRemote.setLogicalSwitch(deviceData.getUuid());
121 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
126 private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
127 //get UUID by locatorRef
128 if (inputMac.getLocatorRef() != null) {
129 @SuppressWarnings("unchecked")
130 InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>)
131 inputMac.getLocatorRef().getValue();
132 UUID locatorUuid = TransactUtils.createPhysicalLocator(transaction, getOperationalState(), iid);
133 if (locatorUuid != null) {
134 ucastMacsRemote.setLocator(locatorUuid);
139 private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
140 if (inputMac.getIpaddr() != null) {
141 ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
145 private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
146 if (inputMac.getMacEntryKey() != null) {
147 ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
151 protected List<RemoteUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
152 return augmentation.getRemoteUcastMacs();
155 static class UcastMacUnMetDependencyGetter extends UnMetDependencyGetter<RemoteUcastMacs> {
157 public List<InstanceIdentifier<?>> getLogicalSwitchDependencies(RemoteUcastMacs data) {
159 return Collections.emptyList();
161 return Collections.singletonList(data.getLogicalSwitchRef().getValue());
164 public List<InstanceIdentifier<?>> getTerminationPointDependencies(RemoteUcastMacs data) {
166 return Collections.emptyList();
168 return Collections.singletonList(data.getLocatorRef().getValue());