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.hwvtepsouthbound.HwvtepSouthboundUtil.schemaMismatchLog;
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
14 import com.google.common.base.Optional;
15 import com.google.common.collect.ImmutableMap;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
22 import java.util.Map.Entry;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
28 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
29 import org.opendaylight.ovsdb.lib.notation.Mutator;
30 import org.opendaylight.ovsdb.lib.notation.UUID;
31 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
32 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
33 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
34 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
35 import org.opendaylight.ovsdb.schema.hardwarevtep.Tunnel;
36 import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerMdsalUtils;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIps;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdLocalConfigs;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdParams;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdRemoteConfigs;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class PhysicalSwitchUpdateCommand extends AbstractTransactCommand {
52 private static final Logger LOG = LoggerFactory.getLogger(PhysicalSwitchUpdateCommand.class);
54 public PhysicalSwitchUpdateCommand(HwvtepOperationalState state,
55 Collection<DataTreeModification<Node>> changes) {
56 super(state, changes);
60 public void execute(TransactionBuilder transaction) {
61 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> created =
62 extractCreated(getChanges(),PhysicalSwitchAugmentation.class);
63 if (!created.isEmpty()) {
64 for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
66 updatePhysicalSwitch(transaction, physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
69 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> updated =
70 extractUpdatedSwitches(getChanges(),PhysicalSwitchAugmentation.class);
71 if (!updated.isEmpty()) {
72 for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
74 updatePhysicalSwitch(transaction, physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
80 private void updatePhysicalSwitch(TransactionBuilder transaction,
81 InstanceIdentifier<Node> iid, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
82 LOG.debug("Creating a physical switch named: {}", physicalSwitchAugmentation.getHwvtepNodeName());
83 Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional =
84 getOperationalState().getPhysicalSwitchAugmentation(iid);
85 PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
86 PhysicalSwitch.class);
87 setDescription(physicalSwitch, physicalSwitchAugmentation);
88 setManagementIps(physicalSwitch, physicalSwitchAugmentation);
89 setTunnuleIps(physicalSwitch, operationalPhysicalSwitchOptional.get());
91 setTunnels(transaction, iid, physicalSwitch, physicalSwitchAugmentation,
92 operationalPhysicalSwitchOptional.isPresent());
93 } catch (SchemaVersionMismatchException e) {
94 schemaMismatchLog("tunnels", "Physical_Switch", e);
96 if (!operationalPhysicalSwitchOptional.isPresent()) {
97 //create a physical switch
98 setName(physicalSwitch, physicalSwitchAugmentation, operationalPhysicalSwitchOptional);
99 String pswitchUuid = "PhysicalSwitch_" + HwvtepSouthboundMapper.getRandomUUID();
100 transaction.add(op.insert(physicalSwitch).withId(pswitchUuid));
101 transaction.add(op.comment("Physical Switch: Creating "
102 + physicalSwitchAugmentation.getHwvtepNodeName().getValue()));
103 //update global table
104 Global global = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Global.class);
105 global.setSwitches(Collections.singleton(new UUID(pswitchUuid)));
107 LOG.trace("execute: create physical switch: {}", physicalSwitch);
108 transaction.add(op.mutate(global)
109 .addMutation(global.getSwitchesColumn().getSchema(), Mutator.INSERT,
110 global.getSwitchesColumn().getData()));
111 transaction.add(op.comment("Global: Mutating "
112 + physicalSwitchAugmentation.getHwvtepNodeName().getValue() + " " + pswitchUuid));
114 PhysicalSwitchAugmentation updatedPhysicalSwitch = operationalPhysicalSwitchOptional.get();
115 String existingPhysicalSwitchName = updatedPhysicalSwitch.getHwvtepNodeName().getValue();
116 /* In case TOR devices don't allow creation of PhysicalSwitch name might be null
117 * as user is only adding configurable parameters to MDSAL like BFD params
119 * TODO Note: Consider handling tunnel udpate/remove in separate command
121 if (existingPhysicalSwitchName == null) {
122 existingPhysicalSwitchName = operationalPhysicalSwitchOptional.get().getHwvtepNodeName().getValue();
124 // Name is immutable, and so we *can't* update it. So we use extraPhysicalSwitch for the schema stuff
125 PhysicalSwitch extraPhysicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
126 PhysicalSwitch.class);
127 extraPhysicalSwitch.setName("");
128 LOG.trace("execute: updating physical switch: {}", physicalSwitch);
129 transaction.add(op.update(physicalSwitch)
130 .where(extraPhysicalSwitch.getNameColumn().getSchema().opEqual(existingPhysicalSwitchName))
132 transaction.add(op.comment("Physical Switch: Updating " + existingPhysicalSwitchName));
136 private void setName(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation,
137 Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional) {
138 if (physicalSwitchAugmentation.getHwvtepNodeName() != null) {
139 physicalSwitch.setName(physicalSwitchAugmentation.getHwvtepNodeName().getValue());
140 } else if (operationalPhysicalSwitchOptional.isPresent()
141 && operationalPhysicalSwitchOptional.get().getHwvtepNodeName() != null) {
142 physicalSwitch.setName(operationalPhysicalSwitchOptional.get().getHwvtepNodeName().getValue());
146 private void setDescription(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
147 if (physicalSwitchAugmentation.getHwvtepNodeDescription() != null) {
148 physicalSwitch.setDescription(physicalSwitchAugmentation.getHwvtepNodeDescription());
152 private void setManagementIps(PhysicalSwitch physicalSwitch,
153 PhysicalSwitchAugmentation physicalSwitchAugmentation) {
154 Set<String> ipSet = new HashSet<>();
155 if (physicalSwitchAugmentation.getManagementIps() != null) {
156 for (ManagementIps ip: physicalSwitchAugmentation.getManagementIps()) {
157 ipSet.add(ip.getManagementIpsKey().getIpv4Address().getValue());
159 physicalSwitch.setManagementIps(ipSet);
163 private void setTunnuleIps(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
164 Set<String> ipSet = new HashSet<>();
165 if (physicalSwitchAugmentation.getTunnelIps() != null) {
166 for (TunnelIps ip: physicalSwitchAugmentation.getTunnelIps()) {
167 ipSet.add(ip.getTunnelIpsKey().getIpv4Address().getValue());
169 physicalSwitch.setTunnelIps(ipSet);
173 @SuppressWarnings("unchecked")
174 private void setTunnels(TransactionBuilder transaction, InstanceIdentifier<Node> iid,
175 PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation,
176 boolean switchExists) {
177 //TODO: revisit this code for optimizations
178 //TODO: needs more testing
179 if (physicalSwitchAugmentation.getTunnels() != null) {
180 for (Tunnels tunnel : physicalSwitchAugmentation.getTunnels()) {
181 Optional<Tunnels> opTunnelOpt = getOperationalState().getTunnels(iid, tunnel.key());
182 Tunnel newTunnel = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Tunnel.class);
184 UUID localUUID = getLocatorUUID(transaction,
185 (InstanceIdentifier<TerminationPoint>) tunnel.getLocalLocatorRef().getValue());
186 UUID remoteUUID = getLocatorUUID(transaction,
187 (InstanceIdentifier<TerminationPoint>) tunnel.getRemoteLocatorRef().getValue());
188 if (localUUID != null && remoteUUID != null) {
189 // local and remote must exist
190 newTunnel.setLocal(localUUID);
191 newTunnel.setRemote(remoteUUID);
192 setBfdParams(newTunnel, tunnel);
193 setBfdLocalConfigs(newTunnel, tunnel);
194 setBfdRemoteConfigs(newTunnel, tunnel);
195 if (!opTunnelOpt.isPresent()) {
196 String tunnelUuid = "Tunnel_" + HwvtepSouthboundMapper.getRandomUUID();
197 transaction.add(op.insert(newTunnel).withId(tunnelUuid));
198 transaction.add(op.comment("Tunnel: Creating " + tunnelUuid));
200 //TODO: Figure out a way to handle this
201 LOG.warn("Tunnel configuration requires pre-existing physicalSwitch");
203 // TODO: Can we reuse physicalSwitch instead?
204 PhysicalSwitch phySwitch =
205 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
206 PhysicalSwitch.class);
207 phySwitch.setTunnels(Collections.singleton(new UUID(tunnelUuid)));
208 phySwitch.setName(physicalSwitchAugmentation.getHwvtepNodeName().getValue());
209 transaction.add(op.mutate(phySwitch)
210 .addMutation(phySwitch.getTunnels().getSchema(), Mutator.INSERT,
211 phySwitch.getTunnels().getData())
212 .where(phySwitch.getNameColumn().getSchema()
213 .opEqual(phySwitch.getNameColumn().getData()))
215 transaction.add(op.comment("PhysicalSwitch: Mutating " + tunnelUuid));
218 UUID uuid = new UUID(opTunnelOpt.get().getTunnelUuid().getValue());
220 TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), Tunnel.class, null);
221 extraTunnel.getUuidColumn().setData(uuid);
222 transaction.add(op.update(newTunnel)
223 .where(extraTunnel.getUuidColumn().getSchema().opEqual(uuid))
225 transaction.add(op.comment("Tunnel: Updating " + uuid));
232 private void setBfdParams(Tunnel tunnel, Tunnels psAugTunnel) {
233 List<BfdParams> bfdParams = psAugTunnel.getBfdParams();
234 if (bfdParams != null) {
235 Map<String, String> bfdParamMap = new HashMap<>();
236 for (BfdParams bfdParam : bfdParams) {
237 bfdParamMap.put(bfdParam.getBfdParamKey(), bfdParam.getBfdParamValue());
240 tunnel.setBfdParams(ImmutableMap.copyOf(bfdParamMap));
241 } catch (NullPointerException e) {
242 LOG.warn("Incomplete BFD Params for tunnel", e);
247 private void setBfdLocalConfigs(Tunnel tunnel, Tunnels psAugTunnel) {
248 List<BfdLocalConfigs> bfdLocalConfigs = psAugTunnel.getBfdLocalConfigs();
249 if (bfdLocalConfigs != null) {
250 Map<String, String> configLocalMap = new HashMap<>();
251 for (BfdLocalConfigs localConfig : bfdLocalConfigs) {
252 configLocalMap.put(localConfig.getBfdLocalConfigKey(), localConfig.getBfdLocalConfigValue());
255 tunnel.setBfdConfigLocal(ImmutableMap.copyOf(configLocalMap));
256 } catch (NullPointerException e) {
257 LOG.warn("Incomplete BFD LocalConfig for tunnel", e);
262 private void setBfdRemoteConfigs(Tunnel tunnel, Tunnels psAugTunnel) {
263 List<BfdRemoteConfigs> bfdRemoteConfigs = psAugTunnel.getBfdRemoteConfigs();
264 if (bfdRemoteConfigs != null) {
265 Map<String, String> configRemoteMap = new HashMap<>();
266 for (BfdRemoteConfigs remoteConfig : bfdRemoteConfigs) {
267 configRemoteMap.put(remoteConfig.getBfdRemoteConfigKey(), remoteConfig.getBfdRemoteConfigValue());
270 tunnel.setBfdConfigRemote(ImmutableMap.copyOf(configRemoteMap));
271 } catch (NullPointerException e) {
272 LOG.warn("Incomplete BFD RemoteConfig for tunnel", e);
277 private UUID getLocatorUUID(TransactionBuilder transaction, InstanceIdentifier<TerminationPoint> iid) {
278 UUID locatorUUID = null;
279 Optional<HwvtepPhysicalLocatorAugmentation> opLocOptional =
280 getOperationalState().getPhysicalLocatorAugmentation(iid);
281 if (opLocOptional.isPresent()) {
282 // Get Locator UUID from operational
283 HwvtepPhysicalLocatorAugmentation locatorAug = opLocOptional.get();
284 locatorUUID = new UUID(locatorAug.getPhysicalLocatorUuid().getValue());
286 // TODO/FIXME: Not in operational, do we create a new one?
287 LOG.warn("Trying to create tunnel without creating physical locators first");
288 Optional<TerminationPoint> confLocOptional = new ControllerMdsalUtils(getOperationalState().getDataBroker())
289 .readOptional(LogicalDatastoreType.CONFIGURATION, iid);
290 if (confLocOptional.isPresent()) {
291 locatorUUID = TransactUtils.createPhysicalLocator(transaction, getOperationalState(), iid);
293 LOG.warn("Unable to find endpoint for tunnel. Endpoint indentifier is {}", iid);
299 private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractCreated(
300 Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
301 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result = new HashMap<>();
302 if (changes != null && !changes.isEmpty()) {
303 for (DataTreeModification<Node> change : changes) {
304 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
305 final DataObjectModification<Node> mod = change.getRootNode();
306 Node created = TransactUtils.getCreated(mod);
307 if (created != null) {
308 PhysicalSwitchAugmentation physicalSwitch =
309 created.augmentation(PhysicalSwitchAugmentation.class);
310 if (physicalSwitch != null) {
311 result.put(key, physicalSwitch);
319 private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractUpdatedSwitches(
320 Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
321 Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result = new HashMap<>();
322 if (changes != null && !changes.isEmpty()) {
323 for (DataTreeModification<Node> change : changes) {
324 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
325 final DataObjectModification<Node> mod = change.getRootNode();
326 Node updated = TransactUtils.getUpdated(mod);
327 if (updated != null) {
328 PhysicalSwitchAugmentation physicalSwitch =
329 updated.augmentation(PhysicalSwitchAugmentation.class);
330 if (physicalSwitch != null) {
331 result.put(key, physicalSwitch);