e1f9638661288e1ba20099ba2df656df61dbfdba
[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         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
61     }
62
63     @Override
64     public ServiceGroupIdentifier getIdentifier() {
65         return IDENTIFIER;
66     }
67
68     @Override
69     public void close() throws Exception {
70         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
71         if (singletonServiceRegistration != null) {
72             try {
73                 singletonServiceRegistration.close();
74             } catch (Exception e) {
75                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
76             }
77             singletonServiceRegistration = null;
78         }
79     }
80
81     @Override
82     public Future<RpcResult<Void>> removeIpSgtBindingFromPeer(RemoveIpSgtBindingFromPeerInput input) {
83         return ipSgtDistributionServiceImpl.removeIpSgtBindingFromPeer(input);
84     }
85
86     @Override
87     public Future<RpcResult<Void>> sendIpSgtBindingToPeer(SendIpSgtBindingToPeerInput input) {
88         return ipSgtDistributionServiceImpl.sendIpSgtBindingToPeer(input);
89     }
90
91     @Override
92     public ListenableFuture<Void> closeServiceInstance() {
93         LOG.info("Instance {} closed", this.getClass().getSimpleName());
94         try {
95             ipSgtDistributionServiceImpl.close();
96             rpcRegistration.close();
97         } catch (Exception e) {
98             LOG.error("Closing {} wasnt succesfull", ipSgtDistributionServiceImpl.getClass().getSimpleName());
99         }
100         return Futures.immediateFuture(null);
101     }
102
103     @Override
104     public void instantiateServiceInstance() {
105         ipSgtDistributionServiceImpl = new IpSgtDistributionServiceImpl(dataBroker, sxpService, sourceIp);
106         rpcRegistration = rpcProviderRegistry.addRpcImplementation(IpSgtDistributionService.class, this);
107     }
108
109 }