Make TransactionBuilder type-aware
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalSwitchUpdateCommand.java
1 /*
2  * Copyright © 2015, 2017 China Telecom Beijing Research Institute and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.hwvtepsouthbound.transact;
9
10 import static org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil.schemaMismatchLog;
11 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Objects;
19 import java.util.Set;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
22 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
26 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class LogicalSwitchUpdateCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
35     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
36
37     public LogicalSwitchUpdateCommand(final HwvtepOperationalState state,
38             final Collection<DataTreeModification<Node>> changes) {
39         super(state, changes);
40     }
41
42     @Override
43     public void execute(final TransactionBuilder transaction) {
44         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
45                 extractUpdated(getChanges(),LogicalSwitches.class);
46         if (!updateds.isEmpty()) {
47             for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
48                 updateds.entrySet()) {
49                 updateLogicalSwitch(transaction,  updated.getKey(), updated.getValue());
50             }
51         }
52     }
53
54     public void updateLogicalSwitch(final TransactionBuilder transaction,
55                                      final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
56         for (LogicalSwitches lswitch: lswitchList) {
57             InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
58                     .child(LogicalSwitches.class, lswitch.key());
59             onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
60         }
61     }
62
63     @Override
64     public void onConfigUpdate(final TransactionBuilder transaction,
65                                final InstanceIdentifier<Node> nodeIid,
66                                final LogicalSwitches lswitch,
67                                final InstanceIdentifier lsKey,
68                                final Object... extraData) {
69         processDependencies(null, transaction, nodeIid, lsKey, lswitch);
70     }
71
72     @Override
73     public void doDeviceTransaction(final TransactionBuilder transaction,
74                                     final InstanceIdentifier<Node> instanceIdentifier,
75                                     final LogicalSwitches lswitch,
76                                     final InstanceIdentifier lsKey,
77                                     final Object... extraData) {
78         LOG.debug("Creating logical switch named: {}", lswitch.getHwvtepNodeName());
79         final HwvtepDeviceInfo.DeviceData operationalSwitchOptional =
80                 getDeviceInfo().getDeviceOperData(LogicalSwitches.class, lsKey);
81         LogicalSwitch logicalSwitch = transaction.getTypedRowWrapper(LogicalSwitch.class);
82         setDescription(logicalSwitch, lswitch);
83         setTunnelKey(logicalSwitch, lswitch);
84         setReplicationMode(logicalSwitch, lswitch);
85         if (operationalSwitchOptional == null) {
86             setName(logicalSwitch, lswitch);
87             LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
88             transaction.add(op.insert(logicalSwitch).withId(TransactUtils.getLogicalSwitchId(lswitch)));
89             transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
90             UUID lsUuid = new UUID(TransactUtils.getLogicalSwitchId(lswitch));
91             updateCurrentTxData(LogicalSwitches.class, lsKey, lsUuid, lswitch);
92             updateControllerTxHistory(TransactionType.ADD, logicalSwitch);
93         } else {
94             String existingLogicalSwitchName = lswitch.getHwvtepNodeName().getValue();
95             // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
96             LogicalSwitch extraLogicalSwitch = transaction.getTypedRowWrapper(LogicalSwitch.class);
97             extraLogicalSwitch.setName("");
98             LOG.trace("execute: updating LogicalSwitch entry: {}", logicalSwitch);
99             transaction.add(op.update(logicalSwitch)
100                     .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
101                     .build());
102             transaction.add(op.comment("Logical Switch: Updating " + existingLogicalSwitchName));
103             updateControllerTxHistory(TransactionType.UPDATE, logicalSwitch);
104         }
105     }
106
107     private static void setDescription(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
108         if (inputSwitch.getHwvtepNodeDescription() != null) {
109             logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
110         }
111     }
112
113     private static void setName(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
114         if (inputSwitch.getHwvtepNodeName() != null) {
115             logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
116         }
117     }
118
119     private static void setTunnelKey(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
120         if (inputSwitch.getTunnelKey() != null) {
121             Set<Long> tunnel = new HashSet<>();
122             tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
123             logicalSwitch.setTunnelKey(tunnel);
124         }
125     }
126
127     private static void setReplicationMode(final LogicalSwitch logicalSwitch, final LogicalSwitches inputSwitch) {
128         if (inputSwitch.getReplicationMode() != null) {
129             Set<String> mode = new HashSet<>();
130             mode.add(inputSwitch.getReplicationMode());
131             try {
132                 logicalSwitch.setReplicationMode(mode);
133             }
134             catch (SchemaVersionMismatchException e) {
135                 schemaMismatchLog("replication_mode", "Logical_Switch", e);
136             }
137         }
138     }
139
140     @Override
141     protected List<LogicalSwitches> getData(final HwvtepGlobalAugmentation augmentation) {
142         return augmentation.getLogicalSwitches();
143     }
144
145     @Override
146     protected boolean areEqual(final LogicalSwitches sw1, final LogicalSwitches sw2) {
147         return sw1.key().equals(sw2.key()) && Objects.equals(sw1.getTunnelKey(), sw2.getTunnelKey());
148     }
149 }