Java SDK and Eclipse plugin for simulator.
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / clientcontroller / SimulatorRemoteResource.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.oic.simulator.clientcontroller;
18
19 import java.util.LinkedList;
20 import java.util.Map;
21
22 import org.oic.simulator.InvalidArgsException;
23 import org.oic.simulator.NoSupportException;
24 import org.oic.simulator.OperationInProgressException;
25 import org.oic.simulator.SimulatorException;
26 import org.oic.simulator.SimulatorResourceModel;
27
28 /**
29  * SimulatorRemoteResource represents a Resource running in the remote Simulator
30  * Server. It comes with a well-defined contract or interface onto which you can
31  * perform different operations or subscribe for event notifications.
32  */
33 public class SimulatorRemoteResource {
34
35     private SimulatorRemoteResource(long nativeHandle) {
36         this.nativeHandle = nativeHandle;
37     }
38
39     /**
40      * Method to get the URI for this resource.
41      * 
42      * @return Resource URI
43      */
44     public String getUri() {
45         return mUri;
46     }
47
48     /**
49      * Method to get the observe capability of this resource.
50      * 
51      * @return True if the resource is observable, otherwise false.
52      */
53     public boolean getIsObservable() {
54         return mIsObservable;
55     }
56
57     /**
58      * Method to get the connectivity type for this resource.
59      * 
60      * @return Connectivity type.
61      */
62     public SimulatorConnectivityType getConnectivityType() {
63         return SimulatorConnectivityType.getConnectivityType(mConnType);
64     }
65
66     /**
67      * Method to get the list of resource types.
68      * 
69      * @return List of resource types.
70      */
71     public LinkedList<String> getResourceTypes() {
72         return mResTypes;
73     }
74
75     /**
76      * Method to get the list of resource interfaces.
77      * 
78      * @return List of resource interfaces.
79      */
80     public LinkedList<String> getResourceInterfaces() {
81         return mResInterfaces;
82     }
83
84     /**
85      * Method to get a string representation of the host address of the remote
86      * resource.
87      * 
88      * @return Host address.
89      */
90     public String getHost() {
91         return mHost;
92     }
93
94     /**
95      * Method to get a unique Id for the resource.
96      * 
97      * @return Unique ID.
98      */
99     public String getId() {
100         return mId;
101     }
102
103     /**
104      * Method to set observation on the remote resource.
105      * 
106      * @param observeType
107      *            Allows the client to specify how it wants to observe.
108      * @param queryParamsMap
109      *            Map which can have the query parameter names and values.
110      * @param onObserveListener
111      *            The handler method which will be invoked with a map of
112      *            attribute names and values whenever there is a change in
113      *            resource model of the remote resource.
114      * 
115      * @throws InvalidArgsException
116      *             This exception will be thrown if any parameter has invalid
117      *             values.
118      * @throws SimulatorException
119      *             This exception will be thrown for other errors.
120      */
121     public native void observe(SimulatorObserveType observeType,
122             Map<String, String> queryParamsMap,
123             IObserveListener onObserveListener) throws InvalidArgsException,
124             SimulatorException;
125
126     /**
127      * Method to cancel the observation on the resource.
128      * 
129      * @throws InvalidArgsException
130      *             This exception will be thrown if the native remote resource
131      *             object is unavailable.
132      * @throws SimulatorException
133      *             This exception will be thrown for other errors.
134      */
135     public native void cancelObserve() throws InvalidArgsException,
136             SimulatorException;
137
138     /**
139      * Method to get the attributes of a resource.
140      * 
141      * @param queryParamsMap
142      *            Map which can have the query parameter name and value.
143      * @param onGetListener
144      *            Event handler which will be invoked with the response for GET
145      *            request with a map of attribute name and values.
146      * 
147      * @throws InvalidArgsException
148      *             This exception will be thrown if any parameter has invalid
149      *             values.
150      * @throws NoSupportException
151      *             This exception will be thrown if we cannot send GET request
152      *             to the remote resource.
153      * @throws SimulatorException
154      *             This exception will be thrown for other errors.
155      */
156     public void get(Map<String, String> queryParamsMap,
157             IGetListener onGetListener) throws InvalidArgsException,
158             NoSupportException, SimulatorException {
159         this.get(null, queryParamsMap, onGetListener);
160     }
161
162     /**
163      * Method to get the attributes of a resource.
164      * 
165      * @param resourceInterface
166      *            Interface type of the resource to operate on.
167      * @param queryParamsMap
168      *            Map which can have the query parameter name and value.
169      * @param onGetListener
170      *            Event handler which will be invoked with the response for GET
171      *            request with a map of attribute name and values.
172      * 
173      * @throws InvalidArgsException
174      *             This exception will be thrown if any parameter has invalid
175      *             values.
176      * @throws NoSupportException
177      *             This exception will be thrown if we cannot send GET request
178      *             to the remote resource.
179      * @throws SimulatorException
180      *             This exception will be thrown for other errors.
181      */
182     public native void get(String resourceInterface,
183             Map<String, String> queryParamsMap, IGetListener onGetListener)
184             throws InvalidArgsException, NoSupportException, SimulatorException;
185
186     /**
187      * Method to set the representation of a resource (via PUT)
188      * 
189      * @param representation
190      *            {@link SimulatorResourceModel} holding the representation of
191      *            the resource.
192      * @param queryParamsMap
193      *            Map which can have the query parameter name and value.
194      * @param onPutListener
195      *            Event handler which will be invoked with the response for PUT
196      *            request with a map of attribute name and values.
197      * 
198      * @throws InvalidArgsException
199      *             This exception will be thrown if any parameter has invalid
200      *             value.
201      * @throws NoSupportException
202      *             This exception will be thrown if we cannot send PUT request
203      *             to the remote resource.
204      * @throws SimulatorException
205      *             This exception will be thrown for other errors.
206      */
207     public void put(SimulatorResourceModel representation,
208             Map<String, String> queryParamsMap, IPutListener onPutListener)
209             throws InvalidArgsException, NoSupportException, SimulatorException {
210         this.put(null, representation, queryParamsMap, onPutListener);
211     }
212
213     /**
214      * Method to set the representation of a resource (via PUT).
215      * 
216      * @param resourceInterface
217      *            Interface type of the resource to operate on.
218      * @param representation
219      *            {@link SimulatorResourceModel} holding the representation of
220      *            the resource.
221      * @param queryParamsMap
222      *            Map which can have the query parameter name and value.
223      * @param onPutListener
224      *            Event handler which will be invoked with the response for PUT
225      *            request with a map of attribute name and values.
226      * 
227      * @throws InvalidArgsException
228      *             This exception will be thrown if any parameter has invalid
229      *             value.
230      * @throws NoSupportException
231      *             This exception will be thrown if we cannot send PUT request
232      *             to the remote resource.
233      * @throws SimulatorException
234      *             This exception will be thrown for other errors.
235      */
236     private native void put(String resourceInterface,
237             SimulatorResourceModel representation,
238             Map<String, String> queryParamsMap, IPutListener onPutListener)
239             throws InvalidArgsException, NoSupportException, SimulatorException;
240
241     /**
242      * Method to POST on a resource.
243      * 
244      * @param representation
245      *            {@link SimulatorResourceModel} holding the representation of
246      *            the resource
247      * @param queryParamsMap
248      *            Map which can have the query parameter name and value
249      * @param onPostListener
250      *            Event handler which will be invoked with the response for POST
251      *            request with a map of attribute name and values.
252      * 
253      * @throws InvalidArgsException
254      *             This exception will be thrown if any parameter has invalid
255      *             value.
256      * @throws NoSupportException
257      *             This exception will be thrown if we cannot send POST request
258      *             on the remote resource.
259      * @throws SimulatorException
260      *             This exception will be thrown for other errors.
261      */
262     public void post(SimulatorResourceModel representation,
263             Map<String, String> queryParamsMap, IPostListener onPostListener)
264             throws InvalidArgsException, NoSupportException, SimulatorException {
265         this.post(null, representation, queryParamsMap, onPostListener);
266     }
267
268     /**
269      * Method to POST on a resource.
270      * 
271      * @param resourceInterface
272      *            Interface type of the resource to operate on.
273      * @param representation
274      *            {@link SimulatorResourceModel} holding the representation of
275      *            the resource.
276      * @param queryParamsMap
277      *            Map which can have the query parameter name and value.
278      * @param onPostListener
279      *            Event handler which will be invoked with the response for POST
280      *            request with a map of attribute name and values.
281      * 
282      * @throws InvalidArgsException
283      *             This exception will be thrown if any parameter has invalid
284      *             value.
285      * @throws NoSupportException
286      *             This exception will be thrown if we cannot send POST request
287      *             on the remote resource.
288      * @throws SimulatorException
289      *             This exception will be thrown for other errors.
290      */
291     public native void post(String resourceInterface,
292             SimulatorResourceModel representation,
293             Map<String, String> queryParamsMap, IPostListener onPostListener)
294             throws InvalidArgsException, NoSupportException, SimulatorException;
295
296     /**
297      * Method to set the RAML file path from application
298      * 
299      * @param ramlPath
300      *            RAML configuration file path
301      * 
302      * @throws InvalidArgsException
303      *             Thrown if the RAML configuration file path is invalid.
304      * @throws SimulatorException
305      *             Thrown for other errors.
306      */
307     public native void configureRAMLPath(String ramlPath)
308             throws InvalidArgsException, SimulatorException;
309
310     /**
311      * Method to start verification of a resource using automation.
312      * 
313      * @param requestType
314      *            Request type to verify.
315      * @param onVerifyListener
316      *            This event handler will be invoked with the current status of
317      *            the automation.
318      * 
319      * @return Automation ID.
320      * 
321      * @throws InvalidArgsException
322      *             This exception will be thrown if any parameter has invalid
323      *             value.
324      * @throws NoSupportException
325      *             Thrown either if the resource does not support the request
326      *             type or the resource is not configured with RAML.
327      * @throws OperationInProgressException
328      *             Thrown if another request generation session is already in
329      *             progress.
330      * @throws SimulatorException
331      *             This exception will be thrown for other errors.
332      */
333     public int startVerification(SimulatorVerificationType requestType,
334             IVerificationListener onVerifyListener)
335             throws InvalidArgsException, NoSupportException,
336             OperationInProgressException, SimulatorException {
337         return startVerification(requestType.ordinal(), onVerifyListener);
338     }
339
340     private native int startVerification(int requestType,
341             IVerificationListener onVerifyListener)
342             throws InvalidArgsException, NoSupportException,
343             OperationInProgressException, SimulatorException;
344
345     /**
346      * Method to stop verification of a resource previously started.
347      * 
348      * @param id
349      *            Automation ID.
350      * 
351      * @throws InvalidArgsException
352      *             Thrown if the automation ID is invalid.
353      * @throws NoSupportException
354      *             Thrown if the resource is not configured with RAML.
355      * @throws SimulatorException
356      *             Thrown for other errors.
357      */
358     public native void stopVerification(int id) throws InvalidArgsException,
359             NoSupportException, SimulatorException;
360
361     @Override
362     protected void finalize() throws Throwable {
363         super.finalize();
364
365         dispose();
366     }
367
368     private native void dispose();
369
370     private long               nativeHandle;
371     private String             mUri;
372     private int                mConnType;
373     private String             mHost;
374     private String             mId;
375     private LinkedList<String> mResTypes;
376     private LinkedList<String> mResInterfaces;
377     private boolean            mIsObservable;
378 }