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
8 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import java.util.Collection;
13 import java.util.List;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
22 import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerMdsalUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class UcastMacsLocalUpdateCommand extends AbstractTransactCommand<LocalUcastMacs, HwvtepGlobalAugmentation> {
35 private static final Logger LOG = LoggerFactory.getLogger(UcastMacsLocalUpdateCommand.class);
37 public UcastMacsLocalUpdateCommand(final HwvtepOperationalState state,
38 final Collection<DataTreeModification<Node>> changes) {
39 super(state, changes);
43 public void execute(final TransactionBuilder transaction) {
44 Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> updateds =
45 extractUpdated(getChanges(),LocalUcastMacs.class);
46 if (!updateds.isEmpty()) {
47 for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> updated:
48 updateds.entrySet()) {
49 updateUcastMacsLocal(transaction, updated.getKey(), updated.getValue());
54 private void updateUcastMacsLocal(final TransactionBuilder transaction,
55 final InstanceIdentifier<Node> instanceIdentifier, final List<LocalUcastMacs> localUcastMacs) {
56 for (LocalUcastMacs localUcastMac: localUcastMacs) {
57 LOG.debug("Creating localUcastMacs, mac address: {}", localUcastMac.getMacEntryKey().getValue());
58 final Optional<LocalUcastMacs> operationalMacOptional =
59 getOperationalState().getLocalUcastMacs(instanceIdentifier, localUcastMac.key());
60 UcastMacsLocal ucastMacsLocal = transaction.getTypedRowWrapper(UcastMacsLocal.class);
61 setIpAddress(ucastMacsLocal, localUcastMac);
62 setLocator(transaction, ucastMacsLocal, localUcastMac);
63 setLogicalSwitch(ucastMacsLocal, localUcastMac);
64 if (!operationalMacOptional.isPresent()) {
65 setMac(ucastMacsLocal, localUcastMac, operationalMacOptional);
66 LOG.trace("execute: creating LocalUcastMac entry: {}", ucastMacsLocal);
67 transaction.add(op.insert(ucastMacsLocal));
68 transaction.add(op.comment("UcastMacLocal: Creating " + localUcastMac.getMacEntryKey().getValue()));
69 } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
70 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
71 UcastMacsLocal extraMac = transaction.getTypedRowSchema(UcastMacsLocal.class);
72 extraMac.getUuidColumn().setData(macEntryUUID);
73 LOG.trace("execute: updating LocalUcastMac entry: {}", ucastMacsLocal);
74 transaction.add(op.update(ucastMacsLocal)
75 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
77 transaction.add(op.comment("UcastMacLocal: Updating " + macEntryUUID));
79 LOG.warn("Unable to update localUcastMacs {} because uuid not found in the operational store",
80 localUcastMac.getMacEntryKey().getValue());
85 private void setLogicalSwitch(final UcastMacsLocal ucastMacsLocal, final LocalUcastMacs inputMac) {
86 if (inputMac.getLogicalSwitchRef() != null) {
87 @SuppressWarnings("unchecked")
88 InstanceIdentifier<LogicalSwitches> lswitchIid =
89 (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
90 Optional<LogicalSwitches> operationalSwitchOptional =
91 getOperationalState().getLogicalSwitches(lswitchIid);
92 if (operationalSwitchOptional.isPresent()) {
93 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
94 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
95 ucastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
98 "Create or update localUcastMacs: No logical switch with iid {} found in operational datastore!",
104 private void setLocator(final TransactionBuilder transaction, final UcastMacsLocal ucastMacsLocal,
105 final LocalUcastMacs inputMac) {
106 //get UUID by locatorRef
107 if (inputMac.getLocatorRef() != null) {
108 UUID locatorUuid = null;
109 @SuppressWarnings("unchecked")
110 InstanceIdentifier<TerminationPoint> iid =
111 (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
112 //try to find locator in operational DS
113 Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
114 getOperationalState().getPhysicalLocatorAugmentation(iid);
115 if (operationalLocatorOptional.isPresent()) {
117 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
118 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
120 //if no, get it from config DS and create id
121 Optional<TerminationPoint> configLocatorOptional = new ControllerMdsalUtils(
122 getOperationalState().getDataBroker()).readOptional(LogicalDatastoreType.CONFIGURATION, iid);
123 if (configLocatorOptional.isPresent()) {
124 HwvtepPhysicalLocatorAugmentation locatorAugmentation =
125 configLocatorOptional.get().augmentation(HwvtepPhysicalLocatorAugmentation.class);
126 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation,
127 getOperationalState());
129 LOG.warn("Create or update localUcastMac: No physical locator found in operational datastore!"
130 + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
133 if (locatorUuid != null) {
134 ucastMacsLocal.setLocator(locatorUuid);
139 private static void setIpAddress(final UcastMacsLocal ucastMacsLocal, final LocalUcastMacs inputMac) {
140 if (inputMac.getIpaddr() != null) {
141 ucastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
145 private static void setMac(final UcastMacsLocal ucastMacsLocal, final LocalUcastMacs inputMac,
146 final Optional<LocalUcastMacs> inputSwitchOptional) {
147 if (inputMac.getMacEntryKey() != null) {
148 ucastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
149 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
150 ucastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
155 protected List<LocalUcastMacs> getData(final HwvtepGlobalAugmentation augmentation) {
156 return augmentation.getLocalUcastMacs();