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;
17 import java.util.Map.Entry;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
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.PhysicalSwitch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIps;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
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 PhysicalSwitchUpdateCommand extends AbstractTransactCommand {
38 private static final Logger LOG = LoggerFactory.getLogger(PhysicalSwitchUpdateCommand.class);
40 public PhysicalSwitchUpdateCommand(HwvtepOperationalState state,
41 Collection<DataTreeModification<Node>> changes) {
42 super(state, changes);
46 public void execute(TransactionBuilder transaction) {
47 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> created =
48 extractCreated(getChanges(),PhysicalSwitchAugmentation.class);
49 if (!created.isEmpty()) {
50 for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
52 updatePhysicalSwitch(transaction, physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
55 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> updated =
56 extractUpdated(getChanges(),PhysicalSwitchAugmentation.class);
57 if (!updated.isEmpty()) {
58 for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
60 updatePhysicalSwitch(transaction, physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
66 private void updatePhysicalSwitch(TransactionBuilder transaction,
67 InstanceIdentifier<Node> iid, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
68 LOG.debug("Creating a physical switch named: {}", physicalSwitchAugmentation.getHwvtepNodeName());
69 Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional =
70 getOperationalState().getPhysicalSwitchAugmentation(iid);
71 PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
72 setDescription(physicalSwitch, physicalSwitchAugmentation);
73 setManagementIps(physicalSwitch, physicalSwitchAugmentation);
74 setTunnuleIps(physicalSwitch, physicalSwitchAugmentation);
75 setTunnels(physicalSwitch, physicalSwitchAugmentation);
76 if (!operationalPhysicalSwitchOptional.isPresent()) {
77 setName(physicalSwitch, physicalSwitchAugmentation, operationalPhysicalSwitchOptional);
78 transaction.add(op.insert(physicalSwitch));
80 PhysicalSwitchAugmentation updatedPhysicalSwitch = operationalPhysicalSwitchOptional.get();
81 String existingPhysicalSwitchName = updatedPhysicalSwitch.getHwvtepNodeName().getValue();
82 // Name is immutable, and so we *can't* update it. So we use extraPhysicalSwitch for the schema stuff
83 PhysicalSwitch extraPhysicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
84 extraPhysicalSwitch.setName("");
85 transaction.add(op.update(physicalSwitch)
86 .where(extraPhysicalSwitch.getNameColumn().getSchema().opEqual(existingPhysicalSwitchName))
91 private void setName(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation,
92 Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional) {
93 if (physicalSwitchAugmentation.getHwvtepNodeName() != null) {
94 physicalSwitch.setName(physicalSwitchAugmentation.getHwvtepNodeName().getValue());
95 } else if (operationalPhysicalSwitchOptional.isPresent() && operationalPhysicalSwitchOptional.get().getHwvtepNodeName() != null) {
96 physicalSwitch.setName(operationalPhysicalSwitchOptional.get().getHwvtepNodeName().getValue());
100 private void setDescription(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
101 if (physicalSwitchAugmentation.getHwvtepNodeDescription() != null) {
102 physicalSwitch.setDescription(physicalSwitchAugmentation.getHwvtepNodeDescription());
106 private void setManagementIps(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
107 Set<String> ipSet = new HashSet<String>();
108 if (physicalSwitchAugmentation.getManagementIps() != null) {
109 for (ManagementIps ip: physicalSwitchAugmentation.getManagementIps()) {
110 ipSet.add(ip.getManagementIpsKey().getIpv4Address().getValue());
112 physicalSwitch.setManagementIps(ipSet);
116 private void setTunnuleIps(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
117 Set<String> ipSet = new HashSet<String>();
118 if (physicalSwitchAugmentation.getTunnelIps() != null) {
119 for (TunnelIps ip: physicalSwitchAugmentation.getTunnelIps()) {
120 ipSet.add(ip.getTunnelIpsKey().getIpv4Address().getValue());
122 physicalSwitch.setTunnelIps(ipSet);
126 private void setTunnels(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
130 private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractCreated(
131 Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
132 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
133 = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
134 if (changes != null && !changes.isEmpty()) {
135 for (DataTreeModification<Node> change : changes) {
136 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
137 final DataObjectModification<Node> mod = change.getRootNode();
138 Node created = TransactUtils.getCreated(mod);
139 if (created != null) {
140 PhysicalSwitchAugmentation physicalSwitch = created.getAugmentation(PhysicalSwitchAugmentation.class);
141 if (physicalSwitch != null) {
142 result.put(key, physicalSwitch);
150 private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractUpdated(
151 Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
152 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
153 = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
154 if (changes != null && !changes.isEmpty()) {
155 for (DataTreeModification<Node> change : changes) {
156 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
157 final DataObjectModification<Node> mod = change.getRootNode();
158 Node updated = TransactUtils.getUpdated(mod);
159 if (updated != null) {
160 PhysicalSwitchAugmentation physicalSwitch = updated.getAugmentation(PhysicalSwitchAugmentation.class);
161 if (physicalSwitch != null) {
162 result.put(key, physicalSwitch);