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.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.McastMacsLocal;
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.LocalMcastMacs;
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.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 McastMacsLocalUpdateCommand extends AbstractTransactCommand {
38 private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortRemoveCommand.class);
40 public McastMacsLocalUpdateCommand(HwvtepOperationalState state,
41 Collection<DataTreeModification<Node>> changes) {
42 super(state, changes);
46 public void execute(TransactionBuilder transaction) {
47 Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> createds =
48 extractCreated(getChanges(),LocalMcastMacs.class);
49 if (!createds.isEmpty()) {
50 for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> created:
51 createds.entrySet()) {
52 updateMcastMacsLocal(transaction, created.getKey(), created.getValue());
55 Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> updateds =
56 extractUpdated(getChanges(),LocalMcastMacs.class);
57 if (!updateds.isEmpty()) {
58 for (Entry<InstanceIdentifier<Node>, List<LocalMcastMacs>> updated:
59 updateds.entrySet()) {
60 updateMcastMacsLocal(transaction, updated.getKey(), updated.getValue());
65 private void updateMcastMacsLocal(TransactionBuilder transaction,
66 InstanceIdentifier<Node> instanceIdentifier, List<LocalMcastMacs> localMcastMacs) {
67 for (LocalMcastMacs localMcastMac: localMcastMacs) {
68 LOG.debug("Creating localMcastMac, mac address: {}", localMcastMac.getMacEntryKey().getValue());
69 Optional<LocalMcastMacs> operationalMacOptional =
70 getOperationalState().getLocalMcastMacs(instanceIdentifier, localMcastMac.getKey());
71 McastMacsLocal mcastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsLocal.class);
72 setIpAddress(mcastMacsLocal, localMcastMac);
73 setLocatorSet(transaction, mcastMacsLocal, localMcastMac);
74 setLogicalSwitch(mcastMacsLocal, localMcastMac);
75 if (!operationalMacOptional.isPresent()) {
76 setMac(mcastMacsLocal, localMcastMac, operationalMacOptional);
77 transaction.add(op.insert(mcastMacsLocal));
79 LocalMcastMacs updatedMac = operationalMacOptional.get();
80 String existingMac = updatedMac.getMacEntryKey().getValue();
81 McastMacsLocal extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), McastMacsLocal.class);
83 transaction.add(op.update(mcastMacsLocal)
84 .where(extraMac.getMacColumn().getSchema().opEqual(existingMac))
90 private void setLogicalSwitch(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
91 if (inputMac.getLogicalSwitchRef() != null) {
92 @SuppressWarnings("unchecked")
93 InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
94 Optional<LogicalSwitches> operationalSwitchOptional =
95 getOperationalState().getLogicalSwitches(lswitchIid);
96 if (operationalSwitchOptional.isPresent()) {
97 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
98 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
99 mcastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
101 LOG.warn("Create or update localMcastMac: No logical switch with iid {} found in operational datastore!",
107 private void setLocatorSet(TransactionBuilder transaction, McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
108 if (inputMac.getLocatorSet() != null && !inputMac.getLocatorSet().isEmpty()) {
109 UUID locatorSetUuid = TransactUtils.createPhysicalLocatorSet(getOperationalState(), transaction, inputMac.getLocatorSet());
110 mcastMacsLocal.setLocatorSet(locatorSetUuid);
114 private void setIpAddress(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac) {
115 if (inputMac.getIpaddr() != null) {
116 mcastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
120 private void setMac(McastMacsLocal mcastMacsLocal, LocalMcastMacs inputMac,
121 Optional<LocalMcastMacs> inputSwitchOptional) {
122 if (inputMac.getMacEntryKey() != null) {
123 if (inputMac.getMacEntryKey().equals(HwvtepSouthboundConstants.UNKNOWN_DST_MAC)) {
124 mcastMacsLocal.setMac(HwvtepSouthboundConstants.UNKNOWN_DST_STRING);
126 mcastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
128 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
129 mcastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
133 private Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> extractCreated(
134 Collection<DataTreeModification<Node>> changes, Class<LocalMcastMacs> class1) {
135 Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> result
136 = new HashMap<InstanceIdentifier<Node>, List<LocalMcastMacs>>();
137 if (changes != null && !changes.isEmpty()) {
138 for (DataTreeModification<Node> change : changes) {
139 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
140 final DataObjectModification<Node> mod = change.getRootNode();
141 Node created = TransactUtils.getCreated(mod);
142 if (created != null) {
143 List<LocalMcastMacs> macListUpdated = null;
144 HwvtepGlobalAugmentation hgAugmentation = created.getAugmentation(HwvtepGlobalAugmentation.class);
145 if (hgAugmentation != null) {
146 macListUpdated = hgAugmentation.getLocalMcastMacs();
148 if (macListUpdated != null) {
149 result.put(key, macListUpdated);
157 private Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> extractUpdated(
158 Collection<DataTreeModification<Node>> changes, Class<LocalMcastMacs> class1) {
159 Map<InstanceIdentifier<Node>, List<LocalMcastMacs>> result
160 = new HashMap<InstanceIdentifier<Node>, List<LocalMcastMacs>>();
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 updated = TransactUtils.getUpdated(mod);
166 Node before = mod.getDataBefore();
167 if (updated != null && before != null) {
168 List<LocalMcastMacs> macListUpdated = null;
169 List<LocalMcastMacs> macListBefore = null;
170 HwvtepGlobalAugmentation hgUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class);
171 if (hgUpdated != null) {
172 macListUpdated = hgUpdated.getLocalMcastMacs();
174 HwvtepGlobalAugmentation hgBefore = before.getAugmentation(HwvtepGlobalAugmentation.class);
175 if (hgBefore != null) {
176 macListBefore = hgBefore.getLocalMcastMacs();
178 if (macListUpdated != null) {
179 if (macListBefore != null) {
180 macListUpdated.removeAll(macListBefore);
182 result.put(key, macListUpdated);