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(instanceIdentifier, 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(InstanceIdentifier<Node> iid, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
94 if (inputMac.getLogicalSwitchRef() != null) {
95 HwvtepNodeName lswitchName = new HwvtepNodeName(inputMac.getLogicalSwitchRef().getValue());
96 Optional<LogicalSwitches> operationalSwitchOptional =
97 getOperationalState().getLogicalSwitches(iid, new LogicalSwitchesKey(lswitchName));
98 if (operationalSwitchOptional.isPresent()) {
99 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
100 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
101 ucastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
103 LOG.warn("Create or update remoteUcastMac: No logical switch named {} found in operational datastore!",
109 private void setLocator(TransactionBuilder transaction, UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
110 //get UUID by locatorRef
111 if (inputMac.getLocatorRef() != null) {
112 UUID locatorUuid = null;
113 @SuppressWarnings("unchecked")
114 InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
115 //try to find locator in operational DS
116 Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
117 getOperationalState().getPhysicalLocatorAugmentation(iid);
118 if (operationalLocatorOptional.isPresent()) {
120 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
121 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
123 //TODO: need to optimize by eliminating reading Configuration datastore
124 //if no, get it from config DS and create id
125 Optional<TerminationPoint> configLocatorOptional =
126 TransactUtils.readNodeFromConfig(getOperationalState().getReadWriteTransaction(), iid);
127 if (configLocatorOptional.isPresent()) {
128 HwvtepPhysicalLocatorAugmentation locatorAugmentation =
129 configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
130 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
132 LOG.warn("Create or update remoteUcastMac: No physical locator found in operational datastore!"
133 + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
136 if (locatorUuid != null) {
137 ucastMacsRemote.setLocator(locatorUuid);
142 private void setIpAddress(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac) {
143 if (inputMac.getIpaddr() != null) {
144 ucastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
148 private void setMac(UcastMacsRemote ucastMacsRemote, RemoteUcastMacs inputMac,
149 Optional<RemoteUcastMacs> inputSwitchOptional) {
150 if (inputMac.getMacEntryKey() != null) {
151 ucastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
152 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
153 ucastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
157 private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractCreated(
158 Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
159 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
160 = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
161 if (changes != null && !changes.isEmpty()) {
162 for (DataTreeModification<Node> change : changes) {
163 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
164 final DataObjectModification<Node> mod = change.getRootNode();
165 Node created = TransactUtils.getCreated(mod);
166 if (created != null) {
167 List<RemoteUcastMacs> macListUpdated = null;
168 HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
169 if (hgAugmentation != null) {
170 macListUpdated = hgAugmentation.getRemoteUcastMacs();
172 if (macListUpdated != null) {
173 result.put(key, macListUpdated);
181 private Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> extractUpdated(
182 Collection<DataTreeModification<Node>> changes, Class<RemoteUcastMacs> class1) {
183 Map<InstanceIdentifier<Node>, List<RemoteUcastMacs>> result
184 = new HashMap<InstanceIdentifier<Node>, List<RemoteUcastMacs>>();
185 if (changes != null && !changes.isEmpty()) {
186 for (DataTreeModification<Node> change : changes) {
187 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
188 final DataObjectModification<Node> mod = change.getRootNode();
189 Node updated = TransactUtils.getUpdated(mod);
190 Node before = mod.getDataBefore();
191 if (updated != null && before != null) {
192 List<RemoteUcastMacs> macListUpdated = null;
193 List<RemoteUcastMacs> macListBefore = null;
194 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
195 if (hgUpdated != null) {
196 macListUpdated = hgUpdated.getRemoteUcastMacs();
198 HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
199 if (hgBefore != null) {
200 macListBefore = hgBefore.getRemoteUcastMacs();
202 if (macListUpdated != null) {
203 if (macListBefore != null) {
204 macListUpdated.removeAll(macListBefore);
206 result.put(key, macListUpdated);