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