c8ff5486ef4ccdd3727234a67c478972690458ca
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / controller / config / yang / config / ofoverlay_provider / impl / OFOverlayProviderInstance.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.controller.config.yang.config.ofoverlay_provider.impl;
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.md.sal.binding.api.NotificationService;
17 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
18 import org.opendaylight.groupbasedpolicy.api.PolicyValidatorRegistry;
19 import org.opendaylight.groupbasedpolicy.api.StatisticsManager;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OFOverlayRenderer;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
24 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class OFOverlayProviderInstance implements ClusterSingletonService, AutoCloseable {
31
32     private static final Logger LOG = LoggerFactory.getLogger(OFOverlayProviderInstance.class);
33
34     private static final ServiceGroupIdentifier IDENTIFIER =
35             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
36     private final DataBroker dataBroker;
37     private final PacketProcessingService packetProcessingService;
38     private final SalFlowService flowService;
39     private final NotificationService notificationService;
40     private final EpRendererAugmentationRegistry epRendererAugmentationRegistry;
41     private final PolicyValidatorRegistry policyValidatorRegistry;
42     private final StatisticsManager statisticsManager;
43     private final short tableOffset;
44     private final ClusterSingletonServiceProvider clusterSingletonService;
45     private ClusterSingletonServiceRegistration singletonServiceRegistration;
46     private OFOverlayRenderer renderer;
47
48     public OFOverlayProviderInstance(final DataBroker dataBroker,
49                                      final PacketProcessingService packetProcessingService,
50                                      final SalFlowService flowService,
51                                      final NotificationService notificationService,
52                                      final EpRendererAugmentationRegistry epRendererAugmentationRegistry,
53                                      final PolicyValidatorRegistry policyValidatorRegistry,
54                                      final StatisticsManager statisticsManager,
55                                      final short tableOffset,
56                                      final ClusterSingletonServiceProvider clusterSingletonService) {
57         this.dataBroker = Preconditions.checkNotNull(dataBroker);
58         this.packetProcessingService = Preconditions.checkNotNull(packetProcessingService);
59         this.flowService = Preconditions.checkNotNull(flowService);
60         this.notificationService = Preconditions.checkNotNull(notificationService);
61         this.epRendererAugmentationRegistry = Preconditions.checkNotNull(epRendererAugmentationRegistry);
62         this.policyValidatorRegistry = Preconditions.checkNotNull(policyValidatorRegistry);
63         this.statisticsManager = Preconditions.checkNotNull(statisticsManager);
64         this.tableOffset = Preconditions.checkNotNull(tableOffset);
65         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
66     }
67
68     public void initialize() {
69         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
70         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
71     }
72
73     @Override
74     public void instantiateServiceInstance() {
75         LOG.info("Instantiating {}", this.getClass().getSimpleName());
76         renderer = new OFOverlayRenderer(dataBroker, packetProcessingService, flowService, notificationService,
77                 epRendererAugmentationRegistry, policyValidatorRegistry, statisticsManager, tableOffset);
78     }
79
80     @Override
81     public ListenableFuture<Void> closeServiceInstance() {
82         LOG.info("Instance {} closed", this.getClass().getSimpleName());
83         try {
84             renderer.close();
85         } catch (Exception e) {
86             LOG.warn("Exception thrown when closing ... {}", e.getMessage());
87         }
88         return Futures.immediateFuture(null);
89     }
90
91     @Override
92     public void close() {
93         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
94         if (singletonServiceRegistration != null) {
95             try {
96                 singletonServiceRegistration.close();
97             } catch (Exception e) {
98                 LOG.warn("{} closed unexpectedly",this.getClass().getSimpleName(), e.getMessage());
99             }
100             singletonServiceRegistration = null;
101         }
102     }
103
104     @Override
105     public ServiceGroupIdentifier getIdentifier() {
106         return IDENTIFIER;
107     }
108 }