Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / serviceprovider / SimulatorResourceServer.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.serviceprovider;
18
19 import java.util.Vector;
20
21 import org.oic.simulator.IAutomation;
22 import org.oic.simulator.InvalidArgsException;
23 import org.oic.simulator.SimulatorException;
24 import org.oic.simulator.SimulatorResourceModel;
25
26 /**
27  * This class represents a resource in the simulator. It provides a set of
28  * native methods for manipulating the simulated resource by adding and removing
29  * attributes to its model, automating the attribute value updates, and changing
30  * the range of acceptable values of the attributes.
31  */
32 public class SimulatorResourceServer {
33
34     private String resourceName;
35     private String resourceURI;
36     private String resourceType;
37     private String interfaceType;
38
39     private long   nativeHandle;
40
41     private SimulatorResourceServer(long nativeHandle) {
42         this.nativeHandle = nativeHandle;
43     }
44
45     /**
46      * API to get the resource name. Example: Light, Fan, etc.
47      *
48      * @return Resource name.
49      */
50     public String getName() {
51         return resourceName;
52     }
53
54     /**
55      * API to get the resource URI. Example: /oic/light, /oic/fan, etc.
56      *
57      * @return Resource URI.
58      */
59     public String getURI() {
60         return resourceURI;
61     }
62
63     /**
64      * API to get the resource Type. Example: oic.light, oic.fan, etc.
65      *
66      * @return Resource type.
67      */
68     public String getResourceType() {
69         return resourceType;
70     }
71
72     /**
73      * API to get the interface type of the resource. Example: oic.if.baseline,
74      * oic.if.b, etc.
75      *
76      * @return Interface type of the resource.
77      */
78     public String getInterfaceType() {
79         return interfaceType;
80     }
81
82    /**
83      * API to get the {@link SimulatorResourceModel} of the
84      * simulated resource.
85      *
86      * @return {@link SimulatorResourceModel} object on success, otherwise null.
87      *
88      * @throws SimulatorException
89      *             This exception will be thrown if simulated resource is not proper.
90      */
91     public native SimulatorResourceModel getModel()
92             throws SimulatorException;
93
94     /**
95      * API to add an attribute whose value is of type int.
96      *
97      * @param key
98      *            Name of the attribute.
99      * @param value
100      *            Initial value of the attribute.
101      *
102      * @throws InvalidArgsException
103      *             This exception will be thrown if any parameter has invalid
104      *             values.
105      * @throws SimulatorException
106      *             This exception will be thrown for other errors.
107      */
108     public native void addAttributeInteger(String key, int value)
109             throws InvalidArgsException, SimulatorException;
110
111     /**
112      * API to  add an attribute whose value is of type double.
113      *
114      * @param key
115      *            Name of the attribute.
116      * @param value
117      *            Initial value of the attribute.
118      *
119      * @throws InvalidArgsException
120      *             This exception will be thrown if any parameter has invalid
121      *             values.
122      * @throws SimulatorException
123      *             This exception will be thrown for other errors.
124      */
125     public native void addAttributeDouble(String key, double value)
126             throws InvalidArgsException, SimulatorException;
127
128     /**
129      * API to  add an attribute whose value is of type boolean.
130      *
131      * @param key
132      *            Name of the attribute.
133      * @param value
134      *            Initial value of the attribute.
135      *
136      * @throws InvalidArgsException
137      *             This exception will be thrown if any parameter has invalid
138      *             values.
139      * @throws SimulatorException
140      *             This exception will be thrown for other errors.
141      */
142     public native void addAttributeBoolean(String key, boolean value)
143             throws InvalidArgsException, SimulatorException;
144
145     /**
146      * API to  add an attribute whose value is of type String.
147      *
148      * @param key
149      *            Name of the attribute.
150      * @param value
151      *            Initial value of the attribute.
152      *
153      * @throws InvalidArgsException
154      *             This exception will be thrown if any parameter has invalid
155      *             values.
156      * @throws SimulatorException
157      *             This exception will be thrown for other errors.
158      */
159     public native void addAttributeString(String key, String value)
160             throws InvalidArgsException, SimulatorException;
161
162     /**
163      * API to  update the value of an attribute whose value is of
164      * type int.
165      *
166      * @param key
167      *            Name of the attribute.
168      * @param value
169      *            New value of the attribute.
170      *
171      * @throws InvalidArgsException
172      *             This exception will be thrown if any parameter has invalid
173      *             values.
174      * @throws SimulatorException
175      *             This exception will be thrown for other errors.
176      */
177     public native void updateAttributeInteger(String key, int value)
178             throws InvalidArgsException, SimulatorException;
179
180     /**
181      * API to  update the value of an attribute whose value is of
182      * type double.
183      *
184      * @param key
185      *            Name of the attribute.
186      * @param value
187      *            New value of the attribute.
188      *
189      * @throws InvalidArgsException
190      *             This exception will be thrown if any parameter has invalid
191      *             values.
192      * @throws SimulatorException
193      *             This exception will be thrown for other errors.
194      */
195     public native void updateAttributeDouble(String key, double value)
196             throws InvalidArgsException, SimulatorException;
197
198     /**
199      * API to  update the value of an attribute whose value is of
200      * type boolean.
201      *
202      * @param key
203      *            Name of the attribute.
204      * @param value
205      *            New value of the attribute.
206      *
207      * @throws InvalidArgsException
208      *             This exception will be thrown if any parameter has invalid
209      *             values.
210      * @throws SimulatorException
211      *             This exception will be thrown for other errors.
212      */
213     public native void updateAttributeBoolean(String key, boolean value)
214             throws InvalidArgsException, SimulatorException;
215
216     /**
217      * API to  update the value of an attribute whose value is of
218      * type String.
219      *
220      * @param key
221      *            Name of the attribute.
222      * @param value
223      *            New value of the attribute.
224      *
225      * @throws InvalidArgsException
226      *             This exception will be thrown if any parameter has invalid
227      *             values.
228      * @throws SimulatorException
229      *             This exception will be thrown for other errors.
230      */
231     public native void updateAttributeString(String key, String value)
232             throws InvalidArgsException, SimulatorException;
233
234     /**
235      * API to update the value of an attribute from
236      * its allowed values.
237      *
238      * @param key
239      *            Name of the attribute.
240      * @param index
241      *            Index of the value in the allowed values.
242      *
243      * @throws InvalidArgsException
244      *             This exception will be thrown if any parameter has invalid
245      *             values.
246      * @throws SimulatorException
247      *             This exception will be thrown for other errors.
248      */
249     public native void updateAttributeFromAllowedValues(String key,
250             int index) throws InvalidArgsException, SimulatorException;
251
252     /**
253      * API to  set the range of allowed values. This function is
254      * intended to be used for integer type attributes.
255      *
256      * @param key
257      *            Name of the attribute.
258      * @param min
259      *            Minimum value in the range.
260      * @param max
261      *            Maximum value in the range.
262      *
263      * @throws InvalidArgsException
264      *             This exception will be thrown if any parameter has invalid
265      *             values.
266      * @throws SimulatorException
267      *             This exception will be thrown for other errors.
268      */
269     public native void setRange(String key, int min, int max)
270             throws InvalidArgsException, SimulatorException;
271
272     /**
273      * API to  set the allowed values of attribute whose value is of
274      * type int.
275      *
276      * @param key
277      *            Name of the attribute.
278      * @param allowedValues
279      *            Allowed values of the attribute.
280      *
281      * @throws InvalidArgsException
282      *             This exception will be thrown if any parameter has invalid
283      *             values.
284      * @throws SimulatorException
285      *             This exception will be thrown for other errors.
286      */
287     public native void setAllowedValuesInteger(String key,
288             Vector<Integer> allowedValues) throws InvalidArgsException,
289             SimulatorException;
290
291     /**
292      * API to  set the allowed values of attribute whose value is of
293      * type double.
294      *
295      * @param key
296      *            Name of the attribute.
297      * @param allowedValues
298      *            Allowed values of the attribute.
299      *
300      * @throws InvalidArgsException
301      *             This exception will be thrown if any parameter has invalid
302      *             values.
303      * @throws SimulatorException
304      *             This exception will be thrown for other errors.
305      */
306     public native void setAllowedValuesDouble(String key,
307             Vector<Double> allowedValues) throws InvalidArgsException,
308             SimulatorException;
309
310     /**
311      * API to  set the allowed values of attribute whose value is of
312      * type String.
313      *
314      * @param key
315      *            Name of the attribute.
316      * @param allowedValues
317      *            Allowed values of the attribute.
318      *
319      * @throws InvalidArgsException
320      *             This exception will be thrown if any parameter has invalid
321      *             values.
322      * @throws SimulatorException
323      *             This exception will be thrown for other errors.
324      */
325     public native void setAllowedValuesString(String key,
326             Vector<String> allowedValues) throws InvalidArgsException,
327             SimulatorException;
328
329     /**
330      * API to  start the resource level automation. This automation
331      * involves automatically updating all the possible values for all the
332      * attributes sequentially.
333      *
334      * @param typeOfAutomation
335      *            {@link AutomationType} indicating whether the automation is
336      *            one-time or recursive.
337      * @param listener
338      *            Listener to be notified when automation ends.
339      *
340      * @return Automation ID using which the automation can be stopped.
341      *
342      * @throws InvalidArgsException
343      *             This exception will be thrown if any parameter has invalid
344      *             values.
345      * @throws SimulatorException
346      *             This exception will be thrown for other errors.
347      */
348     public int startResourceAutomation(AutomationType typeOfAutomation,
349             IAutomation listener) throws InvalidArgsException,
350             SimulatorException {
351         return startResourceAutomation(typeOfAutomation.getValue(), listener);
352     }
353
354     /**
355      * API to  start the attribute level automation. This automation
356      * involves automatically updating all the possible values for a given
357      * attribute sequentially.
358      *
359      * @param attrName
360      *            Name of the attribute to be automated.
361      * @param typeOfAutomation
362      *            {@link AutomationType} indicating whether the automation is
363      *            one-time or recursive.
364      * @param listener
365      *            Listener to be notified when automation ends.
366      *
367      * @return Automation ID using which the automation can be stopped.
368      *
369      * @throws InvalidArgsException
370      *             This exception will be thrown if any parameter has invalid
371      *             values.
372      * @throws SimulatorException
373      *             This exception will be thrown for other errors.
374      */
375     public int startAttributeAutomation(String attrName,
376             AutomationType typeOfAutomation, IAutomation listener)
377             throws InvalidArgsException, SimulatorException {
378         return startAttributeAutomation(attrName, typeOfAutomation.getValue(),
379                 listener);
380     }
381
382     /**
383      * API to  stop the automation based on automation id.
384      *
385      * @param automationId
386      *            Using which a specific automation can be stopped.
387      *
388      * @throws SimulatorException
389      *             This exception will be thrown for other errors.
390      */
391     public native void stopAutomation(int automationId)
392             throws SimulatorException;
393
394     /**
395      * API to remove an attribute from the simulated resource.
396      *
397      * @param key
398      *            Name of the attribute to be deleted.
399      *
400      * @throws InvalidArgsException
401      *             This exception will be thrown either if the parameter has
402      *             invalid value or the native resource object does not exist.
403      * @throws SimulatorException
404      *             This exception will be thrown for other errors.
405      */
406     public native void removeAttribute(String key) throws InvalidArgsException,
407             SimulatorException;
408
409     /**
410      * API to get observers  information.
411      *
412      * @return An array of {@link ObserverInfo} objects.
413      *
414      * @throws InvalidArgsException
415      *             This exception will be thrown if the native resource object
416      *             does not exist.
417      * @throws SimulatorException
418      *             This exception will be thrown for other errors.
419      */
420     public native ObserverInfo[] getObserversList()
421             throws InvalidArgsException, SimulatorException;
422
423     /**
424      * API to set callback to receive notifications when observer is added/removed.
425      *
426      * @param observer
427      *            Listener to be notified when clients start/stop observing.
428      *
429      * @throws InvalidArgsException
430      *             This exception will be thrown either if the parameter has
431      *             invalid value or the native resource object does not exist.
432      * @throws SimulatorException
433      *             This exception will be thrown for other errors.
434      */
435     public native void setObserverCallback(IObserver observer)
436             throws InvalidArgsException, SimulatorException;
437
438     /**
439      * API to notify simulated resource's state to a specific observer.
440      *
441      * @param id
442      *            Observer's Id.
443      *
444      * @throws InvalidArgsException
445      *             This exception will be thrown if the native resource object
446      *             does not exist.
447      * @throws SimulatorException
448      *             This exception will be thrown for other errors.
449      */
450     public native void notifyObserver(int id) throws InvalidArgsException,
451             SimulatorException;
452
453     /**
454      * API to notify simualted resource's state to all observers.
455      *
456      * @throws InvalidArgsException
457      *             This exception will be thrown if the native resource object
458      *             does not exist.
459      * @throws SimulatorException
460      *             This exception will be thrown for other errors.
461      */
462     public native void notifyAllObservers() throws InvalidArgsException,
463             SimulatorException;
464
465     private native int startResourceAutomation(int typeOfAutomation,
466             IAutomation listener) throws InvalidArgsException,
467             SimulatorException;
468
469     private native int startAttributeAutomation(String attrName,
470         int typeOfAutomation, IAutomation listener)
471         throws InvalidArgsException, SimulatorException;
472
473     @Override
474     protected void finalize() throws Throwable {
475         try {
476             dispose();
477         } catch(Throwable t){
478             throw t;
479         } finally{
480             super.finalize();
481         }
482     }
483
484     private native void dispose();
485 }