Merge "Bug 809: Enhancements to the toaster example"
[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.sal.binding.api.data.DataChangeListener;
11 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
12 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class FlowProvider implements AutoCloseable {
27
28     private final static Logger LOG = LoggerFactory.getLogger(FlowProvider.class);
29
30     private SalFlowService salFlowService;
31     private DataProviderService dataService;
32
33     /* DataChangeListener */
34     private FlowChangeListener flowDataChangeListener;
35     ListenerRegistration<DataChangeListener> flowDataChangeListenerRegistration;
36
37     public void start() {
38         /* Build Path */
39         InstanceIdentifierBuilder<Nodes> nodesBuilder = InstanceIdentifier.<Nodes> builder(Nodes.class);
40         InstanceIdentifierBuilder<Node> nodeChild = nodesBuilder.<Node> child(Node.class);
41         InstanceIdentifierBuilder<FlowCapableNode> augmentFlowCapNode = nodeChild.<FlowCapableNode> augmentation(FlowCapableNode.class);
42         InstanceIdentifierBuilder<Table> tableChild = augmentFlowCapNode.<Table> child(Table.class);
43         InstanceIdentifierBuilder<Flow> flowChild = tableChild.<Flow> child(Flow.class);
44         final InstanceIdentifier<? extends DataObject> flowDataObjectPath = flowChild.toInstance();
45         
46         /* DataChangeListener registration */
47         this.flowDataChangeListener = new FlowChangeListener(this.salFlowService);
48         this.flowDataChangeListenerRegistration = this.dataService.registerDataChangeListener(flowDataObjectPath, flowDataChangeListener);
49         LOG.info("Flow Config Provider started.");
50     }
51
52     protected DataModificationTransaction startChange() {
53         return this.dataService.beginTransaction();
54     }
55
56     @Override
57     public void close() throws Exception {
58         if(flowDataChangeListenerRegistration != null){
59             flowDataChangeListenerRegistration.close();
60         }
61     }
62
63     public void setDataService(final DataProviderService dataService) {
64         this.dataService = dataService;
65     }
66
67     public void setSalFlowService(final SalFlowService salFlowService) {
68         this.salFlowService = salFlowService;
69     }
70 }