2 * Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
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
9 package org.opendaylight.controller.config.yang.config.ip.sgt.distribution.service.cfg;
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;
33 public class IpSgtDistributionServiceInstance
34 implements ClusterSingletonService, IpSgtDistributionService, AutoCloseable {
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;
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);
58 public void initialize() {
59 LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
60 singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
64 public ServiceGroupIdentifier getIdentifier() {
69 public void close() throws Exception {
70 LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
71 if (singletonServiceRegistration != null) {
73 singletonServiceRegistration.close();
74 } catch (Exception e) {
75 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
77 singletonServiceRegistration = null;
82 public Future<RpcResult<Void>> removeIpSgtBindingFromPeer(RemoveIpSgtBindingFromPeerInput input) {
83 return ipSgtDistributionServiceImpl.removeIpSgtBindingFromPeer(input);
87 public Future<RpcResult<Void>> sendIpSgtBindingToPeer(SendIpSgtBindingToPeerInput input) {
88 return ipSgtDistributionServiceImpl.sendIpSgtBindingToPeer(input);
92 public ListenableFuture<Void> closeServiceInstance() {
93 LOG.info("Instance {} closed", this.getClass().getSimpleName());
95 ipSgtDistributionServiceImpl.close();
96 rpcRegistration.close();
97 } catch (Exception e) {
98 LOG.error("Closing {} wasnt succesfull", ipSgtDistributionServiceImpl.getClass().getSimpleName());
100 return Futures.immediateFuture(null);
104 public void instantiateServiceInstance() {
105 ipSgtDistributionServiceImpl = new IpSgtDistributionServiceImpl(dataBroker, sxpService, sourceIp);
106 rpcRegistration = rpcProviderRegistry.addRpcImplementation(IpSgtDistributionService.class, this);