c872369a3a496af9ba87192016765e06feb020c0
[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 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepDeviceInfo;
23 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
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.LogicalSwitch;
28 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class LogicalSwitchUpdateCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
37     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
38
39     public LogicalSwitchUpdateCommand(HwvtepOperationalState state,
40             Collection<DataTreeModification<Node>> changes) {
41         super(state, changes);
42     }
43
44     @Override
45     public void execute(TransactionBuilder transaction) {
46         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
47                 extractUpdated(getChanges(),LogicalSwitches.class);
48         if (!updateds.isEmpty()) {
49             for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
50                 updateds.entrySet()) {
51                 updateLogicalSwitch(transaction,  updated.getKey(), updated.getValue());
52             }
53         }
54     }
55
56     public void updateLogicalSwitch(final TransactionBuilder transaction,
57                                      final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
58         for (LogicalSwitches lswitch: lswitchList) {
59             InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class)
60                     .child(LogicalSwitches.class, lswitch.key());
61             onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
62         }
63     }
64
65     @Override
66     public void onConfigUpdate(final TransactionBuilder transaction,
67                                final InstanceIdentifier<Node> nodeIid,
68                                final LogicalSwitches lswitch,
69                                final InstanceIdentifier lsKey,
70                                final Object... extraData) {
71         processDependencies(null, transaction, nodeIid, lsKey, lswitch);
72     }
73
74     @Override
75     public void doDeviceTransaction(final TransactionBuilder transaction,
76                                     final InstanceIdentifier<Node> instanceIdentifier,
77                                     final LogicalSwitches lswitch,
78                                     final InstanceIdentifier lsKey,
79                                     final Object... extraData) {
80         LOG.debug("Creating logical switch named: {}", lswitch.getHwvtepNodeName());
81         final HwvtepDeviceInfo.DeviceData operationalSwitchOptional =
82                 getDeviceInfo().getDeviceOperData(LogicalSwitches.class, lsKey);
83         LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(),
84                 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, logicalSwitch);
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(),
100                     LogicalSwitch.class);
101             extraLogicalSwitch.setName("");
102             LOG.trace("execute: updating LogicalSwitch entry: {}", logicalSwitch);
103             transaction.add(op.update(logicalSwitch)
104                     .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
105                     .build());
106             transaction.add(op.comment("Logical Switch: Updating " + existingLogicalSwitchName));
107             updateControllerTxHistory(TransactionType.UPDATE, logicalSwitch);
108         }
109     }
110
111     private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
112         if (inputSwitch.getHwvtepNodeDescription() != null) {
113             logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
114         }
115     }
116
117     private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
118         if (inputSwitch.getHwvtepNodeName() != null) {
119             logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
120         }
121     }
122
123     private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
124         if (inputSwitch.getTunnelKey() != null) {
125             Set<Long> tunnel = new HashSet<>();
126             tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
127             logicalSwitch.setTunnelKey(tunnel);
128         }
129     }
130
131     private void setReplicationMode(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
132         if (inputSwitch.getReplicationMode() != null) {
133             Set<String> mode = new HashSet<>();
134             mode.add(inputSwitch.getReplicationMode());
135             try {
136                 logicalSwitch.setReplicationMode(mode);
137             }
138             catch (SchemaVersionMismatchException e) {
139                 schemaMismatchLog("replication_mode", "Logical_Switch", e);
140             }
141         }
142     }
143
144     @Override
145     protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
146         return augmentation.getLogicalSwitches();
147     }
148
149     @Override
150     protected boolean areEqual(LogicalSwitches sw1, LogicalSwitches sw2) {
151         return sw1.key().equals(sw2.key()) && Objects.equals(sw1.getTunnelKey(), sw2.getTunnelKey());
152     }
153 }