Bug 6396: Integrate groupbasedpolicy-base with clustering singleton service
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / controller / config / yang / config / groupbasedpolicy / EpRendererAugmentationRegistryImplInstance.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.EpRendererAugmentation;
17 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
18 import org.opendaylight.groupbasedpolicy.endpoint.EndpointRpcRegistry;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.SetEndpointGroupConditionsInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnsetEndpointGroupConditionsInput;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class EpRendererAugmentationRegistryImplInstance implements ClusterSingletonService, EpRendererAugmentationRegistry, EndpointService, AutoCloseable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(EpRendererAugmentationRegistryImplInstance.class);
36
37     private static final ServiceGroupIdentifier IDENTIFIER =
38             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
39     private final DataBroker dataBroker;
40     private ClusterSingletonServiceProvider clusterSingletonService;
41     private ClusterSingletonServiceRegistration singletonServiceRegistration;
42     private EndpointRpcRegistry endpointRpcRegistry;
43
44     public EpRendererAugmentationRegistryImplInstance(final DataBroker dataBroker,
45                                                       final ClusterSingletonServiceProvider clusterSingletonService) {
46         this.dataBroker = Preconditions.checkNotNull(dataBroker);
47         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
48     }
49
50     @Override
51     public void register(EpRendererAugmentation epRendererAugmentation) {
52         endpointRpcRegistry.register(epRendererAugmentation);
53     }
54
55     @Override
56     public void unregister(EpRendererAugmentation epRendererAugmentation) {
57         endpointRpcRegistry.unregister(epRendererAugmentation);
58     }
59
60     @Override
61     public Future<RpcResult<Void>> unsetEndpointGroupConditions(UnsetEndpointGroupConditionsInput input) {
62         return endpointRpcRegistry.unsetEndpointGroupConditions(input);
63     }
64
65     @Override
66     public Future<RpcResult<Void>> registerEndpoint(RegisterEndpointInput input) {
67         return endpointRpcRegistry.registerEndpoint(input);
68     }
69
70     @Override
71     public Future<RpcResult<Void>> setEndpointGroupConditions(SetEndpointGroupConditionsInput input) {
72         return endpointRpcRegistry.setEndpointGroupConditions(input);
73     }
74
75     @Override
76     public Future<RpcResult<Void>> registerL3PrefixEndpoint(RegisterL3PrefixEndpointInput input) {
77         return endpointRpcRegistry.registerL3PrefixEndpoint(input);
78     }
79
80     @Override
81     public Future<RpcResult<Void>> unregisterEndpoint(UnregisterEndpointInput input) {
82         return endpointRpcRegistry.unregisterEndpoint(input);
83     }
84
85     public void initialize() {
86         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
87         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
88     }
89
90     @Override
91     public void instantiateServiceInstance() {
92         LOG.info("Instantiating {}", this.getClass().getSimpleName());
93         endpointRpcRegistry = new EndpointRpcRegistry(dataBroker);
94     }
95
96     @Override
97     public ListenableFuture<Void> closeServiceInstance() {
98         LOG.info("Instance {} closed", this.getClass().getSimpleName());
99         endpointRpcRegistry.close();
100         return Futures.immediateFuture(null);
101     }
102
103     @Override
104     public void close() {
105         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
106         if (singletonServiceRegistration != null) {
107             try {
108                 singletonServiceRegistration.close();
109             } catch (Exception e) {
110                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
111             }
112             singletonServiceRegistration = null;
113         }
114     }
115
116     @Override
117     public ServiceGroupIdentifier getIdentifier() {
118         return IDENTIFIER;
119     }
120 }