SubnetRoute enhancements to VPN Service models
[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         } catch (Exception e) {
74             LOG.error("Error initializing services", e);
75         }
76     }
77
78     public void setNotificationService(NotificationService notificationService) {
79         this.notificationService = notificationService;
80     }
81
82     public void setBgpManager(IBgpManager bgpManager) {
83         LOG.debug("BGP Manager reference initialized");
84         this.bgpManager = bgpManager;
85     }
86
87     public void setMdsalManager(IMdsalApiManager mdsalManager) {
88         this.mdsalManager = mdsalManager;
89     }
90
91     public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
92         this.interfaceManager = interfaceManager;
93     }
94
95     public void setITMProvider(ItmRpcService itmProvider) {
96         this.itmProvider = itmProvider;
97     }
98
99     public void setIdManager(IdManagerService idManager) {
100         this.idManager = idManager;
101     }
102
103     public void setArpManager(OdlArputilService arpManager) {
104         this.arpManager = arpManager;
105     }
106
107     private void createIdPool() {
108         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
109             .setPoolName(VpnConstants.VPN_IDPOOL_NAME)
110             .setLow(VpnConstants.VPN_IDPOOL_START)
111             .setHigh(new BigInteger(VpnConstants.VPN_IDPOOL_SIZE).longValue())
112             .build();
113         try {
114            Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
115            if ((result != null) && (result.get().isSuccessful())) {
116                 LOG.debug("Created IdPool for VPN Service");
117             }
118         } catch (InterruptedException | ExecutionException e) {
119             LOG.error("Failed to create idPool for VPN Service",e);
120         }
121     }
122
123     @Override
124     public void close() throws Exception {
125         vpnManager.close();
126         vpnInterfaceManager.close();
127
128     }
129
130     @Override
131     public void setFibService(IFibManager fibManager) {
132         LOG.debug("Fib service reference is initialized in VPN Manager");
133         this.fibManager = fibManager;
134         vpnInterfaceManager.setFibManager(fibManager);
135     }
136
137     @Override
138     public void addExtraRoute(String destination, String nextHop, String rd, String routerID, int label) {
139         LOG.info("Adding extra route with destination {} and nexthop {}", destination, nextHop);
140         vpnInterfaceManager.addExtraRoute(destination, nextHop, rd, routerID, label, null);
141     }
142
143     @Override
144     public void delExtraRoute(String destination, String rd, String routerID) {
145         LOG.info("Deleting extra route with destination {}", destination);
146         vpnInterfaceManager.delExtraRoute(destination, rd, routerID);
147     }
148 }