2c2126e95cc6f428f9e1117d07cdc387cb32882f
[vpnservice.git] / itm / itm-impl / src / main / java / org / opendaylight / vpnservice / itm / impl / ItmProvider.java
1 /*
2  * Copyright (c) 2015 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.itm.impl;
9
10 import java.math.BigInteger;
11 import java.util.List;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
24 import org.opendaylight.vpnservice.itm.api.IITMProvider;
25 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
26 import org.opendaylight.vpnservice.itm.listeners.TransportZoneListener;
27 import org.opendaylight.vpnservice.itm.rpc.ItmManagerRpcService;
28 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
34
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class ItmProvider implements BindingAwareProvider, AutoCloseable, IITMProvider /*,ItmStateService */{
40
41     private static final Logger LOG = LoggerFactory.getLogger(ItmProvider.class);
42     private IInterfaceManager interfaceManager;
43     private ITMManager itmManager;
44     private IMdsalApiManager mdsalManager;
45     private DataBroker dataBroker;
46     private NotificationPublishService notificationPublishService;
47     private ItmManagerRpcService itmRpcService ;
48     private IdManagerService idManager;
49     private NotificationService notificationService;
50     private TransportZoneListener tzChangeListener;
51     private RpcProviderRegistry rpcProviderRegistry;
52
53     public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
54         this.rpcProviderRegistry = rpcProviderRegistry;
55     }
56
57     public RpcProviderRegistry getRpcProviderRegistry() {
58         return this.rpcProviderRegistry;
59     }
60
61     @Override
62     public void onSessionInitiated(ProviderContext session) {
63         LOG.info("ItmProvider Session Initiated");
64         try {
65             dataBroker = session.getSALService(DataBroker.class);
66             idManager = getRpcProviderRegistry().getRpcService(IdManagerService.class);
67
68             itmManager = new ITMManager(dataBroker);
69             tzChangeListener = new TransportZoneListener(dataBroker, idManager) ;
70             itmRpcService = new ItmManagerRpcService(dataBroker, idManager);
71             final BindingAwareBroker.RpcRegistration<ItmRpcService> rpcRegistration = getRpcProviderRegistry().addRpcImplementation(ItmRpcService.class, itmRpcService);
72             itmRpcService.setMdsalManager(mdsalManager);
73             itmManager.setMdsalManager(mdsalManager);
74             itmManager.setNotificationPublishService(notificationPublishService);
75             itmManager.setMdsalManager(mdsalManager);
76             tzChangeListener.setMdsalManager(mdsalManager);
77             tzChangeListener.setItmManager(itmManager);
78             tzChangeListener.registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
79             createIdPool();
80         } catch (Exception e) {
81             LOG.error("Error initializing services", e);
82         }
83     }
84
85     public void setInterfaceManager(IInterfaceManager interfaceManager) {
86         this.interfaceManager = interfaceManager;
87     }
88
89     public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
90         this.notificationPublishService = notificationPublishService;
91     }
92     
93     public void setMdsalApiManager(IMdsalApiManager mdsalMgr) {
94         this.mdsalManager = mdsalMgr;
95     }
96     public void setNotificationService(NotificationService notificationService) {
97         this.notificationService = notificationService;
98     }
99
100     @Override
101     public void close() throws Exception {
102         if (itmManager != null) {
103             itmManager.close();
104         }
105         if (tzChangeListener != null) {
106             tzChangeListener.close();
107         }
108
109         LOG.info("ItmProvider Closed");
110     }
111
112     private void createIdPool() {
113         CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
114             .setPoolName(ITMConstants.ITM_IDPOOL_NAME)
115             .setLow(ITMConstants.ITM_IDPOOL_START)
116             .setHigh(new BigInteger(ITMConstants.ITM_IDPOOL_SIZE).longValue())
117             .build();
118         try {
119             Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
120             if ((result != null) && (result.get().isSuccessful())) {
121                 LOG.debug("Created IdPool for ITM Service");
122             }
123         } catch (InterruptedException | ExecutionException e) {
124             LOG.error("Failed to create idPool for ITM Service",e);
125         }
126     }
127 }