3ae1a5e5dcb8589bc0c598240e52f66a613f3358
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / hwvtep / confighelpers / HwVTEPInterfaceConfigAddHelper.java
1 /*
2  * Copyright (c) 2016, 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 package org.opendaylight.genius.interfacemanager.renderer.hwvtep.confighelpers;
9
10 import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION;
11 import static org.opendaylight.mdsal.binding.util.Datastore.OPERATIONAL;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
17 import org.opendaylight.genius.interfacemanager.renderer.hwvtep.utilities.SouthboundUtils;
18 import org.opendaylight.mdsal.binding.util.Datastore.Configuration;
19 import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
20 import org.opendaylight.mdsal.binding.util.TypedWriteTransaction;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public final class HwVTEPInterfaceConfigAddHelper {
36     private static final Logger LOG = LoggerFactory.getLogger(HwVTEPInterfaceConfigAddHelper.class);
37
38     private HwVTEPInterfaceConfigAddHelper() {
39
40     }
41
42     public static List<? extends ListenableFuture<?>> addConfiguration(ManagedNewTransactionRunner txRunner,
43             InstanceIdentifier<Node> physicalSwitchNodeId, InstanceIdentifier<Node> globalNodeId,
44             Interface interfaceNew, IfTunnel ifTunnel) {
45         LOG.info("adding hwvtep configuration for {}", interfaceNew.getName());
46
47         // create hwvtep through ovsdb plugin
48         List<ListenableFuture<?>> futures = new ArrayList<>();
49         // Topology config shard
50         if (globalNodeId != null) {
51             futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
52                 tx -> addTerminationPoints(tx, globalNodeId, ifTunnel)));
53         }
54         // Default operational shard
55         // TODO Move this to another listener, reacing to the config write above
56         futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, tx -> {
57             InterfaceMetaUtils.createTunnelToInterfaceMap(interfaceNew.getName(), physicalSwitchNodeId,
58                     tx, ifTunnel);
59             if (globalNodeId != null) {
60                 SouthboundUtils.addStateEntry(interfaceNew, interfaceNew.augmentation(IfTunnel.class), tx);
61             }
62         }));
63         return futures;
64     }
65
66     /*
67      * For each hwvtep configuration, we need to configure Physical LocatorTable
68      * of hwvtep schema with destination IP and tunnel-type. The configuration
69      * needs to be done for both local endpoint as well as remote endpoint
70      */
71     public static void addTerminationPoints(TypedWriteTransaction<Configuration> transaction,
72             InstanceIdentifier<Node> globalNodeId, IfTunnel ifTunnel) {
73         // InstanceIdentifier<TerminationPoint> localTEP =
74         // createLocalPhysicalLocatorEntryIfNotPresent(futures,
75         // dataBroker,transaction, ifTunnel, globalNodeId);
76         createRemotePhysicalLocatorEntry(transaction, globalNodeId, ifTunnel.getTunnelDestination());
77         // InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier =
78         // createTunnelTableEntry(transaction, physicalSwitchNodeId, localTEP,
79         // remoteTEP);
80     }
81
82     private static InstanceIdentifier<TerminationPoint> createRemotePhysicalLocatorEntry(
83             TypedWriteTransaction<Configuration> transaction,
84             InstanceIdentifier<Node> nodeIid, IpAddress destIPAddress) {
85         String remoteIp = destIPAddress.getIpv4Address().getValue();
86         LOG.debug("creating remote physical locator entry {}", remoteIp);
87         TerminationPointKey tpKey = SouthboundUtils.getTerminationPointKey(remoteIp);
88         InstanceIdentifier<TerminationPoint> tpPath = SouthboundUtils.createInstanceIdentifier(nodeIid, tpKey);
89         createPhysicalLocatorEntry(transaction, tpPath, tpKey, destIPAddress);
90         return tpPath;
91     }
92
93     /*
94      * This method writes the termination end point details to the topology
95      * Config DS
96      */
97     private static void createPhysicalLocatorEntry(TypedWriteTransaction<Configuration> transaction,
98             InstanceIdentifier<TerminationPoint> tpPath, TerminationPointKey terminationPointKey,
99             IpAddress destIPAddress) {
100         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
101         HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder = new HwvtepPhysicalLocatorAugmentationBuilder();
102         tpBuilder.withKey(terminationPointKey);
103         tpBuilder.setTpId(terminationPointKey.getTpId());
104         tpAugmentationBuilder.setEncapsulationType(EncapsulationTypeVxlanOverIpv4.class);
105         SouthboundUtils.setDstIp(tpAugmentationBuilder, destIPAddress);
106         tpBuilder.addAugmentation(HwvtepPhysicalLocatorAugmentation.class, tpAugmentationBuilder.build());
107         LOG.debug("creating physical locator entry for {}", terminationPointKey);
108         transaction.mergeParentStructurePut(tpPath, tpBuilder.build());
109     }
110 }