bug 8257 handling back to back logical switches
[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.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
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
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 import com.google.common.base.Optional;
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     private 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.getKey());
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             Optional<LogicalSwitches> operationalSwitchOptional =
82                     getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
83             LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
84             setDescription(logicalSwitch, lswitch);
85             setTunnelKey(logicalSwitch, lswitch);
86             if (!operationalSwitchOptional.isPresent()) {
87                 setName(logicalSwitch, lswitch, operationalSwitchOptional);
88                 LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
89                 transaction.add(op.insert(logicalSwitch).withId(TransactUtils.getLogicalSwitchId(lswitch)));
90                 transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
91                 UUID lsUuid = new UUID(TransactUtils.getLogicalSwitchId(lswitch));
92                 getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteMcastMacs.class, lsKey);
93             } else {
94                 LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
95                 String existingLogicalSwitchName = updatedLSwitch.getHwvtepNodeName().getValue();
96                 // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
97                 LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
98                 extraLogicalSwitch.setName("");
99                 LOG.trace("execute: updating LogicalSwitch entry: {}", logicalSwitch);
100                 transaction.add(op.update(logicalSwitch)
101                         .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
102                         .build());
103                 transaction.add(op.comment("Logical Switch: Updating " + existingLogicalSwitchName));
104             }
105     }
106
107     private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
108         if(inputSwitch.getHwvtepNodeDescription() != null) {
109             logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
110         }
111     }
112
113     private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch,
114             Optional<LogicalSwitches> inputSwitchOptional) {
115         if (inputSwitch.getHwvtepNodeName() != null) {
116             logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
117         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getHwvtepNodeName() != null) {
118             logicalSwitch.setName(inputSwitchOptional.get().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     @Override
131     protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
132         return augmentation.getLogicalSwitches();
133     }
134
135     @Override
136     protected boolean areEqual(LogicalSwitches a , LogicalSwitches b) {
137         return a.getKey().equals(b.getKey()) && Objects.equals(a.getTunnelKey(), b.getTunnelKey());
138     }
139 }