ee4691f39c97bae819f497858146d7c70c93fa89
[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 /**
18  * This file contains a class which has the methods
19  * for creation and deletion of resources.
20  */
21 package org.oic.simulator;
22
23 import java.util.ArrayList;
24 import java.util.Vector;
25
26 import org.oic.simulator.clientcontroller.IFindResourceListener;
27 import org.oic.simulator.clientcontroller.SimulatorRemoteResource;
28 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
29 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
30
31 /**
32  * This class provides a set of methods for creation and deletion of resources.
33  */
34 public class SimulatorManager {
35
36     /**
37      * Set listener for receiving log messages.
38      *
39      * @param logger
40      *            {@link ILogger} to receive the log messages.
41      */
42     public static void setLogger(ILogger logger) {
43         SimulatorManagerNativeInterface.setLogger(logger);
44     }
45
46     /**
47      * API for creating a resource from a RAML configuration file whose path is
48      * given as a parameter.
49      *
50      * @param configPath
51      *            Path to RAML configuration file.
52      * @param listener
53      *            Listener for receiving notifications whenever there is a
54      *            change in the resource model.
55      * 
56      * @return {@link SimulatorResourceServer} - Created resource on success,
57      *         otherwise null.
58      */
59     public static SimulatorResourceServer createResource(String configPath,
60             IResourceModelChangedListener listener) {
61         SimulatorResourceServer simulatorResourceServerObj;
62         simulatorResourceServerObj = SimulatorManagerNativeInterface
63                 .createResource(configPath, listener);
64         return simulatorResourceServerObj;
65     }
66
67     /**
68      * API for creating a set of resources from a RAML configuration file whose
69      * path is given as a parameter.
70      *
71      * @param configPath
72      *            Path to RAML configuration file.
73      * @param count
74      *            Number of resources to be created.
75      * @param listener
76      *            Listener for receiving notifications whenever there is a
77      *            change in the resource model.
78      * 
79      * @return Returns an array of {@link SimulatorResourceServer} objects one
80      *         for each created resource on success, otherwise null.
81      */
82     public static SimulatorResourceServer[] createResource(String configPath,
83             int count, IResourceModelChangedListener listener) {
84         SimulatorResourceServer[] simulatorResourceServers;
85         simulatorResourceServers = SimulatorManagerNativeInterface
86                 .createResources(configPath, count, listener);
87         return simulatorResourceServers;
88     }
89
90     /**
91      * API for getting all locally created resources.
92      *
93      * @return Returns a list of {@link SimulatorResourceServer} objects on
94      *         success, otherwise null.
95      */
96     public static Vector<SimulatorResourceServer> getLocalResources() {
97         Vector<SimulatorResourceServer> simulatorResourceServerVector = null;
98         simulatorResourceServerVector = SimulatorManagerNativeInterface
99                 .getResources();
100         return simulatorResourceServerVector;
101     }
102
103     /**
104      * API for deleting a specific resource.
105      *
106      * @param resource
107      *            {@link SimulatorResourceServer} object of the resource to be
108      *            deleted.
109      */
110     public static void deleteResource(SimulatorResourceServer resource) {
111         SimulatorManagerNativeInterface.deleteResource(resource);
112     }
113
114     /**
115      * API for deleting either all the resources or resources of a specific
116      * type. Ex: If resourceType is oic.light, all resources of oic.light type
117      * will be deleted. If resourceType is null, then all of the resources will
118      * be deleted.
119      *
120      * @param resourceType
121      *            Type of resource to be deleted.
122      */
123     public static void deleteResources(String resourceType) {
124         SimulatorManagerNativeInterface.deleteResources(resourceType);
125     }
126
127     /**
128      * API for discovering resources in the network. Callback is called whenever
129      * 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      * @return OCSimulatorResult - return value of this API. It returns
137      *         OC_STACK_OK if success.
138      */
139     public static OCSimulatorResult findResource(String resourceType,
140             IFindResourceListener listener) {
141         OCSimulatorResult result;
142         int ordinal = SimulatorManagerNativeInterface.findResource(
143                 resourceType, listener);
144         result = OCSimulatorResult.conversion(ordinal);
145         return result;
146     }
147
148     /**
149      * API for getting the list of previously discovered resources in the
150      * network.
151      *
152      * @param resourceType
153      *            Required resource type
154      *
155      * @return A list of {@link SimulatorRemoteResource} - returns list of
156      *         SimulatorRemoteResource
157      *
158      */
159     public static ArrayList<SimulatorRemoteResource> getFoundResources(
160             String resourceType) {
161         ArrayList<SimulatorRemoteResource> resourceList = SimulatorManagerNativeInterface
162                 .getFoundResources(resourceType);
163         return resourceList;
164     }
165 }