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.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.hwvtepsouthbound.HwvtepSouthboundUtil;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
26 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
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.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.common.base.Optional;
37 public class LogicalSwitchUpdateCommand extends AbstractTransactCommand {
38 private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
40 public LogicalSwitchUpdateCommand(HwvtepOperationalState state,
41 Collection<DataTreeModification<Node>> changes) {
42 super(state, changes);
46 public void execute(TransactionBuilder transaction) {
47 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> createds =
48 extractCreated(getChanges(),LogicalSwitches.class);
49 if (!createds.isEmpty()) {
50 for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created:
51 createds.entrySet()) {
52 updateLogicalSwitch(transaction, created.getKey(), created.getValue());
55 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
56 extractUpdated(getChanges(),LogicalSwitches.class);
57 if (!updateds.isEmpty()) {
58 for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
59 updateds.entrySet()) {
60 updateLogicalSwitch(transaction, updated.getKey(), updated.getValue());
65 private void updateLogicalSwitch(TransactionBuilder transaction,
66 InstanceIdentifier<Node> instanceIdentifier, List<LogicalSwitches> lswitchList) {
67 for (LogicalSwitches lswitch: lswitchList) {
68 InstanceIdentifier<LogicalSwitches> lsKey = instanceIdentifier.
69 augmentation(HwvtepGlobalAugmentation.class).child(LogicalSwitches.class, lswitch.getKey());
70 LOG.debug("Creating logcial switch named: {}", lswitch.getHwvtepNodeName());
71 Optional<LogicalSwitches> operationalSwitchOptional =
72 getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
73 LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
74 setDescription(logicalSwitch, lswitch);
75 setTunnelKey(logicalSwitch, lswitch);
76 if (!operationalSwitchOptional.isPresent()) {
77 setName(logicalSwitch, lswitch, operationalSwitchOptional);
78 LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
79 transaction.add(op.insert(logicalSwitch).withId(TransactUtils.getLogicalSwitchId(lswitch)));
80 transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
82 LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
83 String existingLogicalSwitchName = updatedLSwitch.getHwvtepNodeName().getValue();
84 // Name is immutable, and so we *can't* update it. So we use extraBridge for the schema stuff
85 LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
86 extraLogicalSwitch.setName("");
87 LOG.trace("execute: updating LogicalSwitch entry: {}", logicalSwitch);
88 transaction.add(op.update(logicalSwitch)
89 .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
91 transaction.add(op.comment("Logical Switch: Updating " + existingLogicalSwitchName));
93 UUID lsUuid = new UUID(TransactUtils.getLogicalSwitchId(lswitch));
94 updateCurrentTxData(LogicalSwitches.class, lsKey, lsUuid, lswitch);
98 private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
99 if(inputSwitch.getHwvtepNodeDescription() != null) {
100 logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
104 private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch,
105 Optional<LogicalSwitches> inputSwitchOptional) {
106 if (inputSwitch.getHwvtepNodeName() != null) {
107 logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
108 } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getHwvtepNodeName() != null) {
109 logicalSwitch.setName(inputSwitchOptional.get().getHwvtepNodeName().getValue());
113 private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
114 if (inputSwitch.getTunnelKey() != null) {
115 Set<Long> tunnel = new HashSet<Long>();
116 tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
117 logicalSwitch.setTunnelKey(tunnel);
121 private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractCreated(
122 Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
123 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
124 = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
125 if (changes != null && !changes.isEmpty()) {
126 for (DataTreeModification<Node> change : changes) {
127 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
128 final DataObjectModification<Node> mod = change.getRootNode();
129 Node created = TransactUtils.getCreated(mod);
130 if (created != null) {
131 List<LogicalSwitches> lswitchListUpdated = null;
132 if (created.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
133 lswitchListUpdated = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
135 if (lswitchListUpdated != null) {
136 result.put(key, lswitchListUpdated);
144 private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractUpdated(
145 Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
146 Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
147 = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
148 if (changes != null && !changes.isEmpty()) {
149 for (DataTreeModification<Node> change : changes) {
150 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
151 final DataObjectModification<Node> mod = change.getRootNode();
152 Node updated = TransactUtils.getUpdated(mod);
153 Node before = mod.getDataBefore();
154 if (updated != null && before != null) {
155 List<LogicalSwitches> lswitchListUpdated = null;
156 List<LogicalSwitches> lswitchListBefore = null;
157 if (updated.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
158 lswitchListUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
160 if (before.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
161 lswitchListBefore = before.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
163 if (lswitchListUpdated != null) {
164 if (lswitchListBefore != null) {
165 lswitchListUpdated.removeAll(lswitchListBefore);
167 result.put(key, lswitchListUpdated);