2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.serviceprovider.manager;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.LinkedList;
26 import java.util.List;
30 import oic.simulator.serviceprovider.Activator;
31 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
32 import oic.simulator.serviceprovider.listener.IObserverListChangedUIListener;
33 import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
34 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
35 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
36 import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
37 import oic.simulator.serviceprovider.resource.MetaProperty;
38 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
39 import oic.simulator.serviceprovider.resource.SimulatorResource;
40 import oic.simulator.serviceprovider.utils.Constants;
41 import oic.simulator.serviceprovider.utils.Utility;
43 import org.eclipse.swt.graphics.Image;
44 import org.oic.simulator.IAutomation;
45 import org.oic.simulator.ILogger.Level;
46 import org.oic.simulator.ResourceAttribute;
47 import org.oic.simulator.ResourceAttribute.Range;
48 import org.oic.simulator.ResourceAttribute.Type;
49 import org.oic.simulator.SimulatorException;
50 import org.oic.simulator.SimulatorManager;
51 import org.oic.simulator.SimulatorResourceModel;
52 import org.oic.simulator.serviceprovider.AutomationType;
53 import org.oic.simulator.serviceprovider.IObserver;
54 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
55 import org.oic.simulator.serviceprovider.ObserverInfo;
56 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
59 * This class acts as an interface between the simulator java SDK and the
60 * various UI modules. It maintains all the details of resources and provides
61 * other UI modules with the information required. It also handles model change,
62 * automation, and observer related events from native layer and propagates
63 * those events to the registered UI listeners.
65 public class ResourceManager {
67 private Map<String, Map<String, SimulatorResource>> resourceMap;
69 private Map<String, ArrayList<String>> orderedResourceUriMap;
71 private Map<String, String> displayNameMap;
73 private SimulatorResource currentResourceInSelection;
75 private List<IResourceListChangedUIListener> resourceListChangedUIListeners;
77 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
79 private List<IResourceModelChangedUIListener> resourceModelChangedUIListeners;
81 private List<IAutomationUIListener> automationUIListeners;
83 private List<IObserverListChangedUIListener> observerUIListeners;
85 private IResourceModelChangedListener resourceModelChangeListener;
87 private IAutomation automationListener;
89 private IObserver observer;
91 private NotificationSynchronizerThread synchronizerThread;
93 private Thread threadHandle;
95 public ResourceManager() {
96 resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
97 orderedResourceUriMap = new HashMap<String, ArrayList<String>>();
98 displayNameMap = new HashMap<String, String>();
100 resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
101 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
102 resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
103 automationUIListeners = new ArrayList<IAutomationUIListener>();
104 observerUIListeners = new ArrayList<IObserverListChangedUIListener>();
106 resourceModelChangeListener = new IResourceModelChangedListener() {
109 public void onResourceModelChanged(final String resourceURI,
110 final SimulatorResourceModel resourceModelN) {
111 synchronizerThread.addToQueue(new Runnable() {
115 if (null == resourceURI || null == resourceModelN) {
118 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
119 if (null == resource) {
123 ModelChangeNotificationType notificationType;
124 // Fetch the resource attributes
125 Map<String, LocalResourceAttribute> resourceAttributeMapNew;
126 resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
127 if (null == resourceAttributeMapNew) {
128 resource.setResourceAttributesMap(null);
129 resourceModelChangedUINotification(
130 ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL,
135 // Update the resource with new model data
136 Map<String, LocalResourceAttribute> resourceAttributeMapOld;
137 resourceAttributeMapOld = resource
138 .getResourceAttributesMap();
139 if (null == resourceAttributeMapOld) {
140 resource.setResourceAttributesMap(resourceAttributeMapNew);
141 resourceModelChangedUINotification(
142 ModelChangeNotificationType.ATTRIBUTE_ADDED,
146 Set<LocalResourceAttribute> valueChangeSet = new HashSet<LocalResourceAttribute>();
147 notificationType = compareAndUpdateLocalAttributes(
148 resourceAttributeMapOld,
149 resourceAttributeMapNew, valueChangeSet);
150 if (notificationType != ModelChangeNotificationType.NONE) {
151 // Update the UI listeners
152 resourceModelChangedUINotification(
153 notificationType, resourceURI,
161 automationListener = new IAutomation() {
164 public void onAutomationComplete(final String resourceURI,
165 final int automationId) {
166 synchronizerThread.addToQueue(new Runnable() {
170 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
171 if (null == resource) {
174 // Checking whether this notification is for an
175 // attribute or a resource
176 if (resource.isResourceAutomationInProgress()) {
177 changeResourceLevelAutomationStatus(resource, false);
178 // Notify the UI listeners
179 automationCompleteUINotification(resourceURI, null);
180 } else if (resource.isAttributeAutomationInProgress()) {
181 // Find the attribute with the given automation id
182 LocalResourceAttribute attribute;
183 attribute = getAttributeWithGivenAutomationId(
184 resource, automationId);
185 if (null != attribute) {
186 attribute.setAutomationInProgress(false);
187 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
188 // Notify the UI listeners
189 automationCompleteUINotification(resourceURI,
190 attribute.getAttributeName());
193 // Ignoring the notification as there are no
194 // known automation for the current resource.
201 observer = new IObserver() {
204 public void onObserverChanged(final String resourceURI,
205 final int status, final ObserverInfo observer) {
209 if (null == resourceURI || null == observer) {
212 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
213 if (null == resource) {
216 // Update the observers information
218 resource.addObserverInfo(observer);
220 resource.removeObserverInfo(observer);
222 // Notify the UI listeners
223 observerListChangedUINotification(resourceURI);
229 synchronizerThread = new NotificationSynchronizerThread();
230 threadHandle = new Thread(synchronizerThread);
231 threadHandle.setName("Simulator service provider event queue");
232 threadHandle.start();
235 private static class NotificationSynchronizerThread implements Runnable {
237 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
241 while (!Thread.interrupted()) {
242 synchronized (this) {
244 while (notificationQueue.isEmpty()) {
248 } catch (InterruptedException e) {
254 synchronized (this) {
255 thread = notificationQueue.pop();
259 } catch (Exception e) {
260 if (e instanceof InterruptedException) {
268 public void addToQueue(Runnable event) {
269 synchronized (this) {
270 notificationQueue.add(event);
276 public void addResourceListChangedUIListener(
277 IResourceListChangedUIListener resourceListChangedUIListener) {
278 synchronized (resourceListChangedUIListeners) {
279 resourceListChangedUIListeners.add(resourceListChangedUIListener);
283 public void addResourceSelectionChangedUIListener(
284 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
285 synchronized (resourceSelectionChangedUIListeners) {
286 resourceSelectionChangedUIListeners
287 .add(resourceSelectionChangedUIListener);
291 public void addResourceModelChangedUIListener(
292 IResourceModelChangedUIListener resourceModelChangedUIListener) {
293 synchronized (resourceModelChangedUIListeners) {
294 resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
298 public void addAutomationUIListener(
299 IAutomationUIListener automationUIListener) {
300 synchronized (automationUIListeners) {
301 automationUIListeners.add(automationUIListener);
305 public void addObserverListChangedUIListener(
306 IObserverListChangedUIListener observerListChangedUIListener) {
307 synchronized (observerUIListeners) {
308 observerUIListeners.add(observerListChangedUIListener);
312 public void removeResourceListChangedUIListener(
313 IResourceListChangedUIListener listener) {
314 synchronized (resourceListChangedUIListeners) {
315 if (null != listener && resourceListChangedUIListeners.size() > 0) {
316 resourceListChangedUIListeners.remove(listener);
321 public void removeResourceSelectionChangedUIListener(
322 IResourceSelectionChangedUIListener listener) {
323 synchronized (resourceSelectionChangedUIListeners) {
325 && resourceSelectionChangedUIListeners.size() > 0) {
326 resourceSelectionChangedUIListeners.remove(listener);
331 public void removeResourceModelChangedUIListener(
332 IResourceModelChangedUIListener listener) {
333 synchronized (resourceModelChangedUIListeners) {
334 if (null != listener && resourceModelChangedUIListeners.size() > 0) {
335 resourceModelChangedUIListeners.remove(listener);
340 public void removeAutomationUIListener(IAutomationUIListener listener) {
341 synchronized (automationUIListeners) {
342 if (null != listener && automationUIListeners.size() > 0) {
343 automationUIListeners.remove(listener);
348 public void removeObserverListChangedUIListener(
349 IObserverListChangedUIListener listener) {
350 synchronized (observerUIListeners) {
351 if (null != listener && observerUIListeners.size() > 0) {
352 observerUIListeners.remove(listener);
357 public synchronized SimulatorResource getCurrentResourceInSelection() {
358 return currentResourceInSelection;
361 public synchronized void setCurrentResourceInSelection(
362 SimulatorResource resource) {
363 this.currentResourceInSelection = resource;
366 private void addResourceUriToOrderedMap(String resourceType,
367 String resourceURI) {
368 if (null != resourceURI && null != resourceType) {
369 synchronized (orderedResourceUriMap) {
370 ArrayList<String> uriListForType = orderedResourceUriMap
372 if (null == uriListForType) {
373 uriListForType = new ArrayList<String>();
374 orderedResourceUriMap.put(resourceType, uriListForType);
376 uriListForType.add(resourceURI);
381 private void removeResourceUriFromOrderedMap(String resourceType,
382 String resourceURI) {
383 synchronized (orderedResourceUriMap) {
384 if (null != resourceURI && null != resourceType) {
385 ArrayList<String> uriListForType = orderedResourceUriMap
387 if (null != uriListForType) {
388 uriListForType.remove(resourceURI);
389 if (uriListForType.size() < 1) {
390 orderedResourceUriMap.remove(resourceType);
393 } else if (null != resourceType) {
394 orderedResourceUriMap.remove(resourceType);
396 orderedResourceUriMap.clear();
401 private void addResourceToMap(SimulatorResource simulatorResource) {
402 if (null != simulatorResource) {
403 synchronized (resourceMap) {
404 Map<String, SimulatorResource> resourceTypeMap;
405 resourceTypeMap = resourceMap.get(simulatorResource
407 if (null == resourceTypeMap) {
408 resourceTypeMap = new HashMap<String, SimulatorResource>();
409 resourceMap.put(simulatorResource.getResourceType(),
412 resourceTypeMap.put(simulatorResource.getResourceURI(),
418 private void addResourceToMap(String resourceType,
419 Map<String, SimulatorResource> newResourceTypeMap) {
420 if (null != resourceType && null != newResourceTypeMap) {
421 synchronized (resourceMap) {
422 Map<String, SimulatorResource> resourceTypeMap = resourceMap
424 if (null != resourceTypeMap) {
425 resourceTypeMap.putAll(newResourceTypeMap);
427 resourceMap.put(resourceType, newResourceTypeMap);
433 private void removeResourceFromMap(String resourceType, String resourceURI) {
434 if (null != resourceURI && null != resourceType) {
435 synchronized (resourceMap) {
436 Map<String, SimulatorResource> resourceTypeMap = resourceMap
438 if (null != resourceTypeMap) {
439 resourceTypeMap.remove(resourceURI);
440 if (resourceTypeMap.size() < 1) {
441 resourceMap.remove(resourceType);
448 public boolean isResourceExist(String resourceURI) {
449 boolean result = false;
450 if (null != resourceURI) {
451 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
452 if (null != resource) {
459 public void addResourceDisplayName(String displayName, String completeURI) {
460 synchronized (displayNameMap) {
461 displayNameMap.put(displayName, completeURI);
465 public void removeResourceDisplayName(String displayName) {
466 synchronized (displayNameMap) {
467 displayNameMap.remove(displayName);
471 public boolean isDisplayName(String displayName) {
472 boolean exist = false;
473 synchronized (displayNameMap) {
474 exist = displayNameMap.containsKey(displayName);
479 public String getCompleteUriFromDisplayName(String displayName) {
480 String completeURI = null;
481 synchronized (displayNameMap) {
482 completeURI = displayNameMap.get(displayName);
487 public void createResource(final String configFilePath) {
491 SimulatorResourceServer resourceServerN;
493 resourceServerN = SimulatorManager.createResource(
494 configFilePath, resourceModelChangeListener);
495 } catch (SimulatorException e) {
499 .log(Level.ERROR.ordinal(),
501 "[" + e.getClass().getSimpleName() + "]"
502 + e.code().toString() + "-"
506 SimulatorResource simulatorResource;
507 simulatorResource = fetchResourceData(resourceServerN);
508 if (null != simulatorResource) {
509 String uri = simulatorResource.getResourceURI();
510 addResourceToMap(simulatorResource);
511 addResourceUriToOrderedMap(
512 simulatorResource.getResourceType(), uri);
513 resourceCreatedUINotification();
515 // Form the display URI
516 String displayName = Utility.uriToDisplayName(uri);
517 if (null != displayName) {
518 addResourceDisplayName(displayName, uri);
523 .log(Level.ERROR.ordinal(),
525 "Converting the URI to display format for "
529 // Set the observer for the created resource
531 resourceServerN.setObserverCallback(observer);
532 } catch (SimulatorException e) {
536 .log(Level.ERROR.ordinal(),
538 "[" + e.getClass().getSimpleName()
539 + "]" + e.code().toString()
540 + "-" + e.message());
543 // Print the resource data
544 simulatorResource.printResourceInfo();
550 public void createResource(final String configFilePath,
551 final int noOfInstances) {
555 Map<String, SimulatorResource> resourceTypeMap;
556 SimulatorResourceServer[] simulatorResourceServers = null;
558 simulatorResourceServers = SimulatorManager.createResource(
559 configFilePath, noOfInstances,
560 resourceModelChangeListener);
561 } catch (SimulatorException e) {
565 .log(Level.ERROR.ordinal(),
567 "[" + e.getClass().getSimpleName() + "]"
568 + e.code().toString() + "-"
572 if (null == simulatorResourceServers) {
575 resourceTypeMap = new HashMap<String, SimulatorResource>();
576 SimulatorResource resource;
579 for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
580 resource = fetchResourceData(resourceServerN);
581 if (null != resource) {
582 uri = resource.getResourceURI();
583 resourceTypeMap.put(uri, resource);
584 addResourceUriToOrderedMap(resource.getResourceType(),
587 // Form the display URI
588 displayName = Utility.uriToDisplayName(uri);
589 if (null != displayName) {
590 addResourceDisplayName(displayName, uri);
595 .log(Level.ERROR.ordinal(),
597 "Converting the URI to display format for "
601 // Set the observer for the created resource
603 resourceServerN.setObserverCallback(observer);
604 } catch (SimulatorException e) {
608 .log(Level.ERROR.ordinal(),
610 "[" + e.getClass().getSimpleName()
611 + "]" + e.code().toString()
612 + "-" + e.message());
616 // Find the resourceType and add it to the local data
617 // structure and notify UI Listeners
618 if (resourceTypeMap.size() > 0) {
620 Set<String> uriSet = resourceTypeMap.keySet();
621 Iterator<String> itr = uriSet.iterator();
623 SimulatorResource simResource = resourceTypeMap.get(itr
625 if (null != simResource) {
626 resourceType = simResource.getResourceType();
628 addResourceToMap(resourceType, resourceTypeMap);
629 resourceCreatedUINotification();
637 private SimulatorResource fetchResourceData(
638 SimulatorResourceServer resourceServerN) {
639 SimulatorResource simulatorResource = null;
640 if (null != resourceServerN) {
641 simulatorResource = new SimulatorResource();
642 simulatorResource.setResourceServer(resourceServerN);
643 simulatorResource.setResourceURI(resourceServerN.getURI());
645 .setResourceType(resourceServerN.getResourceType());
646 simulatorResource.setResourceName(resourceServerN.getName());
647 simulatorResource.setResourceInterface(resourceServerN
648 .getInterfaceType());
650 SimulatorResourceModel resourceModelN;
652 resourceModelN = resourceServerN.getModel();
653 } catch (SimulatorException e) {
657 .log(Level.ERROR.ordinal(),
659 "[" + e.getClass().getSimpleName() + "]"
660 + e.code().toString() + "-"
664 if (null != resourceModelN) {
665 simulatorResource.setResourceModel(resourceModelN);
667 // Fetch the resource attributes
668 Map<String, LocalResourceAttribute> resourceAttributeMap;
669 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
670 if (null != resourceAttributeMap) {
672 .setResourceAttributesMap(resourceAttributeMap);
676 return simulatorResource;
679 private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
680 SimulatorResourceModel resourceModelN) {
681 Map<String, LocalResourceAttribute> resourceAttributeMap = null;
682 if (null != resourceModelN) {
683 Map<String, ResourceAttribute> attributeMapN;
685 attributeMapN = resourceModelN.getAttributes();
686 } catch (SimulatorException e) {
690 .log(Level.ERROR.ordinal(),
692 "[" + e.getClass().getSimpleName() + "]"
693 + e.code().toString() + "-"
697 if (null != attributeMapN) {
698 resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
700 Set<String> attNameSet = attributeMapN.keySet();
702 ResourceAttribute attributeN;
703 LocalResourceAttribute attribute;
704 Iterator<String> attNameItr = attNameSet.iterator();
705 while (attNameItr.hasNext()) {
706 attName = attNameItr.next();
707 attributeN = attributeMapN.get(attName);
708 if (null != attributeN) {
709 attribute = new LocalResourceAttribute();
710 attribute.setResourceAttribute(attributeN);
712 // Set the attribute value
713 Object valueObj = attributeN.getValue();
714 if (null != valueObj) {
715 attribute.setAttributeValue(valueObj);
718 // Set the attribute value list.
719 attribute.setAttValues(getValueList(attributeN));
721 // Initially disabling the automation
722 attribute.setAutomationInProgress(false);
724 // TODO: Temporarily setting the interval to 500.
725 // This value should come from the native layer.
726 // Native implementation is in progress.
728 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
730 // Setting the default automation type
732 .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
734 resourceAttributeMap.put(attName, attribute);
739 return resourceAttributeMap;
742 // This method gives all known possible values of the attribute in string
743 // format. It takes allowed values or range of values whichever is available
744 private List<String> getValueList(ResourceAttribute attributeN) {
745 Object[] allowedValues = attributeN.getAllowedValues();
746 List<String> valueList = new ArrayList<String>();
747 if (null != allowedValues && allowedValues.length > 0) {
748 for (Object value : allowedValues) {
750 valueList.add(String.valueOf(value));
754 Type valueType = attributeN.getBaseType();
755 Range range = attributeN.getRange();
757 Object min = range.getMin();
758 Object max = range.getMax();
759 if (valueType == Type.INT) {
760 int minI = (Integer) min;
761 int maxI = (Integer) max;
762 for (int value = minI; value <= maxI; value++) {
763 valueList.add(String.valueOf(value));
765 } else if (valueType == Type.DOUBLE) {
766 double minD = (Double) min;
767 double maxD = (Double) max;
768 for (double value = minD; value <= maxD; value++) {
769 valueList.add(String.valueOf(value));
774 Object attValue = attributeN.getValue();
775 if (valueList.size() < 1 && null != attValue) {
776 valueList.add(String.valueOf(attValue));
781 public void deleteResourceByURI(final String resourceURI) {
782 if (null != resourceURI) {
786 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
787 if (null != resource) {
788 String resourceType = resource.getResourceType();
790 // Unregister the resource from the platform
791 deleteResource(resource);
793 // Delete from the local data structure
794 deleteLocalResourceDetails(resourceType, resourceURI);
796 // Notify the UI listener for removing this resource
798 resourceDeletedUINotification();
800 if (null != currentResourceInSelection
801 && resource == currentResourceInSelection) {
802 // Listeners might query the resource being deleted
803 // if exists. So set the currently selection to
805 setCurrentResourceInSelection(null);
807 // Notify all observers for resource selection
809 resourceSelectionChangedUINotification();
817 private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
818 SimulatorResource resource = null;
819 if (null != resourceURI) {
820 synchronized (resourceMap) {
821 Set<String> typeSet = resourceMap.keySet();
822 Iterator<String> typeItr = typeSet.iterator();
824 Map<String, SimulatorResource> resourceTypeMap;
825 while (typeItr.hasNext()) {
826 resourceType = typeItr.next();
827 resourceTypeMap = resourceMap.get(resourceType);
828 if (null != resourceTypeMap) {
829 resource = resourceTypeMap.get(resourceURI);
830 if (null != resource) {
840 private void deleteResource(SimulatorResource resource) {
841 if (null != resource) {
842 SimulatorResourceServer resourceServerN = resource
843 .getResourceServer();
844 if (null != resourceServerN) {
846 SimulatorManager.deleteResource(resourceServerN);
847 } catch (SimulatorException e) {
851 .log(Level.ERROR.ordinal(),
853 "[" + e.getClass().getSimpleName() + "]"
854 + e.code().toString() + "-"
861 public void deleteResourceByType(final String resourceType) {
862 if (null != resourceType) {
866 // Unregister the resources from the platform
867 deleteResource(resourceType);
869 // Delete from the local data structure
870 deleteLocalResourceDetails(resourceType, null);
872 // Notify the UI listener for removing this resource from UI
873 resourceDeletedUINotification();
875 if (null != currentResourceInSelection
876 && resourceType.equals(currentResourceInSelection
877 .getResourceType())) {
878 // Listeners might query the resource being deleted if
879 // exists. So set the currently selection to null.
880 setCurrentResourceInSelection(null);
882 // Notify all observers for resource selection change
884 resourceSelectionChangedUINotification();
891 private void deleteResource(String resourceType) {
892 if (null != resourceType) {
894 SimulatorManager.deleteResources(resourceType);
895 } catch (SimulatorException e) {
899 .log(Level.ERROR.ordinal(),
901 "[" + e.getClass().getSimpleName() + "]"
902 + e.code().toString() + "-"
908 public void deleteAllResources() {
912 // Unregister the resources from the platform
915 // Delete from the local data structure
916 deleteLocalResourceDetails(null, null);
918 // Notify the UI listener for removing this resource from UI
919 resourceDeletedUINotification();
921 // Listeners might query the resource being deleted if exists.
922 // So set the currently selection to null.
923 setCurrentResourceInSelection(null);
925 // Notify all observers for resource selection change event
926 resourceSelectionChangedUINotification();
931 private void deleteResource() {
933 SimulatorManager.deleteResources(null);
934 } catch (SimulatorException e) {
938 .log(Level.ERROR.ordinal(),
940 "[" + e.getClass().getSimpleName() + "]"
941 + e.code().toString() + "-" + e.message());
945 private void deleteLocalResourceDetails(String resourceType,
946 String resourceURI) {
947 if (null != resourceType && null != resourceURI) {
948 removeResourceFromMap(resourceType, resourceURI);
949 removeResourceUriFromOrderedMap(resourceType, resourceURI);
951 synchronized (resourceMap) {
952 if (null != resourceType) {
953 removeResourceUriFromOrderedMap(resourceType, null);
954 resourceMap.remove(resourceType);
957 removeResourceUriFromOrderedMap(null, null);
961 // Remove the display name from the map
962 if (null != resourceURI) {
963 String dispName = Utility.uriToDisplayName(resourceURI);
964 if (null != dispName)
965 removeResourceDisplayName(dispName);
969 private void resourceCreatedUINotification() {
970 synchronized (resourceListChangedUIListeners) {
971 if (resourceListChangedUIListeners.size() > 0) {
972 IResourceListChangedUIListener listener;
973 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
975 while (listenerItr.hasNext()) {
976 listener = listenerItr.next();
977 if (null != listener) {
978 listener.onResourceCreation();
985 private void resourceDeletedUINotification() {
986 synchronized (resourceListChangedUIListeners) {
987 if (resourceListChangedUIListeners.size() > 0) {
988 IResourceListChangedUIListener listener;
989 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
991 while (listenerItr.hasNext()) {
992 listener = listenerItr.next();
993 if (null != listener) {
994 listener.onResourceDeletion();
1001 private void resourceSelectionChangedUINotification() {
1002 synchronized (resourceSelectionChangedUIListeners) {
1003 if (resourceSelectionChangedUIListeners.size() > 0) {
1004 IResourceSelectionChangedUIListener listener;
1005 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
1007 while (listenerItr.hasNext()) {
1008 listener = listenerItr.next();
1009 if (null != listener) {
1010 listener.onResourceSelectionChange();
1017 private void resourceModelChangedUINotification(
1018 ModelChangeNotificationType notificationType, String resourceURI,
1019 Set<LocalResourceAttribute> valueChangeSet) {
1020 synchronized (resourceModelChangedUIListeners) {
1021 if (resourceModelChangedUIListeners.size() > 0
1022 && notificationType != ModelChangeNotificationType.NONE
1023 && null != resourceURI) {
1024 IResourceModelChangedUIListener listener;
1025 Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
1027 while (listenerItr.hasNext()) {
1028 listener = listenerItr.next();
1029 if (null != listener) {
1030 listener.onResourceModelChange(notificationType,
1031 resourceURI, valueChangeSet);
1038 private void resourceAutomationStartedUINotification(String resourceURI) {
1039 synchronized (automationUIListeners) {
1040 if (automationUIListeners.size() > 0 && null != resourceURI) {
1041 IAutomationUIListener listener;
1042 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1044 while (listenerItr.hasNext()) {
1045 listener = listenerItr.next();
1046 if (null != listener) {
1047 listener.onResourceAutomationStart(resourceURI);
1054 private void automationCompleteUINotification(String resourceURI,
1056 synchronized (automationUIListeners) {
1057 if (automationUIListeners.size() > 0 && null != resourceURI) {
1058 IAutomationUIListener listener;
1059 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1061 while (listenerItr.hasNext()) {
1062 listener = listenerItr.next();
1063 if (null != listener) {
1064 listener.onAutomationComplete(resourceURI, attName);
1071 private void observerListChangedUINotification(String resourceURI) {
1072 synchronized (observerUIListeners) {
1073 if (observerUIListeners.size() > 0 && null != resourceURI) {
1074 IObserverListChangedUIListener listener;
1075 Iterator<IObserverListChangedUIListener> listenerItr = observerUIListeners
1077 while (listenerItr.hasNext()) {
1078 listener = listenerItr.next();
1079 if (null != listener) {
1080 listener.onObserverListChanged(resourceURI);
1087 public List<String> getResourceTypeList() {
1088 List<String> typeList = null;
1089 synchronized (resourceMap) {
1090 if (resourceMap.size() > 0) {
1091 typeList = new ArrayList<String>();
1092 Set<String> typeSet = resourceMap.keySet();
1093 Iterator<String> typeItr = typeSet.iterator();
1094 while (typeItr.hasNext()) {
1095 typeList.add(typeItr.next());
1102 public boolean isTypeExist(String resType) {
1103 synchronized (resourceMap) {
1104 if (resourceMap.containsKey(resType)) {
1111 public List<String> getURIList() {
1112 List<String> list = null;
1113 synchronized (orderedResourceUriMap) {
1114 Set<String> typeSet = orderedResourceUriMap.keySet();
1115 List<String> typeList = Utility.convertSetToList(typeSet);
1116 if (null == typeList || typeList.size() < 1) {
1119 list = new ArrayList<String>();
1122 Collections.sort(typeList);
1124 // Add all URIs to the output list
1125 Iterator<String> typeItr = typeList.iterator();
1126 while (typeItr.hasNext()) {
1127 List<String> l = orderedResourceUriMap.get(typeItr.next());
1136 public void resourceSelectionChanged(final String selectedItem) {
1140 // Check whether the item selected is a resource or resource
1142 if (isTypeExist(selectedItem)) {
1143 // Given item is a resource Type
1144 setCurrentResourceInSelection(null);
1146 // Given item is a resource URI
1147 SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
1148 if (null != resource) {
1149 setCurrentResourceInSelection(resource);
1151 setCurrentResourceInSelection(null);
1154 // Notify all observers for resource selection change event
1155 resourceSelectionChangedUINotification();
1160 public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
1161 if (null != resource) {
1165 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1167 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1168 propName = Constants.META_PROPERTIES[index];
1169 if (propName.equals(Constants.RESOURCE_URI)) {
1170 propValue = resource.getResourceURI();
1171 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1172 propValue = resource.getResourceType();
1173 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1174 // TODO: Temporarily ignoring till the implementation.
1179 if (null != propValue) {
1180 metaPropertyList.add(new MetaProperty(propName, propValue));
1184 return metaPropertyList;
1189 public List<LocalResourceAttribute> getAttributes(SimulatorResource resource) {
1190 List<LocalResourceAttribute> attList = null;
1191 if (null != resource) {
1192 Map<String, LocalResourceAttribute> attMap = resource
1193 .getResourceAttributesMap();
1194 if (null != attMap && attMap.size() > 0) {
1195 attList = new ArrayList<LocalResourceAttribute>();
1196 Set<String> attNameSet = attMap.keySet();
1198 LocalResourceAttribute attribute;
1199 Iterator<String> attNameItr = attNameSet.iterator();
1200 while (attNameItr.hasNext()) {
1201 attName = attNameItr.next();
1202 attribute = attMap.get(attName);
1203 if (null != attribute) {
1204 attList.add(attribute);
1212 public void attributeValueUpdated(SimulatorResource resource,
1213 String attributeName, String value) {
1214 if (null != resource && null != attributeName && null != value) {
1215 SimulatorResourceServer server = resource.getResourceServer();
1216 if (null != server) {
1217 LocalResourceAttribute att = resource
1218 .getAttribute(attributeName);
1222 Type baseType = att.getAttValBaseType();
1224 if (baseType == Type.STRING) {
1225 server.updateAttributeString(attributeName, value);
1226 } else if (baseType == Type.INT) {
1229 val = Integer.parseInt(value);
1230 server.updateAttributeInteger(attributeName, val);
1231 } catch (NumberFormatException nfe) {
1234 } else if (baseType == Type.DOUBLE) {
1237 val = Double.parseDouble(value);
1238 server.updateAttributeDouble(attributeName, val);
1239 } catch (NumberFormatException nfe) {
1242 } else if (baseType == Type.BOOL) {
1244 val = Boolean.parseBoolean(value);
1245 server.updateAttributeBoolean(attributeName, val);
1247 } catch (SimulatorException e) {
1251 .log(Level.ERROR.ordinal(),
1253 "[" + e.getClass().getSimpleName() + "]"
1254 + e.code().toString() + "-"
1261 private ModelChangeNotificationType compareAndUpdateLocalAttributes(
1262 Map<String, LocalResourceAttribute> resourceAttributeMapOld,
1263 Map<String, LocalResourceAttribute> resourceAttributeMapNew,
1264 Set<LocalResourceAttribute> valueChangeSet) {
1265 ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
1266 if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
1267 Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
1268 Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
1270 LocalResourceAttribute attributeOld;
1271 LocalResourceAttribute attributeNew;
1276 while (attributeMapOldItr.hasNext()) {
1277 attName = attributeMapOldItr.next();
1278 if (resourceAttributeMapNew.containsKey(attName)) {
1279 attributeOld = resourceAttributeMapOld.get(attName);
1280 attributeNew = resourceAttributeMapNew.get(attName);
1281 // Copy the attribute value from new to old if the value
1283 // Comparing only the attribute's value considering the
1284 // fact that only the value can be changed
1285 if (null != attributeOld && null != attributeNew) {
1286 attValueOld = attributeOld.getAttributeValue();
1287 attValueNew = attributeNew.getAttributeValue();
1289 oldValueStr = String.valueOf(attValueOld);
1290 newValueStr = String.valueOf(attValueNew);
1292 if (null != oldValueStr && null != newValueStr) {
1293 if (!oldValueStr.equals(newValueStr)) {
1294 attributeOld.setAttributeValue(attValueNew);
1295 notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
1296 valueChangeSet.add(attributeOld);
1300 resourceAttributeMapNew.remove(attName);
1302 // Attribute doesn't exist in the new model. Hence
1303 // removing it from the model.
1304 resourceAttributeMapOld.remove(attName);
1305 notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
1308 // Check for new attributes in the new model
1309 if (resourceAttributeMapNew.size() > 0) {
1310 Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
1311 Iterator<String> remainingAttItr = remainingAttSet.iterator();
1312 LocalResourceAttribute attribute;
1313 while (remainingAttItr.hasNext()) {
1314 attName = remainingAttItr.next();
1315 if (null != attName) {
1316 attribute = resourceAttributeMapNew.get(attName);
1317 if (null != attribute) {
1318 resourceAttributeMapOld.put(attName, attribute);
1322 notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
1325 return notificationType;
1328 public int startAutomation(SimulatorResource resource,
1329 LocalResourceAttribute attribute, AutomationType autoType,
1330 int autoUpdateInterval) {
1332 if (null != resource && null != attribute) {
1333 SimulatorResourceServer resourceServerN = resource
1334 .getResourceServer();
1335 if (null != resourceServerN) {
1336 String attrName = attribute.getAttributeName();
1338 autoId = resourceServerN.startAttributeAutomation(attrName,
1339 autoType, autoUpdateInterval, automationListener);
1340 } catch (SimulatorException e) {
1344 .log(Level.ERROR.ordinal(),
1346 "[" + e.getClass().getSimpleName() + "]"
1347 + e.code().toString() + "-"
1352 attribute.setAutomationId(autoId);
1353 attribute.setAutomationType(autoType);
1354 attribute.setAutomationUpdateInterval(autoUpdateInterval);
1355 attribute.setAutomationInProgress(true);
1356 resource.setAttributeAutomationInProgress(true);
1358 attribute.setAutomationInProgress(false);
1365 public void stopAutomation(SimulatorResource resource,
1366 LocalResourceAttribute att, int autoId) {
1367 if (null != resource) {
1368 SimulatorResourceServer resourceServerN = resource
1369 .getResourceServer();
1370 if (null != resourceServerN) {
1372 resourceServerN.stopAutomation(autoId);
1373 } catch (SimulatorException e) {
1377 .log(Level.ERROR.ordinal(),
1379 "[" + e.getClass().getSimpleName() + "]"
1380 + e.code().toString() + "-"
1384 // Change the automation status
1385 att.setAutomationInProgress(false);
1386 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1391 private boolean isAnyAttributeInAutomation(SimulatorResource resource) {
1392 if (null == resource) {
1395 Map<String, LocalResourceAttribute> attMap = resource
1396 .getResourceAttributesMap();
1397 if (null == attMap) {
1400 boolean status = false;
1401 Set<String> keySet = attMap.keySet();
1402 Iterator<String> attItr = keySet.iterator();
1403 while (attItr.hasNext()) {
1404 LocalResourceAttribute attribute = attMap.get(attItr.next());
1405 if (attribute.isAutomationInProgress()) {
1413 private LocalResourceAttribute getAttributeWithGivenAutomationId(
1414 SimulatorResource resource, int automationId) {
1415 LocalResourceAttribute targetAttribute = null;
1416 if (null != resource) {
1417 Map<String, LocalResourceAttribute> attributeMap = resource
1418 .getResourceAttributesMap();
1419 if (null != attributeMap) {
1420 Set<String> attNameSet = attributeMap.keySet();
1421 Iterator<String> attNameItr = attNameSet.iterator();
1423 LocalResourceAttribute attribute;
1424 while (attNameItr.hasNext()) {
1425 attName = attNameItr.next();
1426 if (null != attName) {
1427 attribute = attributeMap.get(attName);
1428 if (null != attribute) {
1429 if (attribute.isAutomationInProgress()
1430 && (attribute.getAutomationId() == automationId)) {
1431 targetAttribute = attribute;
1439 return targetAttribute;
1442 public boolean startResourceAutomationUIRequest(final String resourceURI) {
1443 if (null == resourceURI) {
1446 boolean status = false;
1447 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1448 if (null != resource) {
1449 changeResourceLevelAutomationStatus(resource, true);
1451 // Invoke the native automation method
1452 SimulatorResourceServer resourceServer = resource
1453 .getResourceServer();
1454 if (null != resourceServer) {
1455 // TODO: Temporarily handling the normal one-time automation for
1459 autoId = resourceServer.startResourceAutomation(
1460 AutomationType.NORMAL, 500, automationListener);
1461 } catch (SimulatorException e) {
1465 .log(Level.ERROR.ordinal(),
1467 "[" + e.getClass().getSimpleName() + "]"
1468 + e.code().toString() + "-"
1473 // Automation request failed and hence status is being
1475 changeResourceLevelAutomationStatus(resource, false);
1477 // Automation request accepted.
1478 resource.setAutomationId(autoId);
1480 // Notify the UI listeners in a different thread.
1481 Thread notifyThread = new Thread() {
1483 resourceAutomationStartedUINotification(resourceURI);
1486 notifyThread.setPriority(Thread.MAX_PRIORITY);
1487 notifyThread.start();
1496 public boolean stopResourceAutomationUIRequest(final String resourceURI) {
1497 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1498 if (null == resource) {
1501 final int autoId = resource.getAutomationId();
1505 SimulatorResourceServer resourceServer = resource.getResourceServer();
1506 if (null == resourceServer) {
1509 // Call native method
1511 resourceServer.stopAutomation(autoId);
1512 } catch (SimulatorException e) {
1516 .log(Level.ERROR.ordinal(),
1518 "[" + e.getClass().getSimpleName() + "]"
1519 + e.code().toString() + "-" + e.message());
1523 // Notify the UI Listeners. Invoke the automation complete callback.
1524 Thread stopThread = new Thread() {
1526 automationListener.onAutomationComplete(resourceURI, autoId);
1533 // Changes the automation state of the resource and its attributes
1534 private void changeResourceLevelAutomationStatus(
1535 SimulatorResource resource, boolean status) {
1537 Map<String, LocalResourceAttribute> attributeMap = resource
1538 .getResourceAttributesMap();
1539 if (null != attributeMap) {
1540 Set<String> attrNameSet = attributeMap.keySet();
1541 Iterator<String> attrNameItr = attrNameSet.iterator();
1543 LocalResourceAttribute attribute;
1544 while (attrNameItr.hasNext()) {
1545 attrName = attrNameItr.next();
1546 attribute = attributeMap.get(attrName);
1547 if (null != attribute) {
1548 attribute.setAutomationInProgress(status);
1552 resource.setResourceAutomationInProgress(status);
1555 public boolean isResourceAutomationStarted(String resourceURI) {
1556 boolean status = false;
1557 if (null == resourceURI) {
1561 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1562 if (null != resource) {
1563 status = resource.isResourceAutomationInProgress();
1568 public boolean isAttributeAutomationStarted(String resourceURI) {
1569 boolean status = false;
1570 if (null == resourceURI) {
1573 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1574 if (null != resource) {
1575 status = resource.isAttributeAutomationInProgress();
1580 public LocalResourceAttribute getAttributeByResourceURI(String uri,
1582 if (null == uri || null == attName) {
1585 SimulatorResource resource = getSimulatorResourceByURI(uri);
1586 if (null == resource) {
1589 Map<String, LocalResourceAttribute> attMap = resource
1590 .getResourceAttributesMap();
1591 if (null == attMap) {
1594 return attMap.get(attName);
1597 public void notifyObserverRequest(SimulatorResource res, int observerId) {
1601 SimulatorResourceServer server = res.getResourceServer();
1602 if (null == server) {
1606 server.notifyObserver(observerId);
1607 } catch (SimulatorException e) {
1611 .log(Level.ERROR.ordinal(),
1613 "[" + e.getClass().getSimpleName() + "]"
1614 + e.code().toString() + "-" + e.message());
1618 public boolean isAttHasRangeOrAllowedValues(LocalResourceAttribute att) {
1622 Object[] obj = att.getAllowedValues();
1623 if (null != obj && obj.length > 0) {
1626 Object min = att.getMinValue();
1627 Object max = att.getMaxValue();
1628 if (null != min && null != max) {
1634 public boolean isResourceAutomationAllowed(String resourceURI) {
1635 if (null == resourceURI) {
1638 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1639 if (null == resource) {
1642 Map<String, LocalResourceAttribute> attributes = resource
1643 .getResourceAttributesMap();
1644 if (null == attributes || attributes.size() < 1) {
1647 Set<String> keySet = attributes.keySet();
1648 Iterator<String> itr = keySet.iterator();
1650 boolean allowed = false;
1651 LocalResourceAttribute att;
1652 while (itr.hasNext()) {
1653 attName = itr.next();
1654 if (null != attName) {
1655 att = attributes.get(attName);
1657 allowed = allowed | isAttHasRangeOrAllowedValues(att);
1664 public Image getImage(String resourceURI) {
1665 if (null == resourceURI) {
1668 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1669 if (null == resource) {
1672 return Activator.getDefault().getImageRegistry()
1673 .get(resource.getResourceType());
1676 public void shutdown() {
1677 threadHandle.interrupt();