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.text.NumberFormat;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
30 import java.util.Vector;
32 import org.oic.simulator.ArrayProperty;
33 import org.oic.simulator.AttributeProperty;
34 import org.oic.simulator.AttributeProperty.Type;
35 import org.oic.simulator.AttributeValue;
36 import org.oic.simulator.AttributeValue.TypeInfo;
37 import org.oic.simulator.AttributeValue.ValueType;
38 import org.oic.simulator.BooleanProperty;
39 import org.oic.simulator.DeviceInfo;
40 import org.oic.simulator.DeviceListener;
41 import org.oic.simulator.DoubleProperty;
42 import org.oic.simulator.ILogger.Level;
43 import org.oic.simulator.IntegerProperty;
44 import org.oic.simulator.PlatformInfo;
45 import org.oic.simulator.PlatformListener;
46 import org.oic.simulator.SimulatorException;
47 import org.oic.simulator.SimulatorManager;
48 import org.oic.simulator.SimulatorResourceAttribute;
49 import org.oic.simulator.SimulatorResourceModel;
50 import org.oic.simulator.SimulatorResult;
51 import org.oic.simulator.StringProperty;
52 import org.oic.simulator.client.FindResourceListener;
53 import org.oic.simulator.client.SimulatorRemoteResource;
54 import org.oic.simulator.client.SimulatorRemoteResource.GetResponseListener;
55 import org.oic.simulator.client.SimulatorRemoteResource.ObserveNotificationListener;
56 import org.oic.simulator.client.SimulatorRemoteResource.PostResponseListener;
57 import org.oic.simulator.client.SimulatorRemoteResource.PutResponseListener;
58 import org.oic.simulator.client.SimulatorRemoteResource.RequestType;
59 import org.oic.simulator.client.SimulatorRemoteResource.VerificationListener;
60 import org.oic.simulator.client.SimulatorRequestModel;
62 import oic.simulator.clientcontroller.Activator;
63 import oic.simulator.clientcontroller.remoteresource.DeviceAndPlatformInfo;
64 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
65 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
66 import oic.simulator.clientcontroller.utils.AttributeValueStringConverter;
67 import oic.simulator.clientcontroller.utils.Constants;
68 import oic.simulator.clientcontroller.utils.Utility;
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 // Map with Server ID as key and the complete object as the value
97 private Map<String, RemoteResource> resourceMap;
98 private List<RemoteResource> favoriteResources;
99 // Maintaining a list of resource URIs for favorite resources feature.
100 private List<String> favoriteURIList;
102 // Maintaining a list of observed resource URIs.
103 private List<String> observedResourceURIList;
105 private Map<String, DeviceAndPlatformInfo> hostDeviceAndPlatformMap;
107 public ResourceManager() {
108 resourceMap = new HashMap<String, RemoteResource>();
109 favoriteResources = new ArrayList<RemoteResource>();
110 favoriteURIList = new ArrayList<String>();
111 observedResourceURIList = new ArrayList<String>();
112 hostDeviceAndPlatformMap = new HashMap<String, DeviceAndPlatformInfo>();
114 findResourceListener = new FindResourceListener() {
117 public void onResourceFound(final SimulatorRemoteResource resourceN) {
118 synchronizerThread.addToQueue(new Runnable() {
121 if (null == resourceN) {
125 // Ignore the response if the resource is a device or
127 Vector<String> resTypes = resourceN.getResourceTypes();
128 if (null != resTypes && resTypes.contains("oic.wk.d")
129 || resTypes.contains("oic.wk.p")) {
133 // If id is not available, then it cannot be added to
134 // the local map as null value should not be allowed as
136 String uid = resourceN.getId();
141 // If resource already exist, then ignore it.
142 boolean exist = isUidExist(uid);
147 RemoteResource resource = new RemoteResource();
148 resource.setRemoteResourceRef(resourceN);
150 String uri = resourceN.getURI();
151 if (null != uri && uri.trim().length() > 0) {
152 // Add resource to favorite list if it was in
153 // favorites list during find/refresh operation.
154 if (favoriteURIList.contains(uri)) {
155 addResourcetoFavorites(resource);
157 // Add resource to observed resources list if it was
158 // in observe list during find/refresh operation.
159 if (observedResourceURIList.contains(uri)) {
160 sendObserveRequest(resource);
166 .log(Level.INFO.ordinal(), new Date(),
167 "Found a resource without URI. Ignoring it.");
171 // Add the resource in local data structure
172 addResourceDetails(resource);
174 // Notify the UI listener
175 UiListenerHandler.getInstance()
176 .newResourceFoundNotification(resource);
181 .log(Level.INFO.ordinal(),
183 "Resource Found [" + resourceN.getURI()
186 // Send an initial GET request to get the resource
187 // attributes on an interface supported by the resource.
189 String ifType = null;
190 Vector<String> resInterfaces = resourceN
191 .getResourceInterfaces();
192 if (null != resInterfaces) {
193 ifType = resInterfaces.get(0);
195 resourceN.get(formQueryParameters(ifType, null),
197 } catch (SimulatorException e) {
201 .log(Level.ERROR.ordinal(),
203 Utility.getSimulatorErrorString(e,
207 // Get the device information
208 if (!isDeviceInfoExist(resourceN.getHost())) {
210 SimulatorManager.findDevices(resource
211 .getRemoteResourceRef().getHost(),
213 } catch (SimulatorException e) {
217 .log(Level.ERROR.ordinal(),
219 Utility.getSimulatorErrorString(
224 // Get the platform information
225 if (!isPlatformInfoExist(resourceN.getHost())) {
227 SimulatorManager.getPlatformInformation(
228 resource.getRemoteResourceRef()
229 .getHost(), platformListener);
230 } catch (SimulatorException e) {
234 .log(Level.ERROR.ordinal(),
236 Utility.getSimulatorErrorString(
245 // Listeners for device and platform information.
246 deviceListener = new DeviceListener() {
249 public void onDeviceFound(final String host,
250 final DeviceInfo deviceInfo) {
251 if (null == deviceInfo || null == host) {
254 synchronizerThread.addToQueue(new Runnable() {
257 synchronized (hostDeviceAndPlatformMap) {
258 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
261 info = new DeviceAndPlatformInfo();
263 hostDeviceAndPlatformMap.put(host, info);
265 info.setDeviceInfo(deviceInfo);
268 // Notify UI listeners
269 UiListenerHandler.getInstance()
270 .deviceInfoReceivedNotification();
276 platformListener = new PlatformListener() {
279 public void onPlatformFound(final String host,
280 final PlatformInfo platformInfo) {
281 if (null == platformInfo || null == host) {
284 synchronizerThread.addToQueue(new Runnable() {
287 synchronized (hostDeviceAndPlatformMap) {
288 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
291 info = new DeviceAndPlatformInfo();
293 hostDeviceAndPlatformMap.put(host, info);
295 info.setPlatformInfo(platformInfo);
298 // Notify UI listeners
299 UiListenerHandler.getInstance()
300 .platformInfoReceivedNotification();
306 getListener = new GetResponseListener() {
308 public void onGetResponse(final String uid,
309 final SimulatorResult result,
310 final SimulatorResourceModel resourceModelN) {
311 if (result != SimulatorResult.SIMULATOR_OK) {
315 .log(Level.ERROR.ordinal(),
319 + "] Received error response for GET request.");
322 synchronizerThread.addToQueue(new Runnable() {
325 // Handling the response which includes retrieving the
326 // attributes and updating the local model.
327 RemoteResource resource = handleResponse(uid,
329 if (null != resource) {
330 // Notify the UI listeners
331 UiListenerHandler.getInstance()
332 .getCompleteNotification(resource);
339 putListener = new PutResponseListener() {
342 public void onPutResponse(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 PUT request.");
356 synchronizerThread.addToQueue(new Thread() {
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 UiListenerHandler.getInstance()
366 .putCompleteNotification(resource);
373 postListener = new PostResponseListener() {
375 public void onPostResponse(final String uid,
376 final SimulatorResult result,
377 final SimulatorResourceModel resourceModelN) {
378 if (result != SimulatorResult.SIMULATOR_OK) {
382 .log(Level.ERROR.ordinal(),
386 + "] Received error response for POST request.");
389 synchronizerThread.addToQueue(new Runnable() {
392 // Handling the response which includes retrieving the
393 // attributes and updating the local model.
394 RemoteResource resource = handleResponse(uid,
396 if (null != resource) {
397 // Notify the UI listeners
398 UiListenerHandler.getInstance()
399 .postCompleteNotification(resource);
406 observeListener = new ObserveNotificationListener() {
409 public void onObserveNotification(final String uid,
410 final SimulatorResourceModel resourceModelN, final int seq) {
411 synchronizerThread.addToQueue(new Runnable() {
414 // Handling the response which includes retrieving the
415 // attributes and updating the local model.
416 RemoteResource resource = handleResponse(uid,
418 if (null != resource) {
419 // Notify the UI listeners
420 UiListenerHandler.getInstance()
421 .observeCompleteNotification(resource);
428 verifyListener = new VerificationListener() {
431 public void onVerificationStarted(final String uid, final int autoId) {
432 synchronizerThread.addToQueue(new Runnable() {
435 RemoteResource resource = getResource(uid);
436 if (null == resource) {
439 // Update the automation status.
440 resource.updateAutomationStatus(autoId, true);
442 RequestType reqType = resource
443 .getAutomationtype(autoId);
445 // Notify the listeners.
446 UiListenerHandler.getInstance()
447 .verificationStartedNotification(resource,
454 public void onVerificationCompleted(final String uid,
456 synchronizerThread.addToQueue(new Runnable() {
459 RemoteResource resource = getResource(uid);
460 if (null == resource) {
463 // Update the automation status.
464 resource.updateAutomationStatus(autoId, false);
466 RequestType reqType = resource
467 .getAutomationtype(autoId);
469 // Notify the listeners.
470 UiListenerHandler.getInstance()
471 .verificationCompletedNotification(resource,
478 public void onVerificationAborted(final String uid, final int autoId) {
479 synchronizerThread.addToQueue(new Runnable() {
482 RemoteResource resource = getResource(uid);
483 if (null == resource) {
486 // Update the automation status.
487 resource.updateAutomationStatus(autoId, false);
489 RequestType reqType = resource
490 .getAutomationtype(autoId);
492 // Notify the listeners.
493 UiListenerHandler.getInstance()
494 .verificationAbortedNotification(resource,
501 synchronizerThread = new ResponseSynchronizerThread();
502 threadHandle = new Thread(synchronizerThread);
503 threadHandle.setName("Simulator Client Controller Event Queue");
504 threadHandle.start();
507 private RemoteResource handleResponse(String uid,
508 SimulatorResourceModel resourceModelN) {
509 if (null == uid || null == resourceModelN) {
513 // Update the local model
514 RemoteResource resource;
515 resource = getResource(uid);
516 if (null == resource) {
520 resource.setResourceModelRef(resourceModelN);
521 resource.setResourceRepresentation(resourceModelN);
526 public synchronized boolean isDeviceInfoExist(String host) {
527 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
531 if (null == info.getDeviceInfo()) {
537 public synchronized boolean isPlatformInfoExist(String host) {
538 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
542 if (null == info.getPlatformInfo()) {
548 private static class ResponseSynchronizerThread implements Runnable {
550 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
554 while (!Thread.interrupted()) {
555 synchronized (this) {
557 while (responseQueue.isEmpty()) {
561 } catch (InterruptedException e) {
567 synchronized (this) {
568 thread = responseQueue.pop();
572 } catch (Exception e) {
573 if (e instanceof InterruptedException) {
581 public void addToQueue(Runnable event) {
582 synchronized (this) {
583 responseQueue.add(event);
589 public void addResourcetoFavorites(RemoteResource resource) {
590 if (null == resource) {
593 resource.setFavorite(true);
594 synchronized (favoriteResources) {
595 favoriteResources.add(resource);
599 public void removeResourceFromFavorites(RemoteResource resource) {
600 if (null == resource) {
603 resource.setFavorite(false);
604 synchronized (favoriteResources) {
605 favoriteResources.remove(resource);
609 public void addResourceURItoFavorites(RemoteResource resource) {
610 if (null == resource) {
613 synchronized (favoriteURIList) {
614 favoriteURIList.add(resource.getRemoteResourceRef().getURI());
618 public void removeResourceURIFromFavorites(RemoteResource resource) {
619 if (null == resource) {
622 synchronized (favoriteURIList) {
623 favoriteURIList.remove(resource.getRemoteResourceRef().getURI());
627 public void addObservedResourceURI(String resourceURI) {
628 synchronized (observedResourceURIList) {
629 if (!observedResourceURIList.contains(resourceURI))
630 observedResourceURIList.add(resourceURI);
634 public void removeObservedResourceURI(String resourceURI) {
635 synchronized (observedResourceURIList) {
636 observedResourceURIList.remove(resourceURI);
640 public boolean isResourceObserved(String resourceURI) {
641 boolean observed = false;
642 synchronized (observedResourceURIList) {
643 observed = observedResourceURIList.contains(resourceURI);
648 public synchronized RemoteResource getCurrentResourceInSelection() {
649 return currentResourceInSelection;
652 public synchronized void setCurrentResourceInSelection(
653 RemoteResource resource) {
654 this.currentResourceInSelection = resource;
657 private void addResourceDetails(RemoteResource remoteResource) {
658 if (null != remoteResource) {
659 synchronized (resourceMap) {
660 resourceMap.put(remoteResource.getRemoteResourceRef().getId(),
666 private boolean isUidExist(String uid) {
668 synchronized (resourceMap) {
669 exist = resourceMap.containsKey(uid);
674 private RemoteResource getResource(String uid) {
678 RemoteResource resource;
679 synchronized (resourceMap) {
680 resource = resourceMap.get(uid);
685 public synchronized Set<String> getLastKnownSearchTypes() {
686 return lastKnownSearchTypes;
689 public synchronized void setLastKnownSearchTypes(
690 Set<String> lastKnownSearchTypes) {
691 this.lastKnownSearchTypes = lastKnownSearchTypes;
694 public boolean findResourceRequest(Set<String> searchTypes) {
695 boolean result = false;
696 if (null == searchTypes || searchTypes.size() < 1) {
698 SimulatorManager.findResource(findResourceListener);
700 } catch (SimulatorException e) {
704 .log(Level.ERROR.ordinal(), new Date(),
705 Utility.getSimulatorErrorString(e, null));
708 Iterator<String> searchItr = searchTypes.iterator();
710 while (searchItr.hasNext()) {
711 rType = searchItr.next();
713 SimulatorManager.findResource(rType, findResourceListener);
715 } catch (SimulatorException e) {
719 .log(Level.ERROR.ordinal(), new Date(),
720 Utility.getSimulatorErrorString(e, null));
727 public void deleteResources(final Set<String> searchTypes) {
728 synchronized (resourceMap) {
729 if (null == resourceMap && resourceMap.isEmpty()) {
735 if (null == searchTypes || searchTypes.size() < 1) {
736 synchronized (resourceMap) {
737 // Stop observing all the resources
738 Iterator<String> itr = resourceMap.keySet().iterator();
739 while (itr.hasNext()) {
740 sendCancelObserveRequest(
741 resourceMap.get(itr.next()), false);
743 // Delete all cached details of resources
745 favoriteResources.clear();
747 // Clearing the device and platform information
748 hostDeviceAndPlatformMap.clear();
750 // Change the current resource in selection
751 setCurrentResourceInSelection(null);
752 UiListenerHandler.getInstance()
753 .resourceSelectionChangedUINotification(null);
755 Iterator<String> typeItr = searchTypes.iterator();
757 while (typeItr.hasNext()) {
758 resType = typeItr.next();
759 deleteResourcesByType(resType);
761 // Change the current resource in selection
762 updateCurrentResourceInSelection(searchTypes);
769 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
770 if (null == searchTypes || searchTypes.size() < 1) {
773 RemoteResource resourceInSelection = getCurrentResourceInSelection();
774 if (null == resourceInSelection) {
777 List<String> typesOfSelection = resourceInSelection
778 .getRemoteResourceRef().getResourceTypes();
779 if (null == typesOfSelection || typesOfSelection.size() < 1) {
782 Iterator<String> itr = typesOfSelection.iterator();
784 while (itr.hasNext()) {
786 if (searchTypes.contains(type)) {
787 setCurrentResourceInSelection(null);
788 UiListenerHandler.getInstance()
789 .resourceSelectionChangedUINotification(null);
795 private void deleteResourcesByType(String resourceType) {
796 if (null == resourceType) {
799 synchronized (resourceMap) {
800 Set<String> keySet = resourceMap.keySet();
801 if (null == keySet) {
804 Iterator<String> keyItr = keySet.iterator();
806 RemoteResource resource;
809 while (keyItr.hasNext()) {
811 resource = resourceMap.get(uId);
812 if (null == resource) {
815 types = resource.getRemoteResourceRef().getResourceTypes();
817 exist = types.contains(resourceType);
819 // Cancel observing the resource.
820 sendCancelObserveRequest(resource, false);
821 // Remove the resource from favorites list.
822 removeResourceFromFavorites(resource);
823 // Remove the resource
825 // Remove the device and platform information
826 synchronized (hostDeviceAndPlatformMap) {
827 hostDeviceAndPlatformMap.remove(resource
828 .getRemoteResourceRef().getHost());
836 public void resourceSelectionChanged(final RemoteResource resource) {
840 setCurrentResourceInSelection(resource);
841 // Notify all observers for resource selection change event
842 UiListenerHandler.getInstance()
843 .resourceSelectionChangedUINotification(resource);
848 public List<MetaProperty> getDefaultProperties(RemoteResource resource) {
849 if (null != resource) {
853 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
855 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
856 propName = Constants.META_PROPERTIES[index];
857 if (propName.equals(Constants.RESOURCE_URI)) {
858 propValue = resource.getRemoteResourceRef().getURI();
859 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
860 propValue = resource.getRemoteResourceRef()
861 .getConnectivityType().toString();
862 } else if (propName.equals(Constants.ADDRESS)) {
863 propValue = resource.getRemoteResourceRef().getHost();
864 } else if (propName.equals(Constants.OBSERVABLE)) {
865 propValue = Utility.getObservableInString(resource
866 .getRemoteResourceRef().isObservable());
867 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
868 Vector<String> resTypes = resource.getRemoteResourceRef()
870 if (null != resTypes && !resTypes.isEmpty()) {
872 Iterator<String> itr = resTypes.iterator();
873 while (itr.hasNext()) {
874 propValue += itr.next();
880 propValue = Constants.NOT_AVAILABLE;
882 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
883 Vector<String> interfaces = resource.getRemoteResourceRef()
884 .getResourceInterfaces();
885 if (null != interfaces && !interfaces.isEmpty()) {
887 Iterator<String> itr = interfaces.iterator();
888 while (itr.hasNext()) {
889 propValue += itr.next();
895 propValue = Constants.NOT_AVAILABLE;
900 if (null != propValue) {
901 metaPropertyList.add(new MetaProperty(propName, propValue));
905 return metaPropertyList;
910 public List<MetaProperty> getDeviceProperties() {
911 if (null == currentResourceInSelection) {
915 SimulatorRemoteResource remoteResource = currentResourceInSelection
916 .getRemoteResourceRef();
917 if (null == remoteResource) {
921 String host = remoteResource.getHost();
926 if (!isDeviceInfoExist(host)) {
927 // Device Information
929 SimulatorManager.findDevices(host, deviceListener);
930 } catch (SimulatorException e) {
934 .log(Level.ERROR.ordinal(), new Date(),
935 Utility.getSimulatorErrorString(e, null));
940 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
941 synchronized (hostDeviceAndPlatformMap) {
942 DeviceInfo devInfo = hostDeviceAndPlatformMap.get(host)
944 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, devInfo
946 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, devInfo
948 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
949 devInfo.getSpecVersion()));
950 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV_VERSION,
951 devInfo.getDataModelVersion()));
954 return metaProperties;
957 public List<MetaProperty> getPlatformProperties() {
958 if (null == currentResourceInSelection) {
962 SimulatorRemoteResource remoteResource = currentResourceInSelection
963 .getRemoteResourceRef();
964 if (null == remoteResource) {
968 String host = remoteResource.getHost();
973 if (!isPlatformInfoExist(host)) {
974 // Platform Information
976 SimulatorManager.getPlatformInformation(host, platformListener);
977 } catch (SimulatorException e) {
981 .log(Level.ERROR.ordinal(), new Date(),
982 Utility.getSimulatorErrorString(e, null));
987 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
988 synchronized (hostDeviceAndPlatformMap) {
989 PlatformInfo platInfo = hostDeviceAndPlatformMap.get(host)
991 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platInfo
993 metaProperties.add(new MetaProperty(
994 Constants.PLATFORM_MANUFAC_NAME, platInfo
995 .getManufacturerName()));
996 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
997 platInfo.getManufacturerUrl()));
998 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
999 platInfo.getModelNumber()));
1000 metaProperties.add(new MetaProperty(
1001 Constants.PLATFORM_DATE_OF_MANUFAC, platInfo
1002 .getDateOfManufacture()));
1003 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
1004 platInfo.getPlatformVersion()));
1005 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1006 platInfo.getOperationSystemVersion()));
1007 metaProperties.add(new MetaProperty(
1008 Constants.PLATFORM_HARDWARE_VERSION, platInfo
1009 .getHardwareVersion()));
1010 metaProperties.add(new MetaProperty(
1011 Constants.PLATFORM_FIRMWARE_VERSION, platInfo
1012 .getFirmwareVersion()));
1013 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1014 platInfo.getSupportUrl()));
1015 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
1016 platInfo.getSystemTime()));
1018 return metaProperties;
1021 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1022 if (null == resource) {
1025 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1026 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1027 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1028 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1032 public List<RemoteResource> getResourceList() {
1033 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1034 synchronized (resourceMap) {
1035 Set<String> idSet = resourceMap.keySet();
1036 Iterator<String> idItr = idSet.iterator();
1037 RemoteResource resource;
1038 while (idItr.hasNext()) {
1039 resource = resourceMap.get(idItr.next());
1040 if (null != resource) {
1041 resourceList.add(resource);
1046 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1047 public int compare(RemoteResource res1, RemoteResource res2) {
1048 String s1 = res1.getRemoteResourceRef().getURI();
1049 String s2 = res2.getRemoteResourceRef().getURI();
1051 String s1Part = s1.replaceAll("\\d", "");
1052 String s2Part = s2.replaceAll("\\d", "");
1054 if (s1Part.equalsIgnoreCase(s2Part)) {
1055 return extractInt(s1) - extractInt(s2);
1057 return s1.compareTo(s2);
1060 int extractInt(String s) {
1061 String num = s.replaceAll("\\D", "");
1062 // return 0 if no digits found
1063 return num.isEmpty() ? 0 : Integer.parseInt(num);
1067 return resourceList;
1070 public List<RemoteResource> getFavResourceList() {
1071 List<RemoteResource> resourceList;
1072 synchronized (favoriteResources) {
1073 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1075 return resourceList;
1078 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1083 AttributeValue val = att.value();
1084 if (null == val || null == val.get()) {
1088 TypeInfo type = val.typeInfo();
1090 if (type.mBaseType == ValueType.RESOURCEMODEL) {
1094 List<String> values = new ArrayList<String>();
1096 AttributeProperty prop = att.property();
1098 values.add(new AttributeValueStringConverter(val).toString());
1102 if (type.mType == ValueType.ARRAY) {
1103 if (type.mDepth == 1) {
1104 ArrayProperty arrayProperty = prop.asArray();
1105 if (null != arrayProperty) {
1106 AttributeProperty childProp = arrayProperty
1107 .getElementProperty();
1108 switch (childProp.getType()) {
1110 IntegerProperty intProperty = childProp.asInteger();
1111 if (null != intProperty) {
1112 values.addAll(getAllValues(att, intProperty,
1117 DoubleProperty dblProperty = childProp.asDouble();
1118 if (null != dblProperty) {
1119 values.addAll(getAllValues(att, dblProperty,
1124 BooleanProperty boolProperty = childProp
1126 if (null != boolProperty) {
1127 values.addAll(getAllValues(att, boolProperty,
1132 StringProperty stringProperty = childProp
1134 if (null != stringProperty) {
1135 values.addAll(getAllValues(att, stringProperty,
1145 switch (prop.getType()) {
1147 IntegerProperty intProperty = prop.asInteger();
1148 if (null != intProperty) {
1149 values.addAll(getAllValues(att, intProperty,
1154 DoubleProperty dblProperty = prop.asDouble();
1155 if (null != dblProperty) {
1156 values.addAll(getAllValues(att, dblProperty,
1161 BooleanProperty boolProperty = prop.asBoolean();
1162 if (null != boolProperty) {
1163 values.addAll(getAllValues(att, boolProperty,
1168 StringProperty stringProperty = prop.asString();
1169 if (null != stringProperty) {
1170 values.addAll(getAllValues(att, stringProperty,
1182 public List<String> getAllValues(SimulatorResourceAttribute attribute,
1183 IntegerProperty intProperty, AttributeProperty.Type type) {
1184 List<String> values = new ArrayList<String>();
1186 if (intProperty.hasRange()) {
1187 int min = (int) intProperty.min();
1188 int max = (int) intProperty.max();
1189 for (int iVal = min; iVal <= max; iVal++) {
1190 values.add(String.valueOf(iVal));
1192 } else if (intProperty.hasValues()) {
1193 for (Integer val : intProperty.getValues()) {
1194 values.add(String.valueOf(val));
1197 AttributeValue value = attribute.value();
1198 if (null != value && null != value.get()) {
1199 // Adding the current value of the attribute.
1200 values.add(String.valueOf(value.get()));
1206 public List<String> getAllValues(SimulatorResourceAttribute attribute,
1207 DoubleProperty dblProperty, AttributeProperty.Type type) {
1208 NumberFormat formatter = NumberFormat.getInstance();
1209 List<String> values = new ArrayList<String>();
1211 if (dblProperty.hasRange()) {
1212 double min = (double) dblProperty.min();
1213 double max = (double) dblProperty.max();
1214 for (double val = min; val <= max; val += 0.1) {
1215 formatter.setMaximumFractionDigits(1);
1216 formatter.setMinimumFractionDigits(1);
1217 values.add(formatter.format(val));
1219 } else if (dblProperty.hasValues()) {
1220 for (Double val : dblProperty.getValues()) {
1221 values.add(String.valueOf(val));
1224 AttributeValue value = attribute.value();
1225 if (null != value && null != value.get()) {
1226 // Adding the current value of the attribute.
1227 values.add(String.valueOf(value.get()));
1233 public List<String> getAllValues(SimulatorResourceAttribute attribute,
1234 BooleanProperty boolProperty, AttributeProperty.Type type) {
1235 List<String> values = new ArrayList<String>();
1237 values.add("false");
1241 public List<String> getAllValues(SimulatorResourceAttribute attribute,
1242 StringProperty stringProperty, AttributeProperty.Type type) {
1243 List<String> values = new ArrayList<String>();
1245 if (stringProperty.hasValues()) {
1246 for (String val : stringProperty.getValues()) {
1247 values.add(String.valueOf(val));
1250 AttributeValue value = attribute.value();
1251 if (null != value && null != value.get()) {
1252 // Adding the current value of the attribute.
1253 values.add(String.valueOf(value.get()));
1259 public void sendGetRequest(String ifType, String query,
1260 RemoteResource resource) {
1261 if (null == resource) {
1264 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1265 if (null == resourceN) {
1269 Map<String, String> queryParams = formQueryParameters(ifType, query);
1271 resourceN.get(queryParams, getListener);
1272 } catch (SimulatorException e) {
1276 .log(Level.ERROR.ordinal(), new Date(),
1277 Utility.getSimulatorErrorString(e, null));
1281 public void sendPutRequest(String ifType, RemoteResource resource,
1282 SimulatorResourceModel model) {
1283 if (null == resource || null == model) {
1286 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1287 if (null == resourceN) {
1290 Map<String, String> queryParams = formQueryParameters(ifType, null);
1292 resourceN.put(queryParams, model, putListener);
1293 } catch (Exception e) {
1295 addlInfo = "Invalid Attribute Value. Cannot send PUT request.";
1299 .log(Level.ERROR.ordinal(), new Date(),
1300 Utility.getSimulatorErrorString(e, addlInfo));
1304 public void sendPostRequest(String ifType, RemoteResource resource,
1305 SimulatorResourceModel model) {
1306 if (null == resource || null == model) {
1309 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1310 if (null == resourceN) {
1313 Map<String, String> queryParams = formQueryParameters(ifType, null);
1315 resourceN.post(queryParams, model, postListener);
1316 } catch (Exception e) {
1318 addlInfo = "Invalid Attribute Value. Cannot send POST request.";
1322 .log(Level.ERROR.ordinal(), new Date(),
1323 Utility.getSimulatorErrorString(e, addlInfo));
1327 public boolean sendObserveRequest(RemoteResource resource) {
1328 if (null == resource) {
1331 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1332 if (null == resourceN) {
1336 resourceN.observe(observeListener);
1337 resource.setObserved(true);
1338 // Add observed resource URI to show the proper status after every
1339 // find/refresh operations.
1340 addObservedResourceURI(resource.getRemoteResourceRef().getURI());
1341 } catch (SimulatorException e) {
1345 .log(Level.ERROR.ordinal(), new Date(),
1346 Utility.getSimulatorErrorString(e, null));
1352 private Map<String, String> formQueryParameters(String ifType, String query) {
1353 Map<String, String> queryParams = new HashMap<String, String>();
1355 // Including the interface type, if given,
1356 if (null != ifType) {
1357 ifType = ifType.trim();
1358 if (ifType.length() > 0)
1359 queryParams.put("if", ifType);
1362 // Including other queries, if given.
1363 if (null != query) {
1364 query = query.trim();
1365 if (query.length() > 0) {
1366 // Parse the query parameters and fill the map.
1367 String queries[] = query.split(";");
1368 if (queries.length > 0) {
1369 for (String pair : queries) {
1370 String tok[] = pair.split("=");
1371 if (null != tok && tok.length == 2) {
1372 queryParams.put(tok[0].trim(), tok[1].trim());
1381 public boolean sendCancelObserveRequest(RemoteResource resource,
1382 boolean removeEntry) {
1383 if (null == resource || !resource.isObserved()) {
1386 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1387 if (null == resourceN) {
1391 resourceN.stopObserve();
1392 resource.setObserved(false);
1393 // Remove observed resource URI to show the proper status after
1394 // every find/refresh operations.
1396 removeObservedResourceURI(resource.getRemoteResourceRef()
1398 } catch (SimulatorException e) {
1402 .log(Level.ERROR.ordinal(), new Date(),
1403 Utility.getSimulatorErrorString(e, null));
1409 public void startAutomationRequest(RequestType reqType,
1410 RemoteResource resource) {
1411 if (null == resource) {
1414 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1415 if (null == resourceN) {
1418 if (null == reqType) {
1423 autoId = resourceN.startVerification(reqType, verifyListener);
1425 if (reqType == RequestType.GET) {
1426 resource.setGetAutomtnId(autoId);
1427 } else if (reqType == RequestType.PUT) {
1428 resource.setPutAutomtnId(autoId);
1430 resource.setPostAutomtnId(autoId);
1436 .log(Level.INFO.ordinal(),
1438 "[" + reqType.toString()
1439 + "] Verification Started for \""
1440 + resourceN.getURI() + "\".");
1441 } catch (SimulatorException e) {
1445 .log(Level.ERROR.ordinal(), new Date(),
1446 Utility.getSimulatorErrorString(e, null));
1450 public void stopAutomationRequest(RequestType reqType,
1451 RemoteResource resource) {
1452 if (null == resource) {
1455 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1456 if (null == resourceN) {
1460 if (reqType == RequestType.GET) {
1461 resource.setGetAutomtnInProgress(false);
1462 autoId = resource.getGetAutomtnId();
1463 } else if (reqType == RequestType.PUT) {
1464 resource.setPutAutomtnInProgress(false);
1465 autoId = resource.getPutAutomtnId();
1467 resource.setPostAutomtnInProgress(false);
1468 autoId = resource.getPostAutomtnId();
1471 resourceN.stopVerification(autoId);
1472 } catch (SimulatorException e) {
1476 .log(Level.ERROR.ordinal(), new Date(),
1477 Utility.getSimulatorErrorString(e, null));
1481 public boolean setConfigFilePath(RemoteResource resource,
1482 String configFilePath) throws SimulatorException {
1484 if (null == resource) {
1487 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1488 if (null == resourceN) {
1492 Map<RequestType, SimulatorRequestModel> requestModels;
1493 requestModels = resourceN.setConfigInfo(configFilePath);
1494 if (null == requestModels) {
1498 // Store the resource model in the local cache
1499 resource.setRequestModels(requestModels);
1500 } catch (SimulatorException e) {
1504 .log(Level.ERROR.ordinal(), new Date(),
1505 Utility.getSimulatorErrorString(e, null));
1507 } catch (Exception e) {
1511 .log(Level.ERROR.ordinal(),
1513 Utility.getSimulatorErrorString(e,
1514 "Error while configuring the attribute properties"));
1516 // Update the status
1517 resource.setConfigUploaded(true);
1519 // Notify the UI listeners
1520 UiListenerHandler.getInstance().configUploadedNotification(resource);
1525 public void shutdown() {