Merge "Bug 1088 - flow id in operational not matched; after removal not removed compl...
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / frm / flow / FlowProvider.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.flow;
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.flow.inventory.rev130819.tables.Table;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Preconditions;
27
28 /**
29  * Flow Provider registers the {@link FlowChangeListener} and it holds all needed
30  * services for {@link FlowChangeListener}.
31  *
32  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
33  *
34  */
35 public class FlowProvider implements AutoCloseable {
36
37     private static final Logger LOG = LoggerFactory.getLogger(FlowProvider.class);
38
39     private SalFlowService salFlowService;
40     private DataBroker dataService;
41
42     /* DataChangeListener */
43     private DataChangeListener flowDataChangeListener;
44     private ListenerRegistration<DataChangeListener> flowDataChangeListenerRegistration;
45
46     /**
47      * Provider Initialization Phase.
48      *
49      * @param DataProviderService dataService
50      */
51     public void init (final DataBroker dataService) {
52         LOG.info("FRM Flow Config Provider initialization.");
53         this.dataService = Preconditions.checkNotNull(dataService, "DataProviderService can not be null !");
54     }
55
56     /**
57      * Listener Registration Phase
58      *
59      * @param RpcConsumerRegistry rpcRegistry
60      */
61     public void start(final RpcConsumerRegistry rpcRegistry) {
62         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
63
64         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
65                 "RPC SalFlowService not found.");
66
67         /* Build Path */
68         InstanceIdentifier<Flow> flowIdentifier = InstanceIdentifier.create(Nodes.class)
69                 .child(Node.class).augmentation(FlowCapableNode.class).child(Table.class).child(Flow.class);
70
71         /* DataChangeListener registration */
72         this.flowDataChangeListener = new FlowChangeListener(FlowProvider.this);
73         this.flowDataChangeListenerRegistration =
74                 this.dataService.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
75                         flowIdentifier, flowDataChangeListener, DataChangeScope.SUBTREE);
76
77         LOG.info("FRM Flow Config Provider started.");
78     }
79
80     @Override
81     public void close() {
82         LOG.info("FRM Flow Config Provider stopped.");
83         if (flowDataChangeListenerRegistration != null) {
84             try {
85                 flowDataChangeListenerRegistration.close();
86             } catch (Exception e) {
87                 String errMsg = "Error by stop FRM Flow Config Provider.";
88                 LOG.error(errMsg, e);
89                 throw new IllegalStateException(errMsg, e);
90             } finally {
91                 flowDataChangeListenerRegistration = null;
92             }
93         }
94     }
95
96     public DataChangeListener getFlowDataChangeListener() {
97         return flowDataChangeListener;
98     }
99
100     public SalFlowService getSalFlowService() {
101         return salFlowService;
102     }
103
104     public DataBroker getDataService() {
105         return dataService;
106     }
107 }