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