Bug 6396: Integrate groupbasedpolicy-base with clustering singleton service
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / controller / config / yang / config / groupbasedpolicy / DomainSpecificRegistryInstance.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.groupbasedpolicy;
10
11 import java.util.concurrent.Future;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.groupbasedpolicy.api.DomainSpecificRegistry;
17 import org.opendaylight.groupbasedpolicy.api.EndpointAugmentorRegistry;
18 import org.opendaylight.groupbasedpolicy.api.NetworkDomainAugmentorRegistry;
19 import org.opendaylight.groupbasedpolicy.base_endpoint.BaseEndpointServiceImpl;
20 import org.opendaylight.groupbasedpolicy.base_endpoint.EndpointAugmentorRegistryImpl;
21 import org.opendaylight.groupbasedpolicy.forwarding.NetworkDomainAugmentorRegistryImpl;
22 import org.opendaylight.groupbasedpolicy.renderer.RendererManager;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
26 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInput;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class DomainSpecificRegistryInstance implements ClusterSingletonService, DomainSpecificRegistry, BaseEndpointService, AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(DomainSpecificRegistryInstance.class);
37
38     private static final ServiceGroupIdentifier IDENTIFIER =
39             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
40     private final DataBroker dataBroker;
41     private ClusterSingletonServiceProvider clusterSingletonService;
42     private ClusterSingletonServiceRegistration singletonServiceRegistration;
43     private EndpointAugmentorRegistryImpl endpointAugmentorRegistryImpl;
44     private NetworkDomainAugmentorRegistryImpl netDomainAugmentorRegistryImpl;
45     private BaseEndpointServiceImpl baseEndpointServiceImpl;
46     private RendererManager rendererManager;
47
48     public DomainSpecificRegistryInstance(final DataBroker dataBroker,
49                                           final ClusterSingletonServiceProvider clusterSingletonService) {
50         this.dataBroker = Preconditions.checkNotNull(dataBroker);
51         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
52     }
53
54     @Override
55     public EndpointAugmentorRegistry getEndpointAugmentorRegistry() {
56         return endpointAugmentorRegistryImpl;
57     }
58
59     @Override
60     public NetworkDomainAugmentorRegistry getNetworkDomainAugmentorRegistry() {
61         return netDomainAugmentorRegistryImpl;
62     }
63
64     @Override
65     public Future<RpcResult<Void>> unregisterEndpoint(UnregisterEndpointInput input) {
66         return baseEndpointServiceImpl.unregisterEndpoint(input);
67     }
68
69     @Override
70     public Future<RpcResult<Void>> registerEndpoint(RegisterEndpointInput input) {
71         return baseEndpointServiceImpl.registerEndpoint(input);
72     }
73
74     public void initialize() {
75         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
76         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
77     }
78
79     @Override
80     public void instantiateServiceInstance() {
81         LOG.info("Instantiating {}", this.getClass().getSimpleName());
82         endpointAugmentorRegistryImpl = new EndpointAugmentorRegistryImpl();
83         netDomainAugmentorRegistryImpl = new NetworkDomainAugmentorRegistryImpl();
84         baseEndpointServiceImpl = new BaseEndpointServiceImpl(dataBroker, endpointAugmentorRegistryImpl);
85         rendererManager = new RendererManager(dataBroker, netDomainAugmentorRegistryImpl, endpointAugmentorRegistryImpl);
86     }
87
88     @Override
89     public ListenableFuture<Void> closeServiceInstance() {
90         LOG.info("Instance {} closed", this.getClass().getSimpleName());
91         baseEndpointServiceImpl.close();
92         rendererManager.close();
93         return Futures.immediateFuture(null);
94     }
95
96     @Override
97     public void close() throws Exception {
98         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
99         if (singletonServiceRegistration != null) {
100             try {
101                 singletonServiceRegistration.close();
102             } catch (Exception e) {
103                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
104             }
105             singletonServiceRegistration = null;
106         }
107     }
108
109     @Override
110     public ServiceGroupIdentifier getIdentifier() {
111         return IDENTIFIER;
112     }
113 }