bbde50e671ea8840f8779e72a957c13f1c2876e8
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalSwitchUpdateCommand.java
1 /*
2  * Copyright (c) 2015, 2016 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.HashMap;
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.Set;
20
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.base.Optional;
34
35 public class LogicalSwitchUpdateCommand extends AbstractTransactCommand {
36     private static final Logger LOG = LoggerFactory.getLogger(LogicalSwitchUpdateCommand.class);
37
38     public LogicalSwitchUpdateCommand(HwvtepOperationalState state,
39             Collection<DataTreeModification<Node>> changes) {
40         super(state, changes);
41     }
42
43     @Override
44     public void execute(TransactionBuilder transaction) {
45         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> createds =
46                 extractCreated(getChanges(),LogicalSwitches.class);
47         if (!createds.isEmpty()) {
48             for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> created:
49                 createds.entrySet()) {
50                 updateLogicalSwitch(transaction,  created.getKey(), created.getValue());
51             }
52         }
53         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> updateds =
54                 extractUpdated(getChanges(),LogicalSwitches.class);
55         if (!updateds.isEmpty()) {
56             for (Entry<InstanceIdentifier<Node>, List<LogicalSwitches>> updated:
57                 updateds.entrySet()) {
58                 updateLogicalSwitch(transaction,  updated.getKey(), updated.getValue());
59             }
60         }
61     }
62
63     private void updateLogicalSwitch(TransactionBuilder transaction,
64             InstanceIdentifier<Node> instanceIdentifier, List<LogicalSwitches> lswitchList) {
65         for (LogicalSwitches lswitch: lswitchList) {
66             LOG.debug("Creating logcial switch named: {}", lswitch.getHwvtepNodeName());
67             Optional<LogicalSwitches> operationalSwitchOptional =
68                     getOperationalState().getLogicalSwitches(instanceIdentifier, lswitch.getKey());
69             LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
70             setDescription(logicalSwitch, lswitch);
71             setTunnelKey(logicalSwitch, lswitch);
72             if (!operationalSwitchOptional.isPresent()) {
73                 setName(logicalSwitch, lswitch, operationalSwitchOptional);
74                 LOG.trace("execute: creating LogicalSwitch entry: {}", logicalSwitch);
75                 transaction.add(op.insert(logicalSwitch).withId(TransactUtils.getLogicalSwitchId(lswitch)));
76                 transaction.add(op.comment("Logical Switch: Creating " + lswitch.getHwvtepNodeName().getValue()));
77             } else {
78                 LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
79                 String existingLogicalSwitchName = updatedLSwitch.getHwvtepNodeName().getValue();
80                 // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
81                 LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
82                 extraLogicalSwitch.setName("");
83                 LOG.trace("execute: updating LogicalSwitch entry: {}", logicalSwitch);
84                 transaction.add(op.update(logicalSwitch)
85                         .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
86                         .build());
87                 transaction.add(op.comment("Logical Switch: Updating " + existingLogicalSwitchName));
88             }
89         }
90     }
91
92     private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
93         if(inputSwitch.getHwvtepNodeDescription() != null) {
94             logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
95         }
96     }
97
98     private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch,
99             Optional<LogicalSwitches> inputSwitchOptional) {
100         if (inputSwitch.getHwvtepNodeName() != null) {
101             logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
102         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getHwvtepNodeName() != null) {
103             logicalSwitch.setName(inputSwitchOptional.get().getHwvtepNodeName().getValue());
104         }
105     }
106
107     private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
108         if (inputSwitch.getTunnelKey() != null) {
109             Set<Long> tunnel = new HashSet<Long>();
110             tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
111             logicalSwitch.setTunnelKey(tunnel);
112         }
113     }
114
115     private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractCreated(
116             Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
117         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
118             = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
119         if (changes != null && !changes.isEmpty()) {
120             for (DataTreeModification<Node> change : changes) {
121                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
122                 final DataObjectModification<Node> mod = change.getRootNode();
123                 Node created = TransactUtils.getCreated(mod);
124                 if (created != null) {
125                     List<LogicalSwitches> lswitchListUpdated = null;
126                     if (created.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
127                         lswitchListUpdated = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
128                     }
129                     if (lswitchListUpdated != null) {
130                         result.put(key, lswitchListUpdated);
131                     }
132                 }
133             }
134         }
135         return result;
136     }
137
138     private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractUpdated(
139             Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
140         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
141             = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
142         if (changes != null && !changes.isEmpty()) {
143             for (DataTreeModification<Node> change : changes) {
144                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
145                 final DataObjectModification<Node> mod = change.getRootNode();
146                 Node updated = TransactUtils.getUpdated(mod);
147                 Node before = mod.getDataBefore();
148                 if (updated != null && before != null) {
149                     List<LogicalSwitches> lswitchListUpdated = null;
150                     List<LogicalSwitches> lswitchListBefore = null;
151                     if (updated.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
152                         lswitchListUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
153                     }
154                     if (before.getAugmentation(HwvtepGlobalAugmentation.class) != null) {
155                         lswitchListBefore = before.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
156                     }
157                     if (lswitchListUpdated != null) {
158                         if (lswitchListBefore != null) {
159                             lswitchListUpdated.removeAll(lswitchListBefore);
160                         }
161                         result.put(key, lswitchListUpdated);
162                     }
163                 }
164             }
165         }
166         return result;
167     }
168 }