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.hwvtepsouthbound.HwvtepSouthboundMapper;
23 import org.opendaylight.ovsdb.lib.notation.Mutator;
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.Global;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIps;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.base.Optional;
38 import com.google.common.collect.Sets;
40 public class PhysicalSwitchUpdateCommand extends AbstractTransactCommand {
41 private static final Logger LOG = LoggerFactory.getLogger(PhysicalSwitchUpdateCommand.class);
43 public PhysicalSwitchUpdateCommand(HwvtepOperationalState state,
44 Collection<DataTreeModification<Node>> changes) {
45 super(state, changes);
49 public void execute(TransactionBuilder transaction) {
50 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> created =
51 extractCreated(getChanges(),PhysicalSwitchAugmentation.class);
52 if (!created.isEmpty()) {
53 for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
55 updatePhysicalSwitch(transaction, physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
58 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> updated =
59 extractUpdated(getChanges(),PhysicalSwitchAugmentation.class);
60 if (!updated.isEmpty()) {
61 for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
63 updatePhysicalSwitch(transaction, physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
69 private void updatePhysicalSwitch(TransactionBuilder transaction,
70 InstanceIdentifier<Node> iid, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
71 LOG.debug("Creating a physical switch named: {}", physicalSwitchAugmentation.getHwvtepNodeName());
72 Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional =
73 getOperationalState().getPhysicalSwitchAugmentation(iid);
74 PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
75 setDescription(physicalSwitch, physicalSwitchAugmentation);
76 setManagementIps(physicalSwitch, physicalSwitchAugmentation);
77 setTunnuleIps(physicalSwitch, physicalSwitchAugmentation);
78 setTunnels(physicalSwitch, physicalSwitchAugmentation);
79 if (!operationalPhysicalSwitchOptional.isPresent()) {
80 //create a physical switch
81 setName(physicalSwitch, physicalSwitchAugmentation, operationalPhysicalSwitchOptional);
82 String pswitchUuid = "PhysicalSwitch_" + HwvtepSouthboundMapper.getRandomUUID();
83 transaction.add(op.insert(physicalSwitch).withId(pswitchUuid));
85 Global global = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Global.class);
86 global.setSwitches(Sets.newHashSet(new UUID(pswitchUuid)));
88 LOG.debug("execute: physical switch: {}", physicalSwitch);
89 transaction.add(op.mutate(global)
90 .addMutation(global.getSwitchesColumn().getSchema(), Mutator.INSERT,
91 global.getSwitchesColumn().getData()));
93 PhysicalSwitchAugmentation updatedPhysicalSwitch = operationalPhysicalSwitchOptional.get();
94 String existingPhysicalSwitchName = updatedPhysicalSwitch.getHwvtepNodeName().getValue();
95 // Name is immutable, and so we *can't* update it. So we use extraPhysicalSwitch for the schema stuff
96 PhysicalSwitch extraPhysicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
97 extraPhysicalSwitch.setName("");
98 transaction.add(op.update(physicalSwitch)
99 .where(extraPhysicalSwitch.getNameColumn().getSchema().opEqual(existingPhysicalSwitchName))
104 private void setName(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation,
105 Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional) {
106 if (physicalSwitchAugmentation.getHwvtepNodeName() != null) {
107 physicalSwitch.setName(physicalSwitchAugmentation.getHwvtepNodeName().getValue());
108 } else if (operationalPhysicalSwitchOptional.isPresent() && operationalPhysicalSwitchOptional.get().getHwvtepNodeName() != null) {
109 physicalSwitch.setName(operationalPhysicalSwitchOptional.get().getHwvtepNodeName().getValue());
113 private void setDescription(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
114 if (physicalSwitchAugmentation.getHwvtepNodeDescription() != null) {
115 physicalSwitch.setDescription(physicalSwitchAugmentation.getHwvtepNodeDescription());
119 private void setManagementIps(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
120 Set<String> ipSet = new HashSet<String>();
121 if (physicalSwitchAugmentation.getManagementIps() != null) {
122 for (ManagementIps ip: physicalSwitchAugmentation.getManagementIps()) {
123 ipSet.add(ip.getManagementIpsKey().getIpv4Address().getValue());
125 physicalSwitch.setManagementIps(ipSet);
129 private void setTunnuleIps(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
130 Set<String> ipSet = new HashSet<String>();
131 if (physicalSwitchAugmentation.getTunnelIps() != null) {
132 for (TunnelIps ip: physicalSwitchAugmentation.getTunnelIps()) {
133 ipSet.add(ip.getTunnelIpsKey().getIpv4Address().getValue());
135 physicalSwitch.setTunnelIps(ipSet);
139 private void setTunnels(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
143 private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractCreated(
144 Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
145 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
146 = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
147 if (changes != null && !changes.isEmpty()) {
148 for (DataTreeModification<Node> change : changes) {
149 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
150 final DataObjectModification<Node> mod = change.getRootNode();
151 Node created = TransactUtils.getCreated(mod);
152 if (created != null) {
153 PhysicalSwitchAugmentation physicalSwitch = created.getAugmentation(PhysicalSwitchAugmentation.class);
154 if (physicalSwitch != null) {
155 result.put(key, physicalSwitch);
163 private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractUpdated(
164 Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
165 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
166 = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
167 if (changes != null && !changes.isEmpty()) {
168 for (DataTreeModification<Node> change : changes) {
169 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
170 final DataObjectModification<Node> mod = change.getRootNode();
171 Node updated = TransactUtils.getUpdated(mod);
172 if (updated != null) {
173 PhysicalSwitchAugmentation physicalSwitch = updated.getAugmentation(PhysicalSwitchAugmentation.class);
174 if (physicalSwitch != null) {
175 result.put(key, physicalSwitch);