Bump versions by 0.1.0 for next dev cycle
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / VpnserviceProvider.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;
9
10 import java.math.BigInteger;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13
14 import org.opendaylight.bgpmanager.api.IBgpManager;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.fibmanager.api.IFibManager;
20 import org.opendaylight.vpnmanager.api.IVpnManager;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
24 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.OdlArputilService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.NeutronvpnService;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class VpnserviceProvider implements BindingAwareProvider, IVpnManager,
35                                                        AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(VpnserviceProvider.class);
38     private VpnInterfaceManager vpnInterfaceManager;
39     private VpnManager vpnManager;
40     private IBgpManager bgpManager;
41     private IFibManager fibManager;
42     private IMdsalApiManager mdsalManager;
43     private OdlInterfaceRpcService interfaceManager;
44     private ItmRpcService itmProvider;
45     private IdManagerService idManager;
46     private OdlArputilService arpManager;
47     private NeutronvpnService neuService;
48     private PacketProcessingService m_packetProcessingService;
49     private SubnetRoutePacketInHandler subnetRoutePacketInHandler;
50     private NotificationService notificationService;
51
52     @Override
53     public void onSessionInitiated(ProviderContext session) {
54         LOG.info("VpnserviceProvider Session Initiated");
55         try {
56             final  DataBroker dataBroker = session.getSALService(DataBroker.class);
57             vpnManager = new VpnManager(dataBroker, bgpManager);
58             vpnManager.setIdManager(idManager);
59             vpnInterfaceManager = new VpnInterfaceManager(dataBroker, bgpManager, notificationService);
60             vpnInterfaceManager.setMdsalManager(mdsalManager);
61             vpnInterfaceManager.setInterfaceManager(interfaceManager);
62             vpnInterfaceManager.setITMProvider(itmProvider);
63             vpnInterfaceManager.setIdManager(idManager);
64             vpnInterfaceManager.setArpManager(arpManager);
65             vpnInterfaceManager.setNeutronvpnManager(neuService);
66             //Handles subnet route entries
67             subnetRoutePacketInHandler = new SubnetRoutePacketInHandler(dataBroker, idManager);
68             m_packetProcessingService = session.getRpcService(PacketProcessingService.class);
69             subnetRoutePacketInHandler.setPacketProcessingService(m_packetProcessingService);
70             notificationService.registerNotificationListener(subnetRoutePacketInHandler);
71             vpnManager.setVpnInterfaceManager(vpnInterfaceManager);
72             createIdPool();
73                         
74                         RouterInterfaceListener routerListener = new RouterInterfaceListener(dataBroker);
75             routerListener.setVpnInterfaceManager(vpnInterfaceManager);
76         } catch (Exception e) {
77             LOG.error("Error initializing services", e);
78         }
79     }
80
81     public void setNotificationService(NotificationService notificationService) {
82         this.notificationService = notificationService;
83     }
84
85     public void setBgpManager(IBgpManager bgpManager) {
86         LOG.debug("BGP Manager reference initialized");
87         this.bgpManager = bgpManager;
88     }
89
90     public void setMdsalManager(IMdsalApiManager mdsalManager) {
91         this.mdsalManager = mdsalManager;
92     }
93
94     public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
95         this.interfaceManager = interfaceManager;
96     }
97
98     public void setITMProvider(ItmRpcService itmProvider) {
99         this.itmProvider = itmProvider;
100     }
101
102     public void setIdManager(IdManagerService idManager) {
103         this.idManager = idManager;
104     }
105
106     public void setArpManager(OdlArputilService arpManager) {
107         this.arpManager = arpManager;
108     }
109
110     private void createIdPool() {
111         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
112             .setPoolName(VpnConstants.VPN_IDPOOL_NAME)
113             .setLow(VpnConstants.VPN_IDPOOL_START)
114             .setHigh(new BigInteger(VpnConstants.VPN_IDPOOL_SIZE).longValue())
115             .build();
116         try {
117            Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
118            if ((result != null) && (result.get().isSuccessful())) {
119                 LOG.debug("Created IdPool for VPN Service");
120             }
121         } catch (InterruptedException | ExecutionException e) {
122             LOG.error("Failed to create idPool for VPN Service",e);
123         }
124     }
125
126     @Override
127     public void close() throws Exception {
128         vpnManager.close();
129         vpnInterfaceManager.close();
130
131     }
132
133     @Override
134     public void setFibService(IFibManager fibManager) {
135         LOG.debug("Fib service reference is initialized in VPN Manager");
136         this.fibManager = fibManager;
137         vpnInterfaceManager.setFibManager(fibManager);
138     }
139
140     @Override
141     public void addExtraRoute(String destination, String nextHop, String rd, String routerID, int label) {
142         LOG.info("Adding extra route with destination {} and nexthop {}", destination, nextHop);
143         vpnInterfaceManager.addExtraRoute(destination, nextHop, rd, routerID, label, null);
144     }
145
146     @Override
147     public void delExtraRoute(String destination, String rd, String routerID) {
148         LOG.info("Deleting extra route with destination {}", destination);
149         vpnInterfaceManager.delExtraRoute(destination, rd, routerID);
150     }
151 }