Replace rpc registration via blueprint with rpc-broker
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / controller / config / yang / config / neutron_mapper / impl / NeutronMapperInstance.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.config.yang.config.neutron_mapper.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.controller.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
17 import org.opendaylight.groupbasedpolicy.neutron.mapper.NeutronMapper;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class NeutronMapperInstance implements ClusterSingletonService, AutoCloseable {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NeutronMapperInstance.class);
30
31     private static final ServiceGroupIdentifier IDENTIFIER =
32             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
33     private final DataBroker dataBroker;
34     private final ClusterSingletonServiceProvider clusterSingletonService;
35     private final RpcProviderRegistry rpcBroker;
36     private ClusterSingletonServiceRegistration singletonServiceRegistration;
37     private NeutronMapper mapper;
38
39     public NeutronMapperInstance(final DataBroker dataBroker,
40                                  final RpcProviderRegistry rpcBroker,
41                                  final ClusterSingletonServiceProvider clusterSingletonService) {
42         this.dataBroker = Preconditions.checkNotNull(dataBroker);
43         this.rpcBroker = Preconditions.checkNotNull(rpcBroker);
44         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
45     }
46
47     public void instantiate() {
48         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
49         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
50     }
51
52     @Override
53     public void instantiateServiceInstance() {
54         LOG.info("Instantiating {}", this.getClass().getSimpleName());
55         final EndpointService epService = rpcBroker.getRpcService(EndpointService.class);
56         final BaseEndpointService baseEndpointService = rpcBroker.getRpcService(BaseEndpointService.class);
57         mapper = new NeutronMapper(dataBroker, epService, baseEndpointService);
58     }
59
60     @Override
61     public ListenableFuture<Void> closeServiceInstance() {
62         LOG.info("Instance {} closed", this.getClass().getSimpleName());
63         mapper.close();
64         return Futures.immediateFuture(null);
65     }
66
67     @Override
68     public void close() throws Exception {
69         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
70         if (singletonServiceRegistration != null) {
71             try {
72                 singletonServiceRegistration.close();
73             } catch (Exception e) {
74                 LOG.warn("{} closed unexpectedly", e.getMessage());
75             }
76             singletonServiceRegistration = null;
77         }
78     }
79
80     @Override
81     public ServiceGroupIdentifier getIdentifier() {
82         return IDENTIFIER;
83     }
84
85 }