a999242bc08ed8c16fe2cde4074248435d815131
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / frm / group / GroupProvider.java
1 /**
2  * Copyright (c) 2014 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 package org.opendaylight.controller.frm.group;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.Preconditions;
26
27 /**
28  * Group Provider registers the {@link GroupChangeListener} and it holds all needed
29  * services for {@link GroupChangeListener}.
30  *
31  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
32  *
33  */
34 public class GroupProvider implements AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(GroupProvider.class);
37
38     private SalGroupService salGroupService;
39     private DataBroker dataService;
40
41     /* DataChangeListener */
42     private DataChangeListener groupDataChangeListener;
43     private ListenerRegistration<DataChangeListener> groupDataChangeListenerRegistration;
44
45     /**
46      * Provider Initialization Phase.
47      *
48      * @param DataProviderService dataService
49      */
50     public void init (final DataBroker dataService) {
51         LOG.info("FRM Group Config Provider initialization.");
52         this.dataService = Preconditions.checkNotNull(dataService, "DataService can not be null !");
53     }
54
55     /**
56      * Listener Registration Phase
57      *
58      * @param RpcConsumerRegistry rpcRegistry
59      */
60     public void start(final RpcConsumerRegistry rpcRegistry) {
61         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
62
63         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
64                 "RPC SalGroupService not found.");
65
66         /* Build Path */
67         InstanceIdentifier<Group> groupIdentifier = InstanceIdentifier.create(Nodes.class)
68                 .child(Node.class).augmentation(FlowCapableNode.class).child(Group.class);
69
70         /* DataChangeListener registration */
71         this.groupDataChangeListener = new GroupChangeListener(GroupProvider.this);
72         this.groupDataChangeListenerRegistration = this.dataService.registerDataChangeListener(
73                 LogicalDatastoreType.CONFIGURATION, groupIdentifier, groupDataChangeListener, DataChangeScope.SUBTREE);
74
75         LOG.info("FRM Group Config Provider started.");
76     }
77
78     @Override
79     public void close() {
80         LOG.info("FRM Group Config Provider stopped.");
81         if (groupDataChangeListenerRegistration != null) {
82             try {
83                 groupDataChangeListenerRegistration.close();
84             } catch (Exception e) {
85                 String errMsg = "Error by stop FRM Group Config Provider.";
86                 LOG.error(errMsg, e);
87                 throw new IllegalStateException(errMsg, e);
88             } finally {
89                 groupDataChangeListenerRegistration = null;
90             }
91         }
92     }
93
94     public DataChangeListener getGroupDataChangeListener() {
95         return groupDataChangeListener;
96     }
97
98     public SalGroupService getSalGroupService() {
99         return salGroupService;
100     }
101
102     public DataBroker getDataService() {
103         return dataService;
104     }
105 }