2 * Copyright © 2015, 2017 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.HashSet;
15 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.Objects;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.LogicalSwitch;
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.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.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.base.Optional;
35 public class LogicalSwitchUpdateCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
36 private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
38 public LogicalSwitchUpdateCommand(HwvtepOperationalState state,
39 Collection<DataTreeModification<Node>> changes) {
40 super(state, changes);
44 public void execute(TransactionBuilder transaction) {
45 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
46 extractUpdated(getChanges(),LogicalSwitches.class);
47 if (!updateds.isEmpty()) {
48 for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
49 updateds.entrySet()) {
50 updateLogicalSwitch(transaction, updated.getKey(), updated.getValue());
55 private void updateLogicalSwitch(TransactionBuilder transaction,
56 InstanceIdentifier<Node> instanceIdentifier, List<LogicalSwitches> lswitchList) {
57 for (LogicalSwitches lswitch: lswitchList) {
58 InstanceIdentifier<LogicalSwitches> lsKey = instanceIdentifier.
59 augmentation(HwvtepGlobalAugmentation.class).child(LogicalSwitches.class, lswitch.getKey());
60 LOG.debug("Creating logical switch named: {}", lswitch.getHwvtepNodeName());
61 Optional<LogicalSwitches> operationalSwitchOptional =
62 getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
63 LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
64 setDescription(logicalSwitch, lswitch);
65 setTunnelKey(logicalSwitch, lswitch);
66 if (!operationalSwitchOptional.isPresent()) {
67 setName(logicalSwitch, lswitch, operationalSwitchOptional);
68 LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
69 transaction.add(op.insert(logicalSwitch).withId(TransactUtils.getLogicalSwitchId(lswitch)));
70 transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
71 UUID lsUuid = new UUID(TransactUtils.getLogicalSwitchId(lswitch));
72 updateCurrentTxData(LogicalSwitches.class, lsKey, lsUuid, lswitch);
74 LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
75 String existingLogicalSwitchName = updatedLSwitch.getHwvtepNodeName().getValue();
76 // Name is immutable, and so we *can't* update it. So we use extraBridge for the schema stuff
77 LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
78 extraLogicalSwitch.setName("");
79 LOG.trace("execute: updating LogicalSwitch entry: {}", logicalSwitch);
80 transaction.add(op.update(logicalSwitch)
81 .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
83 transaction.add(op.comment("Logical Switch: Updating " + existingLogicalSwitchName));
88 private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
89 if(inputSwitch.getHwvtepNodeDescription() != null) {
90 logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
94 private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch,
95 Optional<LogicalSwitches> inputSwitchOptional) {
96 if (inputSwitch.getHwvtepNodeName() != null) {
97 logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
98 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getHwvtepNodeName() != null) {
99 logicalSwitch.setName(inputSwitchOptional.get().getHwvtepNodeName().getValue());
103 private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
104 if (inputSwitch.getTunnelKey() != null) {
105 Set<Long> tunnel = new HashSet<>();
106 tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
107 logicalSwitch.setTunnelKey(tunnel);
112 protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
113 return augmentation.getLogicalSwitches();
117 protected boolean areEqual(LogicalSwitches a , LogicalSwitches b) {
118 return a.getKey().equals(b.getKey()) && Objects.equals(a.getTunnelKey(), b.getTunnelKey());