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 com.google.common.base.Optional;
14 import java.util.Collection;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.ovsdb.lib.notation.UUID;
21 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
22 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
23 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
24 import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerMdsalUtils;
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.HwvtepPhysicalLocatorAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class UcastMacsLocalUpdateCommand extends AbstractTransactCommand<LocalUcastMacs, HwvtepGlobalAugmentation> {
37 private static final Logger LOG = LoggerFactory.getLogger(UcastMacsLocalUpdateCommand.class);
39 public UcastMacsLocalUpdateCommand(HwvtepOperationalState state,
40 Collection<DataTreeModification<Node>> changes) {
41 super(state, changes);
45 public void execute(TransactionBuilder transaction) {
46 Map<InstanceIdentifier<Node>, List<LocalUcastMacs>> updateds =
47 extractUpdated(getChanges(),LocalUcastMacs.class);
48 if (!updateds.isEmpty()) {
49 for (Entry<InstanceIdentifier<Node>, List<LocalUcastMacs>> updated:
50 updateds.entrySet()) {
51 updateUcastMacsLocal(transaction, updated.getKey(), updated.getValue());
56 private void updateUcastMacsLocal(TransactionBuilder transaction,
57 InstanceIdentifier<Node> instanceIdentifier, List<LocalUcastMacs> localUcastMacs) {
58 for (LocalUcastMacs localUcastMac: localUcastMacs) {
59 LOG.debug("Creating localUcastMacs, mac address: {}", localUcastMac.getMacEntryKey().getValue());
60 final Optional<LocalUcastMacs> operationalMacOptional =
61 getOperationalState().getLocalUcastMacs(instanceIdentifier, localUcastMac.key());
62 UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
63 UcastMacsLocal.class);
64 setIpAddress(ucastMacsLocal, localUcastMac);
65 setLocator(transaction, ucastMacsLocal, localUcastMac);
66 setLogicalSwitch(ucastMacsLocal, localUcastMac);
67 if (!operationalMacOptional.isPresent()) {
68 setMac(ucastMacsLocal, localUcastMac, operationalMacOptional);
69 LOG.trace("execute: creating LocalUcastMac entry: {}", ucastMacsLocal);
70 transaction.add(op.insert(ucastMacsLocal));
71 transaction.add(op.comment("UcastMacLocal: Creating " + localUcastMac.getMacEntryKey().getValue()));
72 } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
73 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
74 UcastMacsLocal extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
75 UcastMacsLocal.class, null);
76 extraMac.getUuidColumn().setData(macEntryUUID);
77 LOG.trace("execute: updating LocalUcastMac entry: {}", ucastMacsLocal);
78 transaction.add(op.update(ucastMacsLocal)
79 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
81 transaction.add(op.comment("UcastMacLocal: Updating " + macEntryUUID));
83 LOG.warn("Unable to update localUcastMacs {} because uuid not found in the operational store",
84 localUcastMac.getMacEntryKey().getValue());
89 private void setLogicalSwitch(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac) {
90 if (inputMac.getLogicalSwitchRef() != null) {
91 @SuppressWarnings("unchecked")
92 InstanceIdentifier<LogicalSwitches> lswitchIid =
93 (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 ucastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
102 "Create or update localUcastMacs: No logical switch with iid {} found in operational datastore!",
108 private void setLocator(TransactionBuilder transaction, UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac) {
109 //get UUID by locatorRef
110 if (inputMac.getLocatorRef() != null) {
111 UUID locatorUuid = null;
112 @SuppressWarnings("unchecked")
113 InstanceIdentifier<TerminationPoint> iid =
114 (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 //if no, get it from config DS and create id
124 Optional<TerminationPoint> configLocatorOptional = new ControllerMdsalUtils(
125 getOperationalState().getDataBroker()).readOptional(LogicalDatastoreType.CONFIGURATION, iid);
126 if (configLocatorOptional.isPresent()) {
127 HwvtepPhysicalLocatorAugmentation locatorAugmentation =
128 configLocatorOptional.get().augmentation(HwvtepPhysicalLocatorAugmentation.class);
129 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation,
130 getOperationalState());
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());
158 protected List<LocalUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
159 return augmentation.getLocalUcastMacs();