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.UcastMacsLocal;
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.LocalUcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitchesKey;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
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 UcastMacsLocalUpdateCommand extends AbstractTransactCommand {
41 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
43 public UcastMacsLocalUpdateCommand(HwvtepOperationalState state,
44 Collection<DataTreeModification<Node>> changes) {
45 super(state, changes);
49 public void execute(TransactionBuilder transaction) {
50 Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> createds =
51 extractCreated(getChanges(),LocalUcastMacs.class);
52 if (!createds.isEmpty()) {
53 for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> created:
54 createds.entrySet()) {
55 updateUcastMacsLocal(transaction, created.getKey(), created.getValue());
58 Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> updateds =
59 extractUpdated(getChanges(),LocalUcastMacs.class);
60 if (!updateds.isEmpty()) {
61 for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> updated:
62 updateds.entrySet()) {
63 updateUcastMacsLocal(transaction, updated.getKey(), updated.getValue());
68 private void updateUcastMacsLocal(TransactionBuilder transaction,
69 InstanceIdentifier<Node> instanceIdentifier, List<LocalUcastMacs> localUcastMacs) {
70 for (LocalUcastMacs localUcastMac: localUcastMacs) {
71 LOG.debug("Creating localUcastMacs, mac address: {}", localUcastMac.getMacEntryKey().getValue());
72 Optional<LocalUcastMacs> operationalMacOptional =
73 getOperationalState().getLocalUcastMacs(instanceIdentifier, localUcastMac.getKey());
74 UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsLocal.class);
75 setIpAddress(ucastMacsLocal, localUcastMac);
76 setLocator(transaction, ucastMacsLocal, localUcastMac);
77 setLogicalSwitch(ucastMacsLocal, localUcastMac);
78 if (!operationalMacOptional.isPresent()) {
79 setMac(ucastMacsLocal, localUcastMac, operationalMacOptional);
80 transaction.add(op.insert(ucastMacsLocal));
82 LocalUcastMacs updatedMac = operationalMacOptional.get();
83 String existingMac = updatedMac.getMacEntryKey().getValue();
84 UcastMacsLocal extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsLocal.class);
86 transaction.add(op.update(ucastMacsLocal)
87 .where(extraMac.getMacColumn().getSchema().opEqual(existingMac))
93 private void setLogicalSwitch(UcastMacsLocal ucastMacsLocal, LocalUcastMacs 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 ucastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
104 LOG.warn("Create or update localUcastMacs: No logical switch with iid {} found in operational datastore!",
110 private void setLocator(TransactionBuilder transaction, UcastMacsLocal ucastMacsLocal, LocalUcastMacs 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 //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 localUcastMac: No physical locator found in operational datastore!"
133 + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
136 if (locatorUuid != null) {
137 ucastMacsLocal.setLocator(locatorUuid);
142 private void setIpAddress(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac) {
143 if (inputMac.getIpaddr() != null) {
144 ucastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
148 private void setMac(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac,
149 Optional<LocalUcastMacs> inputSwitchOptional) {
150 if (inputMac.getMacEntryKey() != null) {
151 ucastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
152 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
153 ucastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
157 private Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> extractCreated(
158 Collection<DataTreeModification<Node>> changes, Class<LocalUcastMacs> class1) {
159 Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> result
160 = new HashMap<InstanceIdentifier<Node>, List<LocalUcastMacs>>();
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<LocalUcastMacs> macListUpdated = null;
168 HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
169 if (hgAugmentation != null) {
170 macListUpdated = hgAugmentation.getLocalUcastMacs();
172 if (macListUpdated != null) {
173 result.put(key, macListUpdated);
181 private Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> extractUpdated(
182 Collection<DataTreeModification<Node>> changes, Class<LocalUcastMacs> class1) {
183 Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> result
184 = new HashMap<InstanceIdentifier<Node>, List<LocalUcastMacs>>();
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<LocalUcastMacs> macListUpdated = null;
193 List<LocalUcastMacs> macListBefore = null;
194 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
195 if (hgUpdated != null) {
196 macListUpdated = hgUpdated.getLocalUcastMacs();
198 HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
199 if (hgBefore != null) {
200 macListBefore = hgBefore.getLocalUcastMacs();
202 if (macListUpdated != null) {
203 if (macListBefore != null) {
204 macListUpdated.removeAll(macListBefore);
206 result.put(key, macListUpdated);