1 package oic.simulator.serviceprovider.manager;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.LinkedList;
11 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
12 import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
13 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
14 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
15 import oic.simulator.serviceprovider.resource.MetaProperty;
16 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
17 import oic.simulator.serviceprovider.resource.ResourceAttribute;
18 import oic.simulator.serviceprovider.resource.SimulatorResource;
19 import oic.simulator.serviceprovider.resource.StandardConfiguration;
20 import oic.simulator.serviceprovider.utils.Constants;
22 import org.oic.simulator.AutomationType;
23 import org.oic.simulator.IAutomation;
24 import org.oic.simulator.SimulatorManager;
25 import org.oic.simulator.SimulatorResourceAttribute;
26 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
27 import org.oic.simulator.serviceprovider.SimulatorResourceModel;
28 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
30 public class ResourceManager {
32 private Map<String, Map<String, SimulatorResource>> resourceMap;
34 private StandardConfiguration stdConfig;
36 private SimulatorResource currentResourceInSelection;
38 private List<IResourceListChangedUIListener> resourceListChangedUIListeners;
40 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
42 private List<IResourceModelChangedUIListener> resourceModelChangedUIListeners;
44 private List<IAutomationUIListener> automationUIListeners;
46 private IResourceModelChangedListener resourceModelChangeListener;
48 private IAutomation automationListener;
50 private NotificationSynchronizerThread synchronizerThread;
52 private Thread threadHandle;
55 System.loadLibrary("SimulatorManager");
58 public ResourceManager() {
59 resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
60 stdConfig = new StandardConfiguration();
62 resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
63 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
64 resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
65 automationUIListeners = new ArrayList<IAutomationUIListener>();
67 // Populate standard configuration file list
68 populateStandardConfigurationList();
70 resourceModelChangeListener = new IResourceModelChangedListener() {
73 public void onResourceModelChanged(final String resourceURI,
74 final SimulatorResourceModel resourceModelN) {
75 synchronizerThread.addToQueue(new Runnable() {
79 if (null == resourceURI || null == resourceModelN) {
82 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
83 if (null == resource) {
86 // Fetch the resource attributes
87 Map<String, ResourceAttribute> resourceAttributeMapNew;
88 resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
89 if (null == resourceAttributeMapNew) {
92 // Update the resource with new model data
93 Map<String, ResourceAttribute> resourceAttributeMapOld;
94 resourceAttributeMapOld = resource
95 .getResourceAttributesMap();
96 if (null == resourceAttributeMapOld) {
99 ModelChangeNotificationType notificationType;
100 notificationType = compareAndUpdateLocalAttributes(
101 resourceAttributeMapOld,
102 resourceAttributeMapNew);
103 if (notificationType != ModelChangeNotificationType.NONE) {
104 // Update the UI listeners
105 resourceModelChangedUINotification(
106 notificationType, resourceURI);
113 automationListener = new IAutomation() {
116 public void onAutomationComplete(final String resourceURI,
117 final int automationId) {
118 synchronizerThread.addToQueue(new Runnable() {
122 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
123 if (null == resource) {
126 // Checking whether this notification is for an
127 // attribute or a resource
128 if (resource.isResourceAutomationInProgress()) {
129 changeResourceLevelAutomationStatus(resource, false);
130 // Notify the UI listeners
131 automationCompleteUINotification(resourceURI, null);
132 } else if (resource.isAttributeAutomationInProgress()) {
133 // Find the attribute with the given automation id
134 ResourceAttribute attribute;
135 attribute = getAttributeWithGivenAutomationId(
136 resource, automationId);
137 if (null != attribute) {
138 attribute.setAutomationInProgress(false);
139 resource.setAttributeAutomationInProgress(false);
140 // Notify the UI listeners
141 automationCompleteUINotification(resourceURI,
142 attribute.getAttributeName());
145 // Ignoring the notification as there are no
146 // known automation for the current resource.
153 synchronizerThread = new NotificationSynchronizerThread();
154 threadHandle = new Thread(synchronizerThread);
155 threadHandle.setName("Simulator service provider event queue");
156 threadHandle.start();
159 private static class NotificationSynchronizerThread implements Runnable {
161 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
165 while (!Thread.interrupted()) {
166 synchronized (this) {
168 while (notificationQueue.isEmpty()) {
172 } catch (InterruptedException e) {
178 synchronized (this) {
179 thread = notificationQueue.pop();
183 } catch (Exception e) {
184 if (e instanceof InterruptedException) {
192 public void addToQueue(Runnable event) {
193 synchronized (this) {
194 notificationQueue.add(event);
200 private void populateStandardConfigurationList() {
201 // TODO: Add all the standard configuration files
202 // Ex: stdConfig.addResourceConfiguration(LIGHT, LIGHT_FILE);
205 // This method gives a list of RAML resource configurations available.
206 public List<String> getResourceConfigurationList() {
207 List<String> resourceConfigurationList = new ArrayList<String>();
208 synchronized (stdConfig) {
209 Map<String, String> configMap = stdConfig
210 .getStandardResourceConfigurationList();
211 if (null != configMap) {
212 Set<String> keySet = configMap.keySet();
213 Iterator<String> keyItr = keySet.iterator();
214 while (keyItr.hasNext()) {
215 resourceConfigurationList.add(keyItr.next());
219 return resourceConfigurationList;
222 public String getConfigFilePath(String resourceType) {
224 if (null != resourceType) {
225 synchronized (stdConfig) {
226 path = stdConfig.getResourceConfigFilePath(resourceType);
232 public void addResourceListChangedUIListener(
233 IResourceListChangedUIListener resourceListChangedUIListener) {
234 synchronized (resourceListChangedUIListeners) {
235 resourceListChangedUIListeners.add(resourceListChangedUIListener);
239 public void addResourceSelectionChangedUIListener(
240 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
241 synchronized (resourceSelectionChangedUIListeners) {
242 resourceSelectionChangedUIListeners
243 .add(resourceSelectionChangedUIListener);
247 public void addResourceModelChangedUIListener(
248 IResourceModelChangedUIListener resourceModelChangedUIListener) {
249 synchronized (resourceModelChangedUIListeners) {
250 resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
254 public void addAutomationUIListener(
255 IAutomationUIListener automationUIListener) {
256 synchronized (automationUIListeners) {
257 automationUIListeners.add(automationUIListener);
261 public void removeResourceListChangedUIListener(
262 IResourceListChangedUIListener listener) {
263 synchronized (resourceListChangedUIListeners) {
264 if (null != listener && resourceListChangedUIListeners.size() > 0) {
265 resourceListChangedUIListeners.remove(listener);
270 public void removeResourceSelectionChangedUIListener(
271 IResourceSelectionChangedUIListener listener) {
272 synchronized (resourceSelectionChangedUIListeners) {
274 && resourceSelectionChangedUIListeners.size() > 0) {
275 resourceSelectionChangedUIListeners.remove(listener);
280 public void removeResourceModelChangedUIListener(
281 IResourceModelChangedUIListener listener) {
282 synchronized (resourceModelChangedUIListeners) {
283 if (null != listener && resourceModelChangedUIListeners.size() > 0) {
284 resourceModelChangedUIListeners.remove(listener);
289 public void removeAutomationUIListener(IAutomationUIListener listener) {
290 synchronized (automationUIListeners) {
291 if (null != listener && automationUIListeners.size() > 0) {
292 automationUIListeners.remove(listener);
297 public synchronized SimulatorResource getCurrentResourceInSelection() {
298 return currentResourceInSelection;
301 public synchronized void setCurrentResourceInSelection(
302 SimulatorResource resource) {
303 this.currentResourceInSelection = resource;
306 private void addResourceToMap(SimulatorResource simulatorResource) {
307 if (null != simulatorResource) {
308 synchronized (resourceMap) {
309 Map<String, SimulatorResource> resourceTypeMap;
310 resourceTypeMap = resourceMap.get(simulatorResource
312 if (null == resourceTypeMap) {
313 resourceTypeMap = new HashMap<String, SimulatorResource>();
314 resourceMap.put(simulatorResource.getResourceType(),
317 resourceTypeMap.put(simulatorResource.getResourceURI(),
323 private void addResourceToMap(String resourceType,
324 Map<String, SimulatorResource> newResourceTypeMap) {
325 if (null != resourceType && null != newResourceTypeMap) {
326 synchronized (resourceMap) {
327 Map<String, SimulatorResource> resourceTypeMap = resourceMap
329 if (null != resourceTypeMap) {
330 resourceTypeMap.putAll(newResourceTypeMap);
332 resourceMap.put(resourceType, newResourceTypeMap);
338 private void removeResourceFromMap(String resourceType, String resourceURI) {
339 if (null != resourceURI && null != resourceType) {
340 synchronized (resourceMap) {
341 Map<String, SimulatorResource> resourceTypeMap = resourceMap
343 if (null != resourceTypeMap) {
344 resourceTypeMap.remove(resourceURI);
345 if (resourceTypeMap.size() < 1) {
346 resourceMap.remove(resourceType);
353 public boolean isResourceExist(String resourceURI) {
354 boolean result = false;
355 if (null != resourceURI) {
356 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
357 if (null != resource) {
364 public void createResource(final String configFilePath) {
368 SimulatorResourceServer resourceServerN;
369 resourceServerN = SimulatorManager.createResource(
370 configFilePath, resourceModelChangeListener);
372 SimulatorResource simulatorResource;
373 simulatorResource = fetchResourceData(resourceServerN);
374 if (null != simulatorResource) {
375 addResourceToMap(simulatorResource);
377 resourceCreatedUINotification();
383 public void createResource(final String configFilePath,
384 final int noOfInstances) {
388 Map<String, SimulatorResource> resourceTypeMap;
389 SimulatorResourceServer[] simulatorResourceServers = null;
390 simulatorResourceServers = SimulatorManager.createResource(
391 configFilePath, noOfInstances,
392 resourceModelChangeListener);
393 if (null == simulatorResourceServers) {
396 resourceTypeMap = new HashMap<String, SimulatorResource>();
397 SimulatorResource resource;
399 for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
400 resource = fetchResourceData(resourceServerN);
401 if (null != resource) {
402 uri = resource.getResourceURI();
403 resourceTypeMap.put(uri, resource);
407 // Find the resourceType and add it to the local data
408 // structure and notify UI Listeners
409 if (resourceTypeMap.size() > 0) {
411 Set<String> uriSet = resourceTypeMap.keySet();
412 Iterator<String> itr = uriSet.iterator();
414 SimulatorResource simResource = resourceTypeMap.get(itr
416 if (null != simResource) {
417 resourceType = simResource.getResourceType();
419 addResourceToMap(resourceType, resourceTypeMap);
420 resourceCreatedUINotification();
428 private SimulatorResource fetchResourceData(
429 SimulatorResourceServer resourceServerN) {
430 SimulatorResource simulatorResource = null;
431 if (null != resourceServerN) {
432 simulatorResource = new SimulatorResource();
433 simulatorResource.setResourceServer(resourceServerN);
434 simulatorResource.setResourceURI(resourceServerN.getURI());
436 .setResourceType(resourceServerN.getResourceType());
437 simulatorResource.setResourceName(resourceServerN.getName());
438 simulatorResource.setResourceInterface(resourceServerN
439 .getInterfaceType());
441 SimulatorResourceModel resourceModelN = resourceServerN.getModel();
442 if (null != resourceModelN) {
443 simulatorResource.setResourceModel(resourceModelN);
445 // Fetch the resource attributes
446 Map<String, ResourceAttribute> resourceAttributeMap;
447 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
448 if (null != resourceAttributeMap) {
450 .setResourceAttributesMap(resourceAttributeMap);
454 return simulatorResource;
457 private Map<String, ResourceAttribute> fetchResourceAttributesFromModel(
458 SimulatorResourceModel resourceModelN) {
459 Map<String, ResourceAttribute> resourceAttributeMap = null;
460 if (null != resourceModelN) {
461 Map<String, SimulatorResourceAttribute> attributeMapN;
462 attributeMapN = resourceModelN.getAttributes();
463 if (null != attributeMapN) {
464 resourceAttributeMap = new HashMap<String, ResourceAttribute>();
466 Set<String> attNameSet = attributeMapN.keySet();
469 SimulatorResourceAttribute attributeN;
470 ResourceAttribute attribute;
471 Iterator<String> attNameItr = attNameSet.iterator();
472 while (attNameItr.hasNext()) {
473 attName = attNameItr.next();
474 attributeN = attributeMapN.get(attName);
475 if (null != attributeN) {
476 attribute = new ResourceAttribute();
477 attribute.setResourceAttribute(attributeN);
478 attribute.setAttributeName(attName);
480 attValueObj = attributeN.getValue();
481 if (null != attValueObj) {
482 attribute.setAttributeValue(attValueObj);
485 // Read allowed values or min-max values of the
487 // TODO: Temporarily reading the allowed values
489 // If attribute type is known, then appropriate
490 // get method for that type will be called.
491 String[] allowedValues = resourceModelN
492 .getAllowedValues(attName);
493 attribute.setAllowedValues(allowedValues);
494 if (null == allowedValues || allowedValues.length < 1) {
495 // TODO: Get the range(min-max) of the attribute
496 // Implementation of GetRange is in progress
499 // Initially disabling the automation
500 attribute.setAutomationInProgress(false);
502 // TODO: Temporarily setting the interval to 500.
503 // This value should come from the native layer.
504 // Native implementation is in progress.
506 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
508 // Setting the default automation type
510 .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
512 resourceAttributeMap.put(attName, attribute);
517 return resourceAttributeMap;
520 public void deleteResourceByURI(final String resourceURI) {
521 if (null != resourceURI) {
525 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
526 if (null != resource) {
527 String resourceType = resource.getResourceType();
529 // Unregister the resource from the platform
530 deleteResource(resource);
532 // Delete from the local data structure
533 deleteLocalResourceDetails(resourceType, resourceURI);
535 // Notify the UI listener for removing this resource
537 resourceDeletedUINotification();
539 if (null != currentResourceInSelection
540 && resource == currentResourceInSelection) {
541 // Listeners might query the resource being deleted
542 // if exists. So set the currently selection to
544 setCurrentResourceInSelection(null);
546 // Notify all observers for resource selection
548 resourceSelectionChangedUINotification();
556 private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
557 SimulatorResource resource = null;
558 if (null != resourceURI) {
559 synchronized (resourceMap) {
560 Set<String> typeSet = resourceMap.keySet();
561 Iterator<String> typeItr = typeSet.iterator();
563 Map<String, SimulatorResource> resourceTypeMap;
564 while (typeItr.hasNext()) {
565 resourceType = typeItr.next();
566 resourceTypeMap = resourceMap.get(resourceType);
567 if (null != resourceTypeMap) {
568 resource = resourceTypeMap.get(resourceURI);
569 if (null != resource) {
579 private void deleteResource(SimulatorResource resource) {
580 if (null != resource) {
581 SimulatorResourceServer resourceServerN = resource
582 .getResourceServer();
583 if (null != resourceServerN) {
584 SimulatorManager.deleteResource(resourceServerN);
589 public void deleteResourceByType(final String resourceType) {
590 if (null != resourceType) {
594 // Unregister the resources from the platform
595 deleteResource(resourceType);
597 // Delete from the local data structure
598 deleteLocalResourceDetails(resourceType, null);
600 // Notify the UI listener for removing this resource from UI
601 resourceDeletedUINotification();
603 if (null != currentResourceInSelection
604 && resourceType.equals(currentResourceInSelection
605 .getResourceType())) {
606 // Listeners might query the resource being deleted if
607 // exists. So set the currently selection to null.
608 setCurrentResourceInSelection(null);
610 // Notify all observers for resource selection change
612 resourceSelectionChangedUINotification();
619 private void deleteResource(String resourceType) {
620 if (null != resourceType) {
621 SimulatorManager.deleteResources(resourceType);
625 public void deleteAllResources() {
629 // Unregister the resources from the platform
632 // Delete from the local data structure
633 deleteLocalResourceDetails(null, null);
635 // Notify the UI listener for removing this resource from UI
636 resourceDeletedUINotification();
638 // Listeners might query the resource being deleted if exists.
639 // So set the currently selection to null.
640 setCurrentResourceInSelection(null);
642 // Notify all observers for resource selection change event
643 resourceSelectionChangedUINotification();
648 private void deleteResource() {
649 SimulatorManager.deleteResources(null);
652 private void deleteLocalResourceDetails(String resourceType,
653 String resourceURI) {
654 if (null != resourceType && null != resourceURI) {
655 removeResourceFromMap(resourceType, resourceURI);
657 synchronized (resourceMap) {
658 if (null != resourceType) {
659 resourceMap.remove(resourceType);
667 private void resourceCreatedUINotification() {
668 synchronized (resourceListChangedUIListeners) {
669 if (resourceListChangedUIListeners.size() > 0) {
670 IResourceListChangedUIListener listener;
671 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
673 while (listenerItr.hasNext()) {
674 listener = listenerItr.next();
675 if (null != listener) {
676 listener.onResourceCreation();
683 private void resourceDeletedUINotification() {
684 synchronized (resourceListChangedUIListeners) {
685 if (resourceListChangedUIListeners.size() > 0) {
686 IResourceListChangedUIListener listener;
687 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
689 while (listenerItr.hasNext()) {
690 listener = listenerItr.next();
691 if (null != listener) {
692 listener.onResourceDeletion();
699 private void resourceSelectionChangedUINotification() {
700 synchronized (resourceSelectionChangedUIListeners) {
701 if (resourceSelectionChangedUIListeners.size() > 0) {
702 IResourceSelectionChangedUIListener listener;
703 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
705 while (listenerItr.hasNext()) {
706 listener = listenerItr.next();
707 if (null != listener) {
708 listener.onResourceSelectionChange();
715 private void resourceModelChangedUINotification(
716 ModelChangeNotificationType notificationType, String resourceURI) {
717 synchronized (resourceModelChangedUIListeners) {
718 if (resourceModelChangedUIListeners.size() > 0
719 && notificationType != ModelChangeNotificationType.NONE
720 && null != resourceURI) {
721 IResourceModelChangedUIListener listener;
722 Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
724 while (listenerItr.hasNext()) {
725 listener = listenerItr.next();
726 if (null != listener) {
727 listener.onResourceModelChange(notificationType,
735 private void resourceAutomationStartedUINotification(String resourceURI) {
736 synchronized (automationUIListeners) {
737 if (automationUIListeners.size() > 0 && null != resourceURI) {
738 IAutomationUIListener listener;
739 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
741 while (listenerItr.hasNext()) {
742 listener = listenerItr.next();
743 if (null != listener) {
744 listener.onResourceAutomationStart(resourceURI);
751 private void automationCompleteUINotification(String resourceURI,
753 synchronized (automationUIListeners) {
754 if (automationUIListeners.size() > 0 && null != resourceURI) {
755 IAutomationUIListener listener;
756 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
758 while (listenerItr.hasNext()) {
759 listener = listenerItr.next();
760 if (null != listener) {
761 listener.onAutomationComplete(resourceURI, attName);
768 public List<String> getResourceTypeList() {
769 List<String> typeList = null;
770 synchronized (resourceMap) {
771 if (resourceMap.size() > 0) {
772 typeList = new ArrayList<String>();
773 Set<String> typeSet = resourceMap.keySet();
774 Iterator<String> typeItr = typeSet.iterator();
775 while (typeItr.hasNext()) {
776 typeList.add(typeItr.next());
783 public boolean isTypeExist(String resType) {
784 synchronized (resourceMap) {
785 if (resourceMap.containsKey(resType)) {
792 public List<String> getURIListOfResourceType(String type) {
793 List<String> uriList = null;
794 synchronized (resourceMap) {
796 Map<String, SimulatorResource> typeMap = resourceMap.get(type);
797 if (null != typeMap) {
798 Set<String> keySet = typeMap.keySet();
799 uriList = new ArrayList<String>();
800 Iterator<String> keyItr = keySet.iterator();
801 while (keyItr.hasNext()) {
802 uriList.add(keyItr.next());
810 public void resourceSelectionChanged(final String selectedItem) {
814 // Check whether the item selected is a resource or resource
816 if (isTypeExist(selectedItem)) {
817 // Given item is a resource Type
818 setCurrentResourceInSelection(null);
820 // Given item is a resource URI
821 SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
822 if (null != resource) {
823 setCurrentResourceInSelection(resource);
825 setCurrentResourceInSelection(null);
828 // Notify all observers for resource selection change event
829 resourceSelectionChangedUINotification();
834 public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
835 if (null != resource) {
839 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
841 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
842 propName = Constants.META_PROPERTIES[index];
843 if (propName.equals(Constants.RESOURCE_URI)) {
844 propValue = resource.getResourceURI();
845 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
846 propValue = resource.getResourceType();
847 } else if (propName.equals(Constants.RESOURCE_UID)) {
848 // propValue = resource.getResourceUID();
849 propValue = "Dummy123"; // Temporarily adding dummy value to
851 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
852 // propValue = resource.getConnectivityType();
853 propValue = "IP"; // Temporarily adding dummy value to see
858 if (null != propValue) {
859 metaPropertyList.add(new MetaProperty(propName, propValue));
863 return metaPropertyList;
868 public List<ResourceAttribute> getAttributes(SimulatorResource resource) {
869 List<ResourceAttribute> attList = null;
870 if (null != resource) {
871 Map<String, ResourceAttribute> attMap = resource
872 .getResourceAttributesMap();
873 if (null != attMap && attMap.size() > 0) {
874 attList = new ArrayList<ResourceAttribute>();
875 Set<String> attNameSet = attMap.keySet();
877 ResourceAttribute attribute;
878 // ResourceAttribute attributeClone;
879 Iterator<String> attNameItr = attNameSet.iterator();
880 while (attNameItr.hasNext()) {
881 attName = attNameItr.next();
882 attribute = attMap.get(attName);
883 if (null != attribute) {
885 // ResourceAttribute.clone(attribute);
886 attList.add(attribute);
894 public void attributeValueUpdated(SimulatorResource resource,
895 String attributeName, String value) {
896 if (null != resource && null != attributeName && null != value) {
897 SimulatorResourceServer server = resource.getResourceServer();
898 if (null != server) {
899 server.updateAttributeStringN(attributeName, value);
904 private ModelChangeNotificationType compareAndUpdateLocalAttributes(
905 Map<String, ResourceAttribute> resourceAttributeMapOld,
906 Map<String, ResourceAttribute> resourceAttributeMapNew) {
907 ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
908 if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
909 Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
910 Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
912 ResourceAttribute attributeOld;
913 ResourceAttribute attributeNew;
918 while (attributeMapOldItr.hasNext()) {
919 attName = attributeMapOldItr.next();
920 if (resourceAttributeMapNew.containsKey(attName)) {
921 attributeOld = resourceAttributeMapOld.get(attName);
922 attributeNew = resourceAttributeMapNew.get(attName);
923 // Copy the attribute value from new to old if the value
925 // Comparing only the attribute's value considering the
926 // fact that only the value can be changed
927 if (null != attributeOld && null != attributeNew) {
928 attValueOld = attributeOld.getAttributeValue();
929 attValueNew = attributeNew.getAttributeValue();
931 oldValueStr = String.valueOf(attValueOld);
932 newValueStr = String.valueOf(attValueNew);
934 if (null != oldValueStr && null != newValueStr) {
935 if (!oldValueStr.equals(newValueStr)) {
936 attributeOld.setAttributeValue(attValueNew);
937 notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
941 resourceAttributeMapNew.remove(attName);
943 // Attribute doesn't exist in the new model. Hence
944 // removing it from the model.
945 resourceAttributeMapOld.remove(attName);
946 notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
949 // Check for new attributes in the new model
950 if (resourceAttributeMapNew.size() > 0) {
951 Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
952 Iterator<String> remainingAttItr = remainingAttSet.iterator();
953 ResourceAttribute attribute;
954 while (remainingAttItr.hasNext()) {
955 attName = remainingAttItr.next();
956 if (null != attName) {
957 attribute = resourceAttributeMapNew.get(attName);
958 if (null != attribute) {
959 resourceAttributeMapOld.put(attName, attribute);
963 notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
966 return notificationType;
969 public int startAutomation(SimulatorResource resource,
970 ResourceAttribute attribute, AutomationType autoType,
971 int autoUpdateInterval) {
973 if (null != resource && null != attribute) {
974 SimulatorResourceServer resourceServerN = resource
975 .getResourceServer();
976 if (null != resourceServerN) {
977 String attrName = attribute.getAttributeName();
978 autoId = resourceServerN.startAttributeAutomation(attrName,
979 autoType.ordinal(), automationListener);
981 attribute.setAutomationId(autoId);
983 attribute.setAutomationInProgress(false);
984 resource.setAttributeAutomationInProgress(false);
991 public void stopAutomation(SimulatorResource resource, int autoId) {
992 if (null != resource) {
993 SimulatorResourceServer resourceServerN = resource
994 .getResourceServer();
995 if (null != resourceServerN) {
996 resourceServerN.stopAutomation(autoId);
1001 private ResourceAttribute getAttributeWithGivenAutomationId(
1002 SimulatorResource resource, int automationId) {
1003 ResourceAttribute targetAttribute = null;
1004 if (null != resource) {
1005 Map<String, ResourceAttribute> attributeMap = resource
1006 .getResourceAttributesMap();
1007 if (null != attributeMap) {
1008 Set<String> attNameSet = attributeMap.keySet();
1009 Iterator<String> attNameItr = attNameSet.iterator();
1011 ResourceAttribute attribute;
1012 while (attNameItr.hasNext()) {
1013 attName = attNameItr.next();
1014 if (null != attName) {
1015 attribute = attributeMap.get(attName);
1016 if (null != attribute) {
1017 if (attribute.isAutomationInProgress()
1018 && (attribute.getAutomationId() == automationId)) {
1019 targetAttribute = attribute;
1027 return targetAttribute;
1030 public boolean startResourceAutomationUIRequest(final String resourceURI) {
1031 if (null == resourceURI) {
1034 boolean status = false;
1035 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1036 if (null != resource) {
1037 changeResourceLevelAutomationStatus(resource, true);
1039 // Invoke the native automation method
1040 SimulatorResourceServer resourceServer = resource
1041 .getResourceServer();
1042 if (null != resourceServer) {
1043 // TODO: Temporarily handling the normal one-time automation for
1045 int autoId = resourceServer.startResourceAutomation(
1046 AutomationType.NORMAL.ordinal(), automationListener);
1048 // Automation request failed and hence status is being
1050 changeResourceLevelAutomationStatus(resource, false);
1052 // Automation request accepted.
1053 resource.setAutomationId(autoId);
1055 // Notify the UI listeners in a different thread.
1056 Thread notifyThread = new Thread() {
1058 resourceAutomationStartedUINotification(resourceURI);
1061 notifyThread.setPriority(Thread.MAX_PRIORITY);
1062 notifyThread.start();
1071 public void stopResourceAutomationUIRequest(final String resourceURI) {
1072 Thread stopThread = new Thread() {
1074 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1075 if (null == resource) {
1078 int autoId = resource.getAutomationId();
1082 SimulatorResourceServer resourceServer = resource
1083 .getResourceServer();
1084 if (null == resourceServer) {
1087 // Call native method
1088 resourceServer.stopAutomation(autoId);
1090 // Invoke the automation complete callback
1091 automationListener.onAutomationComplete(resourceURI, autoId);
1097 // Changes the automation state of the resource and its attributes
1098 private void changeResourceLevelAutomationStatus(
1099 SimulatorResource resource, boolean status) {
1101 Map<String, ResourceAttribute> attributeMap = resource
1102 .getResourceAttributesMap();
1103 if (null != attributeMap) {
1104 Set<String> attrNameSet = attributeMap.keySet();
1105 Iterator<String> attrNameItr = attrNameSet.iterator();
1107 ResourceAttribute attribute;
1108 while (attrNameItr.hasNext()) {
1109 attrName = attrNameItr.next();
1110 attribute = attributeMap.get(attrName);
1111 if (null != attribute) {
1112 attribute.setAutomationInProgress(status);
1116 resource.setResourceAutomationInProgress(status);
1119 public boolean isResourceAutomationStarted(String resourceURI) {
1120 boolean status = false;
1121 if (null == resourceURI) {
1125 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1126 if (null != resource) {
1127 status = resource.isResourceAutomationInProgress();
1132 public boolean isAttributeAutomationStarted(String resourceURI) {
1133 boolean status = false;
1134 if (null == resourceURI) {
1137 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1138 if (null != resource) {
1139 status = resource.isAttributeAutomationInProgress();
1144 public void shutdown() {
1145 threadHandle.interrupt();