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());
61 singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
62 } catch (Exception e) {
63 LOG.warn("Exception thrown while registering cluster singleton service in {}", this.getClass(), e.getMessage());
68 public ServiceGroupIdentifier getIdentifier() {
73 public void close() throws Exception {
74 LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
75 if (singletonServiceRegistration != null) {
77 singletonServiceRegistration.close();
78 } catch (Exception e) {
79 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e);
81 singletonServiceRegistration = null;
86 public Future<RpcResult<Void>> removeIpSgtBindingFromPeer(RemoveIpSgtBindingFromPeerInput input) {
87 return ipSgtDistributionServiceImpl.removeIpSgtBindingFromPeer(input);
91 public Future<RpcResult<Void>> sendIpSgtBindingToPeer(SendIpSgtBindingToPeerInput input) {
92 return ipSgtDistributionServiceImpl.sendIpSgtBindingToPeer(input);
96 public ListenableFuture<Void> closeServiceInstance() {
97 LOG.info("Instance {} closed", this.getClass().getSimpleName());
99 ipSgtDistributionServiceImpl.close();
100 rpcRegistration.close();
101 } catch (Exception e) {
102 LOG.error("Closing {} wasnt succesfull", ipSgtDistributionServiceImpl.getClass().getSimpleName());
104 return Futures.immediateFuture(null);
108 public void instantiateServiceInstance() {
109 ipSgtDistributionServiceImpl = new IpSgtDistributionServiceImpl(dataBroker, sxpService, sourceIp);
110 rpcRegistration = rpcProviderRegistry.addRpcImplementation(IpSgtDistributionService.class, this);