2 * Copyright (c) 2015 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.HwvtepNodeName;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.Optional;
40 public class UcastMacsRemoteUpdateCommand extends AbstractTransactCommand {
41 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
43 public UcastMacsRemoteUpdateCommand(HwvtepOperationalState state,
44 Collection<DataTreeModification<Node>> changes) {
45 super(state, changes);
49 public void execute(TransactionBuilder transaction) {
50 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> createds =
51 extractCreated(getChanges(),RemoteUcastMacs.class);
52 if (!createds.isEmpty()) {
53 for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> created:
54 createds.entrySet()) {
55 updateUcastMacsRemote(transaction, created.getKey(), created.getValue());
58 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updateds =
59 extractUpdated(getChanges(),RemoteUcastMacs.class);
60 if (!updateds.isEmpty()) {
61 for (Entry<InstanceIdentifier<Node>, List<RemoteUcastMacs>> updated:
62 updateds.entrySet()) {
63 updateUcastMacsRemote(transaction, updated.getKey(), updated.getValue());
68 private void updateUcastMacsRemote(TransactionBuilder transaction,
69 InstanceIdentifier<Node> instanceIdentifier, List<RemoteUcastMacs> remoteUcastMacs) {
70 for (RemoteUcastMacs remoteUcastMac: remoteUcastMacs) {
71 LOG.debug("Creating remoteUcastMacs, mac address: {}", remoteUcastMac.getMacEntryKey().getValue());
72 Optional<RemoteUcastMacs> operationalMacOptional =
73 getOperationalState().getRemoteUcastMacs(instanceIdentifier, remoteUcastMac.getKey());
74 UcastMacsRemote ucastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsRemote.class);
75 setIpAddress(ucastMacsRemote, remoteUcastMac);
76 setLocator(transaction, ucastMacsRemote, remoteUcastMac);
77 setLogicalSwitch(ucastMacsRemote, remoteUcastMac);
78 if (!operationalMacOptional.isPresent()) {
79 setMac(ucastMacsRemote, remoteUcastMac, operationalMacOptional);
80 transaction.add(op.insert(ucastMacsRemote));
82 RemoteUcastMacs updatedMac = operationalMacOptional.get();
83 String existingMac = updatedMac.getMacEntryKey().getValue();
84 UcastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsRemote.class);
86 transaction.add(op.update(ucastMacsRemote)
87 .where(extraMac.getMacColumn().getSchema().opEqual(existingMac))
93 private void setLogicalSwitch(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
94 if (inputMac.getLogicalSwitchRef() != null) {
95 @SuppressWarnings("unchecked")
96 InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
97 Optional<LogicalSwitches> operationalSwitchOptional =
98 getOperationalState().getLogicalSwitches(lswitchIid);
99 if (operationalSwitchOptional.isPresent()) {
100 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
101 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
102 ucastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
104 LOG.warn("Create or update remoteUcastMac: No logical switch with iid {} found in operational datastore!",
110 private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
111 //get UUID by locatorRef
112 if (inputMac.getLocatorRef() != null) {
113 UUID locatorUuid = null;
114 @SuppressWarnings("unchecked")
115 InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
116 //try to find locator in operational DS
117 Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
118 getOperationalState().getPhysicalLocatorAugmentation(iid);
119 if (operationalLocatorOptional.isPresent()) {
121 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
122 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
124 //TODO: need to optimize by eliminating reading Configuration datastore
125 //if no, get it from config DS and create id
126 Optional<TerminationPoint> configLocatorOptional =
127 TransactUtils.readNodeFromConfig(getOperationalState().getReadWriteTransaction(), iid);
128 if (configLocatorOptional.isPresent()) {
129 HwvtepPhysicalLocatorAugmentation locatorAugmentation =
130 configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
131 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
133 LOG.warn("Create or update remoteUcastMac: No physical locator found in operational datastore!"
134 + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
137 if (locatorUuid != null) {
138 ucastMacsRemote.setLocator(locatorUuid);
143 private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
144 if (inputMac.getIpaddr() != null) {
145 ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
149 private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac,
150 Optional<RemoteUcastMacs> inputSwitchOptional) {
151 if (inputMac.getMacEntryKey() != null) {
152 ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
153 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
154 ucastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
158 private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractCreated(
159 Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
160 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
161 = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
162 if (changes != null && !changes.isEmpty()) {
163 for (DataTreeModification<Node> change : changes) {
164 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
165 final DataObjectModification<Node> mod = change.getRootNode();
166 Node created = TransactUtils.getCreated(mod);
167 if (created != null) {
168 List<RemoteUcastMacs> macListUpdated = null;
169 HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
170 if (hgAugmentation != null) {
171 macListUpdated = hgAugmentation.getRemoteUcastMacs();
173 if (macListUpdated != null) {
174 result.put(key, macListUpdated);
182 private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractUpdated(
183 Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
184 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
185 = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
186 if (changes != null && !changes.isEmpty()) {
187 for (DataTreeModification<Node> change : changes) {
188 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
189 final DataObjectModification<Node> mod = change.getRootNode();
190 Node updated = TransactUtils.getUpdated(mod);
191 Node before = mod.getDataBefore();
192 if (updated != null && before != null) {
193 List<RemoteUcastMacs> macListUpdated = null;
194 List<RemoteUcastMacs> macListBefore = null;
195 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
196 if (hgUpdated != null) {
197 macListUpdated = hgUpdated.getRemoteUcastMacs();
199 HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
200 if (hgBefore != null) {
201 macListBefore = hgBefore.getRemoteUcastMacs();
203 if (macListUpdated != null) {
204 if (macListBefore != null) {
205 macListUpdated.removeAll(macListBefore);
207 result.put(key, macListUpdated);