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.clientcontroller.manager;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.LinkedList;
26 import java.util.List;
29 import java.util.Vector;
31 import oic.simulator.clientcontroller.Activator;
32 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
33 import oic.simulator.clientcontroller.listener.IDevicePlatformInfoUIListener;
34 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
35 import oic.simulator.clientcontroller.listener.IGetUIListener;
36 import oic.simulator.clientcontroller.listener.IObserveUIListener;
37 import oic.simulator.clientcontroller.listener.IPostUIListener;
38 import oic.simulator.clientcontroller.listener.IPutUIListener;
39 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
40 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
41 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
42 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
43 import oic.simulator.clientcontroller.utils.Constants;
44 import oic.simulator.clientcontroller.utils.Utility;
46 import org.oic.simulator.AttributeProperty;
47 import org.oic.simulator.AttributeProperty.Type;
48 import org.oic.simulator.AttributeValue;
49 import org.oic.simulator.AttributeValue.TypeInfo;
50 import org.oic.simulator.AttributeValue.ValueType;
51 import org.oic.simulator.DeviceInfo;
52 import org.oic.simulator.DeviceListener;
53 import org.oic.simulator.ILogger.Level;
54 import org.oic.simulator.PlatformInfo;
55 import org.oic.simulator.PlatformListener;
56 import org.oic.simulator.SimulatorException;
57 import org.oic.simulator.SimulatorManager;
58 import org.oic.simulator.SimulatorResourceAttribute;
59 import org.oic.simulator.SimulatorResourceModel;
60 import org.oic.simulator.SimulatorResult;
61 import org.oic.simulator.client.FindResourceListener;
62 import org.oic.simulator.client.SimulatorRemoteResource;
63 import org.oic.simulator.client.SimulatorRemoteResource.GetResponseListener;
64 import org.oic.simulator.client.SimulatorRemoteResource.ObserveNotificationListener;
65 import org.oic.simulator.client.SimulatorRemoteResource.PostResponseListener;
66 import org.oic.simulator.client.SimulatorRemoteResource.PutResponseListener;
67 import org.oic.simulator.client.SimulatorRemoteResource.VerificationListener;
68 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
71 * This class acts as an interface between the simulator java SDK and the
72 * various UI modules. It maintains all the details of resources and provides
73 * other UI modules with the information required. It also handles responses for
74 * find, GET, PUT, POST, Observe and automatic verification operations from
75 * native layer and propagates those events to the registered UI listeners.
77 public class ResourceManager {
79 private Set<String> lastKnownSearchTypes;
81 private RemoteResource currentResourceInSelection;
83 private FindResourceListener findResourceListener;
84 private GetResponseListener getListener;
85 private PutResponseListener putListener;
86 private PostResponseListener postListener;
87 private ObserveNotificationListener observeListener;
88 private VerificationListener verifyListener;
89 private DeviceListener deviceListener;
90 private PlatformListener platformListener;
92 private ResponseSynchronizerThread synchronizerThread;
94 private Thread threadHandle;
96 private List<IFindResourceUIListener> findResourceUIListeners;
97 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
98 private List<IGetUIListener> getUIListeners;
99 private List<IPutUIListener> putUIListeners;
100 private List<IPostUIListener> postUIListeners;
101 private List<IObserveUIListener> observeUIListeners;
102 private List<IVerificationUIListener> verificationUIListeners;
103 private List<IConfigurationUpload> configUploadUIListeners;
104 private List<IDevicePlatformInfoUIListener> devicePlatformInfoUIListeners;
106 // Map with Server ID as key and the complete object as the value
107 private Map<String, RemoteResource> resourceMap;
108 private List<RemoteResource> favoriteResources;
109 // Maintaining a list of resource URIs for favorite resources feature.
110 private List<String> favoriteURIList;
112 // Maintaining a list of observed resource URIs.
113 private List<String> observedResourceURIList;
115 private DeviceInfo devInfo;
116 private PlatformInfo platInfo;
118 public ResourceManager() {
119 resourceMap = new HashMap<String, RemoteResource>();
120 favoriteResources = new ArrayList<RemoteResource>();
121 favoriteURIList = new ArrayList<String>();
122 observedResourceURIList = new ArrayList<String>();
123 findResourceUIListeners = new ArrayList<IFindResourceUIListener>();
124 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
125 getUIListeners = new ArrayList<IGetUIListener>();
126 putUIListeners = new ArrayList<IPutUIListener>();
127 postUIListeners = new ArrayList<IPostUIListener>();
128 observeUIListeners = new ArrayList<IObserveUIListener>();
129 verificationUIListeners = new ArrayList<IVerificationUIListener>();
130 configUploadUIListeners = new ArrayList<IConfigurationUpload>();
131 devicePlatformInfoUIListeners = new ArrayList<IDevicePlatformInfoUIListener>();
133 findResourceListener = new FindResourceListener() {
136 public void onResourceFound(final SimulatorRemoteResource resourceN) {
137 synchronizerThread.addToQueue(new Runnable() {
140 if (null == resourceN) {
144 // If id is not available, then it cannot be added to
145 // the local map as null value should not be allowed as
147 String uid = resourceN.getId();
152 // If resource already exist, then ignore it.
153 boolean exist = isUidExist(uid);
158 // Fetch the resource data
159 // RemoteResource resource =
160 // fetchResourceDetails(resourceN);
161 RemoteResource resource = new RemoteResource();
162 resource.setRemoteResourceRef(resourceN);
164 String uri = resourceN.getURI();
165 if (null != uri && uri.trim().length() > 0) {
166 // Add resource to favorite list if it was in
167 // favorites list during find/refresh operation.
168 if (favoriteURIList.contains(uri)) {
169 addResourcetoFavorites(resource);
171 // Add resource to observed resources list if it was
172 // in observe list during find/refresh operation.
173 if (observedResourceURIList.contains(uri)) {
174 sendObserveRequest(resource);
180 .log(Level.INFO.ordinal(), new Date(),
181 "Found a resource without URI. Ignoring it.");
185 // Add the resource in local data structure
186 addResourceDetails(resource);
188 // Notify the UI listener
189 newResourceFoundNotification(resource);
194 .log(Level.INFO.ordinal(),
196 "Resource Found [" + resourceN.getURI()
199 // Send an initial GET request to get the resource
202 resourceN.get(null, getListener);
203 } catch (SimulatorException e) {
207 .log(Level.ERROR.ordinal(),
209 Utility.getSimulatorErrorString(e,
213 // Get the device and platform information
215 SimulatorManager.findDevices(resource
216 .getRemoteResourceRef().getHost(),
218 SimulatorManager.getPlatformInformation(resource
219 .getRemoteResourceRef().getHost(),
221 } catch (SimulatorException e) {
225 .log(Level.ERROR.ordinal(),
227 Utility.getSimulatorErrorString(e,
235 // TODO: Listeners for device and platform information.
236 deviceListener = new DeviceListener() {
239 public void onDeviceFound(final DeviceInfo deviceInfo) {
240 if (null == deviceInfo) {
243 synchronizerThread.addToQueue(new Runnable() {
246 setDeviceInfo(deviceInfo);
248 // Notify UI listeners
249 deviceInfoReceivedNotification();
255 platformListener = new PlatformListener() {
258 public void onPlatformFound(final PlatformInfo platformInfo) {
259 if (null == platformInfo) {
262 synchronizerThread.addToQueue(new Runnable() {
265 setPlatformInfo(platformInfo);
267 // Notify UI listeners
268 platformInfoReceivedNotification();
274 getListener = new GetResponseListener() {
276 public void onGetResponse(final String uid,
277 final SimulatorResult result,
278 final SimulatorResourceModel resourceModelN) {
279 System.out.println(result);
280 if (result != SimulatorResult.SIMULATOR_OK) {
284 .log(Level.ERROR.ordinal(),
288 + "] Received error response for GET request.");
291 synchronizerThread.addToQueue(new Runnable() {
294 // Handling the response which includes retrieving the
295 // attributes and updating the local model.
296 RemoteResource resource = handleResponse(uid,
298 if (null != resource) {
299 // Notify the UI listeners
300 getCompleteNotification(resource);
307 putListener = new PutResponseListener() {
310 public void onPutResponse(final String uid,
311 final SimulatorResult result,
312 final SimulatorResourceModel resourceModelN) {
313 if (result != SimulatorResult.SIMULATOR_OK) {
317 .log(Level.ERROR.ordinal(),
321 + "] Received error response for PUT request.");
324 synchronizerThread.addToQueue(new Thread() {
327 // Handling the response which includes retrieving the
328 // attributes and updating the local model.
329 RemoteResource resource = handleResponse(uid,
331 if (null != resource) {
332 // Notify the UI listeners
333 putCompleteNotification(resource);
340 postListener = new PostResponseListener() {
342 public void onPostResponse(final String uid,
343 final SimulatorResult result,
344 final SimulatorResourceModel resourceModelN) {
345 if (result != SimulatorResult.SIMULATOR_OK) {
349 .log(Level.ERROR.ordinal(),
353 + "] Received error response for POST request.");
356 synchronizerThread.addToQueue(new Runnable() {
359 // Handling the response which includes retrieving the
360 // attributes and updating the local model.
361 RemoteResource resource = handleResponse(uid,
363 if (null != resource) {
364 // Notify the UI listeners
365 postCompleteNotification(resource);
372 observeListener = new ObserveNotificationListener() {
375 public void onObserveNotification(final String uid,
376 final SimulatorResourceModel resourceModelN, final int seq) {
377 synchronizerThread.addToQueue(new Runnable() {
380 // Handling the response which includes retrieving the
381 // attributes and updating the local model.
382 RemoteResource resource = handleResponse(uid,
384 if (null != resource) {
385 // Notify the UI listeners
386 observeCompleteNotification(resource);
393 verifyListener = new VerificationListener() {
396 public void onVerificationStarted(final String uid, final int autoId) {
397 synchronizerThread.addToQueue(new Runnable() {
400 RemoteResource resource = getResource(uid);
401 if (null == resource) {
404 // Update the automation status.
405 resource.updateAutomationStatus(autoId, true);
407 int autoType = resource.getAutomationtype(autoId);
409 // Notify the listeners.
410 verificationStartedNotification(resource, autoType);
416 public void onVerificationCompleted(final String uid,
418 synchronizerThread.addToQueue(new Runnable() {
421 RemoteResource resource = getResource(uid);
422 if (null == resource) {
425 // Update the automation status.
426 resource.updateAutomationStatus(autoId, false);
428 int autoType = resource.getAutomationtype(autoId);
430 // Notify the listeners.
431 verificationCompletedNotification(resource, autoType);
437 public void onVerificationAborted(final String uid, final int autoId) {
438 synchronizerThread.addToQueue(new Runnable() {
441 RemoteResource resource = getResource(uid);
442 if (null == resource) {
445 // Update the automation status.
446 resource.updateAutomationStatus(autoId, false);
448 int autoType = resource.getAutomationtype(autoId);
450 // Notify the listeners.
451 verificationAbortedNotification(resource, autoType);
457 synchronizerThread = new ResponseSynchronizerThread();
458 threadHandle = new Thread(synchronizerThread);
459 threadHandle.setName("Simulator Client Controller Event Queue");
460 threadHandle.start();
463 private RemoteResource handleResponse(String uid,
464 SimulatorResourceModel resourceModelN) {
465 if (null == uid || null == resourceModelN) {
469 // Update the local model
470 RemoteResource resource;
471 resource = getResource(uid);
472 if (null == resource) {
476 // if(!resource.isConfigUploaded() || null ==
477 // resource.getResourceModelRef())
478 SimulatorResourceModel resourceModel = resource.getResourceModelRef();
479 if (null == resourceModel) {
480 resource.setResourceModelRef(resourceModelN);
482 resourceModel.update(resourceModelN);
485 resource.setResourceRepresentation(resourceModelN, false);
490 public synchronized DeviceInfo getDeviceInfo() {
494 public synchronized void setDeviceInfo(DeviceInfo devInfo) {
495 this.devInfo = devInfo;
498 public synchronized PlatformInfo getPlatformInfo() {
502 public synchronized void setPlatformInfo(PlatformInfo platInfo) {
503 this.platInfo = platInfo;
506 private static class ResponseSynchronizerThread implements Runnable {
508 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
512 while (!Thread.interrupted()) {
513 synchronized (this) {
515 while (responseQueue.isEmpty()) {
519 } catch (InterruptedException e) {
525 synchronized (this) {
526 thread = responseQueue.pop();
530 } catch (Exception e) {
531 if (e instanceof InterruptedException) {
539 public void addToQueue(Runnable event) {
540 synchronized (this) {
541 responseQueue.add(event);
547 public void addResourceSelectionChangedUIListener(
548 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
549 synchronized (resourceSelectionChangedUIListeners) {
550 resourceSelectionChangedUIListeners
551 .add(resourceSelectionChangedUIListener);
555 public void addGetUIListener(IGetUIListener getUIListener) {
556 synchronized (getUIListeners) {
557 getUIListeners.add(getUIListener);
561 public void addPutUIListener(IPutUIListener putUIListener) {
562 synchronized (putUIListeners) {
563 putUIListeners.add(putUIListener);
567 public void addPostUIListener(IPostUIListener postUIListener) {
568 synchronized (postUIListeners) {
569 postUIListeners.add(postUIListener);
573 public void addObserveUIListener(IObserveUIListener observeUIListener) {
574 synchronized (observeUIListeners) {
575 observeUIListeners.add(observeUIListener);
579 public void addVerificationUIListener(
580 IVerificationUIListener verificationUIListener) {
581 synchronized (verificationUIListeners) {
582 verificationUIListeners.add(verificationUIListener);
586 public void addConfigUploadUIListener(IConfigurationUpload configListener) {
587 synchronized (configUploadUIListeners) {
588 configUploadUIListeners.add(configListener);
592 public void addDevicePlatformInfoUIListener(
593 IDevicePlatformInfoUIListener deviceUIListener) {
594 synchronized (devicePlatformInfoUIListeners) {
595 devicePlatformInfoUIListeners.add(deviceUIListener);
599 public void removeDevicePlatformInfoUIListener(
600 IDevicePlatformInfoUIListener platformUIListener) {
601 synchronized (devicePlatformInfoUIListeners) {
602 devicePlatformInfoUIListeners.remove(platformUIListener);
606 public void removeResourceSelectionChangedUIListener(
607 IResourceSelectionChangedUIListener listener) {
608 synchronized (resourceSelectionChangedUIListeners) {
610 && resourceSelectionChangedUIListeners.size() > 0) {
611 resourceSelectionChangedUIListeners.remove(listener);
616 public void removeGetUIListener(IGetUIListener getUIListener) {
617 synchronized (getUIListeners) {
618 getUIListeners.remove(getUIListener);
622 public void removePutUIListener(IPutUIListener putUIListener) {
623 synchronized (putUIListeners) {
624 putUIListeners.remove(putUIListener);
628 public void removePostUIListener(IPostUIListener postUIListener) {
629 synchronized (postUIListeners) {
630 postUIListeners.remove(postUIListener);
634 public void removeObserveUIListener(IObserveUIListener observeUIListener) {
635 synchronized (observeUIListeners) {
636 observeUIListeners.remove(observeUIListener);
640 public void removeVerificationUIListener(
641 IVerificationUIListener verificationUIListener) {
642 synchronized (verificationUIListeners) {
643 verificationUIListeners.remove(verificationUIListener);
647 public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
648 synchronized (configUploadUIListeners) {
649 configUploadUIListeners.remove(configListener);
653 public void addResourcetoFavorites(RemoteResource resource) {
654 if (null == resource) {
657 resource.setFavorite(true);
658 synchronized (favoriteResources) {
659 favoriteResources.add(resource);
663 public void removeResourceFromFavorites(RemoteResource resource) {
664 if (null == resource) {
667 resource.setFavorite(false);
668 synchronized (favoriteResources) {
669 favoriteResources.remove(resource);
673 public void addResourceURItoFavorites(RemoteResource resource) {
674 if (null == resource) {
677 synchronized (favoriteURIList) {
678 favoriteURIList.add(resource.getRemoteResourceRef().getURI());
682 public void removeResourceURIFromFavorites(RemoteResource resource) {
683 if (null == resource) {
686 synchronized (favoriteURIList) {
687 favoriteURIList.remove(resource.getRemoteResourceRef().getURI());
691 public void addObservedResourceURI(String resourceURI) {
692 synchronized (observedResourceURIList) {
693 if (!observedResourceURIList.contains(resourceURI))
694 observedResourceURIList.add(resourceURI);
698 public void removeObservedResourceURI(String resourceURI) {
699 synchronized (observedResourceURIList) {
700 observedResourceURIList.remove(resourceURI);
704 public boolean isResourceObserved(String resourceURI) {
705 boolean observed = false;
706 synchronized (observedResourceURIList) {
707 observed = observedResourceURIList.contains(resourceURI);
712 public synchronized RemoteResource getCurrentResourceInSelection() {
713 return currentResourceInSelection;
716 public synchronized void setCurrentResourceInSelection(
717 RemoteResource resource) {
718 this.currentResourceInSelection = resource;
721 private void addResourceDetails(RemoteResource remoteResource) {
722 if (null != remoteResource) {
723 synchronized (resourceMap) {
724 resourceMap.put(remoteResource.getRemoteResourceRef().getId(),
730 public void addFindresourceUIListener(IFindResourceUIListener listener) {
731 if (null == listener) {
734 synchronized (findResourceUIListeners) {
735 findResourceUIListeners.add(listener);
739 public void removeFindresourceUIListener(IFindResourceUIListener listener) {
740 if (null == listener) {
743 synchronized (findResourceUIListeners) {
744 findResourceUIListeners.remove(listener);
748 private boolean isUidExist(String uid) {
750 synchronized (resourceMap) {
751 exist = resourceMap.containsKey(uid);
756 private RemoteResource getResource(String uid) {
760 RemoteResource resource;
761 synchronized (resourceMap) {
762 resource = resourceMap.get(uid);
767 private void newResourceFoundNotification(RemoteResource resource) {
768 synchronized (findResourceUIListeners) {
769 if (findResourceUIListeners.size() > 0) {
770 IFindResourceUIListener listener;
771 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
773 while (listenerItr.hasNext()) {
774 listener = listenerItr.next();
775 if (null != listener) {
776 listener.onNewResourceFound(resource);
783 private void resourceSelectionChangedUINotification(RemoteResource resource) {
784 synchronized (resourceSelectionChangedUIListeners) {
785 if (resourceSelectionChangedUIListeners.size() > 0) {
786 IResourceSelectionChangedUIListener listener;
787 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
789 while (listenerItr.hasNext()) {
790 listener = listenerItr.next();
791 if (null != listener) {
792 listener.onResourceSelectionChange(resource);
799 private void getCompleteNotification(RemoteResource resource) {
800 synchronized (getUIListeners) {
801 if (getUIListeners.size() > 0) {
802 IGetUIListener listener;
803 Iterator<IGetUIListener> listenerItr = getUIListeners
805 while (listenerItr.hasNext()) {
806 listener = listenerItr.next();
807 if (null != listener) {
808 listener.onGetCompleted(resource);
815 private void putCompleteNotification(RemoteResource resource) {
816 synchronized (putUIListeners) {
817 if (putUIListeners.size() > 0) {
818 IPutUIListener listener;
819 Iterator<IPutUIListener> listenerItr = putUIListeners
821 while (listenerItr.hasNext()) {
822 listener = listenerItr.next();
823 if (null != listener) {
824 listener.onPutCompleted(resource);
831 private void postCompleteNotification(RemoteResource resource) {
832 synchronized (postUIListeners) {
833 if (postUIListeners.size() > 0) {
834 IPostUIListener listener;
835 Iterator<IPostUIListener> listenerItr = postUIListeners
837 while (listenerItr.hasNext()) {
838 listener = listenerItr.next();
839 if (null != listener) {
840 listener.onPostCompleted(resource);
847 private void observeCompleteNotification(RemoteResource resource) {
848 synchronized (observeUIListeners) {
849 if (observeUIListeners.size() > 0) {
850 IObserveUIListener listener;
851 Iterator<IObserveUIListener> listenerItr = observeUIListeners
853 while (listenerItr.hasNext()) {
854 listener = listenerItr.next();
855 if (null != listener) {
856 listener.onObserveCompleted(resource);
863 private void verificationStartedNotification(RemoteResource resource,
865 synchronized (verificationUIListeners) {
866 if (verificationUIListeners.size() > 0) {
867 IVerificationUIListener listener;
868 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
870 while (listenerItr.hasNext()) {
871 listener = listenerItr.next();
872 if (null != listener) {
873 listener.onVerificationStarted(resource, autoType);
880 private void verificationAbortedNotification(RemoteResource resource,
882 synchronized (verificationUIListeners) {
883 if (verificationUIListeners.size() > 0) {
884 IVerificationUIListener listener;
885 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
887 while (listenerItr.hasNext()) {
888 listener = listenerItr.next();
889 if (null != listener) {
890 listener.onVerificationAborted(resource, autoType);
897 private void verificationCompletedNotification(RemoteResource resource,
899 synchronized (verificationUIListeners) {
900 if (verificationUIListeners.size() > 0) {
901 IVerificationUIListener listener;
902 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
904 while (listenerItr.hasNext()) {
905 listener = listenerItr.next();
906 if (null != listener) {
907 listener.onVerificationCompleted(resource, autoType);
914 private void configUploadedNotification(RemoteResource resource) {
915 synchronized (configUploadUIListeners) {
916 if (configUploadUIListeners.size() > 0) {
917 IConfigurationUpload listener;
918 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
920 while (listenerItr.hasNext()) {
921 listener = listenerItr.next();
922 if (null != listener) {
923 listener.onConfigurationUploaded(resource);
930 private void deviceInfoReceivedNotification() {
931 synchronized (devicePlatformInfoUIListeners) {
932 if (devicePlatformInfoUIListeners.size() > 0) {
933 IDevicePlatformInfoUIListener listener;
934 Iterator<IDevicePlatformInfoUIListener> listenerItr = devicePlatformInfoUIListeners
936 while (listenerItr.hasNext()) {
937 listener = listenerItr.next();
938 if (null != listener) {
939 listener.onDeviceInfoFound();
946 private void platformInfoReceivedNotification() {
947 synchronized (devicePlatformInfoUIListeners) {
948 if (devicePlatformInfoUIListeners.size() > 0) {
949 IDevicePlatformInfoUIListener listener;
950 Iterator<IDevicePlatformInfoUIListener> listenerItr = devicePlatformInfoUIListeners
952 while (listenerItr.hasNext()) {
953 listener = listenerItr.next();
954 if (null != listener) {
955 listener.onPlatformInfoFound();
962 public synchronized Set<String> getLastKnownSearchTypes() {
963 return lastKnownSearchTypes;
966 public synchronized void setLastKnownSearchTypes(
967 Set<String> lastKnownSearchTypes) {
968 this.lastKnownSearchTypes = lastKnownSearchTypes;
971 public boolean findResourceRequest(Set<String> searchTypes) {
972 boolean result = false;
973 if (null == searchTypes || searchTypes.size() < 1) {
975 SimulatorManager.findResource(findResourceListener);
977 } catch (SimulatorException e) {
981 .log(Level.ERROR.ordinal(), new Date(),
982 Utility.getSimulatorErrorString(e, null));
985 Iterator<String> searchItr = searchTypes.iterator();
987 while (searchItr.hasNext()) {
988 rType = searchItr.next();
990 SimulatorManager.findResource(rType, findResourceListener);
992 } catch (SimulatorException e) {
996 .log(Level.ERROR.ordinal(), new Date(),
997 Utility.getSimulatorErrorString(e, null));
1004 public void deleteResources(final Set<String> searchTypes) {
1005 synchronized (resourceMap) {
1006 if (null == resourceMap && resourceMap.isEmpty()) {
1012 if (null == searchTypes || searchTypes.size() < 1) {
1013 synchronized (resourceMap) {
1014 // Stop observing all the resources
1015 Iterator<String> itr = resourceMap.keySet().iterator();
1016 while (itr.hasNext()) {
1017 sendCancelObserveRequest(
1018 resourceMap.get(itr.next()), false);
1020 // Delete all cached details of resources
1021 resourceMap.clear();
1022 favoriteResources.clear();
1024 // Change the current resource in selection
1025 setCurrentResourceInSelection(null);
1026 resourceSelectionChangedUINotification(null);
1028 Iterator<String> typeItr = searchTypes.iterator();
1030 while (typeItr.hasNext()) {
1031 resType = typeItr.next();
1032 deleteResourcesByType(resType);
1034 // Change the current resource in selection
1035 updateCurrentResourceInSelection(searchTypes);
1042 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
1043 if (null == searchTypes || searchTypes.size() < 1) {
1046 RemoteResource resourceInSelection = getCurrentResourceInSelection();
1047 if (null == resourceInSelection) {
1050 List<String> typesOfSelection = resourceInSelection
1051 .getRemoteResourceRef().getResourceTypes();
1052 if (null == typesOfSelection || typesOfSelection.size() < 1) {
1055 Iterator<String> itr = typesOfSelection.iterator();
1057 while (itr.hasNext()) {
1059 if (searchTypes.contains(type)) {
1060 setCurrentResourceInSelection(null);
1061 resourceSelectionChangedUINotification(null);
1067 private void deleteResourcesByType(String resourceType) {
1068 if (null == resourceType) {
1071 synchronized (resourceMap) {
1072 Set<String> keySet = resourceMap.keySet();
1073 if (null == keySet) {
1076 Iterator<String> keyItr = keySet.iterator();
1078 RemoteResource resource;
1081 while (keyItr.hasNext()) {
1082 uId = keyItr.next();
1083 resource = resourceMap.get(uId);
1084 if (null == resource) {
1087 types = resource.getRemoteResourceRef().getResourceTypes();
1088 if (null != types) {
1089 exist = types.contains(resourceType);
1091 // Cancel observing the resource.
1092 sendCancelObserveRequest(resource, false);
1093 // Remove the resource from favorites list.
1094 removeResourceFromFavorites(resource);
1095 // Remove the resource
1103 public void resourceSelectionChanged(final RemoteResource resource) {
1107 setCurrentResourceInSelection(resource);
1108 // Notify all observers for resource selection change event
1109 resourceSelectionChangedUINotification(resource);
1114 public List<MetaProperty> getDefaultProperties(RemoteResource resource) {
1115 if (null != resource) {
1119 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1121 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1122 propName = Constants.META_PROPERTIES[index];
1123 if (propName.equals(Constants.RESOURCE_URI)) {
1124 propValue = resource.getRemoteResourceRef().getURI();
1125 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1126 propValue = resource.getRemoteResourceRef()
1127 .getConnectivityType().toString();
1128 } else if (propName.equals(Constants.ADDRESS)) {
1129 propValue = resource.getRemoteResourceRef().getHost();
1130 } else if (propName.equals(Constants.OBSERVABLE)) {
1131 propValue = Utility.getObservableInString(resource
1132 .getRemoteResourceRef().isObservable());
1133 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1134 Vector<String> types = resource.getRemoteResourceRef()
1135 .getResourceTypes();
1136 if (null != types) {
1137 propValue = types.toString();
1139 propValue = Constants.NOT_AVAILABLE;
1141 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1142 Vector<String> interfaces = resource.getRemoteResourceRef()
1143 .getResourceInterfaces();
1144 if (null != interfaces) {
1145 propValue = interfaces.toString();
1147 propValue = Constants.NOT_AVAILABLE;
1152 if (null != propValue) {
1153 metaPropertyList.add(new MetaProperty(propName, propValue));
1157 return metaPropertyList;
1162 public List<MetaProperty> getDeviceProperties() {
1163 if (null == devInfo) {
1167 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
1169 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, devInfo
1171 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, devInfo
1173 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
1174 devInfo.getSpecVersion()));
1175 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV_VERSION,
1176 devInfo.getDataModelVersion()));
1179 * metaProperties.add(new MetaProperty(Constants.DEVICE_ID, ""));
1180 * metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, ""));
1181 * metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
1182 * "")); metaProperties.add(new
1183 * MetaProperty(Constants.DEVICE_DMV_VERSION, ""));
1185 return metaProperties;
1188 public List<MetaProperty> getPlatformProperties() {
1189 if (null == platInfo) {
1193 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
1195 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platInfo
1197 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
1198 platInfo.getManufacturerName()));
1199 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
1200 platInfo.getManufacturerUrl()));
1201 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
1202 platInfo.getModelNumber()));
1203 metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
1204 platInfo.getDateOfManufacture()));
1205 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
1206 platInfo.getPlatformVersion()));
1207 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1208 platInfo.getOperationSystemVersion()));
1209 metaProperties.add(new MetaProperty(
1210 Constants.PLATFORM_HARDWARE_VERSION, platInfo
1211 .getHardwareVersion()));
1212 metaProperties.add(new MetaProperty(
1213 Constants.PLATFORM_FIRMWARE_VERSION, platInfo
1214 .getFirmwareVersion()));
1215 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1216 platInfo.getSupportUrl()));
1217 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
1218 platInfo.getSystemTime()));
1221 * metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, ""));
1222 * metaProperties .add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
1223 * "")); metaProperties .add(new
1224 * MetaProperty(Constants.PLATFORM_MANUFAC_URL, ""));
1225 * metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
1226 * "")); metaProperties.add(new
1227 * MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC, ""));
1228 * metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION, ""));
1229 * metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1230 * "")); metaProperties.add(new MetaProperty(
1231 * Constants.PLATFORM_HARDWARE_VERSION, "")); metaProperties.add(new
1232 * MetaProperty( Constants.PLATFORM_FIRMWARE_VERSION, ""));
1233 * metaProperties .add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1234 * "")); metaProperties .add(new
1235 * MetaProperty(Constants.PLATFORM_SYSTEM_TIME, ""));
1237 return metaProperties;
1240 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1241 if (null == resource) {
1244 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1245 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1246 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1247 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1251 public List<RemoteResource> getResourceList() {
1252 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1253 synchronized (resourceMap) {
1254 Set<String> idSet = resourceMap.keySet();
1255 Iterator<String> idItr = idSet.iterator();
1256 RemoteResource resource;
1257 while (idItr.hasNext()) {
1258 resource = resourceMap.get(idItr.next());
1259 if (null != resource) {
1260 resourceList.add(resource);
1265 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1266 public int compare(RemoteResource res1, RemoteResource res2) {
1267 String s1 = res1.getRemoteResourceRef().getURI();
1268 String s2 = res2.getRemoteResourceRef().getURI();
1270 String s1Part = s1.replaceAll("\\d", "");
1271 String s2Part = s2.replaceAll("\\d", "");
1273 if (s1Part.equalsIgnoreCase(s2Part)) {
1274 return extractInt(s1) - extractInt(s2);
1276 return s1.compareTo(s2);
1279 int extractInt(String s) {
1280 String num = s.replaceAll("\\D", "");
1281 // return 0 if no digits found
1282 return num.isEmpty() ? 0 : Integer.parseInt(num);
1286 return resourceList;
1289 public List<RemoteResource> getFavResourceList() {
1290 List<RemoteResource> resourceList;
1291 synchronized (favoriteResources) {
1292 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1294 return resourceList;
1297 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1302 AttributeValue val = att.value();
1307 List<String> values = new ArrayList<String>();
1309 TypeInfo type = val.typeInfo();
1311 AttributeProperty prop = att.property();
1312 if (null == prop || prop.type().ordinal() == Type.UNKNOWN.ordinal()) {
1313 values.add(Utility.getAttributeValueAsString(val));
1317 Type valuesType = prop.type();
1319 if (type.mType != ValueType.RESOURCEMODEL) {
1320 if (type.mType == ValueType.ARRAY) {
1321 if (type.mDepth == 1) {
1322 AttributeProperty childProp = prop.getChildProperty();
1323 if (null != childProp) {
1324 valuesType = childProp.type();
1325 if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1326 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1327 childProp, type.mBaseType);
1329 values.addAll(list);
1331 } else if (valuesType.ordinal() == Type.VALUESET
1333 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1334 childProp.valueSet(), type.mBaseType);
1336 values.addAll(list);
1342 if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1343 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1346 values.addAll(list);
1348 } else if (valuesType.ordinal() == Type.VALUESET.ordinal()) {
1349 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1350 prop.valueSet(), type.mType);
1352 values.addAll(list);
1358 if (values.isEmpty()) {
1359 values.add(Utility.getAttributeValueAsString(val));
1365 public List<String> getRangeForPrimitiveNonArrayAttributes(
1366 AttributeProperty prop, ValueType type) {
1371 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1375 List<String> values = new ArrayList<String>();
1378 int min = (int) prop.min();
1379 int max = (int) prop.max();
1380 for (int iVal = min; iVal <= max; iVal++) {
1381 values.add(String.valueOf(iVal));
1385 double minD = (double) prop.min();
1386 double maxD = (double) prop.max();
1387 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
1388 values.add(String.valueOf(iVal));
1396 public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
1397 AttributeValue[] attValues, ValueType type) {
1398 if (null == attValues || attValues.length < 1) {
1402 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1407 List<String> values = new ArrayList<String>();
1408 for (AttributeValue val : attValues) {
1416 values.add(String.valueOf(obj));
1422 * public String getAttributeValue(RemoteResource res, String attName) { if
1423 * (null == res || null == attName) { return null; } return
1424 * res.getAttributeValue(attName); }
1426 public void sendGetRequest(RemoteResource resource) {
1427 if (null == resource) {
1430 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1431 if (null == resourceN) {
1435 resourceN.get(null, getListener);
1436 } catch (SimulatorException e) {
1440 .log(Level.ERROR.ordinal(), new Date(),
1441 Utility.getSimulatorErrorString(e, null));
1445 public void sendPutRequest(RemoteResource resource,
1446 SimulatorResourceModel model) {
1447 if (null == resource || null == model) {
1450 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1451 if (null == resourceN) {
1455 resourceN.put(null, model, putListener);
1456 } catch (Exception e) {
1458 addlInfo = "Invalid Attribute Value. Cannot send PUT request.";
1462 .log(Level.ERROR.ordinal(), new Date(),
1463 Utility.getSimulatorErrorString(e, addlInfo));
1467 public void sendPostRequest(RemoteResource resource,
1468 SimulatorResourceModel model) {
1469 if (null == resource || null == model) {
1472 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1473 if (null == resourceN) {
1477 resourceN.post(null, model, postListener);
1478 } catch (Exception e) {
1480 addlInfo = "Invalid Attribute Value. Cannot send POST request.";
1484 .log(Level.ERROR.ordinal(), new Date(),
1485 Utility.getSimulatorErrorString(e, addlInfo));
1489 public boolean sendObserveRequest(RemoteResource resource) {
1490 if (null == resource) {
1493 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1494 if (null == resourceN) {
1498 resourceN.startObserve(null, observeListener);
1499 resource.setObserved(true);
1500 // Add observed resource URI to show the proper status after every
1501 // find/refresh operations.
1502 addObservedResourceURI(resource.getRemoteResourceRef().getURI());
1503 } catch (SimulatorException e) {
1507 .log(Level.ERROR.ordinal(), new Date(),
1508 Utility.getSimulatorErrorString(e, null));
1514 public boolean sendCancelObserveRequest(RemoteResource resource,
1515 boolean removeEntry) {
1516 if (null == resource || !resource.isObserved()) {
1519 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1520 if (null == resourceN) {
1524 resourceN.stopObserve();
1525 resource.setObserved(false);
1526 // Remove observed resource URI to show the proper status after
1527 // every find/refresh operations.
1529 removeObservedResourceURI(resource.getRemoteResourceRef()
1531 } catch (SimulatorException e) {
1535 .log(Level.ERROR.ordinal(), new Date(),
1536 Utility.getSimulatorErrorString(e, null));
1542 public void startAutomationRequest(VerificationType reqType,
1543 RemoteResource resource) {
1544 if (null == resource) {
1547 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1548 if (null == resourceN) {
1551 if (null == reqType) {
1556 autoId = resourceN.startVerification(reqType, verifyListener);
1558 if (reqType == VerificationType.GET) {
1559 // resource.setGetAutomtnInProgress(true);
1560 resource.setGetAutomtnId(autoId);
1561 } else if (reqType == VerificationType.PUT) {
1562 // resource.setPutAutomtnInProgress(true);
1563 resource.setPutAutomtnId(autoId);
1564 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1565 // resource.setPostAutomtnInProgress(true);
1566 resource.setPostAutomtnId(autoId);
1569 } catch (SimulatorException e) {
1573 .log(Level.ERROR.ordinal(), new Date(),
1574 Utility.getSimulatorErrorString(e, null));
1578 public void stopAutomationRequest(VerificationType reqType,
1579 RemoteResource resource) {
1580 if (null == resource) {
1583 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1584 if (null == resourceN) {
1588 if (reqType == VerificationType.GET) {
1589 resource.setGetAutomtnInProgress(false);
1590 autoId = resource.getGetAutomtnId();
1591 } else if (reqType == VerificationType.PUT) {
1592 resource.setPutAutomtnInProgress(false);
1593 autoId = resource.getPutAutomtnId();
1594 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1595 resource.setPostAutomtnInProgress(false);
1596 autoId = resource.getPostAutomtnId();
1599 resourceN.stopVerification(autoId);
1600 } catch (SimulatorException e) {
1604 .log(Level.ERROR.ordinal(), new Date(),
1605 Utility.getSimulatorErrorString(e, null));
1609 public boolean setConfigFilePath(RemoteResource resource,
1610 String configFilePath) throws SimulatorException {
1611 if (null == resource) {
1614 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1615 if (null == resourceN) {
1619 SimulatorResourceModel configuredResourceModel;
1620 configuredResourceModel = resourceN.setConfigInfo(configFilePath);
1621 if (null == configuredResourceModel) {
1625 // Store the resource model in the local cache
1626 SimulatorResourceModel resourceModel = resource
1627 .getResourceModelRef();
1628 if (null != resourceModel) {
1629 configuredResourceModel.update(resourceModel);
1631 resource.setResourceModelRef(configuredResourceModel);
1632 } catch (SimulatorException e) {
1636 .log(Level.ERROR.ordinal(), new Date(),
1637 Utility.getSimulatorErrorString(e, null));
1640 // Update the status
1641 resource.setConfigUploaded(true);
1643 // Notify the UI listeners
1644 configUploadedNotification(resource);
1649 public void shutdown() {