Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / ResourceManager.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.serviceprovider.manager;
18
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;
27 import java.util.Map;
28 import java.util.Set;
29
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;
43
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;
59
60 /**
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.
66  */
67 public class ResourceManager {
68
69     private Map<String, Map<String, SimulatorResource>> resourceMap;
70
71     private Map<String, ArrayList<String>>              orderedResourceUriMap;
72
73     private Map<String, String>                         displayNameMap;
74
75     private StandardConfiguration                       stdConfig;
76
77     private SimulatorResource                           currentResourceInSelection;
78
79     private List<IResourceListChangedUIListener>        resourceListChangedUIListeners;
80
81     private List<IResourceSelectionChangedUIListener>   resourceSelectionChangedUIListeners;
82
83     private List<IResourceModelChangedUIListener>       resourceModelChangedUIListeners;
84
85     private List<IAutomationUIListener>                 automationUIListeners;
86
87     private List<IObserverListChangedUIListener>        observerUIListeners;
88
89     private IResourceModelChangedListener               resourceModelChangeListener;
90
91     private IAutomation                                 automationListener;
92
93     private IObserver                                   observer;
94
95     private NotificationSynchronizerThread              synchronizerThread;
96
97     private Thread                                      threadHandle;
98
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();
104
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>();
110
111         resourceModelChangeListener = new IResourceModelChangedListener() {
112
113             @Override
114             public void onResourceModelChanged(final String resourceURI,
115                     final SimulatorResourceModel resourceModelN) {
116                 synchronizerThread.addToQueue(new Runnable() {
117
118                     @Override
119                     public void run() {
120                         if (null == resourceURI || null == resourceModelN) {
121                             return;
122                         }
123                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
124                         if (null == resource) {
125                             return;
126                         }
127
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,
136                                     resourceURI, null);
137                             return;
138                         }
139
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,
148                                     resourceURI, null);
149                             return;
150                         }
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,
159                                     valueChangeSet);
160                         }
161                     }
162                 });
163             }
164         };
165
166         automationListener = new IAutomation() {
167
168             @Override
169             public void onAutomationComplete(final String resourceURI,
170                     final int automationId) {
171                 synchronizerThread.addToQueue(new Runnable() {
172
173                     @Override
174                     public void run() {
175                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
176                         if (null == resource) {
177                             return;
178                         }
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());
196                             }
197                         } else {
198                             // Ignoring the notification as there are no
199                             // known automation for the current resource.
200                         }
201                     }
202                 });
203             }
204         };
205
206         observer = new IObserver() {
207
208             @Override
209             public void onObserverChanged(final String resourceURI,
210                     final int status, final ObserverInfo observer) {
211                 new Thread() {
212                     @Override
213                     public void run() {
214                         if (null == resourceURI || null == observer) {
215                             return;
216                         }
217                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
218                         if (null == resource) {
219                             return;
220                         }
221                         // Update the observers information
222                         if (status == 0) {
223                             resource.addObserverInfo(observer);
224                         } else {
225                             resource.removeObserverInfo(observer);
226                         }
227                         // Notify the UI listeners
228                         observerListChangedUINotification(resourceURI);
229                     }
230                 }.start();
231             }
232         };
233
234         synchronizerThread = new NotificationSynchronizerThread();
235         threadHandle = new Thread(synchronizerThread);
236         threadHandle.setName("Simulator service provider event queue");
237         threadHandle.start();
238     }
239
240     private static class NotificationSynchronizerThread implements Runnable {
241
242         LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
243
244         @Override
245         public void run() {
246             while (!Thread.interrupted()) {
247                 synchronized (this) {
248                     try {
249                         while (notificationQueue.isEmpty()) {
250                             this.wait();
251                             break;
252                         }
253                     } catch (InterruptedException e) {
254                         return;
255                     }
256                 }
257
258                 Runnable thread;
259                 synchronized (this) {
260                     thread = notificationQueue.pop();
261                 }
262                 try {
263                     thread.run();
264                 } catch (Exception e) {
265                     if (e instanceof InterruptedException) {
266                         return;
267                     }
268                     e.printStackTrace();
269                 }
270             }
271         }
272
273         public void addToQueue(Runnable event) {
274             synchronized (this) {
275                 notificationQueue.add(event);
276                 this.notify();
277             }
278         }
279     }
280
281     // This method gives a list of available RAML resource configurations.
282     public Map<String, String> getResourceConfigurationList() {
283         return stdConfig.getStandardResourceConfigurationList();
284     }
285
286     public String getConfigFilePath(String fileName) {
287         return stdConfig.getFilePath(fileName);
288     }
289
290     public void addResourceListChangedUIListener(
291             IResourceListChangedUIListener resourceListChangedUIListener) {
292         synchronized (resourceListChangedUIListeners) {
293             resourceListChangedUIListeners.add(resourceListChangedUIListener);
294         }
295     }
296
297     public void addResourceSelectionChangedUIListener(
298             IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
299         synchronized (resourceSelectionChangedUIListeners) {
300             resourceSelectionChangedUIListeners
301                     .add(resourceSelectionChangedUIListener);
302         }
303     }
304
305     public void addResourceModelChangedUIListener(
306             IResourceModelChangedUIListener resourceModelChangedUIListener) {
307         synchronized (resourceModelChangedUIListeners) {
308             resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
309         }
310     }
311
312     public void addAutomationUIListener(
313             IAutomationUIListener automationUIListener) {
314         synchronized (automationUIListeners) {
315             automationUIListeners.add(automationUIListener);
316         }
317     }
318
319     public void addObserverListChangedUIListener(
320             IObserverListChangedUIListener observerListChangedUIListener) {
321         synchronized (observerUIListeners) {
322             observerUIListeners.add(observerListChangedUIListener);
323         }
324     }
325
326     public void removeResourceListChangedUIListener(
327             IResourceListChangedUIListener listener) {
328         synchronized (resourceListChangedUIListeners) {
329             if (null != listener && resourceListChangedUIListeners.size() > 0) {
330                 resourceListChangedUIListeners.remove(listener);
331             }
332         }
333     }
334
335     public void removeResourceSelectionChangedUIListener(
336             IResourceSelectionChangedUIListener listener) {
337         synchronized (resourceSelectionChangedUIListeners) {
338             if (null != listener
339                     && resourceSelectionChangedUIListeners.size() > 0) {
340                 resourceSelectionChangedUIListeners.remove(listener);
341             }
342         }
343     }
344
345     public void removeResourceModelChangedUIListener(
346             IResourceModelChangedUIListener listener) {
347         synchronized (resourceModelChangedUIListeners) {
348             if (null != listener && resourceModelChangedUIListeners.size() > 0) {
349                 resourceModelChangedUIListeners.remove(listener);
350             }
351         }
352     }
353
354     public void removeAutomationUIListener(IAutomationUIListener listener) {
355         synchronized (automationUIListeners) {
356             if (null != listener && automationUIListeners.size() > 0) {
357                 automationUIListeners.remove(listener);
358             }
359         }
360     }
361
362     public void removeObserverListChangedUIListener(
363             IObserverListChangedUIListener listener) {
364         synchronized (observerUIListeners) {
365             if (null != listener && observerUIListeners.size() > 0) {
366                 observerUIListeners.remove(listener);
367             }
368         }
369     }
370
371     public synchronized SimulatorResource getCurrentResourceInSelection() {
372         return currentResourceInSelection;
373     }
374
375     public synchronized void setCurrentResourceInSelection(
376             SimulatorResource resource) {
377         this.currentResourceInSelection = resource;
378     }
379
380     private void addResourceUriToOrderedMap(String resourceType,
381             String resourceURI) {
382         if (null != resourceURI && null != resourceType) {
383             synchronized (orderedResourceUriMap) {
384                 ArrayList<String> uriListForType = orderedResourceUriMap
385                         .get(resourceType);
386                 if (null == uriListForType) {
387                     uriListForType = new ArrayList<String>();
388                     orderedResourceUriMap.put(resourceType, uriListForType);
389                 }
390                 uriListForType.add(resourceURI);
391             }
392         }
393     }
394
395     private void removeResourceUriFromOrderedMap(String resourceType,
396             String resourceURI) {
397         synchronized (orderedResourceUriMap) {
398             if (null != resourceURI && null != resourceType) {
399                 ArrayList<String> uriListForType = orderedResourceUriMap
400                         .get(resourceType);
401                 if (null != uriListForType) {
402                     uriListForType.remove(resourceURI);
403                     if (uriListForType.size() < 1) {
404                         orderedResourceUriMap.remove(resourceType);
405                     }
406                 }
407             } else if (null != resourceType) {
408                 orderedResourceUriMap.remove(resourceType);
409             } else {
410                 orderedResourceUriMap.clear();
411             }
412         }
413     }
414
415     private void addResourceToMap(SimulatorResource simulatorResource) {
416         if (null != simulatorResource) {
417             synchronized (resourceMap) {
418                 Map<String, SimulatorResource> resourceTypeMap;
419                 resourceTypeMap = resourceMap.get(simulatorResource
420                         .getResourceType());
421                 if (null == resourceTypeMap) {
422                     resourceTypeMap = new HashMap<String, SimulatorResource>();
423                     resourceMap.put(simulatorResource.getResourceType(),
424                             resourceTypeMap);
425                 }
426                 resourceTypeMap.put(simulatorResource.getResourceURI(),
427                         simulatorResource);
428             }
429         }
430     }
431
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
437                         .get(resourceType);
438                 if (null != resourceTypeMap) {
439                     resourceTypeMap.putAll(newResourceTypeMap);
440                 } else {
441                     resourceMap.put(resourceType, newResourceTypeMap);
442                 }
443             }
444         }
445     }
446
447     private void removeResourceFromMap(String resourceType, String resourceURI) {
448         if (null != resourceURI && null != resourceType) {
449             synchronized (resourceMap) {
450                 Map<String, SimulatorResource> resourceTypeMap = resourceMap
451                         .get(resourceType);
452                 if (null != resourceTypeMap) {
453                     resourceTypeMap.remove(resourceURI);
454                     if (resourceTypeMap.size() < 1) {
455                         resourceMap.remove(resourceType);
456                     }
457                 }
458             }
459         }
460     }
461
462     public boolean isResourceExist(String resourceURI) {
463         boolean result = false;
464         if (null != resourceURI) {
465             SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
466             if (null != resource) {
467                 result = true;
468             }
469         }
470         return result;
471     }
472
473     public void addResourceDisplayName(String displayName, String completeURI) {
474         synchronized (displayNameMap) {
475             displayNameMap.put(displayName, completeURI);
476         }
477     }
478
479     public void removeResourceDisplayName(String displayName) {
480         synchronized (displayNameMap) {
481             displayNameMap.remove(displayName);
482         }
483     }
484
485     public boolean isDisplayName(String displayName) {
486         boolean exist;
487         synchronized (displayNameMap) {
488             exist = displayNameMap.containsKey(displayName);
489         }
490         return exist;
491     }
492
493     public String getCompleteUriFromDisplayName(String displayName) {
494         String completeURI;
495         synchronized (displayNameMap) {
496             completeURI = displayNameMap.get(displayName);
497         }
498         return completeURI;
499     }
500
501     public void createResource(final String configFilePath) {
502         new Thread() {
503             @Override
504             public void run() {
505                 SimulatorResourceServer resourceServerN;
506                 try {
507                     resourceServerN = SimulatorManager.createResource(
508                             configFilePath, resourceModelChangeListener);
509                 } catch (SimulatorException e) {
510                     Activator
511                             .getDefault()
512                             .getLogManager()
513                             .log(Level.ERROR.ordinal(),
514                                     new Date(),
515                                     "[" + e.getClass().getSimpleName() + "]"
516                                             + e.code().toString() + "-"
517                                             + e.message());
518                     return;
519                 }
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();
528
529                     // Form the display URI
530                     String displayName = Utility.uriToDisplayName(uri);
531                     if (null != displayName) {
532                         addResourceDisplayName(displayName, uri);
533                     } else {
534                         Activator
535                                 .getDefault()
536                                 .getLogManager()
537                                 .log(Level.ERROR.ordinal(),
538                                         new Date(),
539                                         "Converting the URI to display format for "
540                                                 + uri + "failed!");
541                     }
542
543                     // Set the observer for the created resource
544                     try {
545                         resourceServerN.setObserverCallback(observer);
546                     } catch (SimulatorException e) {
547                         Activator
548                                 .getDefault()
549                                 .getLogManager()
550                                 .log(Level.ERROR.ordinal(),
551                                         new Date(),
552                                         "[" + e.getClass().getSimpleName()
553                                                 + "]" + e.code().toString()
554                                                 + "-" + e.message());
555                     }
556
557                     // Print the resource data
558                     simulatorResource.printResourceInfo();
559                 }
560             }
561         }.start();
562     }
563
564     public void createResource(final String configFilePath,
565             final int noOfInstances) {
566         new Thread() {
567             @Override
568             public void run() {
569                 Map<String, SimulatorResource> resourceTypeMap;
570                 SimulatorResourceServer[] simulatorResourceServers;
571                 try {
572                     simulatorResourceServers = SimulatorManager.createResource(
573                             configFilePath, noOfInstances,
574                             resourceModelChangeListener);
575                 } catch (SimulatorException e) {
576                     Activator
577                             .getDefault()
578                             .getLogManager()
579                             .log(Level.ERROR.ordinal(),
580                                     new Date(),
581                                     "[" + e.getClass().getSimpleName() + "]"
582                                             + e.code().toString() + "-"
583                                             + e.message());
584                     return;
585                 }
586                 if (null == simulatorResourceServers) {
587                     return;
588                 }
589                 resourceTypeMap = new HashMap<String, SimulatorResource>();
590                 SimulatorResource resource;
591                 String uri;
592                 String displayName;
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(),
599                                 uri);
600
601                         // Form the display URI
602                         displayName = Utility.uriToDisplayName(uri);
603                         if (null != displayName) {
604                             addResourceDisplayName(displayName, uri);
605                         } else {
606                             Activator
607                                     .getDefault()
608                                     .getLogManager()
609                                     .log(Level.ERROR.ordinal(),
610                                             new Date(),
611                                             "Converting the URI to display format for "
612                                                     + uri + "failed!");
613                         }
614                     }
615                     // Set the observer for the created resource
616                     try {
617                         resourceServerN.setObserverCallback(observer);
618                     } catch (SimulatorException e) {
619                         Activator
620                                 .getDefault()
621                                 .getLogManager()
622                                 .log(Level.ERROR.ordinal(),
623                                         new Date(),
624                                         "[" + e.getClass().getSimpleName()
625                                                 + "]" + e.code().toString()
626                                                 + "-" + e.message());
627                     }
628                 }
629
630                 // Find the resourceType and add it to the local data
631                 // structure and notify UI Listeners
632                 if (resourceTypeMap.size() > 0) {
633                     String resourceType;
634                     Set<String> uriSet = resourceTypeMap.keySet();
635                     Iterator<String> itr = uriSet.iterator();
636                     if (itr.hasNext()) {
637                         SimulatorResource simResource = resourceTypeMap.get(itr
638                                 .next());
639                         if (null != simResource) {
640                             resourceType = simResource.getResourceType();
641
642                             addResourceToMap(resourceType, resourceTypeMap);
643                             resourceCreatedUINotification();
644                         }
645                     }
646                 }
647             }
648         }.start();
649     }
650
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());
658             simulatorResource
659                     .setResourceType(resourceServerN.getResourceType());
660             simulatorResource.setResourceName(resourceServerN.getName());
661             simulatorResource.setResourceInterface(resourceServerN
662                     .getInterfaceType());
663
664             SimulatorResourceModel resourceModelN;
665             try {
666                 resourceModelN = resourceServerN.getModel();
667             } catch (SimulatorException e) {
668                 Activator
669                         .getDefault()
670                         .getLogManager()
671                         .log(Level.ERROR.ordinal(),
672                                 new Date(),
673                                 "[" + e.getClass().getSimpleName() + "]"
674                                         + e.code().toString() + "-"
675                                         + e.message());
676                 return null;
677             }
678             if (null != resourceModelN) {
679                 simulatorResource.setResourceModel(resourceModelN);
680
681                 // Fetch the resource attributes
682                 Map<String, LocalResourceAttribute> resourceAttributeMap;
683                 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
684                 if (null != resourceAttributeMap) {
685                     simulatorResource
686                             .setResourceAttributesMap(resourceAttributeMap);
687                 }
688             }
689         }
690         return simulatorResource;
691     }
692
693     private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
694             SimulatorResourceModel resourceModelN) {
695         Map<String, LocalResourceAttribute> resourceAttributeMap = null;
696         if (null != resourceModelN) {
697             Map<String, ResourceAttribute> attributeMapN;
698             try {
699                 attributeMapN = resourceModelN.getAttributes();
700             } catch (SimulatorException e) {
701                 Activator
702                         .getDefault()
703                         .getLogManager()
704                         .log(Level.ERROR.ordinal(),
705                                 new Date(),
706                                 "[" + e.getClass().getSimpleName() + "]"
707                                         + e.code().toString() + "-"
708                                         + e.message());
709                 return null;
710             }
711             if (null != attributeMapN) {
712                 resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
713
714                 Set<String> attNameSet = attributeMapN.keySet();
715                 String attName;
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);
725
726                         // Set the attribute value
727                         Object valueObj = attributeN.getValue();
728                         if (null != valueObj) {
729                             attribute.setAttributeValue(valueObj);
730                         }
731
732                         // Set the attribute value list.
733                         attribute.setAttValues(getValueList(attributeN));
734
735                         // Initially disabling the automation
736                         attribute.setAutomationInProgress(false);
737
738                         // TODO: Temporarily setting the interval to 500.
739                         // This value should come from the native layer.
740                         // Native implementation is in progress.
741                         attribute
742                                 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
743
744                         // Setting the default automation type
745                         attribute
746                                 .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
747
748                         resourceAttributeMap.put(attName, attribute);
749                     }
750                 }
751             }
752         }
753         return resourceAttributeMap;
754     }
755
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) {
763                 if (null != value) {
764                     valueList.add(String.valueOf(value));
765                 }
766             }
767         } else {
768             Type valueType = attributeN.getBaseType();
769             Range range = attributeN.getRange();
770             if (null != range) {
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));
778                     }
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));
784                     }
785                 }
786             }
787         }
788         Object attValue = attributeN.getValue();
789         if (valueList.size() < 1 && null != attValue) {
790             valueList.add(String.valueOf(attValue));
791         }
792         return valueList;
793     }
794
795     public void deleteResourceByURI(final String resourceURI) {
796         if (null != resourceURI) {
797             new Thread() {
798                 @Override
799                 public void run() {
800                     SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
801                     if (null != resource) {
802                         String resourceType = resource.getResourceType();
803
804                         // Unregister the resource from the platform
805                         deleteResource(resource);
806
807                         // Delete from the local data structure
808                         deleteLocalResourceDetails(resourceType, resourceURI);
809
810                         // Notify the UI listener for removing this resource
811                         // from UI
812                         resourceDeletedUINotification();
813
814                         if (null != currentResourceInSelection
815                                 && resource == currentResourceInSelection) {
816                             // Listeners might query the resource being deleted
817                             // if exists. So set the currently selection to
818                             // null.
819                             setCurrentResourceInSelection(null);
820
821                             // Notify all observers for resource selection
822                             // change event
823                             resourceSelectionChangedUINotification();
824                         }
825                     }
826                 }
827             }.start();
828         }
829     }
830
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();
837                 String resourceType;
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) {
845                             break;
846                         }
847                     }
848                 }
849             }
850         }
851         return resource;
852     }
853
854     private void deleteResource(SimulatorResource resource) {
855         if (null != resource) {
856             SimulatorResourceServer resourceServerN = resource
857                     .getResourceServer();
858             if (null != resourceServerN) {
859                 try {
860                     SimulatorManager.deleteResource(resourceServerN);
861                 } catch (SimulatorException e) {
862                     Activator
863                             .getDefault()
864                             .getLogManager()
865                             .log(Level.ERROR.ordinal(),
866                                     new Date(),
867                                     "[" + e.getClass().getSimpleName() + "]"
868                                             + e.code().toString() + "-"
869                                             + e.message());
870                 }
871             }
872         }
873     }
874
875     public void deleteResourceByType(final String resourceType) {
876         if (null != resourceType) {
877             new Thread() {
878                 @Override
879                 public void run() {
880                     // Unregister the resources from the platform
881                     deleteResource(resourceType);
882
883                     // Delete from the local data structure
884                     deleteLocalResourceDetails(resourceType, null);
885
886                     // Notify the UI listener for removing this resource from UI
887                     resourceDeletedUINotification();
888
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);
895
896                         // Notify all observers for resource selection change
897                         // event
898                         resourceSelectionChangedUINotification();
899                     }
900                 }
901             }.start();
902         }
903     }
904
905     private void deleteResource(String resourceType) {
906         if (null != resourceType) {
907             try {
908                 SimulatorManager.deleteResources(resourceType);
909             } catch (SimulatorException e) {
910                 Activator
911                         .getDefault()
912                         .getLogManager()
913                         .log(Level.ERROR.ordinal(),
914                                 new Date(),
915                                 "[" + e.getClass().getSimpleName() + "]"
916                                         + e.code().toString() + "-"
917                                         + e.message());
918             }
919         }
920     }
921
922     public void deleteAllResources() {
923         new Thread() {
924             @Override
925             public void run() {
926                 // Unregister the resources from the platform
927                 deleteResource();
928
929                 // Delete from the local data structure
930                 deleteLocalResourceDetails(null, null);
931
932                 // Notify the UI listener for removing this resource from UI
933                 resourceDeletedUINotification();
934
935                 // Listeners might query the resource being deleted if exists.
936                 // So set the currently selection to null.
937                 setCurrentResourceInSelection(null);
938
939                 // Notify all observers for resource selection change event
940                 resourceSelectionChangedUINotification();
941             }
942         }.start();
943     }
944
945     private void deleteResource() {
946         try {
947             SimulatorManager.deleteResources(null);
948         } catch (SimulatorException e) {
949             Activator
950                     .getDefault()
951                     .getLogManager()
952                     .log(Level.ERROR.ordinal(),
953                             new Date(),
954                             "[" + e.getClass().getSimpleName() + "]"
955                                     + e.code().toString() + "-" + e.message());
956         }
957     }
958
959     private void deleteLocalResourceDetails(String resourceType,
960             String resourceURI) {
961         if (null != resourceType && null != resourceURI) {
962             removeResourceFromMap(resourceType, resourceURI);
963             removeResourceUriFromOrderedMap(resourceType, resourceURI);
964         } else {
965             synchronized (resourceMap) {
966                 if (null != resourceType) {
967                     removeResourceUriFromOrderedMap(resourceType, null);
968                     resourceMap.remove(resourceType);
969                 } else {
970                     resourceMap.clear();
971                     removeResourceUriFromOrderedMap(null, null);
972                 }
973             }
974         }
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);
980         }
981     }
982
983     private void resourceCreatedUINotification() {
984         synchronized (resourceListChangedUIListeners) {
985             if (resourceListChangedUIListeners.size() > 0) {
986                 IResourceListChangedUIListener listener;
987                 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
988                         .iterator();
989                 while (listenerItr.hasNext()) {
990                     listener = listenerItr.next();
991                     if (null != listener) {
992                         listener.onResourceCreation();
993                     }
994                 }
995             }
996         }
997     }
998
999     private void resourceDeletedUINotification() {
1000         synchronized (resourceListChangedUIListeners) {
1001             if (resourceListChangedUIListeners.size() > 0) {
1002                 IResourceListChangedUIListener listener;
1003                 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
1004                         .iterator();
1005                 while (listenerItr.hasNext()) {
1006                     listener = listenerItr.next();
1007                     if (null != listener) {
1008                         listener.onResourceDeletion();
1009                     }
1010                 }
1011             }
1012         }
1013     }
1014
1015     private void resourceSelectionChangedUINotification() {
1016         synchronized (resourceSelectionChangedUIListeners) {
1017             if (resourceSelectionChangedUIListeners.size() > 0) {
1018                 IResourceSelectionChangedUIListener listener;
1019                 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
1020                         .iterator();
1021                 while (listenerItr.hasNext()) {
1022                     listener = listenerItr.next();
1023                     if (null != listener) {
1024                         listener.onResourceSelectionChange();
1025                     }
1026                 }
1027             }
1028         }
1029     }
1030
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
1040                         .iterator();
1041                 while (listenerItr.hasNext()) {
1042                     listener = listenerItr.next();
1043                     if (null != listener) {
1044                         listener.onResourceModelChange(notificationType,
1045                                 resourceURI, valueChangeSet);
1046                     }
1047                 }
1048             }
1049         }
1050     }
1051
1052     private void resourceAutomationStartedUINotification(String resourceURI) {
1053         synchronized (automationUIListeners) {
1054             if (automationUIListeners.size() > 0 && null != resourceURI) {
1055                 IAutomationUIListener listener;
1056                 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1057                         .iterator();
1058                 while (listenerItr.hasNext()) {
1059                     listener = listenerItr.next();
1060                     if (null != listener) {
1061                         listener.onResourceAutomationStart(resourceURI);
1062                     }
1063                 }
1064             }
1065         }
1066     }
1067
1068     private void automationCompleteUINotification(String resourceURI,
1069             String attName) {
1070         synchronized (automationUIListeners) {
1071             if (automationUIListeners.size() > 0 && null != resourceURI) {
1072                 IAutomationUIListener listener;
1073                 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1074                         .iterator();
1075                 while (listenerItr.hasNext()) {
1076                     listener = listenerItr.next();
1077                     if (null != listener) {
1078                         listener.onAutomationComplete(resourceURI, attName);
1079                     }
1080                 }
1081             }
1082         }
1083     }
1084
1085     private void observerListChangedUINotification(String resourceURI) {
1086         synchronized (observerUIListeners) {
1087             if (observerUIListeners.size() > 0 && null != resourceURI) {
1088                 IObserverListChangedUIListener listener;
1089                 Iterator<IObserverListChangedUIListener> listenerItr = observerUIListeners
1090                         .iterator();
1091                 while (listenerItr.hasNext()) {
1092                     listener = listenerItr.next();
1093                     if (null != listener) {
1094                         listener.onObserverListChanged(resourceURI);
1095                     }
1096                 }
1097             }
1098         }
1099     }
1100
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());
1110                 }
1111             }
1112         }
1113         return typeList;
1114     }
1115
1116     public boolean isTypeExist(String resType) {
1117         synchronized (resourceMap) {
1118             if (resourceMap.containsKey(resType)) {
1119                 return true;
1120             }
1121         }
1122         return false;
1123     }
1124
1125     public List<String> getURIList() {
1126         List<String> list;
1127         synchronized (orderedResourceUriMap) {
1128             Set<String> typeSet = orderedResourceUriMap.keySet();
1129             List<String> typeList = Utility.convertSetToList(typeSet);
1130             if (null == typeList || typeList.size() < 1) {
1131                 return null;
1132             }
1133             list = new ArrayList<String>();
1134
1135             // Sort the types
1136             Collections.sort(typeList);
1137
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());
1142                 if (null != l) {
1143                     list.addAll(l);
1144                 }
1145             }
1146         }
1147         return list;
1148     }
1149
1150     public void resourceSelectionChanged(final String selectedItem) {
1151         new Thread() {
1152             @Override
1153             public void run() {
1154                 // Check whether the item selected is a resource or resource
1155                 // category
1156                 if (isTypeExist(selectedItem)) {
1157                     // Given item is a resource Type
1158                     setCurrentResourceInSelection(null);
1159                 } else {
1160                     // Given item is a resource URI
1161                     SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
1162                     if (null != resource) {
1163                         setCurrentResourceInSelection(resource);
1164                     } else {
1165                         setCurrentResourceInSelection(null);
1166                     }
1167                 }
1168                 // Notify all observers for resource selection change event
1169                 resourceSelectionChangedUINotification();
1170             }
1171         }.start();
1172     }
1173
1174     public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
1175         if (null != resource) {
1176             String propName;
1177             String propValue;
1178
1179             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1180
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.
1189                     propValue = null;
1190                 } else {
1191                     propValue = null;
1192                 }
1193                 if (null != propValue) {
1194                     metaPropertyList.add(new MetaProperty(propName, propValue));
1195                 }
1196             }
1197
1198             return metaPropertyList;
1199         }
1200         return null;
1201     }
1202
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();
1211                 String attName;
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);
1219                     }
1220                 }
1221             }
1222         }
1223         return attList;
1224     }
1225
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);
1233                 if (null == att) {
1234                     return;
1235                 }
1236                 Type baseType = att.getAttValBaseType();
1237                 try {
1238                     if (baseType == Type.STRING) {
1239                         server.updateAttributeString(attributeName, value);
1240                     } else if (baseType == Type.INT) {
1241                         int val;
1242                         try {
1243                             val = Integer.parseInt(value);
1244                             server.updateAttributeInteger(attributeName, val);
1245                         } catch (NumberFormatException nfe) {
1246                             return;
1247                         }
1248                     } else if (baseType == Type.DOUBLE) {
1249                         double val;
1250                         try {
1251                             val = Double.parseDouble(value);
1252                             server.updateAttributeDouble(attributeName, val);
1253                         } catch (NumberFormatException nfe) {
1254                             return;
1255                         }
1256                     } else if (baseType == Type.BOOL) {
1257                         boolean val;
1258                         val = Boolean.parseBoolean(value);
1259                         server.updateAttributeBoolean(attributeName, val);
1260                     }
1261                 } catch (SimulatorException e) {
1262                     Activator
1263                             .getDefault()
1264                             .getLogManager()
1265                             .log(Level.ERROR.ordinal(),
1266                                     new Date(),
1267                                     "[" + e.getClass().getSimpleName() + "]"
1268                                             + e.code().toString() + "-"
1269                                             + e.message());
1270                 }
1271             }
1272         }
1273     }
1274
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();
1283             String attName;
1284             LocalResourceAttribute attributeOld;
1285             LocalResourceAttribute attributeNew;
1286             Object attValueOld;
1287             Object attValueNew;
1288             String oldValueStr;
1289             String newValueStr;
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
1296                     // has been changed
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();
1302
1303                         oldValueStr = String.valueOf(attValueOld);
1304                         newValueStr = String.valueOf(attValueNew);
1305
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);
1311                             }
1312                         }
1313                     }
1314                     resourceAttributeMapNew.remove(attName);
1315                 } else {
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;
1320                 }
1321             }
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);
1333                         }
1334                     }
1335                 }
1336                 notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
1337             }
1338         }
1339         return notificationType;
1340     }
1341
1342     public int startAutomation(SimulatorResource resource,
1343             LocalResourceAttribute attribute, AutomationType autoType,
1344             int autoUpdateInterval) {
1345         int autoId = -1;
1346         if (null != resource && null != attribute) {
1347             SimulatorResourceServer resourceServerN = resource
1348                     .getResourceServer();
1349             if (null != resourceServerN) {
1350                 String attrName = attribute.getAttributeName();
1351                 try {
1352                     autoId = resourceServerN.startAttributeAutomation(attrName,
1353                             autoType, automationListener);
1354                 } catch (SimulatorException e) {
1355                     Activator
1356                             .getDefault()
1357                             .getLogManager()
1358                             .log(Level.ERROR.ordinal(),
1359                                     new Date(),
1360                                     "[" + e.getClass().getSimpleName() + "]"
1361                                             + e.code().toString() + "-"
1362                                             + e.message());
1363                     return -1;
1364                 }
1365                 if (-1 != autoId) {
1366                     attribute.setAutomationId(autoId);
1367                     attribute.setAutomationType(autoType);
1368                     attribute.setAutomationUpdateInterval(autoUpdateInterval);
1369                     attribute.setAutomationInProgress(true);
1370                     resource.setAttributeAutomationInProgress(true);
1371                 } else {
1372                     attribute.setAutomationInProgress(false);
1373                 }
1374             }
1375         }
1376         return autoId;
1377     }
1378
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) {
1385                 try {
1386                     resourceServerN.stopAutomation(autoId);
1387                 } catch (SimulatorException e) {
1388                     Activator
1389                             .getDefault()
1390                             .getLogManager()
1391                             .log(Level.ERROR.ordinal(),
1392                                     new Date(),
1393                                     "[" + e.getClass().getSimpleName() + "]"
1394                                             + e.code().toString() + "-"
1395                                             + e.message());
1396                     return;
1397                 }
1398                 // Change the automation status
1399                 att.setAutomationInProgress(false);
1400                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1401             }
1402         }
1403     }
1404
1405     private boolean isAnyAttributeInAutomation(SimulatorResource resource) {
1406         if (null == resource) {
1407             return false;
1408         }
1409         Map<String, LocalResourceAttribute> attMap = resource
1410                 .getResourceAttributesMap();
1411         if (null == attMap) {
1412             return false;
1413         }
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()) {
1420                 status = true;
1421                 break;
1422             }
1423         }
1424         return status;
1425     }
1426
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();
1436                 String attName;
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;
1446                                 break;
1447                             }
1448                         }
1449                     }
1450                 }
1451             }
1452         }
1453         return targetAttribute;
1454     }
1455
1456     public boolean startResourceAutomationUIRequest(final String resourceURI) {
1457         if (null == resourceURI) {
1458             return false;
1459         }
1460         boolean status = false;
1461         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1462         if (null != resource) {
1463             changeResourceLevelAutomationStatus(resource, true);
1464
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
1470                 // resources
1471                 int autoId = -1;
1472                 try {
1473                     autoId = resourceServer.startResourceAutomation(
1474                             AutomationType.NORMAL, automationListener);
1475                 } catch (SimulatorException e) {
1476                     Activator
1477                             .getDefault()
1478                             .getLogManager()
1479                             .log(Level.ERROR.ordinal(),
1480                                     new Date(),
1481                                     "[" + e.getClass().getSimpleName() + "]"
1482                                             + e.code().toString() + "-"
1483                                             + e.message());
1484                     autoId = -1;
1485                 }
1486                 if (-1 == autoId) {
1487                     // Automation request failed and hence status is being
1488                     // rolled back
1489                     changeResourceLevelAutomationStatus(resource, false);
1490                 } else {
1491                     // Automation request accepted.
1492                     resource.setAutomationId(autoId);
1493
1494                     // Notify the UI listeners in a different thread.
1495                     Thread notifyThread = new Thread() {
1496                         public void run() {
1497                             resourceAutomationStartedUINotification(resourceURI);
1498                         };
1499                     };
1500                     notifyThread.setPriority(Thread.MAX_PRIORITY);
1501                     notifyThread.start();
1502
1503                     status = true;
1504                 }
1505             }
1506         }
1507         return status;
1508     }
1509
1510     public boolean stopResourceAutomationUIRequest(final String resourceURI) {
1511         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1512         if (null == resource) {
1513             return false;
1514         }
1515         final int autoId = resource.getAutomationId();
1516         if (-1 == autoId) {
1517             return false;
1518         }
1519         SimulatorResourceServer resourceServer = resource.getResourceServer();
1520         if (null == resourceServer) {
1521             return false;
1522         }
1523         // Call native method
1524         try {
1525             resourceServer.stopAutomation(autoId);
1526         } catch (SimulatorException e) {
1527             Activator
1528                     .getDefault()
1529                     .getLogManager()
1530                     .log(Level.ERROR.ordinal(),
1531                             new Date(),
1532                             "[" + e.getClass().getSimpleName() + "]"
1533                                     + e.code().toString() + "-" + e.message());
1534             return false;
1535         }
1536
1537         // Notify the UI Listeners. Invoke the automation complete callback.
1538         Thread stopThread = new Thread() {
1539             public void run() {
1540                 automationListener.onAutomationComplete(resourceURI, autoId);
1541             }
1542         };
1543         stopThread.start();
1544         return true;
1545     }
1546
1547     // Changes the automation state of the resource and its attributes
1548     private void changeResourceLevelAutomationStatus(
1549             SimulatorResource resource, boolean status) {
1550
1551         Map<String, LocalResourceAttribute> attributeMap = resource
1552                 .getResourceAttributesMap();
1553         if (null != attributeMap) {
1554             Set<String> attrNameSet = attributeMap.keySet();
1555             Iterator<String> attrNameItr = attrNameSet.iterator();
1556             String attrName;
1557             LocalResourceAttribute attribute;
1558             while (attrNameItr.hasNext()) {
1559                 attrName = attrNameItr.next();
1560                 attribute = attributeMap.get(attrName);
1561                 if (null != attribute) {
1562                     attribute.setAutomationInProgress(status);
1563                 }
1564             }
1565         }
1566         resource.setResourceAutomationInProgress(status);
1567     }
1568
1569     public boolean isResourceAutomationStarted(String resourceURI) {
1570         boolean status = false;
1571         if (null == resourceURI) {
1572             return status;
1573         }
1574
1575         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1576         if (null != resource) {
1577             status = resource.isResourceAutomationInProgress();
1578         }
1579         return status;
1580     }
1581
1582     public boolean isAttributeAutomationStarted(String resourceURI) {
1583         boolean status = false;
1584         if (null == resourceURI) {
1585             return status;
1586         }
1587         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1588         if (null != resource) {
1589             status = resource.isAttributeAutomationInProgress();
1590         }
1591         return status;
1592     }
1593
1594     public LocalResourceAttribute getAttributeByResourceURI(String uri,
1595             String attName) {
1596         if (null == uri || null == attName) {
1597             return null;
1598         }
1599         SimulatorResource resource = getSimulatorResourceByURI(uri);
1600         if (null == resource) {
1601             return null;
1602         }
1603         Map<String, LocalResourceAttribute> attMap = resource
1604                 .getResourceAttributesMap();
1605         if (null == attMap) {
1606             return null;
1607         }
1608         return attMap.get(attName);
1609     }
1610
1611     public void notifyObserverRequest(SimulatorResource res, int observerId) {
1612         if (null == res) {
1613             return;
1614         }
1615         SimulatorResourceServer server = res.getResourceServer();
1616         if (null == server) {
1617             return;
1618         }
1619         try {
1620             server.notifyObserver(observerId);
1621         } catch (SimulatorException e) {
1622             Activator
1623                     .getDefault()
1624                     .getLogManager()
1625                     .log(Level.ERROR.ordinal(),
1626                             new Date(),
1627                             "[" + e.getClass().getSimpleName() + "]"
1628                                     + e.code().toString() + "-" + e.message());
1629         }
1630     }
1631
1632     public Image getImage(String resourceURI) {
1633         if (null == resourceURI) {
1634             return null;
1635         }
1636         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1637         if (null == resource) {
1638             return null;
1639         }
1640         return Activator.getDefault().getImageRegistry()
1641                 .get(resource.getResourceType());
1642     }
1643
1644     public void shutdown() {
1645         threadHandle.interrupt();
1646     }
1647 }