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