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