Create Physical Switch by listening on DS
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / PhysicalSwitchUpdateCommand.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.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.PhysicalSwitch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.ManagementIps;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Optional;
36
37 public class PhysicalSwitchUpdateCommand extends AbstractTransactCommand {
38     private static final Logger LOG = LoggerFactory.getLogger(PhysicalSwitchUpdateCommand.class);
39
40     public PhysicalSwitchUpdateCommand(HwvtepOperationalState state,
41             Collection<DataTreeModification<Node>> changes) {
42         super(state, changes);
43     }
44
45     @Override
46     public void execute(TransactionBuilder transaction) {
47         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> created =
48                 extractCreated(getChanges(),PhysicalSwitchAugmentation.class);
49         if (!created.isEmpty()) {
50             for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
51                 created.entrySet()) {
52                 updatePhysicalSwitch(transaction,  physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
53             }
54         }
55         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> updated =
56                 extractUpdated(getChanges(),PhysicalSwitchAugmentation.class);
57         if (!updated.isEmpty()) {
58             for (Entry<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> physicalSwitchEntry:
59                 updated.entrySet()) {
60                 updatePhysicalSwitch(transaction,  physicalSwitchEntry.getKey(), physicalSwitchEntry.getValue());
61             }
62         }
63     }
64
65
66     private void updatePhysicalSwitch(TransactionBuilder transaction,
67             InstanceIdentifier<Node> iid, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
68         LOG.debug("Creating a physical switch named: {}", physicalSwitchAugmentation.getHwvtepNodeName());
69         Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional =
70                 getOperationalState().getPhysicalSwitchAugmentation(iid);
71         PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
72         setDescription(physicalSwitch, physicalSwitchAugmentation);
73         setManagementIps(physicalSwitch, physicalSwitchAugmentation);
74         setTunnuleIps(physicalSwitch, physicalSwitchAugmentation);
75         setTunnels(physicalSwitch, physicalSwitchAugmentation);
76         if (!operationalPhysicalSwitchOptional.isPresent()) {
77             setName(physicalSwitch, physicalSwitchAugmentation, operationalPhysicalSwitchOptional);
78             transaction.add(op.insert(physicalSwitch));
79         } else {
80             PhysicalSwitchAugmentation updatedPhysicalSwitch = operationalPhysicalSwitchOptional.get();
81             String existingPhysicalSwitchName = updatedPhysicalSwitch.getHwvtepNodeName().getValue();
82             // Name is immutable, and so we *can't* update it.  So we use extraPhysicalSwitch for the schema stuff
83             PhysicalSwitch extraPhysicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
84             extraPhysicalSwitch.setName("");
85             transaction.add(op.update(physicalSwitch)
86                     .where(extraPhysicalSwitch.getNameColumn().getSchema().opEqual(existingPhysicalSwitchName))
87                     .build());
88         }
89     }
90
91     private void setName(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation,
92             Optional<PhysicalSwitchAugmentation> operationalPhysicalSwitchOptional) {
93         if (physicalSwitchAugmentation.getHwvtepNodeName() != null) {
94             physicalSwitch.setName(physicalSwitchAugmentation.getHwvtepNodeName().getValue());
95         } else if (operationalPhysicalSwitchOptional.isPresent() && operationalPhysicalSwitchOptional.get().getHwvtepNodeName() != null) {
96             physicalSwitch.setName(operationalPhysicalSwitchOptional.get().getHwvtepNodeName().getValue());
97         }
98     }
99
100     private void setDescription(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
101         if (physicalSwitchAugmentation.getHwvtepNodeDescription() != null) {
102             physicalSwitch.setDescription(physicalSwitchAugmentation.getHwvtepNodeDescription());
103         }
104     }
105
106     private void setManagementIps(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
107         Set<String> ipSet = new HashSet<String>();
108         if (physicalSwitchAugmentation.getManagementIps() != null) {
109             for (ManagementIps ip: physicalSwitchAugmentation.getManagementIps()) {
110                 ipSet.add(ip.getManagementIpsKey().getIpv4Address().getValue());
111             }
112             physicalSwitch.setManagementIps(ipSet);
113         }
114     }
115
116     private void setTunnuleIps(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
117         Set<String> ipSet = new HashSet<String>();
118         if (physicalSwitchAugmentation.getTunnelIps() != null) {
119             for (TunnelIps ip: physicalSwitchAugmentation.getTunnelIps()) {
120                 ipSet.add(ip.getTunnelIpsKey().getIpv4Address().getValue());
121             }
122             physicalSwitch.setTunnelIps(ipSet);
123         }
124     }
125
126     private void setTunnels(PhysicalSwitch physicalSwitch, PhysicalSwitchAugmentation physicalSwitchAugmentation) {
127         //TODO
128     }
129
130     private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractCreated(
131             Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
132         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
133             = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
134         if (changes != null && !changes.isEmpty()) {
135             for (DataTreeModification<Node> change : changes) {
136                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
137                 final DataObjectModification<Node> mod = change.getRootNode();
138                 Node created = TransactUtils.getCreated(mod);
139                 if (created != null) {
140                     PhysicalSwitchAugmentation physicalSwitch = created.getAugmentation(PhysicalSwitchAugmentation.class);
141                     if (physicalSwitch != null) {
142                         result.put(key, physicalSwitch);
143                     }
144                 }
145             }
146         }
147         return result;
148     }
149
150     private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractUpdated(
151             Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
152         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
153             = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
154         if (changes != null && !changes.isEmpty()) {
155             for (DataTreeModification<Node> change : changes) {
156                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
157                 final DataObjectModification<Node> mod = change.getRootNode();
158                 Node updated = TransactUtils.getUpdated(mod);
159                 if (updated != null) {
160                     PhysicalSwitchAugmentation physicalSwitch = updated.getAugmentation(PhysicalSwitchAugmentation.class);
161                     if (physicalSwitch != null) {
162                         result.put(key, physicalSwitch);
163                     }
164                 }
165             }
166         }
167         return result;
168     }
169 }