Change logical-switch-ref to iid in hwvtep.yang
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / PhysicalPortUpdateCommand.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.transactions.md;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
19 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
21 import org.opendaylight.ovsdb.lib.message.TableUpdates;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
24 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
25 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
26 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
40
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 import com.google.common.base.Optional;
52
53 public class PhysicalPortUpdateCommand extends AbstractTransactionCommand {
54
55     private static final Logger LOG = LoggerFactory.getLogger(PhysicalPortUpdateCommand.class);
56     private Map<UUID, PhysicalPort> updatedPPRows;
57     private Map<UUID, PhysicalPort> oldPPRows;
58     private Map<UUID, PhysicalSwitch> switchUpdatedRows;
59     private Map<UUID, LogicalSwitch> lSwitchUpdatedRows;
60
61     public PhysicalPortUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
62         super(key, updates, dbSchema);
63         updatedPPRows = TyperUtils.extractRowsUpdated(PhysicalPort.class, getUpdates(), getDbSchema());
64         oldPPRows = TyperUtils.extractRowsOld(PhysicalPort.class, getUpdates(), getDbSchema());
65         switchUpdatedRows = TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(), getDbSchema());
66         lSwitchUpdatedRows = TyperUtils.extractRowsUpdated(LogicalSwitch.class, getUpdates(), getDbSchema());
67     }
68
69     @Override
70     public void execute(ReadWriteTransaction transaction) {
71         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
72         if (updatedPPRows.isEmpty()) {
73             return;
74         }
75         LOG.trace("PhysicalPortTable updated: {}", updatedPPRows);
76         Optional<Node> node = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
77         if (node.isPresent()) {
78             updateTerminationPoints(transaction, node.get());
79             // TODO: Handle Deletion of VLAN Bindings
80         }
81     }
82
83     private void updateTerminationPoints(ReadWriteTransaction transaction, Node node) {
84         for (Entry<UUID, PhysicalPort> pPortUpdateEntry : updatedPPRows.entrySet()) {
85             PhysicalPort pPortUpdate = pPortUpdateEntry.getValue();
86             String portName = pPortUpdate.getNameColumn().getData();
87             Optional<InstanceIdentifier<Node>> switchIid = getTerminationPointSwitch(pPortUpdateEntry.getKey());
88             if (!switchIid.isPresent()) {
89                 switchIid = getTerminationPointSwitch(transaction, node, portName);
90             }
91             if (switchIid.isPresent()) {
92                 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
93                 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
94                 tpBuilder.setKey(tpKey);
95                 tpBuilder.setTpId(tpKey.getTpId());
96                 InstanceIdentifier<TerminationPoint> tpPath = getInstanceIdentifier(switchIid.get(), pPortUpdate);
97                 HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder =
98                         new HwvtepPhysicalPortAugmentationBuilder();
99                 buildTerminationPoint(tpAugmentationBuilder, pPortUpdate);
100                 tpBuilder.addAugmentation(HwvtepPhysicalPortAugmentation.class, tpAugmentationBuilder.build());
101                 if (oldPPRows.containsKey(pPortUpdateEntry.getKey())) {
102                     transaction.merge(LogicalDatastoreType.OPERATIONAL, tpPath, tpBuilder.build());
103                 } else {
104                     transaction.put(LogicalDatastoreType.OPERATIONAL, tpPath, tpBuilder.build());
105                 }
106                 // Update with Deleted VlanBindings
107                 if (oldPPRows.get(pPortUpdateEntry.getKey()) != null) {
108                     List<InstanceIdentifier<VlanBindings>> vBIiList = new ArrayList<>();
109                     Map<Long, UUID> oldVb = oldPPRows.get(pPortUpdateEntry.getKey()).getVlanBindingsColumn().getData();
110                     Map<Long, UUID> updatedVb = pPortUpdateEntry.getValue().getVlanBindingsColumn().getData();
111                     for (Map.Entry<Long, UUID> oldVbEntry : oldVb.entrySet()) {
112                         Long key = oldVbEntry.getKey();
113                         if (!updatedVb.containsKey(key)) {
114                             VlanBindings vBindings = createVlanBinding(key, oldVbEntry.getValue());
115                             InstanceIdentifier<VlanBindings> vBid = getInstanceIdentifier(tpPath, vBindings);
116                             vBIiList.add(vBid);
117                         }
118                         deleteEntries(transaction, vBIiList);
119                     }
120                 }
121             }
122         }
123     }
124
125     private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
126             List<InstanceIdentifier<T>> entryIids) {
127         for (InstanceIdentifier<T> entryIid : entryIids) {
128             transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
129         }
130     }
131
132     private InstanceIdentifier<VlanBindings> getInstanceIdentifier(InstanceIdentifier<TerminationPoint> tpPath,
133             VlanBindings vBindings) {
134         return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), tpPath, vBindings);
135     }
136
137     private void buildTerminationPoint(HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder,
138             PhysicalPort portUpdate) {
139         updatePhysicalPortId(portUpdate, tpAugmentationBuilder);
140         updatePort(portUpdate, tpAugmentationBuilder);
141     }
142
143     private void updatePort(PhysicalPort portUpdate, HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
144         updateVlanBindings(portUpdate, tpAugmentationBuilder);
145         tpAugmentationBuilder.setPhysicalPortUuid(new Uuid(portUpdate.getUuid().toString()));
146     }
147
148     private void updatePhysicalPortId(PhysicalPort portUpdate,
149             HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
150         tpAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName(portUpdate.getName()));
151         if (portUpdate.getDescription() != null) {
152             tpAugmentationBuilder.setHwvtepNodeDescription(portUpdate.getDescription());
153         }
154     }
155
156     private void updateVlanBindings(PhysicalPort portUpdate,
157             HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
158         Map<Long, UUID> vlanBindings = portUpdate.getVlanBindingsColumn().getData();
159         if (vlanBindings != null && !vlanBindings.isEmpty()) {
160             List<VlanBindings> vlanBindingsList = new ArrayList<>();
161             for (Map.Entry<Long, UUID> vlanBindingEntry : vlanBindings.entrySet()) {
162                 Long vlanBindingKey = vlanBindingEntry.getKey();
163                 UUID vlanBindingValue = vlanBindingEntry.getValue();
164                 if (vlanBindingValue != null && vlanBindingKey != null) {
165                     vlanBindingsList.add(createVlanBinding(vlanBindingKey, vlanBindingValue));
166                 }
167             }
168             tpAugmentationBuilder.setVlanBindings(vlanBindingsList);
169         }
170     }
171
172     private VlanBindings createVlanBinding(Long key, UUID value) {
173         VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
174         VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(key.intValue()));
175         vbBuilder.setKey(vbKey);
176         vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
177         HwvtepLogicalSwitchRef lSwitchRef = this.getLogicalSwitchRef(value);
178         vbBuilder.setLogicalSwitchRef(lSwitchRef);
179         return vbBuilder.build();
180     }
181
182     private HwvtepLogicalSwitchRef getLogicalSwitchRef(UUID switchUUID) {
183         LogicalSwitch logicalSwitch = lSwitchUpdatedRows.get(switchUUID);
184         if (logicalSwitch != null) {
185             InstanceIdentifier<LogicalSwitches> lSwitchIid =
186                     HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
187             return new HwvtepLogicalSwitchRef(lSwitchIid);
188         }
189         return null;
190     }
191
192     private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch(UUID portUUID) {
193         for (PhysicalSwitch updatedPhysicalSwitch : switchUpdatedRows.values()) {
194             if (updatedPhysicalSwitch.getPortsColumn().getData().contains(portUUID)) {
195                 return Optional.of(HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
196                         updatedPhysicalSwitch));
197             }
198         }
199         return Optional.absent();
200     }
201
202     private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch(final ReadWriteTransaction transaction,
203             Node node, String tpName) {
204         HwvtepGlobalAugmentation hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
205         List<Switches> switchNodes = hwvtepNode.getSwitches();
206         for (Switches managedNodeEntry : switchNodes) {
207             @SuppressWarnings("unchecked")
208             Node switchNode = HwvtepSouthboundUtil
209                     .readNode(transaction, (InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue()).get();
210             TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
211             TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpName));
212             tpBuilder.setKey(tpKey);
213             if (switchNode.getTerminationPoint() != null
214                     && switchNode.getTerminationPoint().contains(tpBuilder.build())) {
215                 return Optional.of((InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue());
216             }
217         }
218         return Optional.absent();
219     }
220
221     private InstanceIdentifier<TerminationPoint> getInstanceIdentifier(InstanceIdentifier<Node> switchIid,
222             PhysicalPort pPort) {
223         return switchIid.child(TerminationPoint.class, new TerminationPointKey(new TpId(pPort.getName())));
224     }
225
226 }