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

©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.