17517f12e2270aa774bb4a8a92a963ad89501b70
[platform/upstream/iotivity.git] /
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 oic.simulator.serviceprovider.manager;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import oic.simulator.serviceprovider.model.CollectionResource;
28 import oic.simulator.serviceprovider.model.Device;
29 import oic.simulator.serviceprovider.model.Resource;
30 import oic.simulator.serviceprovider.model.SingleResource;
31
32 public class Data {
33     private Map<String, SingleResource>     singleResourceMap;
34
35     private Map<String, CollectionResource> collectionResourceMap;
36
37     private Map<String, Device>             deviceMap;
38
39     // Holds resource type as key and a resource URI set as values.
40     // Helps in performing operations based on resource type.
41     private Map<String, Set<String>>        resTypeToResUriMap;
42
43     public Data() {
44         singleResourceMap = new HashMap<String, SingleResource>();
45         collectionResourceMap = new HashMap<String, CollectionResource>();
46         deviceMap = new HashMap<String, Device>();
47         resTypeToResUriMap = new HashMap<String, Set<String>>();
48     }
49
50     public synchronized void addResource(Resource resource) {
51         if (null == resource) {
52             return;
53         }
54         if (resource instanceof SingleResource) {
55             singleResourceMap.put(resource.getResourceURI(),
56                     (SingleResource) resource);
57         } else {
58             collectionResourceMap.put(resource.getResourceURI(),
59                     (CollectionResource) resource);
60         }
61         addToTypeAndUriMap(resource);
62     }
63
64     public synchronized void addToTypeAndUriMap(Resource resource) {
65         if (null == resource) {
66             return;
67         }
68         Set<String> resTypes = resource.getResourceTypes();
69         if (null == resTypes || resTypes.isEmpty()) {
70             return;
71         }
72         String uri = resource.getResourceURI();
73         if (null == uri || uri.isEmpty()) {
74             return;
75         }
76         Iterator<String> itr = resTypes.iterator();
77         String rType;
78         Set<String> newTypeSet;
79         while (itr.hasNext()) {
80             rType = itr.next();
81             newTypeSet = resTypeToResUriMap.get(rType);
82             if (null == newTypeSet) {
83                 newTypeSet = new HashSet<String>();
84                 resTypeToResUriMap.put(rType, newTypeSet);
85             }
86             newTypeSet.add(uri);
87         }
88     }
89
90     public void addDevice(Device dev) {
91         if (null == dev) {
92             return;
93         }
94         synchronized (deviceMap) {
95             deviceMap.put(dev.getDeviceName(), dev);
96         }
97     }
98
99     public synchronized void deleteResource(Resource resource) {
100         if (null == resource) {
101             return;
102         }
103         if (resource instanceof SingleResource) {
104             singleResourceMap.remove(resource.getResourceURI());
105         } else {
106             collectionResourceMap.remove(resource.getResourceURI());
107         }
108         removeFromTypeAndUriMap(resource);
109     }
110
111     public synchronized void deleteDevice(Device dev) {
112         if (null == dev) {
113             return;
114         }
115         deviceMap.remove(dev.getDeviceName());
116     }
117
118     public synchronized void removeFromTypeAndUriMap(Resource resource) {
119         if (null == resource) {
120             return;
121         }
122         Set<String> resTypes = resource.getResourceTypes();
123         if (null == resTypes || resTypes.isEmpty()) {
124             return;
125         }
126         String uri = resource.getResourceURI();
127         if (null == uri || uri.isEmpty()) {
128             return;
129         }
130         Iterator<String> itr = resTypes.iterator();
131         String rType;
132         Set<String> newTypeSet;
133         while (itr.hasNext()) {
134             rType = itr.next();
135             newTypeSet = resTypeToResUriMap.get(rType);
136             if (null != newTypeSet) {
137                 newTypeSet.remove(uri);
138             }
139             if (null == newTypeSet || newTypeSet.isEmpty()) {
140                 resTypeToResUriMap.remove(rType);
141             }
142         }
143     }
144
145     public List<SingleResource> getSingleResources() {
146         List<SingleResource> resources;
147         synchronized (singleResourceMap) {
148             if (singleResourceMap.isEmpty()) {
149                 return null;
150             }
151             resources = new ArrayList<SingleResource>();
152             Set<String> uriSet = singleResourceMap.keySet();
153             Iterator<String> itr = uriSet.iterator();
154             while (itr.hasNext()) {
155                 resources.add(singleResourceMap.get(itr.next()));
156             }
157         }
158         return resources;
159     }
160
161     public List<CollectionResource> getCollectionResources() {
162         List<CollectionResource> resources;
163         synchronized (collectionResourceMap) {
164             if (collectionResourceMap.isEmpty()) {
165                 return null;
166             }
167             resources = new ArrayList<CollectionResource>();
168             Set<String> uriSet = collectionResourceMap.keySet();
169             Iterator<String> itr = uriSet.iterator();
170             while (itr.hasNext()) {
171                 resources.add(collectionResourceMap.get(itr.next()));
172             }
173         }
174         return resources;
175     }
176
177     public List<Resource> getResources() {
178         if (singleResourceMap.isEmpty() && collectionResourceMap.isEmpty()) {
179             return null;
180         }
181         List<Resource> resourceList = new ArrayList<Resource>();
182         synchronized (singleResourceMap) {
183             Set<String> uriSet = singleResourceMap.keySet();
184             Iterator<String> itr = uriSet.iterator();
185             while (itr.hasNext()) {
186                 resourceList.add(singleResourceMap.get(itr.next()));
187             }
188         }
189         synchronized (collectionResourceMap) {
190             Set<String> uriSet = collectionResourceMap.keySet();
191             Iterator<String> itr = uriSet.iterator();
192             while (itr.hasNext()) {
193                 resourceList.add(collectionResourceMap.get(itr.next()));
194             }
195         }
196         return resourceList;
197     }
198
199     public List<Device> getDevices() {
200         List<Device> devices;
201         synchronized (deviceMap) {
202             if (deviceMap.isEmpty()) {
203                 return null;
204             }
205             devices = new ArrayList<Device>();
206             Set<String> uriSet = deviceMap.keySet();
207             Iterator<String> itr = uriSet.iterator();
208             while (itr.hasNext()) {
209                 devices.add(deviceMap.get(itr.next()));
210             }
211         }
212         return devices;
213     }
214
215     public synchronized boolean isResourceExist(String resourceURI) {
216         if (null == resourceURI || resourceURI.isEmpty()) {
217             return false;
218         }
219         if ((null != singleResourceMap && singleResourceMap
220                 .containsKey(resourceURI))
221                 || (null != collectionResourceMap && collectionResourceMap
222                         .containsKey(resourceURI))) {
223             return true;
224         }
225         return false;
226     }
227
228     public synchronized boolean isAnyResourceExist() {
229         return ((null != singleResourceMap && !singleResourceMap.isEmpty()) || (null != collectionResourceMap && !collectionResourceMap
230                 .isEmpty()));
231     }
232
233     public void changeResourceURI(Resource resource, String curURI,
234             String newURI) {
235         if (null == resource || null == curURI || null == newURI) {
236             return;
237         }
238         if (resource instanceof SingleResource) {
239             synchronized (singleResourceMap) {
240                 singleResourceMap.remove(curURI);
241                 singleResourceMap.put(newURI, (SingleResource) resource);
242             }
243         } else {
244             synchronized (collectionResourceMap) {
245                 collectionResourceMap.remove(curURI);
246                 collectionResourceMap
247                         .put(newURI, (CollectionResource) resource);
248             }
249         }
250         resource.setResourceURI(newURI);
251     }
252
253     public void changeDeviceName(Device dev, String curName, String newName) {
254         if (null == dev || null == curName || null == newName) {
255             return;
256         }
257         synchronized (deviceMap) {
258             deviceMap.remove(curName);
259             deviceMap.put(newName, dev);
260         }
261         dev.setDeviceName(newName);
262     }
263
264     public SingleResource getSingleResourceByURI(String resourceURI) {
265         if (null == resourceURI) {
266             return null;
267         }
268         SingleResource res;
269         synchronized (singleResourceMap) {
270             res = singleResourceMap.get(resourceURI);
271         }
272         return res;
273     }
274
275     public CollectionResource getCollectionResourceByURI(String resourceURI) {
276         if (null == resourceURI) {
277             return null;
278         }
279         CollectionResource res;
280         synchronized (collectionResourceMap) {
281             res = collectionResourceMap.get(resourceURI);
282         }
283         return res;
284     }
285
286     public Resource getResourceByURI(String resourceURI) {
287         Resource res = getSingleResourceByURI(resourceURI);
288         if (null == res) {
289             res = getCollectionResourceByURI(resourceURI);
290         }
291         return res;
292     }
293
294 }