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.HashMap;
15 import java.util.List;
17 import java.util.Map.Entry;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
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.HwvtepPhysicalLocatorAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.base.Optional;
38 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand {
39 private static final Logger LOG = LoggerFactory.getLogger(UcastMacsRemoteUpdateCommand.class);
41 public UcastMacsRemoteUpdateCommand(HwvtepOperationalState state,
42 Collection<DataTreeModification<Node>> changes) {
43 super(state, changes);
47 public void execute(TransactionBuilder transaction) {
48 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> createds =
49 extractCreated(getChanges(),RemoteUcastMacs.class);
50 if (!createds.isEmpty()) {
51 for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> created:
52 createds.entrySet()) {
53 updateUcastMacsRemote(transaction, created.getKey(), created.getValue());
56 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
57 extractUpdated(getChanges(),RemoteUcastMacs.class);
58 if (!updateds.isEmpty()) {
59 for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
60 updateds.entrySet()) {
61 updateUcastMacsRemote(transaction, updated.getKey(), updated.getValue());
66 private void updateUcastMacsRemote(TransactionBuilder transaction,
67 InstanceIdentifier<Node> instanceIdentifier, List<RemoteUcastMacs> remoteUcastMacs) {
68 for (RemoteUcastMacs remoteUcastMac: remoteUcastMacs) {
69 LOG.debug("Creating remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
70 Optional<RemoteUcastMacs> operationalMacOptional =
71 getOperationalState().getRemoteUcastMacs(instanceIdentifier, remoteUcastMac.getKey());
72 UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsRemote.class);
73 setIpAddress(ucastMacsRemote, remoteUcastMac);
74 setLocator(transaction, ucastMacsRemote, remoteUcastMac);
75 setLogicalSwitch(ucastMacsRemote, remoteUcastMac);
76 if (!operationalMacOptional.isPresent()) {
77 setMac(ucastMacsRemote, remoteUcastMac, operationalMacOptional);
78 LOG.trace("execute: creating RemotUcastMac entry: {}", ucastMacsRemote);
79 transaction.add(op.insert(ucastMacsRemote));
80 transaction.add(op.comment("UcastMacRemote: Creating " + remoteUcastMac.getMacEntryKey().getValue()));
81 } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
82 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
83 UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
84 UcastMacsRemote.class, null);
85 extraMac.getUuidColumn().setData(macEntryUUID);
86 LOG.trace("execute: updating RemotUcastMac entry: {}", ucastMacsRemote);
87 transaction.add(op.update(ucastMacsRemote)
88 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
90 transaction.add(op.comment("UcastMacRemote: Updating " + remoteUcastMac.getMacEntryKey().getValue()));
92 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
93 remoteUcastMac.getMacEntryKey().getValue());
98 private void setLogicalSwitch(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
99 if (inputMac.getLogicalSwitchRef() != null) {
100 @SuppressWarnings("unchecked")
101 InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
102 Optional<LogicalSwitches> operationalSwitchOptional =
103 getOperationalState().getLogicalSwitches(lswitchIid);
104 if (operationalSwitchOptional.isPresent()) {
105 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
106 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
107 ucastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
109 ucastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
114 private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
115 //get UUID by locatorRef
116 if (inputMac.getLocatorRef() != null) {
117 UUID locatorUuid = null;
118 @SuppressWarnings("unchecked")
119 InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
120 //try to find locator in operational DS
121 Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
122 getOperationalState().getPhysicalLocatorAugmentation(iid);
123 if (operationalLocatorOptional.isPresent()) {
125 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
126 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
128 //TODO: need to optimize by eliminating reading Configuration datastore
129 //if no, get it from config DS and create id
130 locatorUuid = getOperationalState().getPhysicalLocatorInFlight(iid);
131 if (locatorUuid == null) {
132 Optional<TerminationPoint> configLocatorOptional =
133 TransactUtils.readNodeFromConfig(getOperationalState().getReadWriteTransaction(), iid);
134 if (configLocatorOptional.isPresent()) {
135 HwvtepPhysicalLocatorAugmentation locatorAugmentation =
136 configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
137 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
138 getOperationalState().setPhysicalLocatorInFlight(iid, locatorUuid);
140 LOG.warn("Create or update remoteUcastMac: No physical locator found in operational datastore!"
141 + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
145 if (locatorUuid != null) {
146 ucastMacsRemote.setLocator(locatorUuid);
151 private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
152 if (inputMac.getIpaddr() != null) {
153 ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
157 private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac,
158 Optional<RemoteUcastMacs> inputSwitchOptional) {
159 if (inputMac.getMacEntryKey() != null) {
160 ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
161 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
162 ucastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
166 private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractCreated(
167 Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
168 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
169 = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
170 if (changes != null && !changes.isEmpty()) {
171 for (DataTreeModification<Node> change : changes) {
172 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
173 final DataObjectModification<Node> mod = change.getRootNode();
174 Node created = TransactUtils.getCreated(mod);
175 if (created != null) {
176 List<RemoteUcastMacs> macListUpdated = null;
177 HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
178 if (hgAugmentation != null) {
179 macListUpdated = hgAugmentation.getRemoteUcastMacs();
181 if (macListUpdated != null) {
182 result.put(key, macListUpdated);
190 private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractUpdated(
191 Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
192 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
193 = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
194 if (changes != null && !changes.isEmpty()) {
195 for (DataTreeModification<Node> change : changes) {
196 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
197 final DataObjectModification<Node> mod = change.getRootNode();
198 Node updated = TransactUtils.getUpdated(mod);
199 Node before = mod.getDataBefore();
200 if (updated != null && before != null) {
201 List<RemoteUcastMacs> macListUpdated = null;
202 List<RemoteUcastMacs> macListBefore = null;
203 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
204 if (hgUpdated != null) {
205 macListUpdated = hgUpdated.getRemoteUcastMacs();
207 HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
208 if (hgBefore != null) {
209 macListBefore = hgBefore.getRemoteUcastMacs();
211 if (macListUpdated != null) {
212 if (macListBefore != null) {
213 macListUpdated.removeAll(macListBefore);
215 result.put(key, macListUpdated);