Change logical-switch-ref to iid in hwvtep.yang
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transact / PhysicalPortUpdateCommand.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.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Map.Entry;
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.PhysicalPort;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.base.Optional;
41 import com.google.common.collect.Sets;
42
43 public class PhysicalPortUpdateCommand extends AbstractTransactCommand {
44     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortUpdateCommand.class);
45
46     public PhysicalPortUpdateCommand(HwvtepOperationalState state,
47             Collection<DataTreeModification<Node>> changes) {
48         super(state, changes);
49     }
50
51     @Override
52     public void execute(TransactionBuilder transaction) {
53         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> createds =
54                 extractCreated(getChanges(),HwvtepPhysicalPortAugmentation.class);
55         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> createdPhysicalSwitches =
56                 extractCreatedPhyscialSwitch(getChanges(),PhysicalSwitchAugmentation.class);
57         if (!createds.isEmpty()) {
58             for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> created:
59                 createds.entrySet()) {
60                 updatePhysicalPort(transaction,  created.getKey(), created.getValue(), createdPhysicalSwitches);
61             }
62         }
63         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> updateds =
64                 extractUpdated(getChanges(),HwvtepPhysicalPortAugmentation.class);
65         if (!updateds.isEmpty()) {
66             for (Entry<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> updated:
67                 updateds.entrySet()) {
68                 updatePhysicalPort(transaction,  updated.getKey(), updated.getValue(), createdPhysicalSwitches);
69             }
70         }
71     }
72
73     private void updatePhysicalPort(TransactionBuilder transaction,
74             InstanceIdentifier<Node> psNodeiid,
75             List<HwvtepPhysicalPortAugmentation> listPort,
76             Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> createdPhysicalSwitches ) {
77         //Get physical switch which the port belong to: in operation DS or new created
78         PhysicalSwitchAugmentation physicalSwitchBelong = getPhysicalSwitchBelong(psNodeiid, createdPhysicalSwitches);
79         for (HwvtepPhysicalPortAugmentation port : listPort) {
80             LOG.debug("Creating a physical port named: {}", port.getHwvtepNodeName().getValue());
81             Optional<HwvtepPhysicalPortAugmentation> operationalPhysicalPortOptional =
82                     getOperationalState().getPhysicalPortAugmentation(psNodeiid, port.getHwvtepNodeName());
83             PhysicalPort physicalPort = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class);
84             //get managing global node of physicalSwitchBelong
85             InstanceIdentifier<?> globalNodeIid = physicalSwitchBelong.getManagedBy().getValue();
86             setVlanBindings(globalNodeIid, physicalPort, port);
87             setDescription(physicalPort, port);
88             if (!operationalPhysicalPortOptional.isPresent()) {
89                 //create a physical port
90                 setName(physicalPort, port, operationalPhysicalPortOptional);
91                 String portUuid = "PhysicalPort_" + HwvtepSouthboundMapper.getRandomUUID();
92                 transaction.add(op.insert(physicalPort).withId(portUuid));
93                 //update physical switch table
94                 PhysicalSwitch physicalSwitch = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalSwitch.class);
95                 physicalSwitch.setName(physicalSwitchBelong.getHwvtepNodeName().getValue());
96                 physicalSwitch.setPorts(Sets.newHashSet(new UUID(portUuid)));
97                 LOG.info("execute: physical switch: {}", physicalSwitch);
98                 transaction.add(op.mutate(physicalSwitch)
99                         .addMutation(physicalSwitch.getPortsColumn().getSchema(), Mutator.INSERT,
100                                 physicalSwitch.getPortsColumn().getData())
101                         .where(physicalSwitch.getNameColumn().getSchema().opEqual(physicalSwitch.getNameColumn().getData()))
102                         .build());
103             } else {
104                 //updated physical port only
105                 HwvtepPhysicalPortAugmentation updatedPhysicalPort = operationalPhysicalPortOptional.get();
106                 String existingPhysicalPortName = updatedPhysicalPort.getHwvtepNodeName().getValue();
107                 PhysicalPort extraPhyscialPort =
108                         TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), PhysicalPort.class);
109                 extraPhyscialPort.setName("");
110                 transaction.add(op.update(physicalPort)
111                         .where(extraPhyscialPort.getNameColumn().getSchema().opEqual(existingPhysicalPortName))
112                         .build());
113             }
114         }
115     }
116
117     private PhysicalSwitchAugmentation getPhysicalSwitchBelong(InstanceIdentifier<Node> psNodeiid,
118             Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> createdPhysicalSwitches) {
119         Optional<PhysicalSwitchAugmentation> physicalSwitchOptional =
120                     getOperationalState().getPhysicalSwitchAugmentation(psNodeiid);
121         PhysicalSwitchAugmentation physicalSwitchAugmentation = null;
122         if (physicalSwitchOptional.isPresent()) {
123             physicalSwitchAugmentation = physicalSwitchOptional.get();
124         } else {
125             physicalSwitchAugmentation = createdPhysicalSwitches.get(psNodeiid);
126         }
127         return physicalSwitchAugmentation;
128     }
129
130     private void setName(PhysicalPort physicalPort, HwvtepPhysicalPortAugmentation inputPhysicalPort,
131             Optional<HwvtepPhysicalPortAugmentation> operationalLogicalSwitchOptional) {
132         if (inputPhysicalPort.getHwvtepNodeName() != null) {
133             physicalPort.setName(inputPhysicalPort.getHwvtepNodeName().getValue());
134         } else if (operationalLogicalSwitchOptional.isPresent()
135                 && operationalLogicalSwitchOptional.get().getHwvtepNodeName() != null) {
136             physicalPort.setName(operationalLogicalSwitchOptional.get().getHwvtepNodeName().getValue());
137         }
138     }
139
140     private void setDescription(PhysicalPort physicalPort, HwvtepPhysicalPortAugmentation inputPhysicalPort) {
141         if (inputPhysicalPort.getHwvtepNodeDescription() != null) {
142             physicalPort.setDescription(inputPhysicalPort.getHwvtepNodeDescription().toString());
143         }
144     }
145
146     private void setVlanBindings(InstanceIdentifier<?> globalNodeIid, PhysicalPort physicalPort,
147             HwvtepPhysicalPortAugmentation inputPhysicalPort) {
148         if (inputPhysicalPort.getVlanBindings() != null) {
149             //get UUID by LogicalSwitchRef
150             Map<Long, UUID> bindingMap = new HashMap<Long, UUID>();
151             for (VlanBindings vlanBinding: inputPhysicalPort.getVlanBindings()) {
152                 @SuppressWarnings("unchecked")
153                 InstanceIdentifier<LogicalSwitches> lswitchIid =
154                         (InstanceIdentifier<LogicalSwitches>) vlanBinding.getLogicalSwitchRef().getValue();
155                 Optional<LogicalSwitches> operationalSwitchOptional =
156                         getOperationalState().getLogicalSwitches(lswitchIid);
157                 if (operationalSwitchOptional.isPresent()) {
158                     Uuid logicalSwitchUuid = operationalSwitchOptional.get().getLogicalSwitchUuid();
159                     bindingMap.put(vlanBinding.getVlanIdKey().getValue().longValue(), new UUID(logicalSwitchUuid.getValue()));
160                 }
161             }
162             physicalPort.setVlanBindings(bindingMap);
163         }
164     }
165
166     private Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractCreated(
167             Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
168         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result
169             = new HashMap<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>>();
170         if (changes != null && !changes.isEmpty()) {
171             for (DataTreeModification<Node> change : changes) {
172                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
173                 final DataObjectModification<Node> mod = change.getRootNode();
174                 Node created = TransactUtils.getCreated(mod);
175                 if (created != null) {
176                     List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<HwvtepPhysicalPortAugmentation>();
177                     if (created.getTerminationPoint() != null) {
178                         for (TerminationPoint tp : created.getTerminationPoint()) {
179                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
180                             if (hppAugmentation != null) {
181                                 portListUpdated.add(hppAugmentation);
182                             }
183                         }
184                     }
185                     result.put(key, portListUpdated);
186                 }
187             }
188         }
189         return result;
190     }
191
192     private Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> extractCreatedPhyscialSwitch(
193             Collection<DataTreeModification<Node>> changes, Class<PhysicalSwitchAugmentation> class1) {
194         Map<InstanceIdentifier<Node>, PhysicalSwitchAugmentation> result
195             = new HashMap<InstanceIdentifier<Node>, PhysicalSwitchAugmentation>();
196         if (changes != null && !changes.isEmpty()) {
197             for (DataTreeModification<Node> change : changes) {
198                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
199                 final DataObjectModification<Node> mod = change.getRootNode();
200                 Node created = TransactUtils.getCreated(mod);
201                 if (created != null) {
202                     PhysicalSwitchAugmentation physicalSwitch = created.getAugmentation(PhysicalSwitchAugmentation.class);
203                     if (physicalSwitch != null) {
204                         result.put(key, physicalSwitch);
205                     }
206                 }
207             }
208         }
209         return result;
210     }
211
212     private Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> extractUpdated(
213             Collection<DataTreeModification<Node>> changes, Class<HwvtepPhysicalPortAugmentation> class1) {
214         Map<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>> result
215             = new HashMap<InstanceIdentifier<Node>, List<HwvtepPhysicalPortAugmentation>>();
216         if (changes != null && !changes.isEmpty()) {
217             for (DataTreeModification<Node> change : changes) {
218                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
219                 final DataObjectModification<Node> mod = change.getRootNode();
220                 Node updated = TransactUtils.getUpdated(mod);
221                 Node before = mod.getDataBefore();
222                 if (updated != null && before != null) {
223                     List<HwvtepPhysicalPortAugmentation> portListUpdated = new ArrayList<HwvtepPhysicalPortAugmentation>();
224                     List<HwvtepPhysicalPortAugmentation> portListBefore = new ArrayList<HwvtepPhysicalPortAugmentation>();
225                     if (updated.getTerminationPoint() != null) {
226                         for (TerminationPoint tp : updated.getTerminationPoint()) {
227                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
228                             if (hppAugmentation != null) {
229                                 portListUpdated.add(hppAugmentation);
230                             }
231                         }
232                     }
233                     if (before.getTerminationPoint() != null) {
234                         for (TerminationPoint tp : before.getTerminationPoint()) {
235                             HwvtepPhysicalPortAugmentation hppAugmentation = tp.getAugmentation(HwvtepPhysicalPortAugmentation.class);
236                             if (hppAugmentation != null) {
237                                 portListBefore.add(hppAugmentation);
238                             }
239                         }
240                     }
241                     portListUpdated.removeAll(portListBefore);
242                     result.put(key, portListUpdated);
243                 }
244             }
245         }
246         return result;
247     }
248 }