4421bd10c6ec62156b95431177c6c4a8aa8ea11c
[groupbasedpolicy.git] / renderers / ios-xe / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / config / IosXeProviderInstance.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.groupbasedpolicy.renderer.ios_xe_provider.impl.config;
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 org.opendaylight.controller.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
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.renderer.ios_xe_provider.impl.IosXeRendererProviderImpl;
19 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.spi.SxpEpProviderProvider;
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.opendaylight.params.xml.ns.yang.controller.config.ip.sgt.distribution.rev160715.IpSgtDistributionService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class IosXeProviderInstance implements ClusterSingletonService, AutoCloseable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(IosXeProviderInstance.class);
31
32     private static final ServiceGroupIdentifier IDENTIFIER =
33             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
34     private final DataBroker dataBroker;
35     private final BindingAwareBroker bindingAwareBroker;
36     private final ClusterSingletonServiceProvider clusterSingletonService;
37     private final SxpEpProviderProvider sxpEpProvider;
38     private final IpSgtDistributionService ipSgtDistributor;
39     private ClusterSingletonServiceRegistration singletonServiceRegistration;
40     private IosXeRendererProviderImpl renderer;
41
42     public IosXeProviderInstance(final DataBroker dataBroker,
43                                  final BindingAwareBroker broker,
44                                  final ClusterSingletonServiceProvider clusterSingletonService,
45                                  final SxpEpProviderProvider sxpEpProvider,
46                                  final RpcProviderRegistry rpcProviderRegistry) {
47         this.dataBroker = Preconditions.checkNotNull(dataBroker);
48         this.bindingAwareBroker = Preconditions.checkNotNull(broker);
49         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
50         this.sxpEpProvider = Preconditions.checkNotNull(sxpEpProvider);
51         this.ipSgtDistributor = Preconditions.checkNotNull(rpcProviderRegistry.getRpcService(IpSgtDistributionService.class));
52     }
53
54     public void initialize() {
55         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
56         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
57     }
58
59     @Override
60     public void instantiateServiceInstance() {
61         LOG.info("Instantiating {}", this.getClass().getSimpleName());
62         renderer = new IosXeRendererProviderImpl(dataBroker, bindingAwareBroker, sxpEpProvider, ipSgtDistributor);
63     }
64
65     @Override
66     public ListenableFuture<Void> closeServiceInstance() {
67         LOG.info("Instance {} closed", this.getClass().getSimpleName());
68         renderer.close();
69         return Futures.immediateFuture(null);
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. Cause: {}", e.getMessage());
80             }
81             singletonServiceRegistration = null;
82         }
83     }
84
85     @Override
86     public ServiceGroupIdentifier getIdentifier() {
87         return IDENTIFIER;
88     }
89 }