2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.manager;
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;
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;
33 private Map<String, SingleResource> singleResourceMap;
35 private Map<String, CollectionResource> collectionResourceMap;
37 private Map<String, Device> deviceMap;
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;
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>>();
50 public synchronized void addResource(Resource resource) {
51 if (null == resource) {
54 if (resource instanceof SingleResource) {
55 singleResourceMap.put(resource.getResourceURI(),
56 (SingleResource) resource);
58 collectionResourceMap.put(resource.getResourceURI(),
59 (CollectionResource) resource);
61 addToTypeAndUriMap(resource);
64 public synchronized void addToTypeAndUriMap(Resource resource) {
65 if (null == resource) {
68 Set<String> resTypes = resource.getResourceTypes();
69 if (null == resTypes || resTypes.isEmpty()) {
72 String uri = resource.getResourceURI();
73 if (null == uri || uri.isEmpty()) {
76 Iterator<String> itr = resTypes.iterator();
78 Set<String> newTypeSet;
79 while (itr.hasNext()) {
81 newTypeSet = resTypeToResUriMap.get(rType);
82 if (null == newTypeSet) {
83 newTypeSet = new HashSet<String>();
84 resTypeToResUriMap.put(rType, newTypeSet);
90 public void addDevice(Device dev) {
94 synchronized (deviceMap) {
95 deviceMap.put(dev.getDeviceName(), dev);
99 public synchronized void deleteResource(Resource resource) {
100 if (null == resource) {
103 if (resource instanceof SingleResource) {
104 singleResourceMap.remove(resource.getResourceURI());
106 collectionResourceMap.remove(resource.getResourceURI());
108 removeFromTypeAndUriMap(resource);
111 public synchronized void deleteDevice(Device dev) {
115 deviceMap.remove(dev.getDeviceName());
118 public synchronized void removeFromTypeAndUriMap(Resource resource) {
119 if (null == resource) {
122 Set<String> resTypes = resource.getResourceTypes();
123 if (null == resTypes || resTypes.isEmpty()) {
126 String uri = resource.getResourceURI();
127 if (null == uri || uri.isEmpty()) {
130 Iterator<String> itr = resTypes.iterator();
132 Set<String> newTypeSet;
133 while (itr.hasNext()) {
135 newTypeSet = resTypeToResUriMap.get(rType);
136 if (null != newTypeSet) {
137 newTypeSet.remove(uri);
139 if (null == newTypeSet || newTypeSet.isEmpty()) {
140 resTypeToResUriMap.remove(rType);
145 public List<SingleResource> getSingleResources() {
146 List<SingleResource> resources;
147 synchronized (singleResourceMap) {
148 if (singleResourceMap.isEmpty()) {
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()));
161 public List<CollectionResource> getCollectionResources() {
162 List<CollectionResource> resources;
163 synchronized (collectionResourceMap) {
164 if (collectionResourceMap.isEmpty()) {
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()));
177 public List<Resource> getResources() {
178 if (singleResourceMap.isEmpty() && collectionResourceMap.isEmpty()) {
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()));
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()));
199 public List<Device> getDevices() {
200 List<Device> devices;
201 synchronized (deviceMap) {
202 if (deviceMap.isEmpty()) {
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()));
215 public synchronized boolean isResourceExist(String resourceURI) {
216 if (null == resourceURI || resourceURI.isEmpty()) {
219 if ((null != singleResourceMap && singleResourceMap
220 .containsKey(resourceURI))
221 || (null != collectionResourceMap && collectionResourceMap
222 .containsKey(resourceURI))) {
228 public synchronized boolean isAnyResourceExist() {
229 return ((null != singleResourceMap && !singleResourceMap.isEmpty()) || (null != collectionResourceMap && !collectionResourceMap
233 public void changeResourceURI(Resource resource, String curURI,
235 if (null == resource || null == curURI || null == newURI) {
238 if (resource instanceof SingleResource) {
239 synchronized (singleResourceMap) {
240 singleResourceMap.remove(curURI);
241 singleResourceMap.put(newURI, (SingleResource) resource);
244 synchronized (collectionResourceMap) {
245 collectionResourceMap.remove(curURI);
246 collectionResourceMap
247 .put(newURI, (CollectionResource) resource);
250 resource.setResourceURI(newURI);
253 public void changeDeviceName(Device dev, String curName, String newName) {
254 if (null == dev || null == curName || null == newName) {
257 synchronized (deviceMap) {
258 deviceMap.remove(curName);
259 deviceMap.put(newName, dev);
261 dev.setDeviceName(newName);
264 public SingleResource getSingleResourceByURI(String resourceURI) {
265 if (null == resourceURI) {
269 synchronized (singleResourceMap) {
270 res = singleResourceMap.get(resourceURI);
275 public CollectionResource getCollectionResourceByURI(String resourceURI) {
276 if (null == resourceURI) {
279 CollectionResource res;
280 synchronized (collectionResourceMap) {
281 res = collectionResourceMap.get(resourceURI);
286 public Resource getResourceByURI(String resourceURI) {
287 Resource res = getSingleResourceByURI(resourceURI);
289 res = getCollectionResourceByURI(resourceURI);