Changes to LogicalSwitchUpdateCommand in transact as per new yang
[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.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                 transaction.add(op.insert(logicalSwitch));
75             } else {
76                 LogicalSwitches updatedLSwitch = operationalSwitchOptional.get();
77                 String existingLogicalSwitchName = updatedLSwitch.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             }
85         }
86     }
87
88     private void setDescription(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
89         if(inputSwitch.getHwvtepNodeDescription() != null) {
90             logicalSwitch.setDescription(inputSwitch.getHwvtepNodeDescription());
91         }
92     }
93
94     private void setName(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch,
95             Optional<LogicalSwitches> inputSwitchOptional) {
96         if (inputSwitch.getHwvtepNodeName() != null) {
97             logicalSwitch.setName(inputSwitch.getHwvtepNodeName().getValue());
98         } else if (inputSwitchOptional.isPresent() && inputSwitchOptional.get().getHwvtepNodeName() != null) {
99             logicalSwitch.setName(inputSwitchOptional.get().getHwvtepNodeName().getValue());
100         }
101     }
102
103     private void setTunnelKey(LogicalSwitch logicalSwitch, LogicalSwitches inputSwitch) {
104         if (inputSwitch.getTunnelKey() != null) {
105             Set<Long> tunnel = new HashSet<Long>();
106             tunnel.add(Long.valueOf(inputSwitch.getTunnelKey()));
107             logicalSwitch.setTunnelKey(tunnel);
108         }
109     }
110
111     private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractCreated(
112             Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
113         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
114             = new HashMap<InstanceIdentifier<Node>, List<LogicalSwitches>>();
115         if (changes != null && !changes.isEmpty()) {
116             for (DataTreeModification<Node> change : changes) {
117                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
118                 final DataObjectModification<Node> mod = change.getRootNode();
119                 Node created = TransactUtils.getCreated(mod);
120                 if (created != null) {
121                     List<LogicalSwitches> lswitchListUpdated = created.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
122                     if (lswitchListUpdated != null) {
123                         result.put(key, lswitchListUpdated);
124                     }
125                 }
126             }
127         }
128         return result;
129     }
130
131     private Map<InstanceIdentifier<Node>, List<LogicalSwitches>> extractUpdated(
132             Collection<DataTreeModification<Node>> changes, Class<LogicalSwitches> class1) {
133         Map<InstanceIdentifier<Node>, List<LogicalSwitches>> result
134             = new HashMap<InstanceIdentifier<Node>, List<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                 Node before = mod.getDataBefore();
141                 if (updated != null && before != null) {
142                     List<LogicalSwitches> lswitchListUpdated = updated.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
143                     List<LogicalSwitches> lswitchListBefore = before.getAugmentation(HwvtepGlobalAugmentation.class).getLogicalSwitches();
144                     if (lswitchListUpdated != null) {
145                         if (lswitchListBefore != null) {
146                             lswitchListUpdated.removeAll(lswitchListBefore);
147                         }
148                         result.put(key, lswitchListUpdated);
149                     }
150                 }
151             }
152         }
153         return result;
154     }
155 }