4e4448b52b3d0960131c160bdee5f3ce0fc26b03
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepPhysicalPortUpdateCommand.java
1 /*
2  * Copyright (c) 2015, 2017 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.PortFaultStatus;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.PortFaultStatusBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.PortFaultStatusKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import com.google.common.base.Optional;
54 import com.google.common.base.Preconditions;
55
56 public class HwvtepPhysicalPortUpdateCommand extends AbstractTransactionCommand {
57
58     private static final Logger LOG = LoggerFactory.getLogger(HwvtepPhysicalPortUpdateCommand.class);
59     private Map<UUID, PhysicalPort> updatedPPRows;
60     private Map<UUID, PhysicalPort> oldPPRows;
61     private Map<UUID, PhysicalSwitch> switchUpdatedRows;
62
63     public HwvtepPhysicalPortUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
64         super(key, updates, dbSchema);
65         updatedPPRows = TyperUtils.extractRowsUpdated(PhysicalPort.class, getUpdates(), getDbSchema());
66         oldPPRows = TyperUtils.extractRowsOld(PhysicalPort.class, getUpdates(), getDbSchema());
67         switchUpdatedRows = TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(), getDbSchema());
68     }
69
70     @Override
71     public void execute(ReadWriteTransaction transaction) {
72         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
73         if (updatedPPRows.isEmpty()) {
74             return;
75         }
76         LOG.trace("PhysicalPortTable updated: {}", updatedPPRows);
77         Optional<Node> node = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
78         if (node.isPresent()) {
79             updateTerminationPoints(transaction, node.get());
80             // TODO: Handle Deletion of VLAN Bindings
81         }
82     }
83
84     private void updateTerminationPoints(ReadWriteTransaction transaction, Node node) {
85         for (Entry<UUID, PhysicalPort> pPortUpdateEntry : updatedPPRows.entrySet()) {
86             PhysicalPort pPortUpdate = pPortUpdateEntry.getValue();
87             String portName = pPortUpdate.getNameColumn().getData();
88             Optional<InstanceIdentifier<Node>> switchIid = getTerminationPointSwitch(pPortUpdateEntry.getKey());
89             if (!switchIid.isPresent()) {
90                 switchIid = getTerminationPointSwitch(transaction, node, portName);
91             }
92             if (switchIid.isPresent()) {
93                 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
94                 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
95                 tpBuilder.setKey(tpKey);
96                 tpBuilder.setTpId(tpKey.getTpId());
97                 InstanceIdentifier<TerminationPoint> tpPath = getInstanceIdentifier(switchIid.get(), pPortUpdate);
98                 HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder =
99                         new HwvtepPhysicalPortAugmentationBuilder();
100                 buildTerminationPoint(tpAugmentationBuilder, pPortUpdate);
101                 setPortFaultStatus(tpAugmentationBuilder, pPortUpdate);
102                 tpBuilder.addAugmentation(HwvtepPhysicalPortAugmentation.class, tpAugmentationBuilder.build());
103                 if (oldPPRows.containsKey(pPortUpdateEntry.getKey())) {
104                     transaction.merge(LogicalDatastoreType.OPERATIONAL, tpPath, tpBuilder.build());
105                 } else {
106                     transaction.put(LogicalDatastoreType.OPERATIONAL, tpPath, tpBuilder.build());
107                 }
108                 // Update with Deleted VlanBindings
109                 if (oldPPRows.get(pPortUpdateEntry.getKey()) != null
110                         && oldPPRows.get(pPortUpdateEntry.getKey()).getVlanBindingsColumn() != null) {
111                     List<InstanceIdentifier<VlanBindings>> vBIiList = new ArrayList<>();
112                     Map<Long, UUID> oldVb = oldPPRows.get(pPortUpdateEntry.getKey()).getVlanBindingsColumn().getData();
113                     Map<Long, UUID> updatedVb = pPortUpdateEntry.getValue().getVlanBindingsColumn().getData();
114                     for (Map.Entry<Long, UUID> oldVbEntry : oldVb.entrySet()) {
115                         Long key = oldVbEntry.getKey();
116                         if (!updatedVb.containsKey(key)) {
117                             VlanBindings vBindings = createVlanBinding(key, oldVbEntry.getValue());
118                             InstanceIdentifier<VlanBindings> vBid = getInstanceIdentifier(tpPath, vBindings);
119                             vBIiList.add(vBid);
120                         }
121                         deleteEntries(transaction, vBIiList);
122                     }
123                 }
124                 // Update with Deleted portfaultstatus
125                 deleteEntries(transaction,getPortFaultStatusToRemove( tpPath, pPortUpdate));
126             }
127         }
128     }
129
130     private <T extends DataObject> void deleteEntries(ReadWriteTransaction transaction,
131             List<InstanceIdentifier<T>> entryIids) {
132         for (InstanceIdentifier<T> entryIid : entryIids) {
133             transaction.delete(LogicalDatastoreType.OPERATIONAL, entryIid);
134         }
135     }
136
137     private InstanceIdentifier<VlanBindings> getInstanceIdentifier(InstanceIdentifier<TerminationPoint> tpPath,
138             VlanBindings vBindings) {
139         return HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), tpPath, vBindings);
140     }
141
142     private void buildTerminationPoint(HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder,
143             PhysicalPort portUpdate) {
144         updatePhysicalPortId(portUpdate, tpAugmentationBuilder);
145         updatePort(portUpdate, tpAugmentationBuilder);
146     }
147
148     private void updatePort(PhysicalPort portUpdate, HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
149         updateVlanBindings(portUpdate, tpAugmentationBuilder);
150         tpAugmentationBuilder.setPhysicalPortUuid(new Uuid(portUpdate.getUuid().toString()));
151     }
152
153     private void updatePhysicalPortId(PhysicalPort portUpdate,
154             HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
155         tpAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName(portUpdate.getName()));
156         if (portUpdate.getDescription() != null) {
157             tpAugmentationBuilder.setHwvtepNodeDescription(portUpdate.getDescription());
158         }
159     }
160
161     private void updateVlanBindings(PhysicalPort portUpdate,
162             HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
163         Map<Long, UUID> vlanBindings = portUpdate.getVlanBindingsColumn().getData();
164         if (vlanBindings != null && !vlanBindings.isEmpty()) {
165             List<VlanBindings> vlanBindingsList = new ArrayList<>();
166             for (Map.Entry<Long, UUID> vlanBindingEntry : vlanBindings.entrySet()) {
167                 Long vlanBindingKey = vlanBindingEntry.getKey();
168                 UUID vlanBindingValue = vlanBindingEntry.getValue();
169                 if (vlanBindingValue != null && vlanBindingKey != null) {
170                     vlanBindingsList.add(createVlanBinding(vlanBindingKey, vlanBindingValue));
171                 }
172             }
173             tpAugmentationBuilder.setVlanBindings(vlanBindingsList);
174         }
175     }
176
177     private VlanBindings createVlanBinding(Long key, UUID value) {
178         VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
179         VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(key.intValue()));
180         vbBuilder.setKey(vbKey);
181         vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
182         HwvtepLogicalSwitchRef lSwitchRef = this.getLogicalSwitchRef(value);
183         vbBuilder.setLogicalSwitchRef(lSwitchRef);
184         return vbBuilder.build();
185     }
186
187     private HwvtepLogicalSwitchRef getLogicalSwitchRef(UUID switchUUID) {
188         LogicalSwitch logicalSwitch = getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitch(switchUUID);
189         if (logicalSwitch != null) {
190             InstanceIdentifier<LogicalSwitches> lSwitchIid =
191                     HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(), logicalSwitch);
192             return new HwvtepLogicalSwitchRef(lSwitchIid);
193         }
194         LOG.debug("Failed to get LogicalSwitch {}", switchUUID);
195         LOG.trace("Available LogicalSwitches: {}",
196                         getOvsdbConnectionInstance().getDeviceInfo().getLogicalSwitches().values());
197         return null;
198     }
199
200     private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch(UUID portUUID) {
201         for (PhysicalSwitch updatedPhysicalSwitch : switchUpdatedRows.values()) {
202             if (updatedPhysicalSwitch.getPortsColumn().getData().contains(portUUID)) {
203                 return Optional.of(HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
204                         updatedPhysicalSwitch));
205             }
206         }
207         return Optional.absent();
208     }
209
210     private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch(final ReadWriteTransaction transaction,
211             Node node, String tpName) {
212         HwvtepGlobalAugmentation hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
213         List<Switches> switchNodes = hwvtepNode.getSwitches();
214         for (Switches managedNodeEntry : switchNodes) {
215             @SuppressWarnings("unchecked")
216             Node switchNode = HwvtepSouthboundUtil
217                     .readNode(transaction, (InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue()).get();
218             TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpName));
219             if (switchNode.getTerminationPoint() != null) {
220                 for (TerminationPoint terminationPoint : switchNode.getTerminationPoint()) {
221                     if (terminationPoint.getKey().equals(tpKey)) {
222                         return Optional.of((InstanceIdentifier<Node>) managedNodeEntry.getSwitchRef().getValue());
223                     }
224                 }
225             }
226         }
227         return Optional.absent();
228     }
229
230     private InstanceIdentifier<TerminationPoint> getInstanceIdentifier(InstanceIdentifier<Node> switchIid,
231             PhysicalPort pPort) {
232         return switchIid.child(TerminationPoint.class, new TerminationPointKey(new TpId(pPort.getName())));
233     }
234
235     private void setPortFaultStatus(HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder,
236             PhysicalPort portUpdate) {
237         if (portUpdate.getPortFaultStatusColumn() != null && portUpdate.getPortFaultStatusColumn().getData() != null
238                 && !portUpdate.getPortFaultStatusColumn().getData().isEmpty()) {
239             List<PortFaultStatus> portFaultStatusLst = new ArrayList<>();
240             for (String portFaultStatus : portUpdate.getPortFaultStatusColumn().getData()) {
241                 portFaultStatusLst.add(new PortFaultStatusBuilder().setKey(new PortFaultStatusKey(portFaultStatus))
242                         .setPortFaultStatusKey(portFaultStatus).build());
243             }
244             tpAugmentationBuilder.setPortFaultStatus(portFaultStatusLst);
245         }
246     }
247
248     private List<InstanceIdentifier<PortFaultStatus>> getPortFaultStatusToRemove(
249             InstanceIdentifier<TerminationPoint> tpPath, PhysicalPort pPort) {
250         Preconditions.checkNotNull(tpPath);
251         Preconditions.checkNotNull(pPort);
252         List<InstanceIdentifier<PortFaultStatus>> result = new ArrayList<>();
253         PhysicalPort oldPort = oldPPRows.get(pPort.getUuid());
254         if (oldPort != null && oldPort.getPortFaultStatusColumn() != null) {
255             for (String portFltStat : oldPort.getPortFaultStatusColumn().getData()) {
256                 if (pPort.getPortFaultStatusColumn() == null
257                         || !pPort.getPortFaultStatusColumn().getData().contains(portFltStat)) {
258                     InstanceIdentifier<PortFaultStatus> iid = tpPath.augmentation(HwvtepPhysicalPortAugmentation.class)
259                             .child(PortFaultStatus.class, new PortFaultStatusKey(portFltStat));
260                     result.add(iid);
261                 }
262             }
263         }
264         return result;
265     }
266
267 }