Removed duplicated dependency declaration: ietf-netconf-monitoring,ietf-netconf-monit...
[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.ListenerRegistration;
26 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
27 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
28 import org.opendaylight.yangtools.restconf.client.api.event.ListenableEventStreamContext;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.DataRoot;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.base.Optional;
37
38 public class DataBrokerServiceImpl implements DataBrokerService  {
39
40     private static final Logger logger = LoggerFactory.getLogger(DataBrokerServiceImpl.class.toString());
41     private final RestconfClientContext restconfClientContext;
42     private final SalRemoteService salRemoteService;
43
44     public DataBrokerServiceImpl(RestconfClientContext restconfClientContext) {
45         this.restconfClientContext = restconfClientContext;
46         this.salRemoteService =  this.restconfClientContext.getRpcServiceContext(SalRemoteService.class).getRpcService();
47     }
48     @Override
49     public <T extends DataRoot> T getData(DataStoreIdentifier store, Class<T> rootType) {
50         throw new UnsupportedOperationException("Deprecated");
51     }
52
53     @Override
54     public <T extends DataRoot> T getData(DataStoreIdentifier store, T filter) {
55         throw new UnsupportedOperationException("Deprecated");
56     }
57
58     @Override
59     public <T extends DataRoot> T getCandidateData(DataStoreIdentifier store, Class<T> rootType) {
60         throw new UnsupportedOperationException("Deprecated");
61     }
62
63     @Override
64     public <T extends DataRoot> T getCandidateData(DataStoreIdentifier store, T filter) {
65         throw new UnsupportedOperationException("Deprecated");
66     }
67
68     @Override
69     public RpcResult<DataRoot> editCandidateData(DataStoreIdentifier store, DataRoot changeSet) {
70         throw new UnsupportedOperationException("Deprecated");
71     }
72
73     @Override
74     public Future<RpcResult<Void>> commit(DataStoreIdentifier store) {
75         throw new UnsupportedOperationException("Deprecated");
76     }
77
78     @Override
79     public DataObject getData(InstanceIdentifier<? extends DataObject> data) {
80         throw new UnsupportedOperationException("Deprecated");
81     }
82
83     @Override
84     public DataObject getConfigurationData(InstanceIdentifier<?> data) {
85         throw new UnsupportedOperationException("Deprecated");
86     }
87
88     @Override
89     public DataModificationTransaction beginTransaction() {
90         Future<RpcResult<BeginTransactionOutput>> rpcResultFuture = this.salRemoteService.beginTransaction();
91         //TODO finish yang model for proper remoteDataModificationTransaction setup
92         RemoteDataModificationTransaction remoteDataModificationTransaction = new RemoteDataModificationTransaction();
93         return remoteDataModificationTransaction;
94     }
95
96     @Override
97     public void registerChangeListener(InstanceIdentifier<? extends DataObject> path, DataChangeListener changeListener) {
98         throw new UnsupportedOperationException("Deprecated");
99     }
100
101     @Override
102     public void unregisterChangeListener(InstanceIdentifier<? extends DataObject> path, DataChangeListener changeListener) {
103         throw new UnsupportedOperationException("Deprecated");
104     }
105
106     @Override
107     public DataObject readConfigurationData(InstanceIdentifier<? extends DataObject> path) {
108         try {
109             Optional<DataObject> optDataObject = (Optional<DataObject>) this.restconfClientContext.getConfigurationDatastore().readData(path).get();
110             if (optDataObject.isPresent()){
111                 return optDataObject.get();
112             }
113         } catch (InterruptedException e) {
114             logger.trace("Reading configuration data interrupted {}",e);
115         } catch (ExecutionException e) {
116             logger.trace("Reading configuration execution exception {}",e);
117         }
118         throw new IllegalStateException("No data to return.");
119     }
120
121     @Override
122     public DataObject readOperationalData(InstanceIdentifier<? extends DataObject> path) {
123         try {
124             Optional<DataObject> optDataObject = (Optional<DataObject>) this.restconfClientContext.getOperationalDatastore().readData(path).get();
125             if (optDataObject.isPresent()){
126                 return optDataObject.get();
127             }
128         } catch (InterruptedException e) {
129             logger.trace("Reading configuration data interrupted {}",e);
130         } catch (ExecutionException e) {
131             logger.trace("Reading configuration execution exception {}",e);
132         }
133         throw new IllegalStateException("No data to return.");
134     }
135     @Override
136     public ListenerRegistration<DataChangeListener> registerDataChangeListener(InstanceIdentifier<? extends DataObject> path, DataChangeListener listener) {
137         CreateDataChangeEventSubscriptionInputBuilder inputBuilder = new CreateDataChangeEventSubscriptionInputBuilder();
138         Future<RpcResult<CreateDataChangeEventSubscriptionOutput>> rpcResultFuture =  salRemoteService.createDataChangeEventSubscription(inputBuilder.setPath(path).build());
139         String streamName = "";
140         try {
141             if (rpcResultFuture.get().isSuccessful()){
142                 streamName = rpcResultFuture.get().getResult().getStreamName();
143             }
144         } catch (InterruptedException e) {
145             logger.trace("Interupted while getting rpc result due to {}",e);
146         } catch (ExecutionException e) {
147             logger.trace("Execution exception while getting rpc result due to {}",e);
148         }
149         final Map<String,EventStreamInfo> desiredEventStream = RemoteStreamTools.createEventStream(restconfClientContext,streamName);
150         ListenableEventStreamContext restConfListenableEventStreamContext = restconfClientContext.getEventStreamContext(desiredEventStream.get(streamName));
151         RemoteDataChangeNotificationListener remoteDataChangeNotificationListener = new RemoteDataChangeNotificationListener(listener);
152         restConfListenableEventStreamContext.registerNotificationListener(remoteDataChangeNotificationListener);
153         return new SalRemoteDataListenerRegistration(listener);
154     }
155
156     private class SalRemoteDataListenerRegistration implements ListenerRegistration<DataChangeListener> {
157         private final DataChangeListener dataChangeListener;
158         public SalRemoteDataListenerRegistration(DataChangeListener dataChangeListener){
159             this.dataChangeListener = dataChangeListener;
160         }
161         @Override
162         public DataChangeListener getInstance() {
163             return this.dataChangeListener;
164         }
165         @Override
166         public void close() {
167             //noop
168         }
169     }
170 }

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