3a90584967e934516f72344e84d3379b348610e9
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / server / SimulatorResource.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.server;
18
19 import java.util.Vector;
20
21 import org.oic.simulator.InvalidArgsException;
22 import org.oic.simulator.NoSupportException;
23 import org.oic.simulator.SimulatorException;
24 import org.oic.simulator.SimulatorResourceModel;
25
26 /**
27  * This class serves as a base class type for all the resources in the
28  * simulator. It provides APIs to get and update the resource details, and
29  * register listeners to get notifications for resource model changes and
30  * observe requests from clients, and APIs to register/ unregister the resource
31  * with the platform.
32  */
33 public class SimulatorResource {
34
35     protected long mNativeHandle;
36
37     @Override
38     protected void finalize() throws Throwable {
39         try {
40             nativeDispose();
41         } catch (Throwable t) {
42             throw t;
43         } finally {
44             super.finalize();
45         }
46     }
47
48     /**
49      * Enum to represent the type of resource.
50      */
51     public enum Type {
52         SINGLE, COLLECTION
53     }
54
55     /**
56      * Enum to represent the update type of automation.
57      */
58     public enum AutoUpdateType {
59         ONE_TIME, REPEAT
60     }
61
62     /**
63      * API which indicates whether the resource is collection or single
64      * resource.
65      *
66      * @return True if the resource is collection, otherwise false.
67      *
68      * @throws SimulatorException
69      *             This exception will be thrown if the native resource object
70      *             does not exist or for some general errors.
71      */
72     public boolean isCollection() {
73         return (this instanceof SimulatorCollectionResource);
74     }
75
76     /**
77      * API to get the type which indicates whether resource is single or
78      * collection resource.
79      *
80      * @return Type of resource.
81      *
82      * @throws SimulatorException
83      *             This exception will be thrown if the native resource object
84      *             does not exist or for some general errors.
85      */
86     public Type getType() {
87         if (this instanceof SimulatorSingleResource) {
88             return Type.SINGLE;
89         } else {
90             return Type.COLLECTION;
91         }
92     }
93
94     /**
95      * API to get the name of the resource.
96      *
97      * @return Name of the resource.
98      *
99      * @throws SimulatorException
100      *             This exception will be thrown if the native resource object
101      *             does not exist or for some general errors.
102      */
103     public String getName() throws SimulatorException {
104         return nativeGetName();
105     }
106
107     /**
108      * API to get the resource URI.
109      *
110      * @return Resource URI.
111      *
112      * @throws SimulatorException
113      *             This exception will be thrown if the native resource object
114      *             does not exist or for some general errors.
115      */
116     public String getURI() throws SimulatorException {
117         return nativeGetURI();
118     }
119
120     /**
121      * API to get the resource type.
122      *
123      * @return Resource type.
124      *
125      * @throws SimulatorException
126      *             This exception will be thrown if the native resource object
127      *             does not exist or for some general errors.
128      */
129     public String getResourceType() throws SimulatorException {
130         return nativeGetResourceType();
131     }
132
133     /**
134      * API to get the interfaces resource is bound with.
135      *
136      * @return Interface types.
137      *
138      * @throws SimulatorException
139      *             This exception will be thrown if the native resource object
140      *             does not exist or for some general errors.
141      */
142     public Vector<String> getInterface() throws SimulatorException {
143         return nativeGetInterface();
144     }
145
146     /**
147      * API to get the observable state of resource.
148      *
149      * @return Observable state - true if resource is observable, otherwise
150      *         false.
151      *
152      * @throws SimulatorException
153      *             This exception will be thrown if the native resource object
154      *             does not exist or for some general errors.
155      */
156     public boolean isObservable() throws SimulatorException {
157         return nativeIsObservable();
158     }
159
160     /**
161      * API to get the discoverable state of resource.
162      *
163      * @return Discoverable state - true if resource is discoverable, otherwise
164      *         false.
165      *
166      * @throws SimulatorException
167      *             This exception will be thrown if the native resource object
168      *             does not exist or for some general errors.
169      */
170     public boolean isDiscoverable() throws SimulatorException {
171         return nativeIsDiscoverable();
172     }
173
174     /**
175      * API to get the start state of resource.
176      *
177      * @return Start state - true if resource is started, otherwise false.
178      *
179      * @throws SimulatorException
180      *             This exception will be thrown if the native resource object
181      *             does not exist or for some general errors.
182      */
183     public boolean isStarted() throws SimulatorException {
184         return nativeIsStarted();
185     }
186
187     /**
188      * API to set the name of the resource.
189      *
190      * @param name
191      *            Name to be set.
192      *
193      * @throws InvalidArgsException
194      *             This exception will be thrown if the resource name is
195      *             invalid.
196      * @throws SimulatorException
197      *             This exception will be thrown if the native resource object
198      *             does not exist or for some general errors.
199      */
200     public void setName(String name) throws InvalidArgsException,
201             SimulatorException {
202         nativeSetName(name);
203     }
204
205     /**
206      * API to set the resource URI.
207      *
208      * @param uri
209      *            URI to be set.
210      *
211      * @throws InvalidArgsException
212      *             This exception will be thrown if the resource URI is invalid.
213      * @throws SimulatorException
214      *             A resource needs to be stopped by calling the stop() before
215      *             calling this API. This exception will be thrown if the native
216      *             resource object has not yet been stopped.
217      */
218     public void setURI(String uri) throws InvalidArgsException,
219             SimulatorException {
220         nativeSetURI(uri);
221     }
222
223     /**
224      * API to set the resource type.
225      *
226      * @param resourceType
227      *            Resource type string.
228      *
229      * @throws InvalidArgsException
230      *             This exception will be thrown if the resource type is
231      *             invalid.
232      * @throws SimulatorException
233      *             This exception will be thrown if the native resource object
234      *             does not exist or for some general errors.
235      */
236     public void setResourceType(String resourceType)
237             throws InvalidArgsException, SimulatorException {
238         nativeSetResourceType(resourceType);
239     }
240
241     /**
242      * API to set interface to resource. Resource should be stopped before
243      * calling this API.
244      *
245      * @param interfaceType
246      *            Interface to be set.
247      *
248      * @throws InvalidArgsException
249      *             This exception will be thrown if the interface type is
250      *             invalid.
251      * @throws SimulatorException
252      *             This exception will be thrown if the native resource object
253      *             does not exist or resource is still running or for some
254      *             general errors.
255      */
256     public void setInterface(String interfaceType) throws InvalidArgsException,
257             SimulatorException {
258         nativeSetInterface(interfaceType);
259     }
260
261     /**
262      * API to set a list of interfaces to resource. Resource should be stopped
263      * before calling this API.
264      *
265      * @param interfaceTypes
266      *            Interfaces to be set.
267      *
268      * @throws InvalidArgsException
269      *             This exception will be thrown if the interface type is
270      *             invalid.
271      * @throws SimulatorException
272      *             This exception will be thrown if the native resource object
273      *             does not exist or resource is still running or for some
274      *             general errors.
275      */
276     public void setInterface(Vector<String> interfaceTypes)
277             throws InvalidArgsException, SimulatorException {
278         nativeSetInterfaces(interfaceTypes);
279     }
280
281     /**
282      * API to add interface type for resource.
283      *
284      * @param interfaceType
285      *            Interface to be added for resource.
286      *
287      * @throws InvalidArgsException
288      *             This exception will be thrown if the interface type is
289      *             invalid.
290      * @throws SimulatorException
291      *             This exception will be thrown if the native resource object
292      *             does not exist or for some general errors.
293      */
294     public void addInterface(String interfaceType) throws InvalidArgsException,
295             NoSupportException, SimulatorException {
296         nativeAddInterface(interfaceType);
297     }
298
299     /**
300      * API to make the resource observable or not observable.
301      *
302      * @param state
303      *            True makes the resource observable, otherwise non-observable.
304      *
305      * @throws SimulatorException
306      *             This exception will be thrown if the native resource object
307      *             does not exist or for some general errors.
308      */
309     public void setObservable(boolean state) throws SimulatorException {
310         nativeSetObservable(state);
311     }
312
313     /**
314      * API to make the resource discoverable or not discoverable.
315      *
316      * @param state
317      *            True makes the resource discoverable, otherwise
318      *            non-discoverable.
319      *
320      * @throws SimulatorException
321      *             This exception will be thrown if the native resource object
322      *             does not exist or for some general errors.
323      */
324     public void setDiscoverable(boolean state) throws SimulatorException {
325         nativeSetDiscoverable(state);
326     }
327
328     /**
329      * API to set the listener for receiving the notifications when observer is
330      * registered or unregistered with resource.
331      *
332      * @param listener
333      *            Callback to be set for receiving the notifications.
334      *
335      * @throws InvalidArgsException
336      *             This exception will be thrown if the listener is invalid.
337      * @throws SimulatorException
338      *             This exception will be thrown if the native resource object
339      *             does not exist or for some general errors.
340      */
341     public void setObserverListener(ObserverListener listener)
342             throws InvalidArgsException, SimulatorException {
343         nativeSetObserverListener(listener);
344     }
345
346     /**
347      * API to set listener for receiving notifications when resource's model
348      * gets changed.
349      *
350      * @param listener
351      *            {@link ResourceModelChangeListener}.
352      *
353      * @throws InvalidArgsException
354      *             This exception will be thrown on invalid input.
355      * @throws SimulatorException
356      *             This exception will be thrown for other errors.
357      */
358     public void setResourceModelChangeListener(
359             ResourceModelChangeListener listener) throws InvalidArgsException,
360             SimulatorException {
361         nativeSetResourceModelChangeListener(listener);
362     }
363
364     /**
365      * API to start(register) the resource.
366      *
367      * @throws SimulatorException
368      *             This exception will be thrown if the native resource object
369      *             does not exist or for some general errors.
370      */
371     public void start() throws SimulatorException {
372         nativeStart();
373     }
374
375     /**
376      * API to stop(unregister) the resource.
377      *
378      * @throws SimulatorException
379      *             This exception will be thrown if the native resource object
380      *             does not exist or for some general errors.
381      */
382     public void stop() throws SimulatorException {
383         nativeStop();
384     }
385
386     /**
387      * API to get the {@link SimulatorResourceModel} of the simulated resource.
388      *
389      * @return {@link SimulatorResourceModel} object on success, otherwise null.
390      *
391      * @throws SimulatorException
392      *             This exception will be thrown if simulated resource is not
393      *             proper.
394      */
395     public SimulatorResourceModel getResourceModel() throws SimulatorException {
396         return nativeGetResourceModel();
397     }
398
399     /**
400      * API to get observers which are registered with resource.
401      *
402      * @return observers as an array of {@link Observer}.
403      *
404      * @throws SimulatorException
405      *             This exception will be thrown if the native resource object
406      *             does not exist or for some general errors.
407      */
408     public Vector<Observer> getObservers() throws SimulatorException {
409         return nativeGetObservers();
410     }
411
412     /**
413      * API to notify current resource model to a specific observer.
414      *
415      * @param observerId
416      *            Observer ID to notify.
417      *
418      * @throws SimulatorException
419      *             This exception will be thrown if the native resource object
420      *             does not exist or for some general errors.
421      */
422     public void notifyObserver(int observerId) throws SimulatorException {
423         nativeNotify(observerId);
424     }
425
426     /**
427      * API to notify current resource model to all registered observers.
428      *
429      * @throws SimulatorException
430      *             This exception will be thrown if the native resource object
431      *             does not exist or for some general errors.
432      */
433     public void notifyAllObservers() throws SimulatorException {
434         nativeNotifyAll();
435     }
436
437     /**
438      * Listener for receiving notification when observer is registered or
439      * unregistered with the resource.
440      */
441     public interface ObserverListener {
442         /**
443          * Method will be invoked when an observer is registered with resource.
444          *
445          * @param resourceURI
446          *            URI of the resource.
447          * @param observer
448          *            {@link Observer} object containing the details of
449          *            observer.
450          */
451         public void onObserverAdded(String resourceURI, Observer observer);
452
453         /**
454          * Method will be invoked when an observer is unregistered from the
455          * resource.
456          *
457          * @param resourceURI
458          *            URI of the resource.
459          * @param observer
460          *            {@link Observer} object containing the details of
461          *            observer.
462          */
463         public void onObserverRemoved(String resourceURI, Observer observer);
464     }
465
466     /**
467      * Listener for receiving notification on completion of automatically
468      * updating attribute value from its range or value set property.
469      */
470     public interface AutoUpdateListener {
471         /**
472          * Method for receiving automation complete notifications.
473          *
474          * @param uri
475          *            URI of resource.
476          * @param id
477          *            Identifier for auto resource/attribute update session.
478          */
479         public void onUpdateComplete(String uri, int id);
480     }
481
482     /**
483      * Listener for receiving notifications whenever there is a change in the
484      * resource model.
485      */
486     public interface ResourceModelChangeListener {
487         /**
488          * Method will be invoked to notify about the changes in the resource
489          * model.
490          *
491          * @param uri
492          *            URI of resource.
493          * @param resourceModel
494          *            {@link SimulatorResourceModel} of the resource.
495          */
496         public void onResourceModelChanged(String uri,
497                 SimulatorResourceModel resourceModel);
498     }
499
500     protected SimulatorResource() {
501     }
502
503     private native String nativeGetName();
504
505     private native String nativeGetURI();
506
507     private native String nativeGetResourceType();
508
509     private native Vector<String> nativeGetInterface();
510
511     private native boolean nativeIsObservable();
512
513     private native boolean nativeIsDiscoverable();
514
515     private native boolean nativeIsStarted();
516
517     private native void nativeSetName(String name);
518
519     private native void nativeSetURI(String uri);
520
521     private native void nativeSetResourceType(String resourceType);
522
523     private native void nativeSetInterface(String interfaceType);
524
525     private native void nativeSetInterfaces(Vector<String> interfaceTypes);
526
527     private native void nativeAddInterface(String interfaceType);
528
529     private native void nativeSetObservable(boolean state);
530
531     private native void nativeSetDiscoverable(boolean state);
532
533     private native void nativeSetObserverListener(ObserverListener listener);
534
535     private native void nativeSetResourceModelChangeListener(
536             ResourceModelChangeListener listener);
537
538     private native void nativeStart();
539
540     private native void nativeStop();
541
542     private native SimulatorResourceModel nativeGetResourceModel();
543
544     private native Vector<Observer> nativeGetObservers();
545
546     private native void nativeNotify(int observerId);
547
548     private native void nativeNotifyAll();
549
550     private native void nativeDispose();
551 }