Imported Upstream version 1.0.1
[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 import org.oic.simulator.SimulatorResult;
28
29 /**
30  * SimulatorRemoteResource represents a Resource running in the remote Simulator
31  * Server. It comes with a well-defined contract or interface onto which you can
32  * perform different operations or subscribe for event notifications.
33  */
34 public class SimulatorRemoteResource {
35
36     private SimulatorRemoteResource(long nativeHandle) {
37         this.nativeHandle = nativeHandle;
38     }
39
40     /**
41      * API to get the URI for this resource.
42      *
43      * @return Resource URI
44      */
45     public String getUri() {
46         return mUri;
47     }
48
49     /**
50      * API to get the observe policy of this resource.
51      *
52      * @return True if the resource is observable, otherwise false.
53      */
54     public boolean getIsObservable() {
55         return mIsObservable;
56     }
57
58     /**
59      * API to get the connectivity type for this resource.
60      *
61      * @return Connectivity type.
62      */
63     public SimulatorConnectivityType getConnectivityType() {
64         return SimulatorConnectivityType.getConnectivityType(mConnType);
65     }
66
67     /**
68      * API to get the list of resource types.
69      *
70      * @return List of resource types.
71      */
72     public LinkedList<String> getResourceTypes() {
73         return mResTypes;
74     }
75
76     /**
77      * API to get the list of resource interfaces.
78      *
79      * @return List of resource interfaces.
80      */
81     public LinkedList<String> getResourceInterfaces() {
82         return mResInterfaces;
83     }
84
85     /**
86      * API to get host address and port information of the resource.
87      *
88      * @return Host address.
89      */
90     public String getHost() {
91         return mHost;
92     }
93
94     /**
95      * API to get a unique Id of the resource .
96      *
97      * @return Unique ID.
98      */
99     public String getId() {
100         return mId;
101     }
102
103     /**
104      * API to start observing the 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 startObserve(SimulatorObserveType observeType,
122             Map<String, String> queryParamsMap,
123             IObserveListener onObserveListener) throws InvalidArgsException,
124             SimulatorException;
125
126     /**
127      * API to stop observing 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 stopObserve() throws InvalidArgsException,
136             SimulatorException;
137
138     /**
139      * API to send GET request to the resource. Response will be notified
140      * asynchronously via callback set for {@link IGetListener}.
141      *
142      * @param queryParamsMap
143      *            Map which can have the query parameter name and value.
144      * @param onGetListener
145      *            Event handler which will be invoked with the response for GET
146      *            request with a map of attribute name and values.
147      *
148      * @throws InvalidArgsException
149      *             This exception will be thrown if any parameter has invalid
150      *             values.
151      * @throws NoSupportException
152      *             This exception will be thrown if we cannot send GET request
153      *             to the remote resource.
154      * @throws SimulatorException
155      *             This exception will be thrown for other errors.
156      */
157     public void get(Map<String, String> queryParamsMap,
158             IGetListener onGetListener) throws InvalidArgsException,
159             NoSupportException, SimulatorException {
160         if (null == onGetListener)
161             throw new InvalidArgsException(
162                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
163                     "Parameter passed in invalid");
164         this.nativeGet(null, queryParamsMap, onGetListener);
165     }
166
167     /**
168      * API to send GET request to the resource. Response will be notified
169      * asynchronously via callback set for {@link IGetListener}.
170      *
171      * @param resourceInterface
172      *            Interface type of the resource to operate on.
173      * @param queryParamsMap
174      *            Map which can have the query parameter name and value.
175      * @param onGetListener
176      *            Event handler which will be invoked with the response for GET
177      *            request with a map of attribute name and values.
178      *
179      * @throws InvalidArgsException
180      *             This exception will be thrown if any parameter has invalid
181      *             values.
182      * @throws NoSupportException
183      *             This exception will be thrown if we cannot send GET request
184      *             to the remote resource.
185      * @throws SimulatorException
186      *             This exception will be thrown for other errors.
187      */
188     public void get(String resourceInterface,
189             Map<String, String> queryParamsMap, IGetListener onGetListener)
190             throws InvalidArgsException, NoSupportException, SimulatorException {
191         if (null == resourceInterface || resourceInterface.isEmpty() || null == onGetListener)
192             throw new InvalidArgsException(
193                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
194                     "Parameter passed in invalid");
195         this.nativeGet(resourceInterface, queryParamsMap, onGetListener);
196     }
197
198     private native void nativeGet(String resourceInterface,
199             Map<String, String> queryParamsMap, IGetListener onGetListener)
200             throws InvalidArgsException, NoSupportException, SimulatorException;
201
202     /**
203      * API to send PUT request to the resource. Response will be notified
204      * asynchronously via callback set for {@link IPutListener}.
205      *
206      * @param representation
207      *            {@link SimulatorResourceModel} holding the representation of
208      *            the resource.
209      * @param queryParamsMap
210      *            Map which can have the query parameter name and value.
211      * @param onPutListener
212      *            Event handler which will be invoked with the response for PUT
213      *            request with a map of attribute name and values.
214      *
215      * @throws InvalidArgsException
216      *             This exception will be thrown if any parameter has invalid
217      *             value.
218      * @throws NoSupportException
219      *             This exception will be thrown if we cannot send PUT request
220      *             to the remote resource.
221      * @throws SimulatorException
222      *             This exception will be thrown for other errors.
223      */
224     public void put(SimulatorResourceModel representation,
225             Map<String, String> queryParamsMap, IPutListener onPutListener)
226             throws InvalidArgsException, NoSupportException, SimulatorException {
227         if (null == representation || null == onPutListener)
228             throw new InvalidArgsException(
229                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
230                     "Parameter passed in invalid");
231         this.nativePut(null, representation, queryParamsMap, onPutListener);
232     }
233
234     /**
235      * API to send PUT request to the resource. Response will be notified
236      * asynchronously via callback set for {@link IPutListener}.
237      *
238      * @param resourceInterface
239      *            Interface type of the resource to operate on.
240      * @param representation
241      *            {@link SimulatorResourceModel} holding the representation of
242      *            the resource.
243      * @param queryParamsMap
244      *            Map which can have the query parameter name and value.
245      * @param onPutListener
246      *            Event handler which will be invoked with the response for PUT
247      *            request with a map of attribute name and values.
248      *
249      * @throws InvalidArgsException
250      *             This exception will be thrown if any parameter has invalid
251      *             value.
252      * @throws NoSupportException
253      *             This exception will be thrown if we cannot send PUT request
254      *             to the remote resource.
255      * @throws SimulatorException
256      *             This exception will be thrown for other errors.
257      */
258     public void put(String resourceInterface,
259             SimulatorResourceModel representation,
260             Map<String, String> queryParamsMap, IPutListener onPutListener)
261             throws InvalidArgsException, NoSupportException, SimulatorException {
262         if (null == resourceInterface || resourceInterface.isEmpty() ||
263             null == representation || null == onPutListener)
264             throw new InvalidArgsException(
265                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
266                     "Parameter passed in invalid");
267         this.nativePut(resourceInterface, representation, queryParamsMap, onPutListener);
268     }
269
270     private native void nativePut(String resourceInterface,
271             SimulatorResourceModel representation,
272             Map<String, String> queryParamsMap, IPutListener onPutListener)
273             throws InvalidArgsException, NoSupportException, SimulatorException;
274
275     /**
276      * API to send POST request to the resource. Response will be notified
277      * asynchronously via callback set for {@link IPostListener}.
278      *
279      * @param representation
280      *            {@link SimulatorResourceModel} holding the representation of
281      *            the resource
282      * @param queryParamsMap
283      *            Map which can have the query parameter name and value
284      * @param onPostListener
285      *            Event handler which will be invoked with the response for POST
286      *            request with a map of attribute name and values.
287      *
288      * @throws InvalidArgsException
289      *             This exception will be thrown if any parameter has invalid
290      *             value.
291      * @throws NoSupportException
292      *             This exception will be thrown if we cannot send POST request
293      *             on the remote resource.
294      * @throws SimulatorException
295      *             This exception will be thrown for other errors.
296      */
297     public void post(SimulatorResourceModel representation,
298             Map<String, String> queryParamsMap, IPostListener onPostListener)
299             throws InvalidArgsException, NoSupportException, SimulatorException {
300         if (null == representation || null == onPostListener)
301             throw new InvalidArgsException(
302                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
303                     "Parameter passed in invalid");
304         this.nativePost(null, representation, queryParamsMap, onPostListener);
305     }
306
307     /**
308      * API to send POST request to the resource. Response will be notified
309      * asynchronously via callback set for {@link IPostListener}.
310      *
311      * @param resourceInterface
312      *            Interface type of the resource to operate on.
313      * @param representation
314      *            {@link SimulatorResourceModel} holding the representation of
315      *            the resource.
316      * @param queryParamsMap
317      *            Map which can have the query parameter name and value.
318      * @param onPostListener
319      *            Event handler which will be invoked with the response for POST
320      *            request with a map of attribute name and values.
321      *
322      * @throws InvalidArgsException
323      *             This exception will be thrown if any parameter has invalid
324      *             value.
325      * @throws NoSupportException
326      *             This exception will be thrown if we cannot send POST request
327      *             on the remote resource.
328      * @throws SimulatorException
329      *             This exception will be thrown for other errors.
330      */
331     public void post(String resourceInterface,
332             SimulatorResourceModel representation,
333             Map<String, String> queryParamsMap, IPostListener onPostListener)
334             throws InvalidArgsException, NoSupportException, SimulatorException {
335         if (null == resourceInterface || resourceInterface.isEmpty() ||
336             null == representation || null == onPostListener)
337             throw new InvalidArgsException(
338                 SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
339                 "Parameter passed in invalid");
340         this.nativePost(resourceInterface, representation, queryParamsMap, onPostListener);
341     }
342
343     private native void nativePost(String resourceInterface,
344             SimulatorResourceModel representation,
345             Map<String, String> queryParamsMap, IPostListener onPostListener)
346             throws InvalidArgsException, NoSupportException, SimulatorException;
347
348     /**
349      * API to provide remote resource configure information,
350      * which is required for using automation feature.
351      *
352      * @param path
353      *            Path to RAML file.
354      *
355      * @throws InvalidArgsException
356      *             Thrown if the RAML configuration file path is invalid.
357      * @throws SimulatorException
358      *             Thrown for other errors.
359      */
360     public native void setConfigInfo(String path)
361             throws InvalidArgsException, SimulatorException;
362
363     /**
364      * API to send multiple requests for the resource, based on
365      * the configure file provided from {@link setConfigInfo}.
366      * This verifies response received as well.
367      *
368      * @param requestType
369      *            Request type to verify.
370      * @param onVerifyListener
371      *            This event handler will be invoked with the current status of
372      *            the automation.
373      *
374      * @return Automation ID.
375      *
376      * @throws InvalidArgsException
377      *             This exception will be thrown if any parameter has invalid
378      *             value.
379      * @throws NoSupportException
380      *             Thrown either if the resource does not support the request
381      *             type or the resource is not configured with RAML.
382      * @throws OperationInProgressException
383      *             Thrown if another request generation session is already in
384      *             progress.
385      * @throws SimulatorException
386      *             This exception will be thrown for other errors.
387      */
388     public int startVerification(SimulatorVerificationType requestType,
389             IVerificationListener onVerifyListener)
390             throws InvalidArgsException, NoSupportException,
391             OperationInProgressException, SimulatorException {
392         return startVerification(requestType.ordinal(), onVerifyListener);
393     }
394
395     /**
396      * API to stop sending requests which has been started using {@link setConfigInfo}.
397      *
398      * @param id
399      *            Automation ID.
400      *
401      * @throws InvalidArgsException
402      *             Thrown if the automation ID is invalid.
403      * @throws NoSupportException
404      *             Thrown if the resource is not configured with RAML.
405      * @throws SimulatorException
406      *             Thrown for other errors.
407      */
408     public native void stopVerification(int id) throws InvalidArgsException,
409             NoSupportException, SimulatorException;
410
411     private native int startVerification(int requestType,
412             IVerificationListener onVerifyListener)
413             throws InvalidArgsException, NoSupportException,
414             OperationInProgressException, SimulatorException;
415
416     @Override
417     protected void finalize() throws Throwable {
418         try {
419             dispose();
420         } finally {
421             super.finalize();
422         }
423     }
424
425     private native void dispose();
426
427     private long               nativeHandle;
428     private String             mUri;
429     private int                mConnType;
430     private String             mHost;
431     private String             mId;
432     private LinkedList<String> mResTypes;
433     private LinkedList<String> mResInterfaces;
434     private boolean            mIsObservable;
435 }