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.HashSet;
16 import java.util.List;
18 import java.util.Map.Entry;
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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 {
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>> createds =
46 extractCreated(getChanges(),LogicalSwitches.class);
47 if (!createds.isEmpty()) {
48 for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created:
49 createds.entrySet()) {
50 updateLogicalSwitch(transaction, created.getKey(), created.getValue());
53 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
54 extractUpdated(getChanges(),LogicalSwitches.class);
55 if (!updateds.isEmpty()) {
56 for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
57 updateds.entrySet()) {
58 updateLogicalSwitch(transaction, updated.getKey(), updated.getValue());
63 private void updateLogicalSwitch(TransactionBuilder transaction,
64 InstanceIdentifier<Node> instanceIdentifier, List<LogicalSwitches> lswitchList) {
65 for (LogicalSwitches lswitch: lswitchList) {
66 LOG.debug("Creating logcial switch named: {}", lswitch.getHwvtepNodeName());
67 Optional<LogicalSwitches> operationalSwitchOptional =
68 getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
69 LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
70 setDescription(logicalSwitch, lswitch);
71 setTunnelKey(logicalSwitch, lswitch);
72 if (!operationalSwitchOptional.isPresent()) {
73 setName(logicalSwitch, lswitch, operationalSwitchOptional);
74 transaction.add(op.insert(logicalSwitch));
76 LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
77 String existingLogicalSwitchName = updatedLSwitch.getHwvtepNodeName().getValue();
78 // Name is immutable, and so we *can't* update it. So we use extraBridge for the schema stuff
79 LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
80 extraLogicalSwitch.setName("");
81 transaction.add(op.update(logicalSwitch)
82 .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(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<Long>();
106 tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
107 logicalSwitch.setTunnelKey(tunnel);
111 private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractCreated(
112 Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
113 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
114 = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
115 if (changes != null && !changes.isEmpty()) {
116 for (DataTreeModification<Node> change : changes) {
117 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
118 final DataObjectModification<Node> mod = change.getRootNode();
119 Node created = TransactUtils.getCreated(mod);
120 if (created != null) {
121 List<LogicalSwitches> lswitchListUpdated = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
122 if (lswitchListUpdated != null) {
123 result.put(key, lswitchListUpdated);
131 private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractUpdated(
132 Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
133 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
134 = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
135 if (changes != null && !changes.isEmpty()) {
136 for (DataTreeModification<Node> change : changes) {
137 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
138 final DataObjectModification<Node> mod = change.getRootNode();
139 Node updated = TransactUtils.getUpdated(mod);
140 Node before = mod.getDataBefore();
141 if (updated != null && before != null) {
142 List<LogicalSwitches> lswitchListUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
143 List<LogicalSwitches> lswitchListBefore = before.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
144 if (lswitchListUpdated != null) {
145 if (lswitchListBefore != null) {
146 lswitchListUpdated.removeAll(lswitchListBefore);
148 result.put(key, lswitchListUpdated);