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