Bug 5010: NeutronVpn: Internal VPN delete/recreate redesign + related fixes
[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.vpnservice.itm.rpcs.rev151217.ItmRpcService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
23 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.OdlArputilService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class VpnserviceProvider implements BindingAwareProvider, IVpnManager,
33                                                        AutoCloseable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(VpnserviceProvider.class);
36     private VpnInterfaceManager vpnInterfaceManager;
37     private VpnManager vpnManager;
38     private IBgpManager bgpManager;
39     private IFibManager fibManager;
40     private IMdsalApiManager mdsalManager;
41     private OdlInterfaceRpcService interfaceManager;
42     private ItmRpcService itmProvider;
43     private IdManagerService idManager;
44     private OdlArputilService arpManager;
45     private NotificationService notificationService;
46
47     @Override
48     public void onSessionInitiated(ProviderContext session) {
49         LOG.info("VpnserviceProvider Session Initiated");
50         try {
51             final  DataBroker dataBroker = session.getSALService(DataBroker.class);
52             vpnManager = new VpnManager(dataBroker, bgpManager);
53             vpnManager.setIdManager(idManager);
54             vpnInterfaceManager = new VpnInterfaceManager(dataBroker, bgpManager, notificationService);
55             vpnInterfaceManager.setMdsalManager(mdsalManager);
56             vpnInterfaceManager.setInterfaceManager(interfaceManager);
57             vpnInterfaceManager.setITMProvider(itmProvider);
58             vpnInterfaceManager.setIdManager(idManager);
59             vpnInterfaceManager.setArpManager(arpManager);
60             vpnManager.setVpnInterfaceManager(vpnInterfaceManager);
61             createIdPool();
62         } catch (Exception e) {
63             LOG.error("Error initializing services", e);
64         }
65     }
66
67     public void setNotificationService(NotificationService notificationService) {
68         this.notificationService = notificationService;
69     }
70
71     public void setBgpManager(IBgpManager bgpManager) {
72         LOG.debug("BGP Manager reference initialized");
73         this.bgpManager = bgpManager;
74     }
75
76     public void setMdsalManager(IMdsalApiManager mdsalManager) {
77         this.mdsalManager = mdsalManager;
78     }
79
80     public void setFibManager(IFibManager fibManager) {
81         this.fibManager = fibManager;
82     }
83
84     public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
85         this.interfaceManager = interfaceManager;
86     }
87
88     public void setITMProvider(ItmRpcService itmProvider) {
89         this.itmProvider = itmProvider;
90     }
91
92     public void setIdManager(IdManagerService idManager) {
93         this.idManager = idManager;
94     }
95
96     public void setArpManager(OdlArputilService arpManager) {
97         this.arpManager = arpManager;
98     }
99
100     private void createIdPool() {
101         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
102             .setPoolName(VpnConstants.VPN_IDPOOL_NAME)
103             .setLow(VpnConstants.VPN_IDPOOL_START)
104             .setHigh(new BigInteger(VpnConstants.VPN_IDPOOL_SIZE).longValue())
105             .build();
106         try {
107            Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
108            if ((result != null) && (result.get().isSuccessful())) {
109                 LOG.debug("Created IdPool for VPN Service");
110             }
111         } catch (InterruptedException | ExecutionException e) {
112             LOG.error("Failed to create idPool for VPN Service",e);
113         }
114     }
115
116     @Override
117     public void close() throws Exception {
118         vpnManager.close();
119         vpnInterfaceManager.close();
120
121     }
122
123     @Override
124     public void setFibService(IFibManager fibManager) {
125         LOG.debug("Fib service reference is initialized in VPN Manager");
126         this.fibManager = fibManager;
127         vpnInterfaceManager.setFibManager(fibManager);
128     }
129
130     @Override
131     public void addExtraRoute(String destination, String nextHop, String rd, String routerID, int label) {
132         LOG.info("Adding extra route with destination {} and nexthop {}", destination, nextHop);
133         vpnInterfaceManager.addExtraRoute(destination, nextHop, rd, routerID, label);
134     }
135
136     @Override
137     public void delExtraRoute(String destination, String rd, String routerID) {
138         LOG.info("Deleting extra route with destination {}", destination);
139         vpnInterfaceManager.delExtraRoute(destination, rd, routerID);
140     }
141 }