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.remoteresource.DeviceAndPlatformInfo;
33 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
34 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
35 import oic.simulator.clientcontroller.utils.Constants;
36 import oic.simulator.clientcontroller.utils.Utility;
38 import org.oic.simulator.AttributeProperty;
39 import org.oic.simulator.AttributeProperty.Type;
40 import org.oic.simulator.AttributeValue;
41 import org.oic.simulator.AttributeValue.TypeInfo;
42 import org.oic.simulator.AttributeValue.ValueType;
43 import org.oic.simulator.DeviceInfo;
44 import org.oic.simulator.DeviceListener;
45 import org.oic.simulator.ILogger.Level;
46 import org.oic.simulator.PlatformInfo;
47 import org.oic.simulator.PlatformListener;
48 import org.oic.simulator.SimulatorException;
49 import org.oic.simulator.SimulatorManager;
50 import org.oic.simulator.SimulatorResourceAttribute;
51 import org.oic.simulator.SimulatorResourceModel;
52 import org.oic.simulator.SimulatorResult;
53 import org.oic.simulator.client.FindResourceListener;
54 import org.oic.simulator.client.SimulatorRemoteResource;
55 import org.oic.simulator.client.SimulatorRemoteResource.GetResponseListener;
56 import org.oic.simulator.client.SimulatorRemoteResource.ObserveNotificationListener;
57 import org.oic.simulator.client.SimulatorRemoteResource.PostResponseListener;
58 import org.oic.simulator.client.SimulatorRemoteResource.PutResponseListener;
59 import org.oic.simulator.client.SimulatorRemoteResource.VerificationListener;
60 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
63 * This class acts as an interface between the simulator java SDK and the
64 * various UI modules. It maintains all the details of resources and provides
65 * other UI modules with the information required. It also handles responses for
66 * find, GET, PUT, POST, Observe and automatic verification operations from
67 * native layer and propagates those events to the registered UI listeners.
69 public class ResourceManager {
71 private Set<String> lastKnownSearchTypes;
73 private RemoteResource currentResourceInSelection;
75 private FindResourceListener findResourceListener;
76 private GetResponseListener getListener;
77 private PutResponseListener putListener;
78 private PostResponseListener postListener;
79 private ObserveNotificationListener observeListener;
80 private VerificationListener verifyListener;
81 private DeviceListener deviceListener;
82 private PlatformListener platformListener;
84 private ResponseSynchronizerThread synchronizerThread;
86 private Thread threadHandle;
88 // Map with Server ID as key and the complete object as the value
89 private Map<String, RemoteResource> resourceMap;
90 private List<RemoteResource> favoriteResources;
91 // Maintaining a list of resource URIs for favorite resources feature.
92 private List<String> favoriteURIList;
94 // Maintaining a list of observed resource URIs.
95 private List<String> observedResourceURIList;
97 private Map<String, DeviceAndPlatformInfo> hostDeviceAndPlatformMap;
99 public ResourceManager() {
100 resourceMap = new HashMap<String, RemoteResource>();
101 favoriteResources = new ArrayList<RemoteResource>();
102 favoriteURIList = new ArrayList<String>();
103 observedResourceURIList = new ArrayList<String>();
104 hostDeviceAndPlatformMap = new HashMap<String, DeviceAndPlatformInfo>();
106 findResourceListener = new FindResourceListener() {
109 public void onResourceFound(final SimulatorRemoteResource resourceN) {
110 synchronizerThread.addToQueue(new Runnable() {
113 if (null == resourceN) {
117 // Ignore the response if the resource is a device or
119 Vector<String> resTypes = resourceN.getResourceTypes();
120 if (null != resTypes && resTypes.contains("oic.wk.d")
121 || resTypes.contains("oic.wk.p")) {
125 // If id is not available, then it cannot be added to
126 // the local map as null value should not be allowed as
128 String uid = resourceN.getId();
133 // If resource already exist, then ignore it.
134 boolean exist = isUidExist(uid);
139 RemoteResource resource = new RemoteResource();
140 resource.setRemoteResourceRef(resourceN);
142 String uri = resourceN.getURI();
143 if (null != uri && uri.trim().length() > 0) {
144 // Add resource to favorite list if it was in
145 // favorites list during find/refresh operation.
146 if (favoriteURIList.contains(uri)) {
147 addResourcetoFavorites(resource);
149 // Add resource to observed resources list if it was
150 // in observe list during find/refresh operation.
151 if (observedResourceURIList.contains(uri)) {
152 sendObserveRequest(resource);
158 .log(Level.INFO.ordinal(), new Date(),
159 "Found a resource without URI. Ignoring it.");
163 // Add the resource in local data structure
164 addResourceDetails(resource);
166 // Notify the UI listener
167 UiListenerHandler.getInstance()
168 .newResourceFoundNotification(resource);
173 .log(Level.INFO.ordinal(),
175 "Resource Found [" + resourceN.getURI()
178 // Send an initial GET request to get the resource
179 // attributes on an interface supported by the resource.
181 String ifType = null;
182 Vector<String> resInterfaces = resourceN
183 .getResourceInterfaces();
184 if (null != resInterfaces) {
185 ifType = resInterfaces.get(0);
187 resourceN.get(formQueryParameters(ifType, null),
189 } catch (SimulatorException e) {
193 .log(Level.ERROR.ordinal(),
195 Utility.getSimulatorErrorString(e,
199 // Get the device information
200 if (!isDeviceInfoExist(resourceN.getHost())) {
202 SimulatorManager.findDevices(resource
203 .getRemoteResourceRef().getHost(),
205 } catch (SimulatorException e) {
209 .log(Level.ERROR.ordinal(),
211 Utility.getSimulatorErrorString(
216 // Get the platform information
217 if (!isPlatformInfoExist(resourceN.getHost())) {
219 SimulatorManager.getPlatformInformation(
220 resource.getRemoteResourceRef()
221 .getHost(), platformListener);
222 } catch (SimulatorException e) {
226 .log(Level.ERROR.ordinal(),
228 Utility.getSimulatorErrorString(
237 // Listeners for device and platform information.
238 deviceListener = new DeviceListener() {
241 public void onDeviceFound(final String host,
242 final DeviceInfo deviceInfo) {
243 if (null == deviceInfo || null == host) {
246 synchronizerThread.addToQueue(new Runnable() {
249 synchronized (hostDeviceAndPlatformMap) {
250 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
253 info = new DeviceAndPlatformInfo();
255 hostDeviceAndPlatformMap.put(host, info);
257 info.setDeviceInfo(deviceInfo);
260 // Notify UI listeners
261 UiListenerHandler.getInstance()
262 .deviceInfoReceivedNotification();
268 platformListener = new PlatformListener() {
271 public void onPlatformFound(final String host,
272 final PlatformInfo platformInfo) {
273 if (null == platformInfo || null == host) {
276 synchronizerThread.addToQueue(new Runnable() {
279 synchronized (hostDeviceAndPlatformMap) {
280 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
283 info = new DeviceAndPlatformInfo();
285 hostDeviceAndPlatformMap.put(host, info);
287 info.setPlatformInfo(platformInfo);
290 // Notify UI listeners
291 UiListenerHandler.getInstance()
292 .platformInfoReceivedNotification();
298 getListener = new GetResponseListener() {
300 public void onGetResponse(final String uid,
301 final SimulatorResult result,
302 final SimulatorResourceModel resourceModelN) {
303 if (result != SimulatorResult.SIMULATOR_OK) {
307 .log(Level.ERROR.ordinal(),
311 + "] Received error response for GET request.");
314 synchronizerThread.addToQueue(new Runnable() {
317 // Handling the response which includes retrieving the
318 // attributes and updating the local model.
319 RemoteResource resource = handleResponse(uid,
321 if (null != resource) {
322 // Notify the UI listeners
323 UiListenerHandler.getInstance()
324 .getCompleteNotification(resource);
331 putListener = new PutResponseListener() {
334 public void onPutResponse(final String uid,
335 final SimulatorResult result,
336 final SimulatorResourceModel resourceModelN) {
337 if (result != SimulatorResult.SIMULATOR_OK) {
341 .log(Level.ERROR.ordinal(),
345 + "] Received error response for PUT request.");
348 synchronizerThread.addToQueue(new Thread() {
351 // Handling the response which includes retrieving the
352 // attributes and updating the local model.
353 RemoteResource resource = handleResponse(uid,
355 if (null != resource) {
356 // Notify the UI listeners
357 UiListenerHandler.getInstance()
358 .putCompleteNotification(resource);
365 postListener = new PostResponseListener() {
367 public void onPostResponse(final String uid,
368 final SimulatorResult result,
369 final SimulatorResourceModel resourceModelN) {
370 if (result != SimulatorResult.SIMULATOR_OK) {
374 .log(Level.ERROR.ordinal(),
378 + "] Received error response for POST request.");
381 synchronizerThread.addToQueue(new Runnable() {
384 // Handling the response which includes retrieving the
385 // attributes and updating the local model.
386 RemoteResource resource = handleResponse(uid,
388 if (null != resource) {
389 // Notify the UI listeners
390 UiListenerHandler.getInstance()
391 .postCompleteNotification(resource);
398 observeListener = new ObserveNotificationListener() {
401 public void onObserveNotification(final String uid,
402 final SimulatorResourceModel resourceModelN, final int seq) {
403 synchronizerThread.addToQueue(new Runnable() {
406 // Handling the response which includes retrieving the
407 // attributes and updating the local model.
408 RemoteResource resource = handleResponse(uid,
410 if (null != resource) {
411 // Notify the UI listeners
412 UiListenerHandler.getInstance()
413 .observeCompleteNotification(resource);
420 verifyListener = new VerificationListener() {
423 public void onVerificationStarted(final String uid, final int autoId) {
424 synchronizerThread.addToQueue(new Runnable() {
427 RemoteResource resource = getResource(uid);
428 if (null == resource) {
431 // Update the automation status.
432 resource.updateAutomationStatus(autoId, true);
434 int autoType = resource.getAutomationtype(autoId);
436 // Notify the listeners.
437 UiListenerHandler.getInstance()
438 .verificationStartedNotification(resource,
445 public void onVerificationCompleted(final String uid,
447 synchronizerThread.addToQueue(new Runnable() {
450 RemoteResource resource = getResource(uid);
451 if (null == resource) {
454 // Update the automation status.
455 resource.updateAutomationStatus(autoId, false);
457 int autoType = resource.getAutomationtype(autoId);
459 // Notify the listeners.
460 UiListenerHandler.getInstance()
461 .verificationCompletedNotification(resource,
468 public void onVerificationAborted(final String uid, final int autoId) {
469 synchronizerThread.addToQueue(new Runnable() {
472 RemoteResource resource = getResource(uid);
473 if (null == resource) {
476 // Update the automation status.
477 resource.updateAutomationStatus(autoId, false);
479 int autoType = resource.getAutomationtype(autoId);
481 // Notify the listeners.
482 UiListenerHandler.getInstance()
483 .verificationAbortedNotification(resource,
490 synchronizerThread = new ResponseSynchronizerThread();
491 threadHandle = new Thread(synchronizerThread);
492 threadHandle.setName("Simulator Client Controller Event Queue");
493 threadHandle.start();
496 private RemoteResource handleResponse(String uid,
497 SimulatorResourceModel resourceModelN) {
498 if (null == uid || null == resourceModelN) {
502 // Update the local model
503 RemoteResource resource;
504 resource = getResource(uid);
505 if (null == resource) {
509 SimulatorResourceModel resourceModel = resource.getResourceModelRef();
510 if (null == resourceModel) {
511 resource.setResourceModelRef(resourceModelN);
513 resourceModel.update(resourceModelN);
516 resource.setResourceRepresentation(resourceModelN, false);
521 public synchronized boolean isDeviceInfoExist(String host) {
522 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
526 if (null == info.getDeviceInfo()) {
532 public synchronized boolean isPlatformInfoExist(String host) {
533 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
537 if (null == info.getPlatformInfo()) {
543 private static class ResponseSynchronizerThread implements Runnable {
545 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
549 while (!Thread.interrupted()) {
550 synchronized (this) {
552 while (responseQueue.isEmpty()) {
556 } catch (InterruptedException e) {
562 synchronized (this) {
563 thread = responseQueue.pop();
567 } catch (Exception e) {
568 if (e instanceof InterruptedException) {
576 public void addToQueue(Runnable event) {
577 synchronized (this) {
578 responseQueue.add(event);
584 public void addResourcetoFavorites(RemoteResource resource) {
585 if (null == resource) {
588 resource.setFavorite(true);
589 synchronized (favoriteResources) {
590 favoriteResources.add(resource);
594 public void removeResourceFromFavorites(RemoteResource resource) {
595 if (null == resource) {
598 resource.setFavorite(false);
599 synchronized (favoriteResources) {
600 favoriteResources.remove(resource);
604 public void addResourceURItoFavorites(RemoteResource resource) {
605 if (null == resource) {
608 synchronized (favoriteURIList) {
609 favoriteURIList.add(resource.getRemoteResourceRef().getURI());
613 public void removeResourceURIFromFavorites(RemoteResource resource) {
614 if (null == resource) {
617 synchronized (favoriteURIList) {
618 favoriteURIList.remove(resource.getRemoteResourceRef().getURI());
622 public void addObservedResourceURI(String resourceURI) {
623 synchronized (observedResourceURIList) {
624 if (!observedResourceURIList.contains(resourceURI))
625 observedResourceURIList.add(resourceURI);
629 public void removeObservedResourceURI(String resourceURI) {
630 synchronized (observedResourceURIList) {
631 observedResourceURIList.remove(resourceURI);
635 public boolean isResourceObserved(String resourceURI) {
636 boolean observed = false;
637 synchronized (observedResourceURIList) {
638 observed = observedResourceURIList.contains(resourceURI);
643 public synchronized RemoteResource getCurrentResourceInSelection() {
644 return currentResourceInSelection;
647 public synchronized void setCurrentResourceInSelection(
648 RemoteResource resource) {
649 this.currentResourceInSelection = resource;
652 private void addResourceDetails(RemoteResource remoteResource) {
653 if (null != remoteResource) {
654 synchronized (resourceMap) {
655 resourceMap.put(remoteResource.getRemoteResourceRef().getId(),
661 private boolean isUidExist(String uid) {
663 synchronized (resourceMap) {
664 exist = resourceMap.containsKey(uid);
669 private RemoteResource getResource(String uid) {
673 RemoteResource resource;
674 synchronized (resourceMap) {
675 resource = resourceMap.get(uid);
680 public synchronized Set<String> getLastKnownSearchTypes() {
681 return lastKnownSearchTypes;
684 public synchronized void setLastKnownSearchTypes(
685 Set<String> lastKnownSearchTypes) {
686 this.lastKnownSearchTypes = lastKnownSearchTypes;
689 public boolean findResourceRequest(Set<String> searchTypes) {
690 boolean result = false;
691 if (null == searchTypes || searchTypes.size() < 1) {
693 SimulatorManager.findResource(findResourceListener);
695 } catch (SimulatorException e) {
699 .log(Level.ERROR.ordinal(), new Date(),
700 Utility.getSimulatorErrorString(e, null));
703 Iterator<String> searchItr = searchTypes.iterator();
705 while (searchItr.hasNext()) {
706 rType = searchItr.next();
708 SimulatorManager.findResource(rType, findResourceListener);
710 } catch (SimulatorException e) {
714 .log(Level.ERROR.ordinal(), new Date(),
715 Utility.getSimulatorErrorString(e, null));
722 public void deleteResources(final Set<String> searchTypes) {
723 synchronized (resourceMap) {
724 if (null == resourceMap && resourceMap.isEmpty()) {
730 if (null == searchTypes || searchTypes.size() < 1) {
731 synchronized (resourceMap) {
732 // Stop observing all the resources
733 Iterator<String> itr = resourceMap.keySet().iterator();
734 while (itr.hasNext()) {
735 sendCancelObserveRequest(
736 resourceMap.get(itr.next()), false);
738 // Delete all cached details of resources
740 favoriteResources.clear();
742 // Clearing the device and platform information
743 hostDeviceAndPlatformMap.clear();
745 // Change the current resource in selection
746 setCurrentResourceInSelection(null);
747 UiListenerHandler.getInstance()
748 .resourceSelectionChangedUINotification(null);
750 Iterator<String> typeItr = searchTypes.iterator();
752 while (typeItr.hasNext()) {
753 resType = typeItr.next();
754 deleteResourcesByType(resType);
756 // Change the current resource in selection
757 updateCurrentResourceInSelection(searchTypes);
764 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
765 if (null == searchTypes || searchTypes.size() < 1) {
768 RemoteResource resourceInSelection = getCurrentResourceInSelection();
769 if (null == resourceInSelection) {
772 List<String> typesOfSelection = resourceInSelection
773 .getRemoteResourceRef().getResourceTypes();
774 if (null == typesOfSelection || typesOfSelection.size() < 1) {
777 Iterator<String> itr = typesOfSelection.iterator();
779 while (itr.hasNext()) {
781 if (searchTypes.contains(type)) {
782 setCurrentResourceInSelection(null);
783 UiListenerHandler.getInstance()
784 .resourceSelectionChangedUINotification(null);
790 private void deleteResourcesByType(String resourceType) {
791 if (null == resourceType) {
794 synchronized (resourceMap) {
795 Set<String> keySet = resourceMap.keySet();
796 if (null == keySet) {
799 Iterator<String> keyItr = keySet.iterator();
801 RemoteResource resource;
804 while (keyItr.hasNext()) {
806 resource = resourceMap.get(uId);
807 if (null == resource) {
810 types = resource.getRemoteResourceRef().getResourceTypes();
812 exist = types.contains(resourceType);
814 // Cancel observing the resource.
815 sendCancelObserveRequest(resource, false);
816 // Remove the resource from favorites list.
817 removeResourceFromFavorites(resource);
818 // Remove the resource
820 // Remove the device and platform information
821 synchronized (hostDeviceAndPlatformMap) {
822 hostDeviceAndPlatformMap.remove(resource
823 .getRemoteResourceRef().getHost());
831 public void resourceSelectionChanged(final RemoteResource resource) {
835 setCurrentResourceInSelection(resource);
836 // Notify all observers for resource selection change event
837 UiListenerHandler.getInstance()
838 .resourceSelectionChangedUINotification(resource);
843 public List<MetaProperty> getDefaultProperties(RemoteResource resource) {
844 if (null != resource) {
848 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
850 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
851 propName = Constants.META_PROPERTIES[index];
852 if (propName.equals(Constants.RESOURCE_URI)) {
853 propValue = resource.getRemoteResourceRef().getURI();
854 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
855 propValue = resource.getRemoteResourceRef()
856 .getConnectivityType().toString();
857 } else if (propName.equals(Constants.ADDRESS)) {
858 propValue = resource.getRemoteResourceRef().getHost();
859 } else if (propName.equals(Constants.OBSERVABLE)) {
860 propValue = Utility.getObservableInString(resource
861 .getRemoteResourceRef().isObservable());
862 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
863 Vector<String> resTypes = resource.getRemoteResourceRef()
865 if (null != resTypes && !resTypes.isEmpty()) {
867 Iterator<String> itr = resTypes.iterator();
868 while (itr.hasNext()) {
869 propValue += itr.next();
875 propValue = Constants.NOT_AVAILABLE;
877 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
878 Vector<String> interfaces = resource.getRemoteResourceRef()
879 .getResourceInterfaces();
880 if (null != interfaces && !interfaces.isEmpty()) {
882 Iterator<String> itr = interfaces.iterator();
883 while (itr.hasNext()) {
884 propValue += itr.next();
890 propValue = Constants.NOT_AVAILABLE;
895 if (null != propValue) {
896 metaPropertyList.add(new MetaProperty(propName, propValue));
900 return metaPropertyList;
905 public List<MetaProperty> getDeviceProperties() {
906 if (null == currentResourceInSelection) {
910 SimulatorRemoteResource remoteResource = currentResourceInSelection
911 .getRemoteResourceRef();
912 if (null == remoteResource) {
916 String host = remoteResource.getHost();
921 if (!isDeviceInfoExist(host)) {
922 // Device Information
924 SimulatorManager.findDevices(host, deviceListener);
925 } catch (SimulatorException e) {
929 .log(Level.ERROR.ordinal(), new Date(),
930 Utility.getSimulatorErrorString(e, null));
935 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
936 synchronized (hostDeviceAndPlatformMap) {
937 DeviceInfo devInfo = hostDeviceAndPlatformMap.get(host)
939 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, devInfo
941 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, devInfo
943 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
944 devInfo.getSpecVersion()));
945 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV_VERSION,
946 devInfo.getDataModelVersion()));
949 return metaProperties;
952 public List<MetaProperty> getPlatformProperties() {
953 if (null == currentResourceInSelection) {
957 SimulatorRemoteResource remoteResource = currentResourceInSelection
958 .getRemoteResourceRef();
959 if (null == remoteResource) {
963 String host = remoteResource.getHost();
968 if (!isPlatformInfoExist(host)) {
969 // Platform Information
971 SimulatorManager.getPlatformInformation(host, platformListener);
972 } catch (SimulatorException e) {
976 .log(Level.ERROR.ordinal(), new Date(),
977 Utility.getSimulatorErrorString(e, null));
982 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
983 synchronized (hostDeviceAndPlatformMap) {
984 PlatformInfo platInfo = hostDeviceAndPlatformMap.get(host)
986 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platInfo
988 metaProperties.add(new MetaProperty(
989 Constants.PLATFORM_MANUFAC_NAME, platInfo
990 .getManufacturerName()));
991 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
992 platInfo.getManufacturerUrl()));
993 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
994 platInfo.getModelNumber()));
995 metaProperties.add(new MetaProperty(
996 Constants.PLATFORM_DATE_OF_MANUFAC, platInfo
997 .getDateOfManufacture()));
998 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
999 platInfo.getPlatformVersion()));
1000 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1001 platInfo.getOperationSystemVersion()));
1002 metaProperties.add(new MetaProperty(
1003 Constants.PLATFORM_HARDWARE_VERSION, platInfo
1004 .getHardwareVersion()));
1005 metaProperties.add(new MetaProperty(
1006 Constants.PLATFORM_FIRMWARE_VERSION, platInfo
1007 .getFirmwareVersion()));
1008 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1009 platInfo.getSupportUrl()));
1010 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
1011 platInfo.getSystemTime()));
1013 return metaProperties;
1016 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1017 if (null == resource) {
1020 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1021 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1022 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1023 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1027 public List<RemoteResource> getResourceList() {
1028 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1029 synchronized (resourceMap) {
1030 Set<String> idSet = resourceMap.keySet();
1031 Iterator<String> idItr = idSet.iterator();
1032 RemoteResource resource;
1033 while (idItr.hasNext()) {
1034 resource = resourceMap.get(idItr.next());
1035 if (null != resource) {
1036 resourceList.add(resource);
1041 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1042 public int compare(RemoteResource res1, RemoteResource res2) {
1043 String s1 = res1.getRemoteResourceRef().getURI();
1044 String s2 = res2.getRemoteResourceRef().getURI();
1046 String s1Part = s1.replaceAll("\\d", "");
1047 String s2Part = s2.replaceAll("\\d", "");
1049 if (s1Part.equalsIgnoreCase(s2Part)) {
1050 return extractInt(s1) - extractInt(s2);
1052 return s1.compareTo(s2);
1055 int extractInt(String s) {
1056 String num = s.replaceAll("\\D", "");
1057 // return 0 if no digits found
1058 return num.isEmpty() ? 0 : Integer.parseInt(num);
1062 return resourceList;
1065 public List<RemoteResource> getFavResourceList() {
1066 List<RemoteResource> resourceList;
1067 synchronized (favoriteResources) {
1068 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1070 return resourceList;
1073 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1078 AttributeValue val = att.value();
1083 List<String> values = new ArrayList<String>();
1085 TypeInfo type = val.typeInfo();
1087 AttributeProperty prop = att.property();
1088 if (null == prop || prop.type().ordinal() == Type.UNKNOWN.ordinal()) {
1089 values.add(Utility.getAttributeValueAsString(val));
1093 Type valuesType = prop.type();
1095 if (type.mType != ValueType.RESOURCEMODEL) {
1096 if (type.mType == ValueType.ARRAY) {
1097 if (type.mDepth == 1) {
1098 AttributeProperty childProp = prop.getChildProperty();
1099 if (null != childProp) {
1100 valuesType = childProp.type();
1101 if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1102 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1103 childProp, type.mBaseType);
1105 values.addAll(list);
1107 } else if (valuesType.ordinal() == Type.VALUESET
1109 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1110 childProp.valueSet(), type.mBaseType);
1112 values.addAll(list);
1118 if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1119 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1122 values.addAll(list);
1124 } else if (valuesType.ordinal() == Type.VALUESET.ordinal()) {
1125 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1126 prop.valueSet(), type.mType);
1128 values.addAll(list);
1134 if (values.isEmpty()) {
1135 values.add(Utility.getAttributeValueAsString(val));
1141 public List<String> getRangeForPrimitiveNonArrayAttributes(
1142 AttributeProperty prop, ValueType type) {
1147 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1151 List<String> values = new ArrayList<String>();
1154 int min = (int) prop.min();
1155 int max = (int) prop.max();
1156 for (int iVal = min; iVal <= max; iVal++) {
1157 values.add(String.valueOf(iVal));
1161 double minD = (double) prop.min();
1162 double maxD = (double) prop.max();
1163 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
1164 values.add(String.valueOf(iVal));
1172 public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
1173 AttributeValue[] attValues, ValueType type) {
1174 if (null == attValues || attValues.length < 1) {
1178 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1183 List<String> values = new ArrayList<String>();
1184 for (AttributeValue val : attValues) {
1192 values.add(String.valueOf(obj));
1197 public void sendGetRequest(String ifType, String query,
1198 RemoteResource resource) {
1199 if (null == resource) {
1202 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1203 if (null == resourceN) {
1207 Map<String, String> queryParams = formQueryParameters(ifType, query);
1209 resourceN.get(queryParams, getListener);
1210 } catch (SimulatorException e) {
1214 .log(Level.ERROR.ordinal(), new Date(),
1215 Utility.getSimulatorErrorString(e, null));
1219 public void sendPutRequest(String ifType, RemoteResource resource,
1220 SimulatorResourceModel model) {
1221 if (null == resource || null == model) {
1224 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1225 if (null == resourceN) {
1228 Map<String, String> queryParams = formQueryParameters(ifType, null);
1230 resourceN.put(queryParams, model, putListener);
1231 } catch (Exception e) {
1233 addlInfo = "Invalid Attribute Value. Cannot send PUT request.";
1237 .log(Level.ERROR.ordinal(), new Date(),
1238 Utility.getSimulatorErrorString(e, addlInfo));
1242 public void sendPostRequest(String ifType, RemoteResource resource,
1243 SimulatorResourceModel model) {
1244 if (null == resource || null == model) {
1247 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1248 if (null == resourceN) {
1251 Map<String, String> queryParams = formQueryParameters(ifType, null);
1253 resourceN.post(queryParams, model, postListener);
1254 } catch (Exception e) {
1256 addlInfo = "Invalid Attribute Value. Cannot send POST request.";
1260 .log(Level.ERROR.ordinal(), new Date(),
1261 Utility.getSimulatorErrorString(e, addlInfo));
1265 public boolean sendObserveRequest(RemoteResource resource) {
1266 if (null == resource) {
1269 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1270 if (null == resourceN) {
1274 resourceN.startObserve(null, observeListener);
1275 resource.setObserved(true);
1276 // Add observed resource URI to show the proper status after every
1277 // find/refresh operations.
1278 addObservedResourceURI(resource.getRemoteResourceRef().getURI());
1279 } catch (SimulatorException e) {
1283 .log(Level.ERROR.ordinal(), new Date(),
1284 Utility.getSimulatorErrorString(e, null));
1290 private Map<String, String> formQueryParameters(String ifType, String query) {
1291 Map<String, String> queryParams = new HashMap<String, String>();
1293 // Including the interface type, if given,
1294 if (null != ifType) {
1295 ifType = ifType.trim();
1296 if (ifType.length() > 0)
1297 queryParams.put("if", ifType);
1300 // Including other queries, if given.
1301 if (null != query) {
1302 query = query.trim();
1303 if (query.length() > 0) {
1304 // Parse the query parameters and fill the map.
1305 String queries[] = query.split(";");
1306 if (queries.length > 0) {
1307 for (String pair : queries) {
1308 String tok[] = pair.split("=");
1309 if (null != tok && tok.length == 2) {
1310 queryParams.put(tok[0].trim(), tok[1].trim());
1319 public boolean sendCancelObserveRequest(RemoteResource resource,
1320 boolean removeEntry) {
1321 if (null == resource || !resource.isObserved()) {
1324 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1325 if (null == resourceN) {
1329 resourceN.stopObserve();
1330 resource.setObserved(false);
1331 // Remove observed resource URI to show the proper status after
1332 // every find/refresh operations.
1334 removeObservedResourceURI(resource.getRemoteResourceRef()
1336 } catch (SimulatorException e) {
1340 .log(Level.ERROR.ordinal(), new Date(),
1341 Utility.getSimulatorErrorString(e, null));
1347 public void startAutomationRequest(VerificationType reqType,
1348 RemoteResource resource) {
1349 if (null == resource) {
1352 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1353 if (null == resourceN) {
1356 if (null == reqType) {
1361 autoId = resourceN.startVerification(reqType, verifyListener);
1363 if (reqType == VerificationType.GET) {
1364 resource.setGetAutomtnId(autoId);
1365 } else if (reqType == VerificationType.PUT) {
1366 resource.setPutAutomtnId(autoId);
1368 resource.setPostAutomtnId(autoId);
1374 .log(Level.INFO.ordinal(),
1376 "[" + reqType.toString()
1377 + "] Verification Started for \""
1378 + resourceN.getURI() + "\".");
1379 } catch (SimulatorException e) {
1383 .log(Level.ERROR.ordinal(), new Date(),
1384 Utility.getSimulatorErrorString(e, null));
1388 public void stopAutomationRequest(VerificationType reqType,
1389 RemoteResource resource) {
1390 if (null == resource) {
1393 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1394 if (null == resourceN) {
1398 if (reqType == VerificationType.GET) {
1399 resource.setGetAutomtnInProgress(false);
1400 autoId = resource.getGetAutomtnId();
1401 } else if (reqType == VerificationType.PUT) {
1402 resource.setPutAutomtnInProgress(false);
1403 autoId = resource.getPutAutomtnId();
1405 resource.setPostAutomtnInProgress(false);
1406 autoId = resource.getPostAutomtnId();
1409 resourceN.stopVerification(autoId);
1410 } catch (SimulatorException e) {
1414 .log(Level.ERROR.ordinal(), new Date(),
1415 Utility.getSimulatorErrorString(e, null));
1419 public boolean setConfigFilePath(RemoteResource resource,
1420 String configFilePath) throws SimulatorException {
1421 if (null == resource) {
1424 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1425 if (null == resourceN) {
1429 SimulatorResourceModel configuredResourceModel;
1430 configuredResourceModel = resourceN.setConfigInfo(configFilePath);
1431 if (null == configuredResourceModel) {
1435 // Store the resource model in the local cache
1436 SimulatorResourceModel resourceModel = resource
1437 .getResourceModelRef();
1438 if (null != resourceModel) {
1439 configuredResourceModel.update(resourceModel);
1441 resource.setResourceModelRef(configuredResourceModel);
1442 } catch (SimulatorException e) {
1446 .log(Level.ERROR.ordinal(), new Date(),
1447 Utility.getSimulatorErrorString(e, null));
1450 // Update the status
1451 resource.setConfigUploaded(true);
1453 // Notify the UI listeners
1454 UiListenerHandler.getInstance().configUploadedNotification(resource);
1459 public void shutdown() {