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.resource.StandardConfiguration;
41 import oic.simulator.serviceprovider.utils.Constants;
42 import oic.simulator.serviceprovider.utils.Utility;
44 import org.eclipse.swt.graphics.Image;
45 import org.oic.simulator.IAutomation;
46 import org.oic.simulator.ILogger.Level;
47 import org.oic.simulator.InvalidArgsException;
48 import org.oic.simulator.ResourceAttribute;
49 import org.oic.simulator.ResourceAttribute.Range;
50 import org.oic.simulator.ResourceAttribute.Type;
51 import org.oic.simulator.SimulatorException;
52 import org.oic.simulator.SimulatorManager;
53 import org.oic.simulator.SimulatorResourceModel;
54 import org.oic.simulator.serviceprovider.AutomationType;
55 import org.oic.simulator.serviceprovider.IObserver;
56 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
57 import org.oic.simulator.serviceprovider.ObserverInfo;
58 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
61 * This class acts as an interface between the simulator java SDK and the
62 * various UI modules. It maintains all the details of resources and provides
63 * other UI modules with the information required. It also handles model change,
64 * automation, and observer related events from native layer and propagates
65 * those events to the registered UI listeners.
67 public class ResourceManager {
69 private Map<String, Map<String, SimulatorResource>> resourceMap;
71 private Map<String, ArrayList<String>> orderedResourceUriMap;
73 private Map<String, String> displayNameMap;
75 private StandardConfiguration stdConfig;
77 private SimulatorResource currentResourceInSelection;
79 private List<IResourceListChangedUIListener> resourceListChangedUIListeners;
81 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
83 private List<IResourceModelChangedUIListener> resourceModelChangedUIListeners;
85 private List<IAutomationUIListener> automationUIListeners;
87 private List<IObserverListChangedUIListener> observerUIListeners;
89 private IResourceModelChangedListener resourceModelChangeListener;
91 private IAutomation automationListener;
93 private IObserver observer;
95 private NotificationSynchronizerThread synchronizerThread;
97 private Thread threadHandle;
99 public ResourceManager() {
100 resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
101 orderedResourceUriMap = new HashMap<String, ArrayList<String>>();
102 displayNameMap = new HashMap<String, String>();
103 stdConfig = new StandardConfiguration();
105 resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
106 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
107 resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
108 automationUIListeners = new ArrayList<IAutomationUIListener>();
109 observerUIListeners = new ArrayList<IObserverListChangedUIListener>();
111 resourceModelChangeListener = new IResourceModelChangedListener() {
114 public void onResourceModelChanged(final String resourceURI,
115 final SimulatorResourceModel resourceModelN) {
116 synchronizerThread.addToQueue(new Runnable() {
120 if (null == resourceURI || null == resourceModelN) {
123 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
124 if (null == resource) {
128 ModelChangeNotificationType notificationType;
129 // Fetch the resource attributes
130 Map<String, LocalResourceAttribute> resourceAttributeMapNew;
131 resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
132 if (null == resourceAttributeMapNew) {
133 resource.setResourceAttributesMap(null);
134 resourceModelChangedUINotification(
135 ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL,
140 // Update the resource with new model data
141 Map<String, LocalResourceAttribute> resourceAttributeMapOld;
142 resourceAttributeMapOld = resource
143 .getResourceAttributesMap();
144 if (null == resourceAttributeMapOld) {
145 resource.setResourceAttributesMap(resourceAttributeMapNew);
146 resourceModelChangedUINotification(
147 ModelChangeNotificationType.ATTRIBUTE_ADDED,
151 Set<LocalResourceAttribute> valueChangeSet = new HashSet<LocalResourceAttribute>();
152 notificationType = compareAndUpdateLocalAttributes(
153 resourceAttributeMapOld,
154 resourceAttributeMapNew, valueChangeSet);
155 if (notificationType != ModelChangeNotificationType.NONE) {
156 // Update the UI listeners
157 resourceModelChangedUINotification(
158 notificationType, resourceURI,
166 automationListener = new IAutomation() {
169 public void onAutomationComplete(final String resourceURI,
170 final int automationId) {
171 synchronizerThread.addToQueue(new Runnable() {
175 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
176 if (null == resource) {
179 // Checking whether this notification is for an
180 // attribute or a resource
181 if (resource.isResourceAutomationInProgress()) {
182 changeResourceLevelAutomationStatus(resource, false);
183 // Notify the UI listeners
184 automationCompleteUINotification(resourceURI, null);
185 } else if (resource.isAttributeAutomationInProgress()) {
186 // Find the attribute with the given automation id
187 LocalResourceAttribute attribute;
188 attribute = getAttributeWithGivenAutomationId(
189 resource, automationId);
190 if (null != attribute) {
191 attribute.setAutomationInProgress(false);
192 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
193 // Notify the UI listeners
194 automationCompleteUINotification(resourceURI,
195 attribute.getAttributeName());
198 // Ignoring the notification as there are no
199 // known automation for the current resource.
206 observer = new IObserver() {
209 public void onObserverChanged(final String resourceURI,
210 final int status, final ObserverInfo observer) {
214 if (null == resourceURI || null == observer) {
217 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
218 if (null == resource) {
221 // Update the observers information
223 resource.addObserverInfo(observer);
225 resource.removeObserverInfo(observer);
227 // Notify the UI listeners
228 observerListChangedUINotification(resourceURI);
234 synchronizerThread = new NotificationSynchronizerThread();
235 threadHandle = new Thread(synchronizerThread);
236 threadHandle.setName("Simulator service provider event queue");
237 threadHandle.start();
240 private static class NotificationSynchronizerThread implements Runnable {
242 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
246 while (!Thread.interrupted()) {
247 synchronized (this) {
249 while (notificationQueue.isEmpty()) {
253 } catch (InterruptedException e) {
259 synchronized (this) {
260 thread = notificationQueue.pop();
264 } catch (Exception e) {
265 if (e instanceof InterruptedException) {
273 public void addToQueue(Runnable event) {
274 synchronized (this) {
275 notificationQueue.add(event);
281 // This method gives a list of available RAML resource configurations.
282 public Map<String, String> getResourceConfigurationList() {
283 return stdConfig.getStandardResourceConfigurationList();
286 public String getConfigFilePath(String fileName) {
287 return stdConfig.getFilePath(fileName);
290 public void addResourceListChangedUIListener(
291 IResourceListChangedUIListener resourceListChangedUIListener) {
292 synchronized (resourceListChangedUIListeners) {
293 resourceListChangedUIListeners.add(resourceListChangedUIListener);
297 public void addResourceSelectionChangedUIListener(
298 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
299 synchronized (resourceSelectionChangedUIListeners) {
300 resourceSelectionChangedUIListeners
301 .add(resourceSelectionChangedUIListener);
305 public void addResourceModelChangedUIListener(
306 IResourceModelChangedUIListener resourceModelChangedUIListener) {
307 synchronized (resourceModelChangedUIListeners) {
308 resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
312 public void addAutomationUIListener(
313 IAutomationUIListener automationUIListener) {
314 synchronized (automationUIListeners) {
315 automationUIListeners.add(automationUIListener);
319 public void addObserverListChangedUIListener(
320 IObserverListChangedUIListener observerListChangedUIListener) {
321 synchronized (observerUIListeners) {
322 observerUIListeners.add(observerListChangedUIListener);
326 public void removeResourceListChangedUIListener(
327 IResourceListChangedUIListener listener) {
328 synchronized (resourceListChangedUIListeners) {
329 if (null != listener && resourceListChangedUIListeners.size() > 0) {
330 resourceListChangedUIListeners.remove(listener);
335 public void removeResourceSelectionChangedUIListener(
336 IResourceSelectionChangedUIListener listener) {
337 synchronized (resourceSelectionChangedUIListeners) {
339 && resourceSelectionChangedUIListeners.size() > 0) {
340 resourceSelectionChangedUIListeners.remove(listener);
345 public void removeResourceModelChangedUIListener(
346 IResourceModelChangedUIListener listener) {
347 synchronized (resourceModelChangedUIListeners) {
348 if (null != listener && resourceModelChangedUIListeners.size() > 0) {
349 resourceModelChangedUIListeners.remove(listener);
354 public void removeAutomationUIListener(IAutomationUIListener listener) {
355 synchronized (automationUIListeners) {
356 if (null != listener && automationUIListeners.size() > 0) {
357 automationUIListeners.remove(listener);
362 public void removeObserverListChangedUIListener(
363 IObserverListChangedUIListener listener) {
364 synchronized (observerUIListeners) {
365 if (null != listener && observerUIListeners.size() > 0) {
366 observerUIListeners.remove(listener);
371 public synchronized SimulatorResource getCurrentResourceInSelection() {
372 return currentResourceInSelection;
375 public synchronized void setCurrentResourceInSelection(
376 SimulatorResource resource) {
377 this.currentResourceInSelection = resource;
380 private void addResourceUriToOrderedMap(String resourceType,
381 String resourceURI) {
382 if (null != resourceURI && null != resourceType) {
383 synchronized (orderedResourceUriMap) {
384 ArrayList<String> uriListForType = orderedResourceUriMap
386 if (null == uriListForType) {
387 uriListForType = new ArrayList<String>();
388 orderedResourceUriMap.put(resourceType, uriListForType);
390 uriListForType.add(resourceURI);
395 private void removeResourceUriFromOrderedMap(String resourceType,
396 String resourceURI) {
397 synchronized (orderedResourceUriMap) {
398 if (null != resourceURI && null != resourceType) {
399 ArrayList<String> uriListForType = orderedResourceUriMap
401 if (null != uriListForType) {
402 uriListForType.remove(resourceURI);
403 if (uriListForType.size() < 1) {
404 orderedResourceUriMap.remove(resourceType);
407 } else if (null != resourceType) {
408 orderedResourceUriMap.remove(resourceType);
410 orderedResourceUriMap.clear();
415 private void addResourceToMap(SimulatorResource simulatorResource) {
416 if (null != simulatorResource) {
417 synchronized (resourceMap) {
418 Map<String, SimulatorResource> resourceTypeMap;
419 resourceTypeMap = resourceMap.get(simulatorResource
421 if (null == resourceTypeMap) {
422 resourceTypeMap = new HashMap<String, SimulatorResource>();
423 resourceMap.put(simulatorResource.getResourceType(),
426 resourceTypeMap.put(simulatorResource.getResourceURI(),
432 private void addResourceToMap(String resourceType,
433 Map<String, SimulatorResource> newResourceTypeMap) {
434 if (null != resourceType && null != newResourceTypeMap) {
435 synchronized (resourceMap) {
436 Map<String, SimulatorResource> resourceTypeMap = resourceMap
438 if (null != resourceTypeMap) {
439 resourceTypeMap.putAll(newResourceTypeMap);
441 resourceMap.put(resourceType, newResourceTypeMap);
447 private void removeResourceFromMap(String resourceType, String resourceURI) {
448 if (null != resourceURI && null != resourceType) {
449 synchronized (resourceMap) {
450 Map<String, SimulatorResource> resourceTypeMap = resourceMap
452 if (null != resourceTypeMap) {
453 resourceTypeMap.remove(resourceURI);
454 if (resourceTypeMap.size() < 1) {
455 resourceMap.remove(resourceType);
462 public boolean isResourceExist(String resourceURI) {
463 boolean result = false;
464 if (null != resourceURI) {
465 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
466 if (null != resource) {
473 public void addResourceDisplayName(String displayName, String completeURI) {
474 synchronized (displayNameMap) {
475 displayNameMap.put(displayName, completeURI);
479 public void removeResourceDisplayName(String displayName) {
480 synchronized (displayNameMap) {
481 displayNameMap.remove(displayName);
485 public boolean isDisplayName(String displayName) {
487 synchronized (displayNameMap) {
488 exist = displayNameMap.containsKey(displayName);
493 public String getCompleteUriFromDisplayName(String displayName) {
495 synchronized (displayNameMap) {
496 completeURI = displayNameMap.get(displayName);
501 public void createResource(final String configFilePath) {
505 SimulatorResourceServer resourceServerN;
507 resourceServerN = SimulatorManager.createResource(
508 configFilePath, resourceModelChangeListener);
509 } catch (SimulatorException e) {
513 .log(Level.ERROR.ordinal(),
515 "[" + e.getClass().getSimpleName() + "]"
516 + e.code().toString() + "-"
520 SimulatorResource simulatorResource;
521 simulatorResource = fetchResourceData(resourceServerN);
522 if (null != simulatorResource) {
523 String uri = simulatorResource.getResourceURI();
524 addResourceToMap(simulatorResource);
525 addResourceUriToOrderedMap(
526 simulatorResource.getResourceType(), uri);
527 resourceCreatedUINotification();
529 // Form the display URI
530 String displayName = Utility.uriToDisplayName(uri);
531 if (null != displayName) {
532 addResourceDisplayName(displayName, uri);
537 .log(Level.ERROR.ordinal(),
539 "Converting the URI to display format for "
543 // Set the observer for the created resource
545 resourceServerN.setObserverCallback(observer);
546 } catch (SimulatorException e) {
550 .log(Level.ERROR.ordinal(),
552 "[" + e.getClass().getSimpleName()
553 + "]" + e.code().toString()
554 + "-" + e.message());
557 // Print the resource data
558 simulatorResource.printResourceInfo();
564 public void createResource(final String configFilePath,
565 final int noOfInstances) {
569 Map<String, SimulatorResource> resourceTypeMap;
570 SimulatorResourceServer[] simulatorResourceServers;
572 simulatorResourceServers = SimulatorManager.createResource(
573 configFilePath, noOfInstances,
574 resourceModelChangeListener);
575 } catch (SimulatorException e) {
579 .log(Level.ERROR.ordinal(),
581 "[" + e.getClass().getSimpleName() + "]"
582 + e.code().toString() + "-"
586 if (null == simulatorResourceServers) {
589 resourceTypeMap = new HashMap<String, SimulatorResource>();
590 SimulatorResource resource;
593 for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
594 resource = fetchResourceData(resourceServerN);
595 if (null != resource) {
596 uri = resource.getResourceURI();
597 resourceTypeMap.put(uri, resource);
598 addResourceUriToOrderedMap(resource.getResourceType(),
601 // Form the display URI
602 displayName = Utility.uriToDisplayName(uri);
603 if (null != displayName) {
604 addResourceDisplayName(displayName, uri);
609 .log(Level.ERROR.ordinal(),
611 "Converting the URI to display format for "
615 // Set the observer for the created resource
617 resourceServerN.setObserverCallback(observer);
618 } catch (SimulatorException e) {
622 .log(Level.ERROR.ordinal(),
624 "[" + e.getClass().getSimpleName()
625 + "]" + e.code().toString()
626 + "-" + e.message());
630 // Find the resourceType and add it to the local data
631 // structure and notify UI Listeners
632 if (resourceTypeMap.size() > 0) {
634 Set<String> uriSet = resourceTypeMap.keySet();
635 Iterator<String> itr = uriSet.iterator();
637 SimulatorResource simResource = resourceTypeMap.get(itr
639 if (null != simResource) {
640 resourceType = simResource.getResourceType();
642 addResourceToMap(resourceType, resourceTypeMap);
643 resourceCreatedUINotification();
651 private SimulatorResource fetchResourceData(
652 SimulatorResourceServer resourceServerN) {
653 SimulatorResource simulatorResource = null;
654 if (null != resourceServerN) {
655 simulatorResource = new SimulatorResource();
656 simulatorResource.setResourceServer(resourceServerN);
657 simulatorResource.setResourceURI(resourceServerN.getURI());
659 .setResourceType(resourceServerN.getResourceType());
660 simulatorResource.setResourceName(resourceServerN.getName());
661 simulatorResource.setResourceInterface(resourceServerN
662 .getInterfaceType());
664 SimulatorResourceModel resourceModelN;
666 resourceModelN = resourceServerN.getModel();
667 } catch (SimulatorException e) {
671 .log(Level.ERROR.ordinal(),
673 "[" + e.getClass().getSimpleName() + "]"
674 + e.code().toString() + "-"
678 if (null != resourceModelN) {
679 simulatorResource.setResourceModel(resourceModelN);
681 // Fetch the resource attributes
682 Map<String, LocalResourceAttribute> resourceAttributeMap;
683 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
684 if (null != resourceAttributeMap) {
686 .setResourceAttributesMap(resourceAttributeMap);
690 return simulatorResource;
693 private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
694 SimulatorResourceModel resourceModelN) {
695 Map<String, LocalResourceAttribute> resourceAttributeMap = null;
696 if (null != resourceModelN) {
697 Map<String, ResourceAttribute> attributeMapN;
699 attributeMapN = resourceModelN.getAttributes();
700 } catch (SimulatorException e) {
704 .log(Level.ERROR.ordinal(),
706 "[" + e.getClass().getSimpleName() + "]"
707 + e.code().toString() + "-"
711 if (null != attributeMapN) {
712 resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
714 Set<String> attNameSet = attributeMapN.keySet();
716 ResourceAttribute attributeN;
717 LocalResourceAttribute attribute;
718 Iterator<String> attNameItr = attNameSet.iterator();
719 while (attNameItr.hasNext()) {
720 attName = attNameItr.next();
721 attributeN = attributeMapN.get(attName);
722 if (null != attributeN) {
723 attribute = new LocalResourceAttribute();
724 attribute.setResourceAttribute(attributeN);
726 // Set the attribute value
727 Object valueObj = attributeN.getValue();
728 if (null != valueObj) {
729 attribute.setAttributeValue(valueObj);
732 // Set the attribute value list.
733 attribute.setAttValues(getValueList(attributeN));
735 // Initially disabling the automation
736 attribute.setAutomationInProgress(false);
738 // TODO: Temporarily setting the interval to 500.
739 // This value should come from the native layer.
740 // Native implementation is in progress.
742 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
744 // Setting the default automation type
746 .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
748 resourceAttributeMap.put(attName, attribute);
753 return resourceAttributeMap;
756 // This method gives all known possible values of the attribute in string
757 // format. It takes allowed values or range of values whichever is available
758 private List<String> getValueList(ResourceAttribute attributeN) {
759 Object[] allowedValues = attributeN.getAllowedValues();
760 List<String> valueList = new ArrayList<String>();
761 if (null != allowedValues && allowedValues.length > 0) {
762 for (Object value : allowedValues) {
764 valueList.add(String.valueOf(value));
768 Type valueType = attributeN.getBaseType();
769 Range range = attributeN.getRange();
771 Object min = range.getMin();
772 Object max = range.getMax();
773 if (valueType == Type.INT) {
774 int minI = (Integer) min;
775 int maxI = (Integer) max;
776 for (int value = minI; value <= maxI; value++) {
777 valueList.add(String.valueOf(value));
779 } else if (valueType == Type.DOUBLE) {
780 double minD = (Double) min;
781 double maxD = (Double) max;
782 for (double value = minD; value <= maxD; value++) {
783 valueList.add(String.valueOf(value));
788 Object attValue = attributeN.getValue();
789 if (valueList.size() < 1 && null != attValue) {
790 valueList.add(String.valueOf(attValue));
795 public void deleteResourceByURI(final String resourceURI) {
796 if (null != resourceURI) {
800 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
801 if (null != resource) {
802 String resourceType = resource.getResourceType();
804 // Unregister the resource from the platform
805 deleteResource(resource);
807 // Delete from the local data structure
808 deleteLocalResourceDetails(resourceType, resourceURI);
810 // Notify the UI listener for removing this resource
812 resourceDeletedUINotification();
814 if (null != currentResourceInSelection
815 && resource == currentResourceInSelection) {
816 // Listeners might query the resource being deleted
817 // if exists. So set the currently selection to
819 setCurrentResourceInSelection(null);
821 // Notify all observers for resource selection
823 resourceSelectionChangedUINotification();
831 private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
832 SimulatorResource resource = null;
833 if (null != resourceURI) {
834 synchronized (resourceMap) {
835 Set<String> typeSet = resourceMap.keySet();
836 Iterator<String> typeItr = typeSet.iterator();
838 Map<String, SimulatorResource> resourceTypeMap;
839 while (typeItr.hasNext()) {
840 resourceType = typeItr.next();
841 resourceTypeMap = resourceMap.get(resourceType);
842 if (null != resourceTypeMap) {
843 resource = resourceTypeMap.get(resourceURI);
844 if (null != resource) {
854 private void deleteResource(SimulatorResource resource) {
855 if (null != resource) {
856 SimulatorResourceServer resourceServerN = resource
857 .getResourceServer();
858 if (null != resourceServerN) {
860 SimulatorManager.deleteResource(resourceServerN);
861 } catch (SimulatorException e) {
865 .log(Level.ERROR.ordinal(),
867 "[" + e.getClass().getSimpleName() + "]"
868 + e.code().toString() + "-"
875 public void deleteResourceByType(final String resourceType) {
876 if (null != resourceType) {
880 // Unregister the resources from the platform
881 deleteResource(resourceType);
883 // Delete from the local data structure
884 deleteLocalResourceDetails(resourceType, null);
886 // Notify the UI listener for removing this resource from UI
887 resourceDeletedUINotification();
889 if (null != currentResourceInSelection
890 && resourceType.equals(currentResourceInSelection
891 .getResourceType())) {
892 // Listeners might query the resource being deleted if
893 // exists. So set the currently selection to null.
894 setCurrentResourceInSelection(null);
896 // Notify all observers for resource selection change
898 resourceSelectionChangedUINotification();
905 private void deleteResource(String resourceType) {
906 if (null != resourceType) {
908 SimulatorManager.deleteResources(resourceType);
909 } catch (SimulatorException e) {
913 .log(Level.ERROR.ordinal(),
915 "[" + e.getClass().getSimpleName() + "]"
916 + e.code().toString() + "-"
922 public void deleteAllResources() {
926 // Unregister the resources from the platform
929 // Delete from the local data structure
930 deleteLocalResourceDetails(null, null);
932 // Notify the UI listener for removing this resource from UI
933 resourceDeletedUINotification();
935 // Listeners might query the resource being deleted if exists.
936 // So set the currently selection to null.
937 setCurrentResourceInSelection(null);
939 // Notify all observers for resource selection change event
940 resourceSelectionChangedUINotification();
945 private void deleteResource() {
947 SimulatorManager.deleteResources(null);
948 } catch (SimulatorException e) {
952 .log(Level.ERROR.ordinal(),
954 "[" + e.getClass().getSimpleName() + "]"
955 + e.code().toString() + "-" + e.message());
959 private void deleteLocalResourceDetails(String resourceType,
960 String resourceURI) {
961 if (null != resourceType && null != resourceURI) {
962 removeResourceFromMap(resourceType, resourceURI);
963 removeResourceUriFromOrderedMap(resourceType, resourceURI);
965 synchronized (resourceMap) {
966 if (null != resourceType) {
967 removeResourceUriFromOrderedMap(resourceType, null);
968 resourceMap.remove(resourceType);
971 removeResourceUriFromOrderedMap(null, null);
975 // Remove the display name from the map
976 if (null != resourceURI) {
977 String dispName = Utility.uriToDisplayName(resourceURI);
978 if (null != dispName)
979 removeResourceDisplayName(dispName);
983 private void resourceCreatedUINotification() {
984 synchronized (resourceListChangedUIListeners) {
985 if (resourceListChangedUIListeners.size() > 0) {
986 IResourceListChangedUIListener listener;
987 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
989 while (listenerItr.hasNext()) {
990 listener = listenerItr.next();
991 if (null != listener) {
992 listener.onResourceCreation();
999 private void resourceDeletedUINotification() {
1000 synchronized (resourceListChangedUIListeners) {
1001 if (resourceListChangedUIListeners.size() > 0) {
1002 IResourceListChangedUIListener listener;
1003 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
1005 while (listenerItr.hasNext()) {
1006 listener = listenerItr.next();
1007 if (null != listener) {
1008 listener.onResourceDeletion();
1015 private void resourceSelectionChangedUINotification() {
1016 synchronized (resourceSelectionChangedUIListeners) {
1017 if (resourceSelectionChangedUIListeners.size() > 0) {
1018 IResourceSelectionChangedUIListener listener;
1019 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
1021 while (listenerItr.hasNext()) {
1022 listener = listenerItr.next();
1023 if (null != listener) {
1024 listener.onResourceSelectionChange();
1031 private void resourceModelChangedUINotification(
1032 ModelChangeNotificationType notificationType, String resourceURI,
1033 Set<LocalResourceAttribute> valueChangeSet) {
1034 synchronized (resourceModelChangedUIListeners) {
1035 if (resourceModelChangedUIListeners.size() > 0
1036 && notificationType != ModelChangeNotificationType.NONE
1037 && null != resourceURI) {
1038 IResourceModelChangedUIListener listener;
1039 Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
1041 while (listenerItr.hasNext()) {
1042 listener = listenerItr.next();
1043 if (null != listener) {
1044 listener.onResourceModelChange(notificationType,
1045 resourceURI, valueChangeSet);
1052 private void resourceAutomationStartedUINotification(String resourceURI) {
1053 synchronized (automationUIListeners) {
1054 if (automationUIListeners.size() > 0 && null != resourceURI) {
1055 IAutomationUIListener listener;
1056 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1058 while (listenerItr.hasNext()) {
1059 listener = listenerItr.next();
1060 if (null != listener) {
1061 listener.onResourceAutomationStart(resourceURI);
1068 private void automationCompleteUINotification(String resourceURI,
1070 synchronized (automationUIListeners) {
1071 if (automationUIListeners.size() > 0 && null != resourceURI) {
1072 IAutomationUIListener listener;
1073 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1075 while (listenerItr.hasNext()) {
1076 listener = listenerItr.next();
1077 if (null != listener) {
1078 listener.onAutomationComplete(resourceURI, attName);
1085 private void observerListChangedUINotification(String resourceURI) {
1086 synchronized (observerUIListeners) {
1087 if (observerUIListeners.size() > 0 && null != resourceURI) {
1088 IObserverListChangedUIListener listener;
1089 Iterator<IObserverListChangedUIListener> listenerItr = observerUIListeners
1091 while (listenerItr.hasNext()) {
1092 listener = listenerItr.next();
1093 if (null != listener) {
1094 listener.onObserverListChanged(resourceURI);
1101 public List<String> getResourceTypeList() {
1102 List<String> typeList = null;
1103 synchronized (resourceMap) {
1104 if (resourceMap.size() > 0) {
1105 typeList = new ArrayList<String>();
1106 Set<String> typeSet = resourceMap.keySet();
1107 Iterator<String> typeItr = typeSet.iterator();
1108 while (typeItr.hasNext()) {
1109 typeList.add(typeItr.next());
1116 public boolean isTypeExist(String resType) {
1117 synchronized (resourceMap) {
1118 if (resourceMap.containsKey(resType)) {
1125 public List<String> getURIList() {
1127 synchronized (orderedResourceUriMap) {
1128 Set<String> typeSet = orderedResourceUriMap.keySet();
1129 List<String> typeList = Utility.convertSetToList(typeSet);
1130 if (null == typeList || typeList.size() < 1) {
1133 list = new ArrayList<String>();
1136 Collections.sort(typeList);
1138 // Add all URIs to the output list
1139 Iterator<String> typeItr = typeList.iterator();
1140 while (typeItr.hasNext()) {
1141 List<String> l = orderedResourceUriMap.get(typeItr.next());
1150 public void resourceSelectionChanged(final String selectedItem) {
1154 // Check whether the item selected is a resource or resource
1156 if (isTypeExist(selectedItem)) {
1157 // Given item is a resource Type
1158 setCurrentResourceInSelection(null);
1160 // Given item is a resource URI
1161 SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
1162 if (null != resource) {
1163 setCurrentResourceInSelection(resource);
1165 setCurrentResourceInSelection(null);
1168 // Notify all observers for resource selection change event
1169 resourceSelectionChangedUINotification();
1174 public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
1175 if (null != resource) {
1179 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1181 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1182 propName = Constants.META_PROPERTIES[index];
1183 if (propName.equals(Constants.RESOURCE_URI)) {
1184 propValue = resource.getResourceURI();
1185 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1186 propValue = resource.getResourceType();
1187 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1188 // TODO: Temporarily ignoring till the implementation.
1193 if (null != propValue) {
1194 metaPropertyList.add(new MetaProperty(propName, propValue));
1198 return metaPropertyList;
1203 public List<LocalResourceAttribute> getAttributes(SimulatorResource resource) {
1204 List<LocalResourceAttribute> attList = null;
1205 if (null != resource) {
1206 Map<String, LocalResourceAttribute> attMap = resource
1207 .getResourceAttributesMap();
1208 if (null != attMap && attMap.size() > 0) {
1209 attList = new ArrayList<LocalResourceAttribute>();
1210 Set<String> attNameSet = attMap.keySet();
1212 LocalResourceAttribute attribute;
1213 Iterator<String> attNameItr = attNameSet.iterator();
1214 while (attNameItr.hasNext()) {
1215 attName = attNameItr.next();
1216 attribute = attMap.get(attName);
1217 if (null != attribute) {
1218 attList.add(attribute);
1226 public void attributeValueUpdated(SimulatorResource resource,
1227 String attributeName, String value) {
1228 if (null != resource && null != attributeName && null != value) {
1229 SimulatorResourceServer server = resource.getResourceServer();
1230 if (null != server) {
1231 LocalResourceAttribute att = resource
1232 .getAttribute(attributeName);
1236 Type baseType = att.getAttValBaseType();
1238 if (baseType == Type.STRING) {
1239 server.updateAttributeString(attributeName, value);
1240 } else if (baseType == Type.INT) {
1243 val = Integer.parseInt(value);
1244 server.updateAttributeInteger(attributeName, val);
1245 } catch (NumberFormatException nfe) {
1248 } else if (baseType == Type.DOUBLE) {
1251 val = Double.parseDouble(value);
1252 server.updateAttributeDouble(attributeName, val);
1253 } catch (NumberFormatException nfe) {
1256 } else if (baseType == Type.BOOL) {
1258 val = Boolean.parseBoolean(value);
1259 server.updateAttributeBoolean(attributeName, val);
1261 } catch (SimulatorException e) {
1265 .log(Level.ERROR.ordinal(),
1267 "[" + e.getClass().getSimpleName() + "]"
1268 + e.code().toString() + "-"
1275 private ModelChangeNotificationType compareAndUpdateLocalAttributes(
1276 Map<String, LocalResourceAttribute> resourceAttributeMapOld,
1277 Map<String, LocalResourceAttribute> resourceAttributeMapNew,
1278 Set<LocalResourceAttribute> valueChangeSet) {
1279 ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
1280 if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
1281 Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
1282 Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
1284 LocalResourceAttribute attributeOld;
1285 LocalResourceAttribute attributeNew;
1290 while (attributeMapOldItr.hasNext()) {
1291 attName = attributeMapOldItr.next();
1292 if (resourceAttributeMapNew.containsKey(attName)) {
1293 attributeOld = resourceAttributeMapOld.get(attName);
1294 attributeNew = resourceAttributeMapNew.get(attName);
1295 // Copy the attribute value from new to old if the value
1297 // Comparing only the attribute's value considering the
1298 // fact that only the value can be changed
1299 if (null != attributeOld && null != attributeNew) {
1300 attValueOld = attributeOld.getAttributeValue();
1301 attValueNew = attributeNew.getAttributeValue();
1303 oldValueStr = String.valueOf(attValueOld);
1304 newValueStr = String.valueOf(attValueNew);
1306 if (null != oldValueStr && null != newValueStr) {
1307 if (!oldValueStr.equals(newValueStr)) {
1308 attributeOld.setAttributeValue(attValueNew);
1309 notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
1310 valueChangeSet.add(attributeOld);
1314 resourceAttributeMapNew.remove(attName);
1316 // Attribute doesn't exist in the new model. Hence
1317 // removing it from the model.
1318 resourceAttributeMapOld.remove(attName);
1319 notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
1322 // Check for new attributes in the new model
1323 if (resourceAttributeMapNew.size() > 0) {
1324 Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
1325 Iterator<String> remainingAttItr = remainingAttSet.iterator();
1326 LocalResourceAttribute attribute;
1327 while (remainingAttItr.hasNext()) {
1328 attName = remainingAttItr.next();
1329 if (null != attName) {
1330 attribute = resourceAttributeMapNew.get(attName);
1331 if (null != attribute) {
1332 resourceAttributeMapOld.put(attName, attribute);
1336 notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
1339 return notificationType;
1342 public int startAutomation(SimulatorResource resource,
1343 LocalResourceAttribute attribute, AutomationType autoType,
1344 int autoUpdateInterval) {
1346 if (null != resource && null != attribute) {
1347 SimulatorResourceServer resourceServerN = resource
1348 .getResourceServer();
1349 if (null != resourceServerN) {
1350 String attrName = attribute.getAttributeName();
1352 autoId = resourceServerN.startAttributeAutomation(attrName,
1353 autoType, automationListener);
1354 } catch (SimulatorException e) {
1358 .log(Level.ERROR.ordinal(),
1360 "[" + e.getClass().getSimpleName() + "]"
1361 + e.code().toString() + "-"
1366 attribute.setAutomationId(autoId);
1367 attribute.setAutomationType(autoType);
1368 attribute.setAutomationUpdateInterval(autoUpdateInterval);
1369 attribute.setAutomationInProgress(true);
1370 resource.setAttributeAutomationInProgress(true);
1372 attribute.setAutomationInProgress(false);
1379 public void stopAutomation(SimulatorResource resource,
1380 LocalResourceAttribute att, int autoId) {
1381 if (null != resource) {
1382 SimulatorResourceServer resourceServerN = resource
1383 .getResourceServer();
1384 if (null != resourceServerN) {
1386 resourceServerN.stopAutomation(autoId);
1387 } catch (SimulatorException e) {
1391 .log(Level.ERROR.ordinal(),
1393 "[" + e.getClass().getSimpleName() + "]"
1394 + e.code().toString() + "-"
1398 // Change the automation status
1399 att.setAutomationInProgress(false);
1400 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1405 private boolean isAnyAttributeInAutomation(SimulatorResource resource) {
1406 if (null == resource) {
1409 Map<String, LocalResourceAttribute> attMap = resource
1410 .getResourceAttributesMap();
1411 if (null == attMap) {
1414 boolean status = false;
1415 Set<String> keySet = attMap.keySet();
1416 Iterator<String> attItr = keySet.iterator();
1417 while (attItr.hasNext()) {
1418 LocalResourceAttribute attribute = attMap.get(attItr.next());
1419 if (attribute.isAutomationInProgress()) {
1427 private LocalResourceAttribute getAttributeWithGivenAutomationId(
1428 SimulatorResource resource, int automationId) {
1429 LocalResourceAttribute targetAttribute = null;
1430 if (null != resource) {
1431 Map<String, LocalResourceAttribute> attributeMap = resource
1432 .getResourceAttributesMap();
1433 if (null != attributeMap) {
1434 Set<String> attNameSet = attributeMap.keySet();
1435 Iterator<String> attNameItr = attNameSet.iterator();
1437 LocalResourceAttribute attribute;
1438 while (attNameItr.hasNext()) {
1439 attName = attNameItr.next();
1440 if (null != attName) {
1441 attribute = attributeMap.get(attName);
1442 if (null != attribute) {
1443 if (attribute.isAutomationInProgress()
1444 && (attribute.getAutomationId() == automationId)) {
1445 targetAttribute = attribute;
1453 return targetAttribute;
1456 public boolean startResourceAutomationUIRequest(final String resourceURI) {
1457 if (null == resourceURI) {
1460 boolean status = false;
1461 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1462 if (null != resource) {
1463 changeResourceLevelAutomationStatus(resource, true);
1465 // Invoke the native automation method
1466 SimulatorResourceServer resourceServer = resource
1467 .getResourceServer();
1468 if (null != resourceServer) {
1469 // TODO: Temporarily handling the normal one-time automation for
1473 autoId = resourceServer.startResourceAutomation(
1474 AutomationType.NORMAL, automationListener);
1475 } catch (SimulatorException e) {
1479 .log(Level.ERROR.ordinal(),
1481 "[" + e.getClass().getSimpleName() + "]"
1482 + e.code().toString() + "-"
1487 // Automation request failed and hence status is being
1489 changeResourceLevelAutomationStatus(resource, false);
1491 // Automation request accepted.
1492 resource.setAutomationId(autoId);
1494 // Notify the UI listeners in a different thread.
1495 Thread notifyThread = new Thread() {
1497 resourceAutomationStartedUINotification(resourceURI);
1500 notifyThread.setPriority(Thread.MAX_PRIORITY);
1501 notifyThread.start();
1510 public boolean stopResourceAutomationUIRequest(final String resourceURI) {
1511 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1512 if (null == resource) {
1515 final int autoId = resource.getAutomationId();
1519 SimulatorResourceServer resourceServer = resource.getResourceServer();
1520 if (null == resourceServer) {
1523 // Call native method
1525 resourceServer.stopAutomation(autoId);
1526 } catch (SimulatorException e) {
1530 .log(Level.ERROR.ordinal(),
1532 "[" + e.getClass().getSimpleName() + "]"
1533 + e.code().toString() + "-" + e.message());
1537 // Notify the UI Listeners. Invoke the automation complete callback.
1538 Thread stopThread = new Thread() {
1540 automationListener.onAutomationComplete(resourceURI, autoId);
1547 // Changes the automation state of the resource and its attributes
1548 private void changeResourceLevelAutomationStatus(
1549 SimulatorResource resource, boolean status) {
1551 Map<String, LocalResourceAttribute> attributeMap = resource
1552 .getResourceAttributesMap();
1553 if (null != attributeMap) {
1554 Set<String> attrNameSet = attributeMap.keySet();
1555 Iterator<String> attrNameItr = attrNameSet.iterator();
1557 LocalResourceAttribute attribute;
1558 while (attrNameItr.hasNext()) {
1559 attrName = attrNameItr.next();
1560 attribute = attributeMap.get(attrName);
1561 if (null != attribute) {
1562 attribute.setAutomationInProgress(status);
1566 resource.setResourceAutomationInProgress(status);
1569 public boolean isResourceAutomationStarted(String resourceURI) {
1570 boolean status = false;
1571 if (null == resourceURI) {
1575 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1576 if (null != resource) {
1577 status = resource.isResourceAutomationInProgress();
1582 public boolean isAttributeAutomationStarted(String resourceURI) {
1583 boolean status = false;
1584 if (null == resourceURI) {
1587 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1588 if (null != resource) {
1589 status = resource.isAttributeAutomationInProgress();
1594 public LocalResourceAttribute getAttributeByResourceURI(String uri,
1596 if (null == uri || null == attName) {
1599 SimulatorResource resource = getSimulatorResourceByURI(uri);
1600 if (null == resource) {
1603 Map<String, LocalResourceAttribute> attMap = resource
1604 .getResourceAttributesMap();
1605 if (null == attMap) {
1608 return attMap.get(attName);
1611 public void notifyObserverRequest(SimulatorResource res, int observerId) {
1615 SimulatorResourceServer server = res.getResourceServer();
1616 if (null == server) {
1620 server.notifyObserver(observerId);
1621 } catch (SimulatorException e) {
1625 .log(Level.ERROR.ordinal(),
1627 "[" + e.getClass().getSimpleName() + "]"
1628 + e.code().toString() + "-" + e.message());
1632 public Image getImage(String resourceURI) {
1633 if (null == resourceURI) {
1636 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1637 if (null == resource) {
1640 return Activator.getDefault().getImageRegistry()
1641 .get(resource.getResourceType());
1644 public void shutdown() {
1645 threadHandle.interrupt();