Merge "L2 Gw create changes related to ITM Tunnels creation in neutronvpn module"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / renderer / hwvtep / confighelpers / HwVTEPInterfaceConfigAddHelper.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 package org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.confighelpers;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
15 import org.opendaylight.vpnservice.interfacemgr.renderer.hwvtep.utilities.SouthboundUtils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.EncapsulationTypeVxlanOverIpv4;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalLocatorAugmentationBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 public class HwVTEPInterfaceConfigAddHelper {
34     private static final Logger LOG = LoggerFactory.getLogger(HwVTEPInterfaceConfigAddHelper.class);
35
36     public static List<ListenableFuture<Void>> addConfiguration(DataBroker dataBroker, InstanceIdentifier<Node> physicalSwitchNodeId,
37                                                                 InstanceIdentifier<Node> globalNodeId,
38                                                                 Interface interfaceNew, IfTunnel ifTunnel) {
39         List<ListenableFuture<Void>> futures = new ArrayList<ListenableFuture<Void>>();
40         LOG.info("adding hwvtep configuration for {}", interfaceNew.getName());
41
42         // create hwvtep through ovsdb plugin
43         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
44         InterfaceMetaUtils.createTunnelToInterfaceMap(interfaceNew.getName(), physicalSwitchNodeId, transaction, ifTunnel);
45         if(globalNodeId != null) {
46             addTerminationPoints(transaction, futures, globalNodeId,ifTunnel);
47             SouthboundUtils.addStateEntry(interfaceNew, interfaceNew.getAugmentation(IfTunnel.class), transaction);
48         }else{
49             LOG.debug("specified physical switch is not connected {}", physicalSwitchNodeId);
50         }
51         futures.add(transaction.submit());
52         return futures;
53     }
54
55     /*
56      * For each hwvtep configuration, we need to configure Physical LocatorTable of hwvtep schema with
57      * destination IP and tunnel-type. The configuration needs to be done for both local endpoint as well as remote endpoint
58      */
59     public static void addTerminationPoints(WriteTransaction transaction,
60                                              List<ListenableFuture<Void>> futures,
61                                              InstanceIdentifier<Node> globalNodeId,
62                                              IfTunnel ifTunnel) {
63         //InstanceIdentifier<TerminationPoint> localTEP =
64         //         createLocalPhysicalLocatorEntryIfNotPresent(futures, dataBroker,transaction, ifTunnel, globalNodeId);
65         createRemotePhysicalLocatorEntry(transaction, futures, globalNodeId, ifTunnel.getTunnelDestination());
66         //InstanceIdentifier<Tunnels> tunnelsInstanceIdentifier = createTunnelTableEntry(transaction, physicalSwitchNodeId, localTEP, remoteTEP);
67     }
68
69     private static InstanceIdentifier<TerminationPoint> createRemotePhysicalLocatorEntry(WriteTransaction transaction, List<ListenableFuture<Void>> futures,
70                                                          InstanceIdentifier<Node> nodeIid, IpAddress destIPAddress){
71         String remoteIp = destIPAddress.getIpv4Address().getValue();
72         LOG.debug("creating remote physical locator entry {}", remoteIp);
73         TerminationPointKey tpKey = SouthboundUtils.getTerminationPointKey(remoteIp);
74         InstanceIdentifier<TerminationPoint> tpPath =
75                 SouthboundUtils.createInstanceIdentifier(nodeIid, tpKey);
76         createPhysicalLocatorEntry(transaction, futures, tpPath, tpKey, destIPAddress);
77         return tpPath;
78     }
79
80
81     private static InstanceIdentifier<TerminationPoint> createLocalPhysicalLocatorEntryIfNotPresent(List<ListenableFuture<Void>> futures,
82                                                                     DataBroker dataBroker,
83                                                                     WriteTransaction transaction,
84                                                                     IfTunnel ifTunnel,
85                                                                     InstanceIdentifier<Node> nodeIid){
86         String localIp = ifTunnel.getTunnelSource().getIpv4Address().getValue();
87         LOG.debug("creating local physical locator entry {}", localIp);
88         TerminationPointKey tpKey = SouthboundUtils.getTerminationPointKey(localIp);
89         InstanceIdentifier<TerminationPoint> tpPath =
90                 SouthboundUtils.createInstanceIdentifier(nodeIid, tpKey);
91         // if local tep is not present, create it
92         TerminationPoint terminationPoint = SouthboundUtils.getTEPFromConfigDS(tpPath, dataBroker);
93         if(terminationPoint == null) {
94           createPhysicalLocatorEntry(transaction, futures, tpPath, tpKey, ifTunnel.getTunnelSource());
95         }
96         return tpPath;
97     }
98
99     /*
100      * This method writes the termination end point details to the topology Config DS
101      */
102     private static void createPhysicalLocatorEntry(WriteTransaction transaction, List<ListenableFuture<Void>> futures,
103                                                    InstanceIdentifier<TerminationPoint> tpPath, TerminationPointKey terminationPointKey,
104                                                    IpAddress destIPAddress){
105         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
106         HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder =
107                 new HwvtepPhysicalLocatorAugmentationBuilder();
108         tpBuilder.setKey(terminationPointKey);
109         tpBuilder.setTpId(terminationPointKey.getTpId());
110         tpAugmentationBuilder.setEncapsulationType(EncapsulationTypeVxlanOverIpv4.class);
111         SouthboundUtils.setDstIp(tpAugmentationBuilder, destIPAddress);
112         tpBuilder.addAugmentation(HwvtepPhysicalLocatorAugmentation.class, tpAugmentationBuilder.build());
113         LOG.debug("creating physical locator entry for {}", terminationPointKey);
114         transaction.put(LogicalDatastoreType.CONFIGURATION,
115                 tpPath, tpBuilder.build(), true);
116     }
117 }