Merge "Avoid calling the String constructor explicitly"
[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 import java.util.Set;
16
17 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
23 import org.opendaylight.ovsdb.lib.message.TableUpdates;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
26 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
27 import org.opendaylight.ovsdb.schema.hardwarevtep.LogicalSwitch;
28 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalPort;
29 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalSwitch;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalPortAugmentationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindings;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical.port.attributes.VlanBindingsKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
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,
62             DatabaseSchema dbSchema) {
63         super(key, updates, dbSchema);
64         updatedPPRows = TyperUtils.extractRowsUpdated(PhysicalPort.class, getUpdates(),getDbSchema());
65         oldPPRows = TyperUtils.extractRowsOld(PhysicalPort.class, getUpdates(),getDbSchema());
66         switchUpdatedRows = TyperUtils.extractRowsUpdated(PhysicalSwitch.class, getUpdates(),getDbSchema());
67         lSwitchUpdatedRows = TyperUtils.extractRowsUpdated(LogicalSwitch.class, getUpdates(),getDbSchema());
68     }
69
70     @Override
71     public void execute(ReadWriteTransaction transaction) {
72         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
73         if ( updatedPPRows == null
74                 || updatedPPRows.isEmpty()) {
75             return;
76         }
77         LOG.trace("PhysicalPortTable updated: {}", updatedPPRows);
78         Optional<Node> node = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
79         if (node.isPresent()) {
80             updateTerminationPoints(transaction, node.get());
81             //TODO: Handle Deletion of VLAN Bindings
82         }
83     }
84
85     private void updateTerminationPoints(ReadWriteTransaction transaction, Node node) {
86         for (Entry<UUID, PhysicalPort> pPortUpdate : updatedPPRows.entrySet()) {
87             String portName = null;
88             portName = pPortUpdate.getValue().getNameColumn().getData();
89             Optional<InstanceIdentifier<Node>> switchIid = getTerminationPointSwitch(pPortUpdate.getKey());
90             if (!switchIid.isPresent()) {
91                 switchIid = getTerminationPointSwitch( transaction, node, portName);
92             }
93             if (switchIid.isPresent()) {
94                 NodeId switchId = HwvtepSouthboundMapper.createManagedNodeId(switchIid.get());
95                 TerminationPointKey tpKey = new TerminationPointKey(new TpId(portName));
96                 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
97                 tpBuilder.setKey(tpKey);
98                 tpBuilder.setTpId(tpKey.getTpId());
99                 InstanceIdentifier<TerminationPoint> tpPath =
100                         getInstanceIdentifier(switchIid.get(), pPortUpdate.getValue());
101                 HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder =
102                         new HwvtepPhysicalPortAugmentationBuilder();
103                 buildTerminationPoint(tpAugmentationBuilder,pPortUpdate.getValue());
104                 tpBuilder.addAugmentation(HwvtepPhysicalPortAugmentation.class, tpAugmentationBuilder.build());
105                 if (oldPPRows.containsKey(pPortUpdate.getKey())) {
106                     transaction.merge(LogicalDatastoreType.OPERATIONAL,
107                             tpPath, tpBuilder.build());
108                 } else {
109                     transaction.put(LogicalDatastoreType.OPERATIONAL,
110                             tpPath, tpBuilder.build());
111                 }
112             }
113         }
114     }
115
116     private void buildTerminationPoint(HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder,
117                     PhysicalPort portUpdate) {
118         updatePhysicalPortId(portUpdate, tpAugmentationBuilder);
119         updatePort(portUpdate, tpAugmentationBuilder);
120     }
121
122     private void updatePort(PhysicalPort portUpdate,
123                     HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
124         updateVlanBindings(portUpdate, tpAugmentationBuilder);
125     }
126
127     private void updatePhysicalPortId(PhysicalPort portUpdate,
128                     HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
129         tpAugmentationBuilder.setHwvtepNodeName(new HwvtepNodeName(portUpdate.getName()));
130         if(portUpdate.getDescription() != null) {
131             tpAugmentationBuilder.setHwvtepNodeDescription(portUpdate.getDescription());
132         }
133     }
134
135     private void updateVlanBindings(PhysicalPort portUpdate,
136                     HwvtepPhysicalPortAugmentationBuilder tpAugmentationBuilder) {
137         Map<Long, UUID> vlanBindings = portUpdate.getVlanBindingsColumn().getData();
138         if(vlanBindings != null && !vlanBindings.isEmpty()) {
139             Set<Long> vlanBindingsKeys = vlanBindings.keySet();
140             List<VlanBindings> vlanBindingsList = new ArrayList<>();
141             UUID vlanBindingValue = null;
142             for(Long vlanBindingKey: vlanBindingsKeys) {
143                 vlanBindingValue = vlanBindings.get(vlanBindingKey);
144                 if(vlanBindingValue != null && vlanBindingKey != null) {
145                     vlanBindingsList.add(createVlanBinding(portUpdate, vlanBindingKey, vlanBindingValue));
146                 }
147             }
148             tpAugmentationBuilder.setVlanBindings(vlanBindingsList);
149         }
150     }
151
152     private VlanBindings createVlanBinding(PhysicalPort portUpdate, Long key, UUID value) {
153         VlanBindingsBuilder vbBuilder = new VlanBindingsBuilder();
154         VlanBindingsKey vbKey = new VlanBindingsKey(new VlanId(key.intValue()));
155         vbBuilder.setKey(vbKey);
156         vbBuilder.setVlanIdKey(vbKey.getVlanIdKey());
157         HwvtepLogicalSwitchRef lSwitchRef = this.getLogicalSwitchRef(value, portUpdate.getUuid());
158         vbBuilder.setLogicalSwitchRef(lSwitchRef);
159         return vbBuilder.build();
160     }
161
162     private HwvtepLogicalSwitchRef getLogicalSwitchRef( UUID switchUUID, UUID portUUID) {
163             if (lSwitchUpdatedRows.get(switchUUID) != null) {
164                 return new HwvtepLogicalSwitchRef(lSwitchUpdatedRows.get(switchUUID).getName());
165             }
166         return null;
167     }
168
169     private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch( UUID portUUID) {
170         for (UUID switchUUID : this.switchUpdatedRows.keySet()) {
171             if (this.switchUpdatedRows.get(switchUUID).getPortsColumn().getData().contains(portUUID)) {
172                 return Optional.of(HwvtepSouthboundMapper.createInstanceIdentifier(getOvsdbConnectionInstance(),
173                         this.switchUpdatedRows.get(switchUUID)));
174             }
175         }
176         return Optional.absent();
177     }
178
179     private Optional<InstanceIdentifier<Node>> getTerminationPointSwitch(
180                     final ReadWriteTransaction transaction, Node node, String tpName) {
181                 HwvtepGlobalAugmentation hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
182                 List<Switches> switchNodes = hwvtepNode.getSwitches();
183                 for ( Switches managedNodeEntry : switchNodes ) {
184                     @SuppressWarnings("unchecked")
185                     Node switchNode = HwvtepSouthboundUtil.readNode(transaction,
186                             (InstanceIdentifier<Node>)managedNodeEntry.getSwitchRef().getValue()).get();
187                     TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
188                     TerminationPointKey tpKey = new TerminationPointKey(new TpId(tpName));
189                     tpBuilder.setKey(tpKey);
190                     if (switchNode.getTerminationPoint() != null
191                             && switchNode.getTerminationPoint().contains(tpBuilder.build())) {
192                         PhysicalSwitchAugmentation pSwitchAugment
193                             = switchNode.getAugmentation(PhysicalSwitchAugmentation.class);
194                         return Optional.of((InstanceIdentifier<Node>)managedNodeEntry.getSwitchRef().getValue());
195                     }
196                 }
197                 return Optional.absent();
198             }
199
200     private InstanceIdentifier<TerminationPoint> getInstanceIdentifier(InstanceIdentifier<Node> switchIid,
201                     PhysicalPort pPort) {
202         return switchIid.child(TerminationPoint.class, new TerminationPointKey(new TpId(pPort.getName())));
203     }
204
205 }