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.List;
16 import java.util.Map.Entry;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
21 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
22 import org.opendaylight.ovsdb.schema.hardwarevtep.UcastMacsLocal;
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 import com.google.common.base.Optional;
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 Optional<LocalUcastMacs> operationalMacOptional =
61 getOperationalState().getLocalUcastMacs(instanceIdentifier, localUcastMac.getKey());
62 UcastMacsLocal ucastMacsLocal = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), UcastMacsLocal.class);
63 setIpAddress(ucastMacsLocal, localUcastMac);
64 setLocator(transaction, ucastMacsLocal, localUcastMac);
65 setLogicalSwitch(ucastMacsLocal, localUcastMac);
66 if (!operationalMacOptional.isPresent()) {
67 setMac(ucastMacsLocal, localUcastMac, operationalMacOptional);
68 LOG.trace("execute: creating LocalUcastMac entry: {}", ucastMacsLocal);
69 transaction.add(op.insert(ucastMacsLocal));
70 transaction.add(op.comment("UcastMacLocal: Creating " + localUcastMac.getMacEntryKey().getValue()));
71 } else if (operationalMacOptional.get().getMacEntryUuid() != null) {
72 UUID macEntryUUID = new UUID(operationalMacOptional.get().getMacEntryUuid().getValue());
73 UcastMacsLocal extraMac = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
74 UcastMacsLocal.class, null);
75 extraMac.getUuidColumn().setData(macEntryUUID);
76 LOG.trace("execute: updating LocalUcastMac entry: {}", ucastMacsLocal);
77 transaction.add(op.update(ucastMacsLocal)
78 .where(extraMac.getUuidColumn().getSchema().opEqual(macEntryUUID))
80 transaction.add(op.comment("UcastMacLocal: Updating " + macEntryUUID));
82 LOG.warn("Unable to update localUcastMacs {} because uuid not found in the operational store",
83 localUcastMac.getMacEntryKey().getValue());
88 private void setLogicalSwitch(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac) {
89 if (inputMac.getLogicalSwitchRef() != null) {
90 @SuppressWarnings("unchecked")
91 InstanceIdentifier<LogicalSwitches> lswitchIid = (InstanceIdentifier<LogicalSwitches>) inputMac.getLogicalSwitchRef().getValue();
92 Optional<LogicalSwitches> operationalSwitchOptional =
93 getOperationalState().getLogicalSwitches(lswitchIid);
94 if (operationalSwitchOptional.isPresent()) {
95 Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
96 UUID logicalSwitchUUID = new UUID(logicalSwitchUuid.getValue());
97 ucastMacsLocal.setLogicalSwitch(logicalSwitchUUID);
99 LOG.warn("Create or update localUcastMacs: No logical switch with iid {} found in operational datastore!",
105 private void setLocator(TransactionBuilder transaction, UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac) {
106 //get UUID by locatorRef
107 if (inputMac.getLocatorRef() != null) {
108 UUID locatorUuid = null;
109 @SuppressWarnings("unchecked")
110 InstanceIdentifier<TerminationPoint> iid = (InstanceIdentifier<TerminationPoint>) inputMac.getLocatorRef().getValue();
111 //try to find locator in operational DS
112 Optional<HwvtepPhysicalLocatorAugmentation> operationalLocatorOptional =
113 getOperationalState().getPhysicalLocatorAugmentation(iid);
114 if (operationalLocatorOptional.isPresent()) {
116 HwvtepPhysicalLocatorAugmentation locatorAugmentation = operationalLocatorOptional.get();
117 locatorUuid = new UUID(locatorAugmentation.getPhysicalLocatorUuid().getValue());
119 //if no, get it from config DS and create id
120 Optional<TerminationPoint> configLocatorOptional =
121 TransactUtils.readNodeFromConfig(getOperationalState().getReadWriteTransaction(), iid);
122 if (configLocatorOptional.isPresent()) {
123 HwvtepPhysicalLocatorAugmentation locatorAugmentation =
124 configLocatorOptional.get().getAugmentation(HwvtepPhysicalLocatorAugmentation.class);
125 locatorUuid = TransactUtils.createPhysicalLocator(transaction, locatorAugmentation);
127 LOG.warn("Create or update localUcastMac: No physical locator found in operational datastore!"
128 + "Its indentifier is {}", inputMac.getLocatorRef().getValue());
131 if (locatorUuid != null) {
132 ucastMacsLocal.setLocator(locatorUuid);
137 private void setIpAddress(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac) {
138 if (inputMac.getIpaddr() != null) {
139 ucastMacsLocal.setIpAddress(inputMac.getIpaddr().getIpv4Address().getValue());
143 private void setMac(UcastMacsLocal ucastMacsLocal, LocalUcastMacs inputMac,
144 Optional<LocalUcastMacs> inputSwitchOptional) {
145 if (inputMac.getMacEntryKey() != null) {
146 ucastMacsLocal.setMac(inputMac.getMacEntryKey().getValue());
147 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getMacEntryKey() != null) {
148 ucastMacsLocal.setMac(inputSwitchOptional.get().getMacEntryKey().getValue());
152 protected List<LocalUcastMacs> getData(HwvtepGlobalAugmentation augmentation) {
153 return augmentation.getLocalUcastMacs();