catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / sxp-integration / ip-sgt-distribution-service / src / main / java / org / opendaylight / controller / config / yang / config / ip / sgt / distribution / service / cfg / IpSgtDistributionServiceInstance.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.ip.sgt.distribution.service.cfg;
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.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
19 import org.opendaylight.groupbasedpolicy.ip.sgt.distribution.service.impl.IpSgtDistributionServiceImpl;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
23 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.ip.sgt.distribution.rev160715.IpSgtDistributionService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.ip.sgt.distribution.rev160715.RemoveIpSgtBindingFromPeerInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.ip.sgt.distribution.rev160715.SendIpSgtBindingToPeerInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.controller.rev141002.SxpControllerService;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class IpSgtDistributionServiceInstance
34         implements ClusterSingletonService, IpSgtDistributionService, AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(IpSgtDistributionServiceInstance.class);
37     private static final ServiceGroupIdentifier IDENTIFIER =
38             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
39     private IpSgtDistributionServiceImpl ipSgtDistributionServiceImpl;
40     private final DataBroker dataBroker;
41     private final SxpControllerService sxpService;
42     private final IpAddress sourceIp;
43     private final ClusterSingletonServiceProvider clusterSingletonService;
44     private final RpcProviderRegistry rpcProviderRegistry;
45     private ClusterSingletonServiceRegistration singletonServiceRegistration;
46     private BindingAwareBroker.RpcRegistration<IpSgtDistributionService> rpcRegistration;
47
48     public IpSgtDistributionServiceInstance(final DataBroker dataBroker,
49                                             final String sourceIp, final ClusterSingletonServiceProvider clusterSingletonService,
50                                             final RpcProviderRegistry rpcProviderRegistry) {
51         this.dataBroker = Preconditions.checkNotNull(dataBroker);
52         this.sxpService = Preconditions.checkNotNull(rpcProviderRegistry.getRpcService(SxpControllerService.class));
53         this.sourceIp = new IpAddress(Preconditions.checkNotNull(sourceIp.toCharArray()));
54         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
55         this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
56     }
57
58     public void initialize() {
59         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
60         try {
61             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
62         } catch (Exception e) {
63             LOG.warn("Exception thrown while registering cluster singleton service in {}", this.getClass(), e.getMessage());
64         }
65     }
66
67     @Override
68     public ServiceGroupIdentifier getIdentifier() {
69         return IDENTIFIER;
70     }
71
72     @Override
73     public void close() throws Exception {
74         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
75         if (singletonServiceRegistration != null) {
76             try {
77                 singletonServiceRegistration.close();
78             } catch (Exception e) {
79                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
80             }
81             singletonServiceRegistration = null;
82         }
83     }
84
85     @Override
86     public Future<RpcResult<Void>> removeIpSgtBindingFromPeer(RemoveIpSgtBindingFromPeerInput input) {
87         return ipSgtDistributionServiceImpl.removeIpSgtBindingFromPeer(input);
88     }
89
90     @Override
91     public Future<RpcResult<Void>> sendIpSgtBindingToPeer(SendIpSgtBindingToPeerInput input) {
92         return ipSgtDistributionServiceImpl.sendIpSgtBindingToPeer(input);
93     }
94
95     @Override
96     public ListenableFuture<Void> closeServiceInstance() {
97         LOG.info("Instance {} closed", this.getClass().getSimpleName());
98         try {
99             ipSgtDistributionServiceImpl.close();
100             rpcRegistration.close();
101         } catch (Exception e) {
102             LOG.error("Closing {} wasnt succesfull", ipSgtDistributionServiceImpl.getClass().getSimpleName());
103         }
104         return Futures.immediateFuture(null);
105     }
106
107     @Override
108     public void instantiateServiceInstance() {
109         ipSgtDistributionServiceImpl = new IpSgtDistributionServiceImpl(dataBroker, sxpService, sourceIp);
110         rpcRegistration = rpcProviderRegistry.addRpcImplementation(IpSgtDistributionService.class, this);
111     }
112
113 }