018c0910124ce1614eb5a59e821bded0b7cc906d
[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.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
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 import com.google.common.base.Optional;
37
38 public class LogicalSwitchUpdateCommand extends AbstractTransactCommand<LogicalSwitches, HwvtepGlobalAugmentation> {
39     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
40
41     public LogicalSwitchUpdateCommand(HwvtepOperationalState state,
42             Collection<DataTreeModification<Node>> changes) {
43         super(state, changes);
44     }
45
46     @Override
47     public void execute(TransactionBuilder transaction) {
48         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
49                 extractUpdated(getChanges(),LogicalSwitches.class);
50         if (!updateds.isEmpty()) {
51             for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
52                 updateds.entrySet()) {
53                 updateLogicalSwitch(transaction,  updated.getKey(), updated.getValue());
54             }
55         }
56     }
57
58     private void updateLogicalSwitch(final TransactionBuilder transaction,
59                                      final InstanceIdentifier<Node> nodeIid, final List<LogicalSwitches> lswitchList) {
60         for (LogicalSwitches lswitch: lswitchList) {
61             InstanceIdentifier<LogicalSwitches> lsKey = nodeIid.augmentation(HwvtepGlobalAugmentation.class).
62                     child(LogicalSwitches.class, lswitch.getKey());
63             onConfigUpdate(transaction, nodeIid, lswitch, lsKey);
64         }
65     }
66
67     @Override
68     public void onConfigUpdate(final TransactionBuilder transaction,
69                                final InstanceIdentifier<Node> nodeIid,
70                                final LogicalSwitches lswitch,
71                                final InstanceIdentifier lsKey,
72                                final Object... extraData) {
73         processDependencies(null, transaction, nodeIid, lsKey, lswitch);
74     }
75
76     @Override
77     public void doDeviceTransaction(final TransactionBuilder transaction,
78                                     final InstanceIdentifier<Node> instanceIdentifier,
79                                     final LogicalSwitches lswitch,
80                                     final InstanceIdentifier lsKey,
81                                     final Object... extraData) {
82             LOG.debug("Creating logical switch named: {}", lswitch.getHwvtepNodeName());
83             Optional<LogicalSwitches> operationalSwitchOptional =
84                     getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
85             LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
86             setDescription(logicalSwitch, lswitch);
87             setTunnelKey(logicalSwitch, lswitch);
88             setReplicationMode(logicalSwitch, lswitch);
89             if (!operationalSwitchOptional.isPresent()) {
90                 setName(logicalSwitch, lswitch, operationalSwitchOptional);
91                 LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
92                 transaction.add(op.insert(logicalSwitch).withId(TransactUtils.getLogicalSwitchId(lswitch)));
93                 transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
94                 UUID lsUuid = new UUID(TransactUtils.getLogicalSwitchId(lswitch));
95                 getOperationalState().getDeviceInfo().markKeyAsInTransit(RemoteMcastMacs.class, lsKey);
96             } else {
97                 LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
98                 String existingLogicalSwitchName = updatedLSwitch.getHwvtepNodeName().getValue();
99                 // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
100                 LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), 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             }
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             Optional<LogicalSwitches> inputSwitchOptional) {
118         if (inputSwitch.getHwvtepNodeName() != null) {
119             logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
120         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getHwvtepNodeName() != null) {
121             logicalSwitch.setName(inputSwitchOptional.get().getHwvtepNodeName().getValue());
122         }
123     }
124
125     private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
126         if (inputSwitch.getTunnelKey() != null) {
127             Set<Long> tunnel = new HashSet<>();
128             tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
129             logicalSwitch.setTunnelKey(tunnel);
130         }
131     }
132
133     private void setReplicationMode(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
134         if (inputSwitch.getReplicationMode() != null) {
135             Set<String> mode = new HashSet<>();
136             mode.add(inputSwitch.getReplicationMode());
137             try {
138                 logicalSwitch.setReplicationMode(mode);
139             }
140             catch (SchemaVersionMismatchException e) {
141                 schemaMismatchLog("replication_mode", "Logical_Switch", e);
142             }
143         }
144     }
145
146     @Override
147     protected List<LogicalSwitches> getData(HwvtepGlobalAugmentation augmentation) {
148         return augmentation.getLogicalSwitches();
149     }
150
151     @Override
152     protected boolean areEqual(LogicalSwitches a , LogicalSwitches b) {
153         return a.getKey().equals(b.getKey()) && Objects.equals(a.getTunnelKey(), b.getTunnelKey());
154     }
155 }