Merge changes I1b834def,Iece78be2
[controller.git] / opendaylight / md-sal / sal-restconf-broker / src / main / java / org / opendaylight / controller / sal / restconf / broker / impl / DataBrokerServiceImpl.java
1 /*
2  * Copyright (c) 2013 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.sal.restconf.broker.impl;
9
10 import java.util.Map;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13
14 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
15 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
16 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
17 import org.opendaylight.controller.sal.common.DataStoreIdentifier;
18 import org.opendaylight.controller.sal.restconf.broker.listeners.RemoteDataChangeNotificationListener;
19 import org.opendaylight.controller.sal.restconf.broker.tools.RemoteStreamTools;
20 import org.opendaylight.controller.sal.restconf.broker.transactions.RemoteDataModificationTransaction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.BeginTransactionOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateDataChangeEventSubscriptionInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateDataChangeEventSubscriptionOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.SalRemoteService;
25 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
28 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
29 import org.opendaylight.yangtools.restconf.client.api.event.ListenableEventStreamContext;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.DataRoot;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.base.Optional;
38
39 public class DataBrokerServiceImpl implements DataBrokerService  {
40
41     private static final Logger logger = LoggerFactory.getLogger(DataBrokerServiceImpl.class.toString());
42     private final RestconfClientContext restconfClientContext;
43     private final SalRemoteService salRemoteService;
44
45     public DataBrokerServiceImpl(RestconfClientContext restconfClientContext) {
46         this.restconfClientContext = restconfClientContext;
47         this.salRemoteService =  this.restconfClientContext.getRpcServiceContext(SalRemoteService.class).getRpcService();
48     }
49     @Override
50     public <T extends DataRoot> T getData(DataStoreIdentifier store, Class<T> rootType) {
51         throw new UnsupportedOperationException("Deprecated");
52     }
53
54     @Override
55     public <T extends DataRoot> T getData(DataStoreIdentifier store, T filter) {
56         throw new UnsupportedOperationException("Deprecated");
57     }
58
59     @Override
60     public <T extends DataRoot> T getCandidateData(DataStoreIdentifier store, Class<T> rootType) {
61         throw new UnsupportedOperationException("Deprecated");
62     }
63
64     @Override
65     public <T extends DataRoot> T getCandidateData(DataStoreIdentifier store, T filter) {
66         throw new UnsupportedOperationException("Deprecated");
67     }
68
69     @Override
70     public RpcResult<DataRoot> editCandidateData(DataStoreIdentifier store, DataRoot changeSet) {
71         throw new UnsupportedOperationException("Deprecated");
72     }
73
74     @Override
75     public Future<RpcResult<Void>> commit(DataStoreIdentifier store) {
76         throw new UnsupportedOperationException("Deprecated");
77     }
78
79     @Override
80     public DataObject getData(InstanceIdentifier<? extends DataObject> data) {
81         throw new UnsupportedOperationException("Deprecated");
82     }
83
84     @Override
85     public DataObject getConfigurationData(InstanceIdentifier<?> data) {
86         throw new UnsupportedOperationException("Deprecated");
87     }
88
89     @Override
90     public DataModificationTransaction beginTransaction() {
91         Future<RpcResult<BeginTransactionOutput>> rpcResultFuture = this.salRemoteService.beginTransaction();
92         //TODO finish yang model for proper remoteDataModificationTransaction setup
93         RemoteDataModificationTransaction remoteDataModificationTransaction = new RemoteDataModificationTransaction();
94         return remoteDataModificationTransaction;
95     }
96
97     @Override
98     public void registerChangeListener(InstanceIdentifier<? extends DataObject> path, DataChangeListener changeListener) {
99         throw new UnsupportedOperationException("Deprecated");
100     }
101
102     @Override
103     public void unregisterChangeListener(InstanceIdentifier<? extends DataObject> path, DataChangeListener changeListener) {
104         throw new UnsupportedOperationException("Deprecated");
105     }
106
107     @Override
108     public DataObject readConfigurationData(InstanceIdentifier<? extends DataObject> path) {
109         try {
110             Optional<DataObject> optDataObject = (Optional<DataObject>) this.restconfClientContext.getConfigurationDatastore().readData(path).get();
111             if (optDataObject.isPresent()){
112                 return optDataObject.get();
113             }
114         } catch (InterruptedException e) {
115             logger.trace("Reading configuration data interrupted {}",e);
116         } catch (ExecutionException e) {
117             logger.trace("Reading configuration execution exception {}",e);
118         }
119         throw new IllegalStateException("No data to return.");
120     }
121
122     @Override
123     public DataObject readOperationalData(InstanceIdentifier<? extends DataObject> path) {
124         try {
125             Optional<DataObject> optDataObject = (Optional<DataObject>) this.restconfClientContext.getOperationalDatastore().readData(path).get();
126             if (optDataObject.isPresent()){
127                 return optDataObject.get();
128             }
129         } catch (InterruptedException e) {
130             logger.trace("Reading configuration data interrupted {}",e);
131         } catch (ExecutionException e) {
132             logger.trace("Reading configuration execution exception {}",e);
133         }
134         throw new IllegalStateException("No data to return.");
135     }
136     @Override
137     public ListenerRegistration<DataChangeListener> registerDataChangeListener(InstanceIdentifier<? extends DataObject> path, DataChangeListener listener) {
138         CreateDataChangeEventSubscriptionInputBuilder inputBuilder = new CreateDataChangeEventSubscriptionInputBuilder();
139         Future<RpcResult<CreateDataChangeEventSubscriptionOutput>> rpcResultFuture =  salRemoteService.createDataChangeEventSubscription(inputBuilder.setPath(path).build());
140         String streamName = "";
141         try {
142             if (rpcResultFuture.get().isSuccessful()){
143                 streamName = rpcResultFuture.get().getResult().getStreamName();
144             }
145         } catch (InterruptedException e) {
146             logger.trace("Interupted while getting rpc result due to {}",e);
147         } catch (ExecutionException e) {
148             logger.trace("Execution exception while getting rpc result due to {}",e);
149         }
150         final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext,streamName);
151         ListenableEventStreamContext restConfListenableEventStreamContext = restconfClientContext.getEventStreamContext(desiredEventStream.get(streamName));
152         RemoteDataChangeNotificationListener remoteDataChangeNotificationListener = new RemoteDataChangeNotificationListener(listener);
153         final ListenerRegistration<?> reg = restConfListenableEventStreamContext.registerNotificationListener(remoteDataChangeNotificationListener);
154         return new AbstractListenerRegistration<DataChangeListener>(listener) {
155             @Override
156             protected void removeRegistration() {
157                 reg.close();
158             }
159         };
160     }
161 }

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