Merge "Add option to use netdev datapath_type"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / LogicalSwitchUpdateCommand.java
1 /*
2  * Copyright (c) 2015 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.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
24 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
25 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
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 {
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<LogicalSwitches>, LogicalSwitches> created =
47                 extractCreated(getChanges(),LogicalSwitches.class);
48         if (!created.isEmpty()) {
49             for(Entry<InstanceIdentifier<LogicalSwitches>, LogicalSwitches> logicalSwitchEntry:
50                 created.entrySet()) {
51                 updateLogicalSwitch(transaction,  logicalSwitchEntry.getKey(), logicalSwitchEntry.getValue());
52             }
53         }
54         Map<InstanceIdentifier<LogicalSwitches>, LogicalSwitches> updated =
55                 extractUpdated(getChanges(),LogicalSwitches.class);
56         if(!updated.isEmpty()) {
57             for(Entry<InstanceIdentifier<LogicalSwitches>, LogicalSwitches> logicalSwitchEntry:
58                 updated.entrySet()) {
59                 updateLogicalSwitch(transaction,  logicalSwitchEntry.getKey(), logicalSwitchEntry.getValue());
60             }
61         }
62     }
63
64
65     private void updateLogicalSwitch(TransactionBuilder transaction,
66             InstanceIdentifier<LogicalSwitches> iid, LogicalSwitches logicalSwitches) {
67         LOG.debug("Creating a logical switch named: {}", logicalSwitches.getHwvtepNodeName());
68         Optional<LogicalSwitches> operationalLogicalSwitchOptional =
69                 getOperationalState().getLogicalSwitches(iid);
70         DatabaseSchema dbSchema = transaction.getDatabaseSchema();
71         LogicalSwitch logicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
72         if(!operationalLogicalSwitchOptional.isPresent()) {
73             setName(logicalSwitch, logicalSwitches, operationalLogicalSwitchOptional);
74             setTunnelKey(logicalSwitch, logicalSwitches, operationalLogicalSwitchOptional);
75             transaction.add(op.insert(logicalSwitch));
76         } else {
77             String existingLogicalSwitchName = operationalLogicalSwitchOptional.get().getHwvtepNodeName().getValue();
78             // Name is immutable, and so we *can't* update it.  So we use extraBridge for the schema stuff
79             LogicalSwitch extraLogicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), LogicalSwitch.class);
80             extraLogicalSwitch.setName("");
81             transaction.add(op.update(logicalSwitch)
82                     .where(extraLogicalSwitch.getNameColumn().getSchema().opEqual(existingLogicalSwitchName))
83                     .build());
84             //stampInstanceIdentifier(transaction, iid.firstIdentifierOf(Node.class),existingBridgeName);
85         }
86     }
87
88     private void setName(LogicalSwitch logicalSwitch, LogicalSwitches logicalSwitches,
89             Optional<LogicalSwitches> operationalLogicalSwitchOptional) {
90         if(logicalSwitches.getHwvtepNodeName() != null) {
91             logicalSwitch.setName(logicalSwitches.getHwvtepNodeName().getValue());
92         } else if(operationalLogicalSwitchOptional.isPresent() && operationalLogicalSwitchOptional.get().getHwvtepNodeName() != null) {
93             logicalSwitch.setName(operationalLogicalSwitchOptional.get().getHwvtepNodeName().getValue());
94         }
95     }
96
97     private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches logicalSwitches,
98             Optional<LogicalSwitches> operationalLogicalSwitchOptional) {
99         if(logicalSwitches.getTunnelKey() != null) {
100             Set<Long> tunnel = new HashSet<Long>();
101             tunnel.add(Long.valueOf(logicalSwitches.getTunnelKey()));
102             logicalSwitch.setTunnelKey(tunnel);
103         } else if(operationalLogicalSwitchOptional.isPresent() && operationalLogicalSwitchOptional.get().getTunnelKey() != null) {
104             Set<Long> tunnel = new HashSet<Long>();
105             tunnel.add(Long.valueOf(operationalLogicalSwitchOptional.get().getTunnelKey()));
106             logicalSwitch.setTunnelKey(tunnel);
107         }
108     }
109
110     private Map<InstanceIdentifier<LogicalSwitches>, LogicalSwitches> extractCreated(
111             Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
112         Map<InstanceIdentifier<LogicalSwitches>, LogicalSwitches> result
113             = new HashMap<InstanceIdentifier<LogicalSwitches>, LogicalSwitches>();
114         if(changes != null && !changes.isEmpty()) {
115             for(DataTreeModification<Node> change : changes) {
116                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
117                 final DataObjectModification<Node> mod = change.getRootNode();
118                 Node created = TransactUtils.getCreated(mod);
119                 if(created != null) {
120 /*                    LogicalSwitches logicalSwitch = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
121                     InstanceIdentifier<LogicalSwitches> iid = change.getRootPath().getRootIdentifier().augmentation(LogicalSwitches.class);
122                     if(logicalSwitch != null) {
123                         result.put(iid, logicalSwitch);
124                     }*/
125                 }
126             }
127         }
128         return result;
129     }
130
131     private Map<InstanceIdentifier<LogicalSwitches>, LogicalSwitches> extractUpdated(
132             Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
133         Map<InstanceIdentifier<LogicalSwitches>, LogicalSwitches> result
134             = new HashMap<InstanceIdentifier<LogicalSwitches>, LogicalSwitches>();
135         if(changes != null && !changes.isEmpty()) {
136             for(DataTreeModification<Node> change : changes) {
137                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
138                 final DataObjectModification<Node> mod = change.getRootNode();
139                 Node updated = TransactUtils.getUpdated(mod);
140                 if(updated != null) {
141 /*                    LogicalSwitches logicalSwitch = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
142                     InstanceIdentifier<LogicalSwitches> iid = change.getRootPath().getRootIdentifier().augmentation(LogicalSwitches.class);
143                     if(logicalSwitch != null) {
144                         result.put(iid, logicalSwitch);
145                     }*/
146                 }
147             }
148         }
149         return result;
150     }
151 }