Imported Upstream version 1.0.1
[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 updateInterval
338      *            Interval time in milliseconds for attribute value update automation.
339      * @param listener
340      *            Listener to be notified when automation ends.
341      *
342      * @return Automation ID using which the automation can be stopped.
343      *
344      * @throws InvalidArgsException
345      *             This exception will be thrown if any parameter has invalid
346      *             values.
347      * @throws SimulatorException
348      *             This exception will be thrown for other errors.
349      */
350     public int startResourceAutomation(AutomationType typeOfAutomation,
351             int updateInterval, IAutomation listener) throws InvalidArgsException,
352             SimulatorException {
353         return startResourceAutomation(typeOfAutomation.getValue(), updateInterval, listener);
354     }
355
356     /**
357      * API to  start the attribute level automation. This automation
358      * involves automatically updating all the possible values for a given
359      * attribute sequentially.
360      *
361      * @param attrName
362      *            Name of the attribute to be automated.
363      * @param typeOfAutomation
364      *            {@link AutomationType} indicating whether the automation is
365      *            one-time or recursive.
366      * @param updateInterval
367      *            Interval time in milliseconds for attribute value update automation.
368      * @param listener
369      *            Listener to be notified when automation ends.
370      *
371      * @return Automation ID using which the automation can be stopped.
372      *
373      * @throws InvalidArgsException
374      *             This exception will be thrown if any parameter has invalid
375      *             values.
376      * @throws SimulatorException
377      *             This exception will be thrown for other errors.
378      */
379     public int startAttributeAutomation(String attrName,
380             AutomationType typeOfAutomation, int updateInterval,
381             IAutomation listener)
382             throws InvalidArgsException, SimulatorException {
383         return startAttributeAutomation(attrName, typeOfAutomation.getValue(),
384                 updateInterval, listener);
385     }
386
387     /**
388      * API to  stop the automation based on automation id.
389      *
390      * @param automationId
391      *            Using which a specific automation can be stopped.
392      *
393      * @throws SimulatorException
394      *             This exception will be thrown for other errors.
395      */
396     public native void stopAutomation(int automationId)
397             throws SimulatorException;
398
399     /**
400      * API to remove an attribute from the simulated resource.
401      *
402      * @param key
403      *            Name of the attribute to be deleted.
404      *
405      * @throws InvalidArgsException
406      *             This exception will be thrown either if the parameter has
407      *             invalid value or the native resource object does not exist.
408      * @throws SimulatorException
409      *             This exception will be thrown for other errors.
410      */
411     public native void removeAttribute(String key) throws InvalidArgsException,
412             SimulatorException;
413
414     /**
415      * API to get observers  information.
416      *
417      * @return An array of {@link ObserverInfo} objects.
418      *
419      * @throws InvalidArgsException
420      *             This exception will be thrown if the native resource object
421      *             does not exist.
422      * @throws SimulatorException
423      *             This exception will be thrown for other errors.
424      */
425     public native ObserverInfo[] getObserversList()
426             throws InvalidArgsException, SimulatorException;
427
428     /**
429      * API to set callback to receive notifications when observer is added/removed.
430      *
431      * @param observer
432      *            Listener to be notified when clients start/stop observing.
433      *
434      * @throws InvalidArgsException
435      *             This exception will be thrown either if the parameter has
436      *             invalid value or the native resource object does not exist.
437      * @throws SimulatorException
438      *             This exception will be thrown for other errors.
439      */
440     public native void setObserverCallback(IObserver observer)
441             throws InvalidArgsException, SimulatorException;
442
443     /**
444      * API to notify simulated resource's state to a specific observer.
445      *
446      * @param id
447      *            Observer's Id.
448      *
449      * @throws InvalidArgsException
450      *             This exception will be thrown if the native resource object
451      *             does not exist.
452      * @throws SimulatorException
453      *             This exception will be thrown for other errors.
454      */
455     public native void notifyObserver(int id) throws InvalidArgsException,
456             SimulatorException;
457
458     /**
459      * API to notify simualted resource's state to all observers.
460      *
461      * @throws InvalidArgsException
462      *             This exception will be thrown if the native resource object
463      *             does not exist.
464      * @throws SimulatorException
465      *             This exception will be thrown for other errors.
466      */
467     public native void notifyAllObservers() throws InvalidArgsException,
468             SimulatorException;
469
470     private native int startResourceAutomation(int typeOfAutomation,
471             int updateInterval, IAutomation listener) throws InvalidArgsException,
472             SimulatorException;
473
474     private native int startAttributeAutomation(String attrName,
475         int typeOfAutomation, int updateInterval, IAutomation listener)
476         throws InvalidArgsException, SimulatorException;
477
478     @Override
479     protected void finalize() throws Throwable {
480         try {
481             dispose();
482         } finally {
483             super.finalize();
484         }
485     }
486
487     private native void dispose();
488 }