Upgrade ietf-{inet,yang}-types to 2013-07-15
[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.rev130715.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      * This method writes the termination end point details to the topology Config DS
82      */
83     private static void createPhysicalLocatorEntry(WriteTransaction transaction, List<ListenableFuture<Void>> futures,
84                                                    InstanceIdentifier<TerminationPoint> tpPath, TerminationPointKey terminationPointKey,
85                                                    IpAddress destIPAddress){
86         TerminationPointBuilder tpBuilder = new TerminationPointBuilder();
87         HwvtepPhysicalLocatorAugmentationBuilder tpAugmentationBuilder =
88                 new HwvtepPhysicalLocatorAugmentationBuilder();
89         tpBuilder.setKey(terminationPointKey);
90         tpBuilder.setTpId(terminationPointKey.getTpId());
91         tpAugmentationBuilder.setEncapsulationType(EncapsulationTypeVxlanOverIpv4.class);
92         SouthboundUtils.setDstIp(tpAugmentationBuilder, destIPAddress);
93         tpBuilder.addAugmentation(HwvtepPhysicalLocatorAugmentation.class, tpAugmentationBuilder.build());
94         LOG.debug("creating physical locator entry for {}", terminationPointKey);
95         transaction.put(LogicalDatastoreType.CONFIGURATION,
96                 tpPath, tpBuilder.build(), true);
97     }
98 }