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