bug 6579 removed boilerplate code
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / transactions / md / HwvtepPhysicalLocatorUpdateCommand.java
1 /*
2  * Copyright (c) 2015 - 2016 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 com.google.common.base.Optional;
12 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepConnectionInstance;
15 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
16 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundUtil;
17 import org.opendaylight.ovsdb.lib.message.TableUpdates;
18 import org.opendaylight.ovsdb.lib.notation.UUID;
19 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
20 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
21 import org.opendaylight.ovsdb.schema.hardwarevtep.PhysicalLocator;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 import java.util.Map;
33 import java.util.Map.Entry;
34
35 public class HwvtepPhysicalLocatorUpdateCommand extends AbstractTransactionCommand {
36
37     private Map<UUID, PhysicalLocator> updatedPLocRows;
38     private Map<UUID, PhysicalLocator> oldPLocRows;
39
40     public HwvtepPhysicalLocatorUpdateCommand(HwvtepConnectionInstance key, TableUpdates updates, DatabaseSchema dbSchema) {
41         super(key, updates, dbSchema);
42         updatedPLocRows = TyperUtils.extractRowsUpdated(PhysicalLocator.class, getUpdates(), getDbSchema());
43         oldPLocRows = TyperUtils.extractRowsOld(PhysicalLocator.class, getUpdates(), getDbSchema());
44     }
45
46     @Override
47     public void execute(ReadWriteTransaction transaction) {
48         final InstanceIdentifier<Node> connectionIId = getOvsdbConnectionInstance().getInstanceIdentifier();
49         if (updatedPLocRows.isEmpty()) {
50             return;
51         }
52         Optional<Node> node = HwvtepSouthboundUtil.readNode(transaction, connectionIId);
53         if (node.isPresent()) {
54             updateTerminationPoints(transaction, node.get());
55         }
56     }
57
58     private void updateTerminationPoints(ReadWriteTransaction transaction, Node node) {
59         for (Entry<UUID, PhysicalLocator> pLocUpdate : updatedPLocRows.entrySet()) {
60             PhysicalLocator pLoc = pLocUpdate.getValue();
61             InstanceIdentifier<Node> nodeIid = HwvtepSouthboundMapper.createInstanceIdentifier(node.getNodeId());
62             TerminationPointKey tpKey = HwvtepSouthboundMapper.getTerminationPointKey(pLoc);
63             if (nodeIid != null && tpKey != null) {
64                 TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
65                 tpBuilder.setKey(tpKey);
66                 tpBuilder.setTpId(tpKey.getTpId());
67                 InstanceIdentifier<TerminationPoint> tpPath =
68                         HwvtepSouthboundMapper.createInstanceIdentifier(nodeIid, pLoc);
69                 HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder =
70                         new HwvtepPhysicalLocatorAugmentationBuilder();
71                 tpAugmentationBuilder.setPhysicalLocatorUuid(new Uuid(pLoc.getUuid().toString()));
72                 setEncapsType(tpAugmentationBuilder, pLoc);
73                 setDstIp(tpAugmentationBuilder, pLoc);
74                 tpBuilder.addAugmentation(HwvtepPhysicalLocatorAugmentation.class, tpAugmentationBuilder.build());
75                 if (oldPLocRows.containsKey(pLocUpdate.getKey())) {
76                     transaction.merge(LogicalDatastoreType.OPERATIONAL,
77                             tpPath, tpBuilder.build());
78                 } else {
79                     transaction.put(LogicalDatastoreType.OPERATIONAL,
80                             tpPath, tpBuilder.build());
81                 }
82                 getOvsdbConnectionInstance().getDeviceInfo().updateDeviceOpData(
83                         TerminationPoint.class, tpPath, pLoc.getUuid(), pLoc);
84             }
85         }
86     }
87
88     private void setEncapsType(HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder, PhysicalLocator pLoc) {
89         String encapsType = pLoc.getEncapsulationTypeColumn().getData();
90         if (HwvtepSouthboundMapper.createEncapsulationType(encapsType) != null) {
91             tpAugmentationBuilder.setEncapsulationType(HwvtepSouthboundMapper.createEncapsulationType(encapsType));
92         }
93     }
94
95     private void setDstIp(HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder, PhysicalLocator pLoc) {
96         IpAddress ip = new IpAddress(pLoc.getDstIpColumn().getData().toCharArray());
97         tpAugmentationBuilder.setDstIp(ip);
98     }
99
100 }