Junit testing fixes for Simulator APIs.
[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 org.oic.simulator.clientcontroller.IFindResourceListener;
20 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
21 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
22
23 /**
24  * This class provides a set of methods for creation, discovery and deletion of
25  * resources.
26  */
27 public class SimulatorManager {
28
29     /**
30      * API for creating a resource from a RAML configuration file whose path is
31      * given as a parameter.
32      *
33      * @param configPath
34      *            Path to RAML configuration file.
35      * @param listener
36      *            Listener for receiving notifications whenever there is a
37      *            change in the resource model.
38      *
39      * @return {@link SimulatorResourceServer} - Created resource on success,
40      *         otherwise null.
41      *
42      * @throws InvalidArgsException
43      *             Thrown if the input parameters are empty.
44      * @throws SimulatorException
45      *             Thrown for other errors.
46      */
47     public static SimulatorResourceServer createResource(String configPath,
48             IResourceModelChangedListener listener)
49             throws InvalidArgsException, SimulatorException {
50         if (configPath.isEmpty() || null == listener)
51             throw new InvalidArgsException(
52                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
53                     "Parameter passed in invalid");
54         SimulatorResourceServer simulatorResourceServerObj;
55         simulatorResourceServerObj = SimulatorManagerNativeInterface
56                 .createResource(configPath, listener);
57         return simulatorResourceServerObj;
58     }
59
60     /**
61      * API for creating a set of resources from a RAML configuration file whose
62      * path is given as a parameter.
63      *
64      * @param configPath
65      *            Path to RAML configuration file.
66      * @param count
67      *            Number of resources to be created.
68      * @param listener
69      *            Listener for receiving notifications whenever there is a
70      *            change in the resource model.
71      *
72      * @return Returns an array of {@link SimulatorResourceServer} objects one
73      *         for each created resource on success, otherwise null.
74      *
75      * @throws InvalidArgsException
76      *             Thrown if the input parameters are empty.
77      * @throws SimulatorException
78      *             Thrown for other errors.
79      */
80     public static SimulatorResourceServer[] createResource(String configPath,
81             int count, IResourceModelChangedListener listener)
82             throws InvalidArgsException, SimulatorException {
83         if (configPath.isEmpty() || count < 0 || null == listener)
84             throw new InvalidArgsException(
85                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
86                     "Parameter passed in invalid");
87         SimulatorResourceServer[] simulatorResourceServers;
88         simulatorResourceServers = SimulatorManagerNativeInterface
89                 .createResources(configPath, count, listener);
90         return simulatorResourceServers;
91     }
92
93     /**
94      * API for deleting a specific resource.
95      *
96      * @param resource
97      *            {@link SimulatorResourceServer} object of the resource to be
98      *            deleted.
99      *
100      * @throws InvalidArgsException
101      *             Thrown if the input parameter is empty.
102      * @throws SimulatorException
103      *             Thrown for other errors.
104      */
105     public static void deleteResource(SimulatorResourceServer resource)
106             throws InvalidArgsException, SimulatorException {
107         SimulatorManagerNativeInterface.deleteResource(resource);
108     }
109
110     /**
111      * API for deleting either all the resources or resources of a specific
112      * type. Ex: If resourceType is oic.light, all resources of oic.light type
113      * will be deleted. If resourceType is null, then all of the resources will
114      * be deleted.
115      *
116      * @param resourceType
117      *            Type of resource to be deleted.
118      *
119      * @throws SimulatorException
120      *             Thrown for other errors.
121      */
122     public static void deleteResources(String resourceType)
123             throws SimulatorException {
124         SimulatorManagerNativeInterface.deleteResources(resourceType);
125     }
126
127     /**
128      * API for discovering all types of resources in the network. Callback is
129      * called when a resource is discovered in the network.
130      *
131      * @param listener
132      *            Interface to receive the discovered remote resources.
133      *
134      * @throws InvalidArgsException
135      *             Thrown if the input parameter is empty.
136      * @throws SimulatorException
137      *             Thrown for other errors.
138      */
139     public static void findResource(IFindResourceListener listener)
140             throws InvalidArgsException, SimulatorException {
141         SimulatorManagerNativeInterface.findResource(null, listener);
142     }
143
144     /**
145      * API for discovering specific type of resources in the network. Callback
146      * is called when a resource is discovered in the network.
147      *
148      * @param resourceType
149      *            Required resource type
150      * @param listener
151      *            Interface to receive the discovered remote resources.
152      *
153      * @throws InvalidArgsException
154      *             Thrown if the input parameter is empty.
155      * @throws SimulatorException
156      *             Thrown for other errors.
157      */
158     public static void findResource(String resourceType,
159             IFindResourceListener listener) throws InvalidArgsException,
160             SimulatorException {
161         if (null == resourceType || resourceType.isEmpty()) {
162             throw new InvalidArgsException(
163                     SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
164                     "Resource type is empty");
165         }
166         SimulatorManagerNativeInterface.findResource(resourceType, listener);
167     }
168
169     /**
170      * API to set the listener for receiving log messages.
171      *
172      * @param logger
173      *            {@link ILogger} to receive the log messages.
174      */
175     public static void setLogger(ILogger logger) {
176         SimulatorManagerNativeInterface.setLogger(logger);
177     }
178
179     /**
180      * API to set the device information.
181      *
182      * @param deviceInfo
183      *            Device information.
184      */
185     public static void setDeviceInfo(String deviceInfo) {
186         SimulatorManagerNativeInterface.setDeviceInfo(deviceInfo);
187     }
188
189     /**
190      * API to get the device information asynchronously via callback
191      * using {@link IDeviceInfo}.
192      *
193      * @param listener
194      *            Interface for receiving the device information.
195      */
196     public static void getDeviceInfo(IDeviceInfo listener) {
197         SimulatorManagerNativeInterface.getDeviceInfo(listener);
198     }
199
200     /**
201      * API to set the platform information.
202      *
203      * @param platformInfo
204      *            {@link PlatformInfo} - Platform information.
205      */
206     public static void setPlatformInfo(PlatformInfo platformInfo) {
207         SimulatorManagerNativeInterface.setPlatformInfo(platformInfo);
208     }
209
210     /**
211      * API to get the platform information asynchronously via callback
212      * using {@link IPlatformInfo}..
213      *
214      * @param listener
215      *            Interface for receiving the platform information.
216      */
217     public static void getPlatformInfo(IPlatformInfo listener) {
218         SimulatorManagerNativeInterface.getPlatformInfo(listener);
219     }
220 }