[IOT-1538] Add support for Protocol-Independent ID
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / SimulatorManager.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;
18
19 import java.util.Vector;
20
21 import org.oic.simulator.client.FindResourceListener;
22 import org.oic.simulator.server.SimulatorResource;
23
24 /**
25  * This class provides a set of methods for creation, discovery and deletion of
26  * resources.
27  */
28 public class SimulatorManager {
29
30     static {
31         System.loadLibrary("connectivity_abstraction");
32         System.loadLibrary("oc_logger");
33         System.loadLibrary("octbstack");
34         System.loadLibrary("oc");
35         System.loadLibrary("RamlParser");
36         System.loadLibrary("SimulatorManager");
37     }
38
39     /**
40      * API for creating a resource from a RAML configuration file.
41      *
42      * @param configPath
43      *            Path to RAML configuration file.
44      *
45      * @return {@link SimulatorResource} - Created resource on success,
46      *         otherwise null.
47      *
48      * @throws InvalidArgsException
49      *             Thrown if the input parameters are empty.
50      * @throws SimulatorException
51      *             Thrown for other errors.
52      */
53     public static SimulatorResource createResource(String configPath)
54             throws InvalidArgsException, SimulatorException {
55         return nativeCreateResource(configPath);
56     }
57
58     /**
59      * API for creating a set of resources from a RAML configuration file.
60      *
61      * @param configPath
62      *            Path to RAML configuration file.
63      * @param count
64      *            Number of resources to be created.
65      *
66      * @return Returns an array of {@link SimulatorResource} objects one for
67      *         each created resource on success, otherwise null.
68      *
69      * @throws InvalidArgsException
70      *             Thrown if the input parameters are empty.
71      * @throws SimulatorException
72      *             Thrown for other errors.
73      */
74     public static Vector<SimulatorResource> createResource(String configPath,
75             int count) throws InvalidArgsException, SimulatorException {
76         return nativeCreateResources(configPath, count);
77     };
78
79     /**
80      * API for creating a resource either single or collection type.
81      *
82      * @param type
83      *            Indicates whether single or collection type.
84      * @param name
85      *            Resource Name.
86      * @param uri
87      *            Resource URI.
88      * @param resourceType
89      *            Resource Type.
90      *
91      * @return {@link SimulatorResource} - Created resource on success,
92      *         otherwise null.
93      *
94      * @throws InvalidArgsException
95      *             Thrown if the input parameters are empty.
96      * @throws SimulatorException
97      *             Thrown for other errors.
98      */
99     public static SimulatorResource createResource(SimulatorResource.Type type,
100             String name, String uri, String resourceType)
101             throws InvalidArgsException, SimulatorException {
102         SimulatorResource resource;
103         if (type == SimulatorResource.Type.SINGLE)
104             resource = nativeCreateSingleResource(name, uri, resourceType);
105         else
106             resource = nativeCreateCollectionResource(name, uri, resourceType);
107         return resource;
108     }
109
110     /**
111      * API for discovering all types of resources in the network. Callback is
112      * called when a resource is discovered in the network.
113      *
114      * @param listener
115      *            Interface to receive the discovered remote resources.
116      *
117      * @throws InvalidArgsException
118      *             Thrown if the input parameter is empty.
119      * @throws SimulatorException
120      *             Thrown for other errors.
121      */
122     public static void findResource(FindResourceListener listener)
123             throws InvalidArgsException, SimulatorException {
124         nativeSearchResource(null, listener);
125     }
126
127     /**
128      * API for discovering specific type of resources in the network. Callback
129      * is called when a resource is discovered in the network.
130      *
131      * @param resourceType
132      *            Required resource type.
133      * @param listener
134      *            Interface to receive the discovered remote resources.
135      *
136      * @throws InvalidArgsException
137      *             Thrown if the input parameter is empty.
138      * @throws SimulatorException
139      *             Thrown for other errors.
140      */
141     public static void findResource(String resourceType,
142             FindResourceListener listener) throws InvalidArgsException,
143             SimulatorException {
144         if (null == resourceType || resourceType.isEmpty()) {
145             throw new InvalidArgsException(
146                     SimulatorResult.SIMULATOR_INVALID_PARAM,
147                     "Invalid resource type!");
148         }
149
150         nativeSearchResource(resourceType, listener);
151     }
152
153     /**
154      * API to set the device information.
155      *
156      * @deprecated Use setDeviceInfo2 instead.
157      *
158      * @param deviceInfo
159      *            Device information.
160      *
161      * @throws InvalidArgsException
162      *             Thrown if the input parameter is empty.
163      * @throws SimulatorException
164      *             Thrown for other errors.
165      */
166     public static void setDeviceInfo(String deviceInfo)
167             throws InvalidArgsException, SimulatorException {
168         nativeSetDeviceInfo(deviceInfo);
169     }
170
171     /**
172      * API to set the device information.
173      *
174      * @param deviceInfo
175      *            Device information.
176      * @param protocolIndependentID
177      *            Protocol-independent UUID of the device.
178      *
179      * @throws InvalidArgsException
180      *             Thrown if the input parameter is empty.
181      * @throws SimulatorException
182      *             Thrown for other errors.
183      */
184     public static void setDeviceInfo2(String deviceInfo, String protocolIndependentID)
185             throws InvalidArgsException, SimulatorException {
186         nativeSetDeviceInfo2(deviceInfo, protocolIndependentID);
187     }
188
189     /**
190      * API to search for devices on the given host in the network.
191      *
192      * @param hostUri
193      *            URI of the host device.
194      * @param listener
195      *            Listener for receiving the device information.
196      *
197      * @throws InvalidArgsException
198      *             Thrown if the input parameter is empty.
199      * @throws SimulatorException
200      *             Thrown for other errors.
201      */
202     public static void findDevices(String hostUri, DeviceListener listener)
203             throws InvalidArgsException, SimulatorException {
204         nativeFindDevices(hostUri, listener);
205     }
206
207     /**
208      * API to set the platform information.
209      *
210      * @param platformInfo
211      *            {@link PlatformInfo} - Platform information.
212      *
213      * @throws InvalidArgsException
214      *             Thrown if the input parameter is empty.
215      * @throws SimulatorException
216      *             Thrown for other errors.
217      */
218     public static void setPlatformInfo(PlatformInfo platformInfo)
219             throws InvalidArgsException, SimulatorException {
220         nativeSetPlatformInfo(platformInfo);
221     }
222
223     /**
224      * API to find the platform information of the given host in the network.
225      *
226      * @param hostUri
227      *            URI of the host device.
228      * @param listener
229      *            Listener for receiving the platform information.
230      *
231      * @throws InvalidArgsException
232      *             Thrown if the input parameter is empty.
233      * @throws SimulatorException
234      *             Thrown for other errors.
235      */
236     public static void getPlatformInformation(String hostUri,
237             PlatformListener listener) throws InvalidArgsException,
238             SimulatorException {
239         nativeGetPlatformInformation(hostUri, listener);
240     }
241
242     /**
243      * API to set the listener for receiving log messages.
244      *
245      * @param logger
246      *            {@link ILogger} to receive the log messages.
247      */
248     public static void setLogger(ILogger logger) {
249         nativeSetLogger(logger);
250     }
251
252     private SimulatorManager() {
253     }
254
255     private static native SimulatorResource nativeCreateResource(
256             String configPath);
257
258     private static native Vector<SimulatorResource> nativeCreateResources(
259             String configPath, int count);
260
261     private static native SimulatorResource nativeCreateSingleResource(
262             String name, String uri, String resourceType);
263
264     private static native SimulatorResource nativeCreateCollectionResource(
265             String name, String uri, String resourceType);
266
267     private static native void nativeSearchResource(String resourceType,
268             FindResourceListener listener);
269
270     private static native void nativeSetDeviceInfo(String deviceInfo);
271     private static native void nativeSetDeviceInfo2(String deviceInfo,
272             String protocolIndependentID);
273
274     private static native void nativeFindDevices(String hostUri,
275             DeviceListener listener);
276
277     private static native void nativeSetPlatformInfo(PlatformInfo platformInfo);
278
279     private static native void nativeGetPlatformInformation(String hostUri,
280             PlatformListener listener);
281
282     private static native void nativeSetLogger(ILogger logger);
283 }