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.hwvtepsouthbound.HwvtepSouthboundConstants;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.McastMacsRemote;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
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.RemoteMcastMacs;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.common.base.Optional;
37 public class McastMacsRemoteUpdateCommand extends AbstractTransactCommand {
38 private static final Logger LOG = LoggerFactory.getLogger(McastMacsRemoteUpdateCommand.class);
40 public McastMacsRemoteUpdateCommand(HwvtepOperationalState state,
41 Collection<DataTreeModification<Node>> changes) {
42 super(state, changes);
46 public void execute(TransactionBuilder transaction) {
47 Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> createds =
48 extractCreated(getChanges(),RemoteMcastMacs.class);
49 if (!createds.isEmpty()) {
50 for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> created:
51 createds.entrySet()) {
52 updateMcastMacRemote(transaction, created.getKey(), created.getValue());
55 Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updateds =
56 extractUpdated(getChanges(),RemoteMcastMacs.class);
57 if (!updateds.isEmpty()) {
58 for (Entry<InstanceIdentifier<Node>, List<RemoteMcastMacs>> updated:
59 updateds.entrySet()) {
60 updateMcastMacRemote(transaction, updated.getKey(), updated.getValue());
65 private void updateMcastMacRemote(TransactionBuilder transaction,
66 InstanceIdentifier<Node> instanceIdentifier, List<RemoteMcastMacs> macList) {
67 for (RemoteMcastMacs mac: macList) {
68 LOG.debug("Creating remoteMcastMacs, mac address: {}", mac.getMacEntryKey().getValue());
69 Optional<RemoteMcastMacs> operationalMacOptional =
70 getOperationalState().getRemoteMcastMacs(instanceIdentifier, mac.getKey());
71 McastMacsRemote mcastMacsRemote = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsRemote.class);
72 setIpAddress(mcastMacsRemote, mac);
73 setLocatorSet(transaction, mcastMacsRemote, mac);
74 setLogicalSwitch(mcastMacsRemote, mac);
75 if (!operationalMacOptional.isPresent()) {
76 setMac(mcastMacsRemote, mac, operationalMacOptional);
77 LOG.trace("execute: create RemoteMcastMac entry: {}", mcastMacsRemote);
78 transaction.add(op.insert(mcastMacsRemote));
79 transaction.add(op.comment("McastMacRemote: Creating " + mac.getMacEntryKey().getValue()));
80 } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
81 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
82 McastMacsRemote extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
83 McastMacsRemote.class, null);
84 extraMac.getUuidColumn().setData(macEntryUUID);
85 LOG.trace("execute: update RemoteMcastMac entry: {}", mcastMacsRemote);
86 transaction.add(op.update(mcastMacsRemote)
87 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
89 transaction.add(op.comment("McastMacRemote: Updating " + macEntryUUID));
91 LOG.warn("Unable to update remoteMcastMacs {} because uuid not found in the operational store",
92 mac.getMacEntryKey().getValue());
97 private void setLogicalSwitch(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
98 if (inputMac.getLogicalSwitchRef() != null) {
99 @SuppressWarnings("unchecked")
100 InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
101 Optional<LogicalSwitches> operationalSwitchOptional =
102 getOperationalState().getLogicalSwitches(lswitchIid);
103 if (operationalSwitchOptional.isPresent()) {
104 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
105 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
106 mcastMacsRemote.setLogicalSwitch(logicalSwitchUUID);
108 mcastMacsRemote.setLogicalSwitch(TransactUtils.getLogicalSwitchUUID(lswitchIid));
113 private void setLocatorSet(TransactionBuilder transaction, McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
114 if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
115 UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction, inputMac.getLocatorSet());
116 mcastMacsRemote.setLocatorSet(locatorSetUuid);
120 private void setIpAddress(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac) {
121 if (inputMac.getIpaddr() != null) {
122 mcastMacsRemote.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
126 private void setMac(McastMacsRemote mcastMacsRemote, RemoteMcastMacs inputMac,
127 Optional<RemoteMcastMacs> inputSwitchOptional) {
128 if (inputMac.getMacEntryKey() != null) {
129 if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
130 mcastMacsRemote.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
132 mcastMacsRemote.setMac(inputMac.getMacEntryKey().getValue());
134 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
135 mcastMacsRemote.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
139 private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractCreated(
140 Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
141 Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
142 = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
143 if (changes != null && !changes.isEmpty()) {
144 for (DataTreeModification<Node> change : changes) {
145 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
146 final DataObjectModification<Node> mod = change.getRootNode();
147 Node created = TransactUtils.getCreated(mod);
148 if (created != null) {
149 List<RemoteMcastMacs> macListUpdated = null;
150 HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
151 if (hgAugmentation != null) {
152 macListUpdated = hgAugmentation.getRemoteMcastMacs();
154 if (macListUpdated != null) {
155 result.put(key, macListUpdated);
163 private Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> extractUpdated(
164 Collection<DataTreeModification<Node>> changes, Class<RemoteMcastMacs> class1) {
165 Map<InstanceIdentifier<Node>, List<RemoteMcastMacs>> result
166 = new HashMap<InstanceIdentifier<Node>, List<RemoteMcastMacs>>();
167 if (changes != null && !changes.isEmpty()) {
168 for (DataTreeModification<Node> change : changes) {
169 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
170 final DataObjectModification<Node> mod = change.getRootNode();
171 Node updated = TransactUtils.getUpdated(mod);
172 Node before = mod.getDataBefore();
173 if (updated != null && before != null) {
174 List<RemoteMcastMacs> macListUpdated = null;
175 List<RemoteMcastMacs> macListBefore = null;
176 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
177 if (hgUpdated != null) {
178 macListUpdated = hgUpdated.getRemoteMcastMacs();
180 HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
181 if (hgBefore != null) {
182 macListBefore = hgBefore.getRemoteMcastMacs();
184 if (macListUpdated != null) {
185 if (macListBefore != null) {
186 macListUpdated.removeAll(macListBefore);
188 result.put(key, macListUpdated);