Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / compatibility / flow-management-compatibility / src / main / java / org / opendaylight / controller / md / frm / compatibility / FRMRuntimeDataProvider.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.md.frm.compatibility;
9
10 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
11 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
12 import org.opendaylight.controller.md.sal.common.api.data.DataChangeListener;
13 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
14 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
15 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
16 import org.opendaylight.controller.sal.binding.api.data.RuntimeDataProvider;
17 import org.opendaylight.controller.sal.common.util.Arguments;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.Flows;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.FlowKey;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.Identifier;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28
29 import com.google.common.base.Objects;
30 import com.google.common.base.Preconditions;
31
32 public class FRMRuntimeDataProvider implements RuntimeDataProvider, DataCommitHandler<InstanceIdentifier<? extends DataObject>,DataObject> {
33
34     private final static InstanceIdentifier<Flows> FLOWS_PATH = InstanceIdentifier.<Flows> builder(Flows.class).toInstance();
35
36     private final FlowManagementReader configuration = new ConfigurationReader();
37     private DataChangeListener<InstanceIdentifier<? extends DataObject>, DataObject> changeListener;
38     private DataProviderService dataService;
39     private IForwardingRulesManager manager;
40
41     public Registration<DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject>> init() {
42         return this.dataService.registerCommitHandler(FRMRuntimeDataProvider.FLOWS_PATH, this);
43     }
44
45     @Override
46     public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
47         return this.readFrom(this.configuration, path);
48     }
49
50     @Override
51     public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
52         return this.readFrom(this.configuration, path);
53     }
54
55     public DataObject readFrom(final FlowManagementReader store, final InstanceIdentifier<? extends DataObject> path) {
56         if (Objects.equal(FRMRuntimeDataProvider.FLOWS_PATH, path)) {
57             return store.readAllFlows();
58         }
59         if (FRMRuntimeDataProvider.FLOWS_PATH.contains(path)) {
60             return store.readFlow(this.toFlowKey(path));
61         }
62         return null;
63     }
64
65     @Override
66     public FlowCommitTransaction requestCommit(final DataModification<InstanceIdentifier<? extends DataObject>, DataObject> modification) {
67         return new FlowCommitTransaction(this, modification);
68     }
69
70     public FlowKey toFlowKey(final InstanceIdentifier<? extends DataObject> identifier) {
71         Preconditions.<InstanceIdentifier<? extends DataObject>> checkNotNull(identifier);
72
73         Iterable<PathArgument> path = identifier.getPathArguments();
74         PathArgument get = path.iterator().next();
75         final Identifier itemKey = Arguments.<IdentifiableItem> checkInstanceOf(get, IdentifiableItem.class).getKey();
76         return Arguments.<FlowKey> checkInstanceOf(itemKey, FlowKey.class);
77     }
78
79     public RpcResult<Void> finish(final FlowCommitTransaction transaction) {
80         Iterable<FlowConfig> toRemove = transaction.getToRemove();
81         for (final FlowConfig flow : toRemove) {
82             this.manager.removeStaticFlow(flow.getName(), flow.getNode());
83         }
84         Iterable<FlowConfig> toUpdate = transaction.getToUpdate();
85         for (final FlowConfig flow : toUpdate) {
86             this.manager.removeStaticFlow(flow.getName(), flow.getNode());
87             this.manager.addStaticFlow(flow);
88         }
89         return RpcResultBuilder.<Void> success().build();
90     }
91
92     public RpcResult<Void> rollback(final FlowCommitTransaction transaction) {
93         return null;
94     }
95
96     public DataProviderService getDataService() {
97         return this.dataService;
98     }
99
100     public void setDataService(final DataProviderService dataService) {
101         this.dataService = dataService;
102     }
103
104     public DataChangeListener<InstanceIdentifier<? extends DataObject>, DataObject> getChangeListener() {
105         return this.changeListener;
106     }
107
108     public void setChangeListener(final DataChangeListener<InstanceIdentifier<? extends DataObject>, DataObject> changeListener) {
109         this.changeListener = changeListener;
110     }
111
112     public IForwardingRulesManager getManager() {
113         return this.manager;
114     }
115
116     public void setManager(final IForwardingRulesManager manager) {
117         this.manager = manager;
118     }
119 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.