Simulator Service Provider plug-in changes:
[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.utils.Constants;
41 import oic.simulator.serviceprovider.utils.Utility;
42
43 import org.eclipse.swt.graphics.Image;
44 import org.oic.simulator.IAutomation;
45 import org.oic.simulator.ILogger.Level;
46 import org.oic.simulator.ResourceAttribute;
47 import org.oic.simulator.ResourceAttribute.Range;
48 import org.oic.simulator.ResourceAttribute.Type;
49 import org.oic.simulator.SimulatorException;
50 import org.oic.simulator.SimulatorManager;
51 import org.oic.simulator.SimulatorResourceModel;
52 import org.oic.simulator.serviceprovider.AutomationType;
53 import org.oic.simulator.serviceprovider.IObserver;
54 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
55 import org.oic.simulator.serviceprovider.ObserverInfo;
56 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
57
58 /**
59  * This class acts as an interface between the simulator java SDK and the
60  * various UI modules. It maintains all the details of resources and provides
61  * other UI modules with the information required. It also handles model change,
62  * automation, and observer related events from native layer and propagates
63  * those events to the registered UI listeners.
64  */
65 public class ResourceManager {
66
67     private Map<String, Map<String, SimulatorResource>> resourceMap;
68
69     private Map<String, ArrayList<String>>              orderedResourceUriMap;
70
71     private Map<String, String>                         displayNameMap;
72
73     private SimulatorResource                           currentResourceInSelection;
74
75     private List<IResourceListChangedUIListener>        resourceListChangedUIListeners;
76
77     private List<IResourceSelectionChangedUIListener>   resourceSelectionChangedUIListeners;
78
79     private List<IResourceModelChangedUIListener>       resourceModelChangedUIListeners;
80
81     private List<IAutomationUIListener>                 automationUIListeners;
82
83     private List<IObserverListChangedUIListener>        observerUIListeners;
84
85     private IResourceModelChangedListener               resourceModelChangeListener;
86
87     private IAutomation                                 automationListener;
88
89     private IObserver                                   observer;
90
91     private NotificationSynchronizerThread              synchronizerThread;
92
93     private Thread                                      threadHandle;
94
95     public ResourceManager() {
96         resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
97         orderedResourceUriMap = new HashMap<String, ArrayList<String>>();
98         displayNameMap = new HashMap<String, String>();
99
100         resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
101         resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
102         resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
103         automationUIListeners = new ArrayList<IAutomationUIListener>();
104         observerUIListeners = new ArrayList<IObserverListChangedUIListener>();
105
106         resourceModelChangeListener = new IResourceModelChangedListener() {
107
108             @Override
109             public void onResourceModelChanged(final String resourceURI,
110                     final SimulatorResourceModel resourceModelN) {
111                 synchronizerThread.addToQueue(new Runnable() {
112
113                     @Override
114                     public void run() {
115                         if (null == resourceURI || null == resourceModelN) {
116                             return;
117                         }
118                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
119                         if (null == resource) {
120                             return;
121                         }
122
123                         ModelChangeNotificationType notificationType;
124                         // Fetch the resource attributes
125                         Map<String, LocalResourceAttribute> resourceAttributeMapNew;
126                         resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
127                         if (null == resourceAttributeMapNew) {
128                             resource.setResourceAttributesMap(null);
129                             resourceModelChangedUINotification(
130                                     ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL,
131                                     resourceURI, null);
132                             return;
133                         }
134
135                         // Update the resource with new model data
136                         Map<String, LocalResourceAttribute> resourceAttributeMapOld;
137                         resourceAttributeMapOld = resource
138                                 .getResourceAttributesMap();
139                         if (null == resourceAttributeMapOld) {
140                             resource.setResourceAttributesMap(resourceAttributeMapNew);
141                             resourceModelChangedUINotification(
142                                     ModelChangeNotificationType.ATTRIBUTE_ADDED,
143                                     resourceURI, null);
144                             return;
145                         }
146                         Set<LocalResourceAttribute> valueChangeSet = new HashSet<LocalResourceAttribute>();
147                         notificationType = compareAndUpdateLocalAttributes(
148                                 resourceAttributeMapOld,
149                                 resourceAttributeMapNew, valueChangeSet);
150                         if (notificationType != ModelChangeNotificationType.NONE) {
151                             // Update the UI listeners
152                             resourceModelChangedUINotification(
153                                     notificationType, resourceURI,
154                                     valueChangeSet);
155                         }
156                     }
157                 });
158             }
159         };
160
161         automationListener = new IAutomation() {
162
163             @Override
164             public void onAutomationComplete(final String resourceURI,
165                     final int automationId) {
166                 synchronizerThread.addToQueue(new Runnable() {
167
168                     @Override
169                     public void run() {
170                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
171                         if (null == resource) {
172                             return;
173                         }
174                         // Checking whether this notification is for an
175                         // attribute or a resource
176                         if (resource.isResourceAutomationInProgress()) {
177                             changeResourceLevelAutomationStatus(resource, false);
178                             // Notify the UI listeners
179                             automationCompleteUINotification(resourceURI, null);
180                         } else if (resource.isAttributeAutomationInProgress()) {
181                             // Find the attribute with the given automation id
182                             LocalResourceAttribute attribute;
183                             attribute = getAttributeWithGivenAutomationId(
184                                     resource, automationId);
185                             if (null != attribute) {
186                                 attribute.setAutomationInProgress(false);
187                                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
188                                 // Notify the UI listeners
189                                 automationCompleteUINotification(resourceURI,
190                                         attribute.getAttributeName());
191                             }
192                         } else {
193                             // Ignoring the notification as there are no
194                             // known automation for the current resource.
195                         }
196                     }
197                 });
198             }
199         };
200
201         observer = new IObserver() {
202
203             @Override
204             public void onObserverChanged(final String resourceURI,
205                     final int status, final ObserverInfo observer) {
206                 new Thread() {
207                     @Override
208                     public void run() {
209                         if (null == resourceURI || null == observer) {
210                             return;
211                         }
212                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
213                         if (null == resource) {
214                             return;
215                         }
216                         // Update the observers information
217                         if (status == 0) {
218                             resource.addObserverInfo(observer);
219                         } else {
220                             resource.removeObserverInfo(observer);
221                         }
222                         // Notify the UI listeners
223                         observerListChangedUINotification(resourceURI);
224                     }
225                 }.start();
226             }
227         };
228
229         synchronizerThread = new NotificationSynchronizerThread();
230         threadHandle = new Thread(synchronizerThread);
231         threadHandle.setName("Simulator service provider event queue");
232         threadHandle.start();
233     }
234
235     private static class NotificationSynchronizerThread implements Runnable {
236
237         LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
238
239         @Override
240         public void run() {
241             while (!Thread.interrupted()) {
242                 synchronized (this) {
243                     try {
244                         while (notificationQueue.isEmpty()) {
245                             this.wait();
246                             break;
247                         }
248                     } catch (InterruptedException e) {
249                         return;
250                     }
251                 }
252
253                 Runnable thread;
254                 synchronized (this) {
255                     thread = notificationQueue.pop();
256                 }
257                 try {
258                     thread.run();
259                 } catch (Exception e) {
260                     if (e instanceof InterruptedException) {
261                         return;
262                     }
263                     e.printStackTrace();
264                 }
265             }
266         }
267
268         public void addToQueue(Runnable event) {
269             synchronized (this) {
270                 notificationQueue.add(event);
271                 this.notify();
272             }
273         }
274     }
275
276     public void addResourceListChangedUIListener(
277             IResourceListChangedUIListener resourceListChangedUIListener) {
278         synchronized (resourceListChangedUIListeners) {
279             resourceListChangedUIListeners.add(resourceListChangedUIListener);
280         }
281     }
282
283     public void addResourceSelectionChangedUIListener(
284             IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
285         synchronized (resourceSelectionChangedUIListeners) {
286             resourceSelectionChangedUIListeners
287                     .add(resourceSelectionChangedUIListener);
288         }
289     }
290
291     public void addResourceModelChangedUIListener(
292             IResourceModelChangedUIListener resourceModelChangedUIListener) {
293         synchronized (resourceModelChangedUIListeners) {
294             resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
295         }
296     }
297
298     public void addAutomationUIListener(
299             IAutomationUIListener automationUIListener) {
300         synchronized (automationUIListeners) {
301             automationUIListeners.add(automationUIListener);
302         }
303     }
304
305     public void addObserverListChangedUIListener(
306             IObserverListChangedUIListener observerListChangedUIListener) {
307         synchronized (observerUIListeners) {
308             observerUIListeners.add(observerListChangedUIListener);
309         }
310     }
311
312     public void removeResourceListChangedUIListener(
313             IResourceListChangedUIListener listener) {
314         synchronized (resourceListChangedUIListeners) {
315             if (null != listener && resourceListChangedUIListeners.size() > 0) {
316                 resourceListChangedUIListeners.remove(listener);
317             }
318         }
319     }
320
321     public void removeResourceSelectionChangedUIListener(
322             IResourceSelectionChangedUIListener listener) {
323         synchronized (resourceSelectionChangedUIListeners) {
324             if (null != listener
325                     && resourceSelectionChangedUIListeners.size() > 0) {
326                 resourceSelectionChangedUIListeners.remove(listener);
327             }
328         }
329     }
330
331     public void removeResourceModelChangedUIListener(
332             IResourceModelChangedUIListener listener) {
333         synchronized (resourceModelChangedUIListeners) {
334             if (null != listener && resourceModelChangedUIListeners.size() > 0) {
335                 resourceModelChangedUIListeners.remove(listener);
336             }
337         }
338     }
339
340     public void removeAutomationUIListener(IAutomationUIListener listener) {
341         synchronized (automationUIListeners) {
342             if (null != listener && automationUIListeners.size() > 0) {
343                 automationUIListeners.remove(listener);
344             }
345         }
346     }
347
348     public void removeObserverListChangedUIListener(
349             IObserverListChangedUIListener listener) {
350         synchronized (observerUIListeners) {
351             if (null != listener && observerUIListeners.size() > 0) {
352                 observerUIListeners.remove(listener);
353             }
354         }
355     }
356
357     public synchronized SimulatorResource getCurrentResourceInSelection() {
358         return currentResourceInSelection;
359     }
360
361     public synchronized void setCurrentResourceInSelection(
362             SimulatorResource resource) {
363         this.currentResourceInSelection = resource;
364     }
365
366     private void addResourceUriToOrderedMap(String resourceType,
367             String resourceURI) {
368         if (null != resourceURI && null != resourceType) {
369             synchronized (orderedResourceUriMap) {
370                 ArrayList<String> uriListForType = orderedResourceUriMap
371                         .get(resourceType);
372                 if (null == uriListForType) {
373                     uriListForType = new ArrayList<String>();
374                     orderedResourceUriMap.put(resourceType, uriListForType);
375                 }
376                 uriListForType.add(resourceURI);
377             }
378         }
379     }
380
381     private void removeResourceUriFromOrderedMap(String resourceType,
382             String resourceURI) {
383         synchronized (orderedResourceUriMap) {
384             if (null != resourceURI && null != resourceType) {
385                 ArrayList<String> uriListForType = orderedResourceUriMap
386                         .get(resourceType);
387                 if (null != uriListForType) {
388                     uriListForType.remove(resourceURI);
389                     if (uriListForType.size() < 1) {
390                         orderedResourceUriMap.remove(resourceType);
391                     }
392                 }
393             } else if (null != resourceType) {
394                 orderedResourceUriMap.remove(resourceType);
395             } else {
396                 orderedResourceUriMap.clear();
397             }
398         }
399     }
400
401     private void addResourceToMap(SimulatorResource simulatorResource) {
402         if (null != simulatorResource) {
403             synchronized (resourceMap) {
404                 Map<String, SimulatorResource> resourceTypeMap;
405                 resourceTypeMap = resourceMap.get(simulatorResource
406                         .getResourceType());
407                 if (null == resourceTypeMap) {
408                     resourceTypeMap = new HashMap<String, SimulatorResource>();
409                     resourceMap.put(simulatorResource.getResourceType(),
410                             resourceTypeMap);
411                 }
412                 resourceTypeMap.put(simulatorResource.getResourceURI(),
413                         simulatorResource);
414             }
415         }
416     }
417
418     private void addResourceToMap(String resourceType,
419             Map<String, SimulatorResource> newResourceTypeMap) {
420         if (null != resourceType && null != newResourceTypeMap) {
421             synchronized (resourceMap) {
422                 Map<String, SimulatorResource> resourceTypeMap = resourceMap
423                         .get(resourceType);
424                 if (null != resourceTypeMap) {
425                     resourceTypeMap.putAll(newResourceTypeMap);
426                 } else {
427                     resourceMap.put(resourceType, newResourceTypeMap);
428                 }
429             }
430         }
431     }
432
433     private void removeResourceFromMap(String resourceType, String resourceURI) {
434         if (null != resourceURI && null != resourceType) {
435             synchronized (resourceMap) {
436                 Map<String, SimulatorResource> resourceTypeMap = resourceMap
437                         .get(resourceType);
438                 if (null != resourceTypeMap) {
439                     resourceTypeMap.remove(resourceURI);
440                     if (resourceTypeMap.size() < 1) {
441                         resourceMap.remove(resourceType);
442                     }
443                 }
444             }
445         }
446     }
447
448     public boolean isResourceExist(String resourceURI) {
449         boolean result = false;
450         if (null != resourceURI) {
451             SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
452             if (null != resource) {
453                 result = true;
454             }
455         }
456         return result;
457     }
458
459     public void addResourceDisplayName(String displayName, String completeURI) {
460         synchronized (displayNameMap) {
461             displayNameMap.put(displayName, completeURI);
462         }
463     }
464
465     public void removeResourceDisplayName(String displayName) {
466         synchronized (displayNameMap) {
467             displayNameMap.remove(displayName);
468         }
469     }
470
471     public boolean isDisplayName(String displayName) {
472         boolean exist = false;
473         synchronized (displayNameMap) {
474             exist = displayNameMap.containsKey(displayName);
475         }
476         return exist;
477     }
478
479     public String getCompleteUriFromDisplayName(String displayName) {
480         String completeURI = null;
481         synchronized (displayNameMap) {
482             completeURI = displayNameMap.get(displayName);
483         }
484         return completeURI;
485     }
486
487     public void createResource(final String configFilePath) {
488         new Thread() {
489             @Override
490             public void run() {
491                 SimulatorResourceServer resourceServerN;
492                 try {
493                     resourceServerN = SimulatorManager.createResource(
494                             configFilePath, resourceModelChangeListener);
495                 } catch (SimulatorException e) {
496                     Activator
497                             .getDefault()
498                             .getLogManager()
499                             .log(Level.ERROR.ordinal(),
500                                     new Date(),
501                                     "[" + e.getClass().getSimpleName() + "]"
502                                             + e.code().toString() + "-"
503                                             + e.message());
504                     return;
505                 }
506                 SimulatorResource simulatorResource;
507                 simulatorResource = fetchResourceData(resourceServerN);
508                 if (null != simulatorResource) {
509                     String uri = simulatorResource.getResourceURI();
510                     addResourceToMap(simulatorResource);
511                     addResourceUriToOrderedMap(
512                             simulatorResource.getResourceType(), uri);
513                     resourceCreatedUINotification();
514
515                     // Form the display URI
516                     String displayName = Utility.uriToDisplayName(uri);
517                     if (null != displayName) {
518                         addResourceDisplayName(displayName, uri);
519                     } else {
520                         Activator
521                                 .getDefault()
522                                 .getLogManager()
523                                 .log(Level.ERROR.ordinal(),
524                                         new Date(),
525                                         "Converting the URI to display format for "
526                                                 + uri + "failed!");
527                     }
528
529                     // Set the observer for the created resource
530                     try {
531                         resourceServerN.setObserverCallback(observer);
532                     } catch (SimulatorException e) {
533                         Activator
534                                 .getDefault()
535                                 .getLogManager()
536                                 .log(Level.ERROR.ordinal(),
537                                         new Date(),
538                                         "[" + e.getClass().getSimpleName()
539                                                 + "]" + e.code().toString()
540                                                 + "-" + e.message());
541                     }
542
543                     // Print the resource data
544                     simulatorResource.printResourceInfo();
545                 }
546             }
547         }.start();
548     }
549
550     public void createResource(final String configFilePath,
551             final int noOfInstances) {
552         new Thread() {
553             @Override
554             public void run() {
555                 Map<String, SimulatorResource> resourceTypeMap;
556                 SimulatorResourceServer[] simulatorResourceServers = null;
557                 try {
558                     simulatorResourceServers = SimulatorManager.createResource(
559                             configFilePath, noOfInstances,
560                             resourceModelChangeListener);
561                 } catch (SimulatorException e) {
562                     Activator
563                             .getDefault()
564                             .getLogManager()
565                             .log(Level.ERROR.ordinal(),
566                                     new Date(),
567                                     "[" + e.getClass().getSimpleName() + "]"
568                                             + e.code().toString() + "-"
569                                             + e.message());
570                     return;
571                 }
572                 if (null == simulatorResourceServers) {
573                     return;
574                 }
575                 resourceTypeMap = new HashMap<String, SimulatorResource>();
576                 SimulatorResource resource;
577                 String uri;
578                 String displayName;
579                 for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
580                     resource = fetchResourceData(resourceServerN);
581                     if (null != resource) {
582                         uri = resource.getResourceURI();
583                         resourceTypeMap.put(uri, resource);
584                         addResourceUriToOrderedMap(resource.getResourceType(),
585                                 uri);
586
587                         // Form the display URI
588                         displayName = Utility.uriToDisplayName(uri);
589                         if (null != displayName) {
590                             addResourceDisplayName(displayName, uri);
591                         } else {
592                             Activator
593                                     .getDefault()
594                                     .getLogManager()
595                                     .log(Level.ERROR.ordinal(),
596                                             new Date(),
597                                             "Converting the URI to display format for "
598                                                     + uri + "failed!");
599                         }
600                     }
601                     // Set the observer for the created resource
602                     try {
603                         resourceServerN.setObserverCallback(observer);
604                     } catch (SimulatorException e) {
605                         Activator
606                                 .getDefault()
607                                 .getLogManager()
608                                 .log(Level.ERROR.ordinal(),
609                                         new Date(),
610                                         "[" + e.getClass().getSimpleName()
611                                                 + "]" + e.code().toString()
612                                                 + "-" + e.message());
613                     }
614                 }
615
616                 // Find the resourceType and add it to the local data
617                 // structure and notify UI Listeners
618                 if (resourceTypeMap.size() > 0) {
619                     String resourceType;
620                     Set<String> uriSet = resourceTypeMap.keySet();
621                     Iterator<String> itr = uriSet.iterator();
622                     if (itr.hasNext()) {
623                         SimulatorResource simResource = resourceTypeMap.get(itr
624                                 .next());
625                         if (null != simResource) {
626                             resourceType = simResource.getResourceType();
627
628                             addResourceToMap(resourceType, resourceTypeMap);
629                             resourceCreatedUINotification();
630                         }
631                     }
632                 }
633             }
634         }.start();
635     }
636
637     private SimulatorResource fetchResourceData(
638             SimulatorResourceServer resourceServerN) {
639         SimulatorResource simulatorResource = null;
640         if (null != resourceServerN) {
641             simulatorResource = new SimulatorResource();
642             simulatorResource.setResourceServer(resourceServerN);
643             simulatorResource.setResourceURI(resourceServerN.getURI());
644             simulatorResource
645                     .setResourceType(resourceServerN.getResourceType());
646             simulatorResource.setResourceName(resourceServerN.getName());
647             simulatorResource.setResourceInterface(resourceServerN
648                     .getInterfaceType());
649
650             SimulatorResourceModel resourceModelN;
651             try {
652                 resourceModelN = resourceServerN.getModel();
653             } catch (SimulatorException e) {
654                 Activator
655                         .getDefault()
656                         .getLogManager()
657                         .log(Level.ERROR.ordinal(),
658                                 new Date(),
659                                 "[" + e.getClass().getSimpleName() + "]"
660                                         + e.code().toString() + "-"
661                                         + e.message());
662                 return null;
663             }
664             if (null != resourceModelN) {
665                 simulatorResource.setResourceModel(resourceModelN);
666
667                 // Fetch the resource attributes
668                 Map<String, LocalResourceAttribute> resourceAttributeMap;
669                 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
670                 if (null != resourceAttributeMap) {
671                     simulatorResource
672                             .setResourceAttributesMap(resourceAttributeMap);
673                 }
674             }
675         }
676         return simulatorResource;
677     }
678
679     private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
680             SimulatorResourceModel resourceModelN) {
681         Map<String, LocalResourceAttribute> resourceAttributeMap = null;
682         if (null != resourceModelN) {
683             Map<String, ResourceAttribute> attributeMapN;
684             try {
685                 attributeMapN = resourceModelN.getAttributes();
686             } catch (SimulatorException e) {
687                 Activator
688                         .getDefault()
689                         .getLogManager()
690                         .log(Level.ERROR.ordinal(),
691                                 new Date(),
692                                 "[" + e.getClass().getSimpleName() + "]"
693                                         + e.code().toString() + "-"
694                                         + e.message());
695                 return null;
696             }
697             if (null != attributeMapN) {
698                 resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
699
700                 Set<String> attNameSet = attributeMapN.keySet();
701                 String attName;
702                 ResourceAttribute attributeN;
703                 LocalResourceAttribute attribute;
704                 Iterator<String> attNameItr = attNameSet.iterator();
705                 while (attNameItr.hasNext()) {
706                     attName = attNameItr.next();
707                     attributeN = attributeMapN.get(attName);
708                     if (null != attributeN) {
709                         attribute = new LocalResourceAttribute();
710                         attribute.setResourceAttribute(attributeN);
711
712                         // Set the attribute value
713                         Object valueObj = attributeN.getValue();
714                         if (null != valueObj) {
715                             attribute.setAttributeValue(valueObj);
716                         }
717
718                         // Set the attribute value list.
719                         attribute.setAttValues(getValueList(attributeN));
720
721                         // Initially disabling the automation
722                         attribute.setAutomationInProgress(false);
723
724                         // TODO: Temporarily setting the interval to 500.
725                         // This value should come from the native layer.
726                         // Native implementation is in progress.
727                         attribute
728                                 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
729
730                         // Setting the default automation type
731                         attribute
732                                 .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
733
734                         resourceAttributeMap.put(attName, attribute);
735                     }
736                 }
737             }
738         }
739         return resourceAttributeMap;
740     }
741
742     // This method gives all known possible values of the attribute in string
743     // format. It takes allowed values or range of values whichever is available
744     private List<String> getValueList(ResourceAttribute attributeN) {
745         Object[] allowedValues = attributeN.getAllowedValues();
746         List<String> valueList = new ArrayList<String>();
747         if (null != allowedValues && allowedValues.length > 0) {
748             for (Object value : allowedValues) {
749                 if (null != value) {
750                     valueList.add(String.valueOf(value));
751                 }
752             }
753         } else {
754             Type valueType = attributeN.getBaseType();
755             Range range = attributeN.getRange();
756             if (null != range) {
757                 Object min = range.getMin();
758                 Object max = range.getMax();
759                 if (valueType == Type.INT) {
760                     int minI = (Integer) min;
761                     int maxI = (Integer) max;
762                     for (int value = minI; value <= maxI; value++) {
763                         valueList.add(String.valueOf(value));
764                     }
765                 } else if (valueType == Type.DOUBLE) {
766                     double minD = (Double) min;
767                     double maxD = (Double) max;
768                     for (double value = minD; value <= maxD; value++) {
769                         valueList.add(String.valueOf(value));
770                     }
771                 }
772             }
773         }
774         Object attValue = attributeN.getValue();
775         if (valueList.size() < 1 && null != attValue) {
776             valueList.add(String.valueOf(attValue));
777         }
778         return valueList;
779     }
780
781     public void deleteResourceByURI(final String resourceURI) {
782         if (null != resourceURI) {
783             new Thread() {
784                 @Override
785                 public void run() {
786                     SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
787                     if (null != resource) {
788                         String resourceType = resource.getResourceType();
789
790                         // Unregister the resource from the platform
791                         deleteResource(resource);
792
793                         // Delete from the local data structure
794                         deleteLocalResourceDetails(resourceType, resourceURI);
795
796                         // Notify the UI listener for removing this resource
797                         // from UI
798                         resourceDeletedUINotification();
799
800                         if (null != currentResourceInSelection
801                                 && resource == currentResourceInSelection) {
802                             // Listeners might query the resource being deleted
803                             // if exists. So set the currently selection to
804                             // null.
805                             setCurrentResourceInSelection(null);
806
807                             // Notify all observers for resource selection
808                             // change event
809                             resourceSelectionChangedUINotification();
810                         }
811                     }
812                 }
813             }.start();
814         }
815     }
816
817     private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
818         SimulatorResource resource = null;
819         if (null != resourceURI) {
820             synchronized (resourceMap) {
821                 Set<String> typeSet = resourceMap.keySet();
822                 Iterator<String> typeItr = typeSet.iterator();
823                 String resourceType;
824                 Map<String, SimulatorResource> resourceTypeMap;
825                 while (typeItr.hasNext()) {
826                     resourceType = typeItr.next();
827                     resourceTypeMap = resourceMap.get(resourceType);
828                     if (null != resourceTypeMap) {
829                         resource = resourceTypeMap.get(resourceURI);
830                         if (null != resource) {
831                             break;
832                         }
833                     }
834                 }
835             }
836         }
837         return resource;
838     }
839
840     private void deleteResource(SimulatorResource resource) {
841         if (null != resource) {
842             SimulatorResourceServer resourceServerN = resource
843                     .getResourceServer();
844             if (null != resourceServerN) {
845                 try {
846                     SimulatorManager.deleteResource(resourceServerN);
847                 } catch (SimulatorException e) {
848                     Activator
849                             .getDefault()
850                             .getLogManager()
851                             .log(Level.ERROR.ordinal(),
852                                     new Date(),
853                                     "[" + e.getClass().getSimpleName() + "]"
854                                             + e.code().toString() + "-"
855                                             + e.message());
856                 }
857             }
858         }
859     }
860
861     public void deleteResourceByType(final String resourceType) {
862         if (null != resourceType) {
863             new Thread() {
864                 @Override
865                 public void run() {
866                     // Unregister the resources from the platform
867                     deleteResource(resourceType);
868
869                     // Delete from the local data structure
870                     deleteLocalResourceDetails(resourceType, null);
871
872                     // Notify the UI listener for removing this resource from UI
873                     resourceDeletedUINotification();
874
875                     if (null != currentResourceInSelection
876                             && resourceType.equals(currentResourceInSelection
877                                     .getResourceType())) {
878                         // Listeners might query the resource being deleted if
879                         // exists. So set the currently selection to null.
880                         setCurrentResourceInSelection(null);
881
882                         // Notify all observers for resource selection change
883                         // event
884                         resourceSelectionChangedUINotification();
885                     }
886                 }
887             }.start();
888         }
889     }
890
891     private void deleteResource(String resourceType) {
892         if (null != resourceType) {
893             try {
894                 SimulatorManager.deleteResources(resourceType);
895             } catch (SimulatorException e) {
896                 Activator
897                         .getDefault()
898                         .getLogManager()
899                         .log(Level.ERROR.ordinal(),
900                                 new Date(),
901                                 "[" + e.getClass().getSimpleName() + "]"
902                                         + e.code().toString() + "-"
903                                         + e.message());
904             }
905         }
906     }
907
908     public void deleteAllResources() {
909         new Thread() {
910             @Override
911             public void run() {
912                 // Unregister the resources from the platform
913                 deleteResource();
914
915                 // Delete from the local data structure
916                 deleteLocalResourceDetails(null, null);
917
918                 // Notify the UI listener for removing this resource from UI
919                 resourceDeletedUINotification();
920
921                 // Listeners might query the resource being deleted if exists.
922                 // So set the currently selection to null.
923                 setCurrentResourceInSelection(null);
924
925                 // Notify all observers for resource selection change event
926                 resourceSelectionChangedUINotification();
927             }
928         }.start();
929     }
930
931     private void deleteResource() {
932         try {
933             SimulatorManager.deleteResources(null);
934         } catch (SimulatorException e) {
935             Activator
936                     .getDefault()
937                     .getLogManager()
938                     .log(Level.ERROR.ordinal(),
939                             new Date(),
940                             "[" + e.getClass().getSimpleName() + "]"
941                                     + e.code().toString() + "-" + e.message());
942         }
943     }
944
945     private void deleteLocalResourceDetails(String resourceType,
946             String resourceURI) {
947         if (null != resourceType && null != resourceURI) {
948             removeResourceFromMap(resourceType, resourceURI);
949             removeResourceUriFromOrderedMap(resourceType, resourceURI);
950         } else {
951             synchronized (resourceMap) {
952                 if (null != resourceType) {
953                     removeResourceUriFromOrderedMap(resourceType, null);
954                     resourceMap.remove(resourceType);
955                 } else {
956                     resourceMap.clear();
957                     removeResourceUriFromOrderedMap(null, null);
958                 }
959             }
960         }
961         // Remove the display name from the map
962         if (null != resourceURI) {
963             String dispName = Utility.uriToDisplayName(resourceURI);
964             if (null != dispName)
965                 removeResourceDisplayName(dispName);
966         }
967     }
968
969     private void resourceCreatedUINotification() {
970         synchronized (resourceListChangedUIListeners) {
971             if (resourceListChangedUIListeners.size() > 0) {
972                 IResourceListChangedUIListener listener;
973                 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
974                         .iterator();
975                 while (listenerItr.hasNext()) {
976                     listener = listenerItr.next();
977                     if (null != listener) {
978                         listener.onResourceCreation();
979                     }
980                 }
981             }
982         }
983     }
984
985     private void resourceDeletedUINotification() {
986         synchronized (resourceListChangedUIListeners) {
987             if (resourceListChangedUIListeners.size() > 0) {
988                 IResourceListChangedUIListener listener;
989                 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
990                         .iterator();
991                 while (listenerItr.hasNext()) {
992                     listener = listenerItr.next();
993                     if (null != listener) {
994                         listener.onResourceDeletion();
995                     }
996                 }
997             }
998         }
999     }
1000
1001     private void resourceSelectionChangedUINotification() {
1002         synchronized (resourceSelectionChangedUIListeners) {
1003             if (resourceSelectionChangedUIListeners.size() > 0) {
1004                 IResourceSelectionChangedUIListener listener;
1005                 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
1006                         .iterator();
1007                 while (listenerItr.hasNext()) {
1008                     listener = listenerItr.next();
1009                     if (null != listener) {
1010                         listener.onResourceSelectionChange();
1011                     }
1012                 }
1013             }
1014         }
1015     }
1016
1017     private void resourceModelChangedUINotification(
1018             ModelChangeNotificationType notificationType, String resourceURI,
1019             Set<LocalResourceAttribute> valueChangeSet) {
1020         synchronized (resourceModelChangedUIListeners) {
1021             if (resourceModelChangedUIListeners.size() > 0
1022                     && notificationType != ModelChangeNotificationType.NONE
1023                     && null != resourceURI) {
1024                 IResourceModelChangedUIListener listener;
1025                 Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
1026                         .iterator();
1027                 while (listenerItr.hasNext()) {
1028                     listener = listenerItr.next();
1029                     if (null != listener) {
1030                         listener.onResourceModelChange(notificationType,
1031                                 resourceURI, valueChangeSet);
1032                     }
1033                 }
1034             }
1035         }
1036     }
1037
1038     private void resourceAutomationStartedUINotification(String resourceURI) {
1039         synchronized (automationUIListeners) {
1040             if (automationUIListeners.size() > 0 && null != resourceURI) {
1041                 IAutomationUIListener listener;
1042                 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1043                         .iterator();
1044                 while (listenerItr.hasNext()) {
1045                     listener = listenerItr.next();
1046                     if (null != listener) {
1047                         listener.onResourceAutomationStart(resourceURI);
1048                     }
1049                 }
1050             }
1051         }
1052     }
1053
1054     private void automationCompleteUINotification(String resourceURI,
1055             String attName) {
1056         synchronized (automationUIListeners) {
1057             if (automationUIListeners.size() > 0 && null != resourceURI) {
1058                 IAutomationUIListener listener;
1059                 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
1060                         .iterator();
1061                 while (listenerItr.hasNext()) {
1062                     listener = listenerItr.next();
1063                     if (null != listener) {
1064                         listener.onAutomationComplete(resourceURI, attName);
1065                     }
1066                 }
1067             }
1068         }
1069     }
1070
1071     private void observerListChangedUINotification(String resourceURI) {
1072         synchronized (observerUIListeners) {
1073             if (observerUIListeners.size() > 0 && null != resourceURI) {
1074                 IObserverListChangedUIListener listener;
1075                 Iterator<IObserverListChangedUIListener> listenerItr = observerUIListeners
1076                         .iterator();
1077                 while (listenerItr.hasNext()) {
1078                     listener = listenerItr.next();
1079                     if (null != listener) {
1080                         listener.onObserverListChanged(resourceURI);
1081                     }
1082                 }
1083             }
1084         }
1085     }
1086
1087     public List<String> getResourceTypeList() {
1088         List<String> typeList = null;
1089         synchronized (resourceMap) {
1090             if (resourceMap.size() > 0) {
1091                 typeList = new ArrayList<String>();
1092                 Set<String> typeSet = resourceMap.keySet();
1093                 Iterator<String> typeItr = typeSet.iterator();
1094                 while (typeItr.hasNext()) {
1095                     typeList.add(typeItr.next());
1096                 }
1097             }
1098         }
1099         return typeList;
1100     }
1101
1102     public boolean isTypeExist(String resType) {
1103         synchronized (resourceMap) {
1104             if (resourceMap.containsKey(resType)) {
1105                 return true;
1106             }
1107         }
1108         return false;
1109     }
1110
1111     public List<String> getURIList() {
1112         List<String> list = null;
1113         synchronized (orderedResourceUriMap) {
1114             Set<String> typeSet = orderedResourceUriMap.keySet();
1115             List<String> typeList = Utility.convertSetToList(typeSet);
1116             if (null == typeList || typeList.size() < 1) {
1117                 return list;
1118             }
1119             list = new ArrayList<String>();
1120
1121             // Sort the types
1122             Collections.sort(typeList);
1123
1124             // Add all URIs to the output list
1125             Iterator<String> typeItr = typeList.iterator();
1126             while (typeItr.hasNext()) {
1127                 List<String> l = orderedResourceUriMap.get(typeItr.next());
1128                 if (null != l) {
1129                     list.addAll(l);
1130                 }
1131             }
1132         }
1133         return list;
1134     }
1135
1136     public void resourceSelectionChanged(final String selectedItem) {
1137         new Thread() {
1138             @Override
1139             public void run() {
1140                 // Check whether the item selected is a resource or resource
1141                 // category
1142                 if (isTypeExist(selectedItem)) {
1143                     // Given item is a resource Type
1144                     setCurrentResourceInSelection(null);
1145                 } else {
1146                     // Given item is a resource URI
1147                     SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
1148                     if (null != resource) {
1149                         setCurrentResourceInSelection(resource);
1150                     } else {
1151                         setCurrentResourceInSelection(null);
1152                     }
1153                 }
1154                 // Notify all observers for resource selection change event
1155                 resourceSelectionChangedUINotification();
1156             }
1157         }.start();
1158     }
1159
1160     public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
1161         if (null != resource) {
1162             String propName;
1163             String propValue;
1164
1165             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1166
1167             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1168                 propName = Constants.META_PROPERTIES[index];
1169                 if (propName.equals(Constants.RESOURCE_URI)) {
1170                     propValue = resource.getResourceURI();
1171                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1172                     propValue = resource.getResourceType();
1173                 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1174                     // TODO: Temporarily ignoring till the implementation.
1175                     propValue = null;
1176                 } else {
1177                     propValue = null;
1178                 }
1179                 if (null != propValue) {
1180                     metaPropertyList.add(new MetaProperty(propName, propValue));
1181                 }
1182             }
1183
1184             return metaPropertyList;
1185         }
1186         return null;
1187     }
1188
1189     public List<LocalResourceAttribute> getAttributes(SimulatorResource resource) {
1190         List<LocalResourceAttribute> attList = null;
1191         if (null != resource) {
1192             Map<String, LocalResourceAttribute> attMap = resource
1193                     .getResourceAttributesMap();
1194             if (null != attMap && attMap.size() > 0) {
1195                 attList = new ArrayList<LocalResourceAttribute>();
1196                 Set<String> attNameSet = attMap.keySet();
1197                 String attName;
1198                 LocalResourceAttribute attribute;
1199                 Iterator<String> attNameItr = attNameSet.iterator();
1200                 while (attNameItr.hasNext()) {
1201                     attName = attNameItr.next();
1202                     attribute = attMap.get(attName);
1203                     if (null != attribute) {
1204                         attList.add(attribute);
1205                     }
1206                 }
1207             }
1208         }
1209         return attList;
1210     }
1211
1212     public void attributeValueUpdated(SimulatorResource resource,
1213             String attributeName, String value) {
1214         if (null != resource && null != attributeName && null != value) {
1215             SimulatorResourceServer server = resource.getResourceServer();
1216             if (null != server) {
1217                 LocalResourceAttribute att = resource
1218                         .getAttribute(attributeName);
1219                 if (null == att) {
1220                     return;
1221                 }
1222                 Type baseType = att.getAttValBaseType();
1223                 try {
1224                     if (baseType == Type.STRING) {
1225                         server.updateAttributeString(attributeName, value);
1226                     } else if (baseType == Type.INT) {
1227                         int val;
1228                         try {
1229                             val = Integer.parseInt(value);
1230                             server.updateAttributeInteger(attributeName, val);
1231                         } catch (NumberFormatException nfe) {
1232                             return;
1233                         }
1234                     } else if (baseType == Type.DOUBLE) {
1235                         double val;
1236                         try {
1237                             val = Double.parseDouble(value);
1238                             server.updateAttributeDouble(attributeName, val);
1239                         } catch (NumberFormatException nfe) {
1240                             return;
1241                         }
1242                     } else if (baseType == Type.BOOL) {
1243                         boolean val;
1244                         val = Boolean.parseBoolean(value);
1245                         server.updateAttributeBoolean(attributeName, val);
1246                     }
1247                 } catch (SimulatorException e) {
1248                     Activator
1249                             .getDefault()
1250                             .getLogManager()
1251                             .log(Level.ERROR.ordinal(),
1252                                     new Date(),
1253                                     "[" + e.getClass().getSimpleName() + "]"
1254                                             + e.code().toString() + "-"
1255                                             + e.message());
1256                 }
1257             }
1258         }
1259     }
1260
1261     private ModelChangeNotificationType compareAndUpdateLocalAttributes(
1262             Map<String, LocalResourceAttribute> resourceAttributeMapOld,
1263             Map<String, LocalResourceAttribute> resourceAttributeMapNew,
1264             Set<LocalResourceAttribute> valueChangeSet) {
1265         ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
1266         if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
1267             Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
1268             Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
1269             String attName;
1270             LocalResourceAttribute attributeOld;
1271             LocalResourceAttribute attributeNew;
1272             Object attValueOld;
1273             Object attValueNew;
1274             String oldValueStr;
1275             String newValueStr;
1276             while (attributeMapOldItr.hasNext()) {
1277                 attName = attributeMapOldItr.next();
1278                 if (resourceAttributeMapNew.containsKey(attName)) {
1279                     attributeOld = resourceAttributeMapOld.get(attName);
1280                     attributeNew = resourceAttributeMapNew.get(attName);
1281                     // Copy the attribute value from new to old if the value
1282                     // has been changed
1283                     // Comparing only the attribute's value considering the
1284                     // fact that only the value can be changed
1285                     if (null != attributeOld && null != attributeNew) {
1286                         attValueOld = attributeOld.getAttributeValue();
1287                         attValueNew = attributeNew.getAttributeValue();
1288
1289                         oldValueStr = String.valueOf(attValueOld);
1290                         newValueStr = String.valueOf(attValueNew);
1291
1292                         if (null != oldValueStr && null != newValueStr) {
1293                             if (!oldValueStr.equals(newValueStr)) {
1294                                 attributeOld.setAttributeValue(attValueNew);
1295                                 notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
1296                                 valueChangeSet.add(attributeOld);
1297                             }
1298                         }
1299                     }
1300                     resourceAttributeMapNew.remove(attName);
1301                 } else {
1302                     // Attribute doesn't exist in the new model. Hence
1303                     // removing it from the model.
1304                     resourceAttributeMapOld.remove(attName);
1305                     notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
1306                 }
1307             }
1308             // Check for new attributes in the new model
1309             if (resourceAttributeMapNew.size() > 0) {
1310                 Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
1311                 Iterator<String> remainingAttItr = remainingAttSet.iterator();
1312                 LocalResourceAttribute attribute;
1313                 while (remainingAttItr.hasNext()) {
1314                     attName = remainingAttItr.next();
1315                     if (null != attName) {
1316                         attribute = resourceAttributeMapNew.get(attName);
1317                         if (null != attribute) {
1318                             resourceAttributeMapOld.put(attName, attribute);
1319                         }
1320                     }
1321                 }
1322                 notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
1323             }
1324         }
1325         return notificationType;
1326     }
1327
1328     public int startAutomation(SimulatorResource resource,
1329             LocalResourceAttribute attribute, AutomationType autoType,
1330             int autoUpdateInterval) {
1331         int autoId = -1;
1332         if (null != resource && null != attribute) {
1333             SimulatorResourceServer resourceServerN = resource
1334                     .getResourceServer();
1335             if (null != resourceServerN) {
1336                 String attrName = attribute.getAttributeName();
1337                 try {
1338                     autoId = resourceServerN.startAttributeAutomation(attrName,
1339                             autoType, autoUpdateInterval, automationListener);
1340                 } catch (SimulatorException e) {
1341                     Activator
1342                             .getDefault()
1343                             .getLogManager()
1344                             .log(Level.ERROR.ordinal(),
1345                                     new Date(),
1346                                     "[" + e.getClass().getSimpleName() + "]"
1347                                             + e.code().toString() + "-"
1348                                             + e.message());
1349                     return -1;
1350                 }
1351                 if (-1 != autoId) {
1352                     attribute.setAutomationId(autoId);
1353                     attribute.setAutomationType(autoType);
1354                     attribute.setAutomationUpdateInterval(autoUpdateInterval);
1355                     attribute.setAutomationInProgress(true);
1356                     resource.setAttributeAutomationInProgress(true);
1357                 } else {
1358                     attribute.setAutomationInProgress(false);
1359                 }
1360             }
1361         }
1362         return autoId;
1363     }
1364
1365     public void stopAutomation(SimulatorResource resource,
1366             LocalResourceAttribute att, int autoId) {
1367         if (null != resource) {
1368             SimulatorResourceServer resourceServerN = resource
1369                     .getResourceServer();
1370             if (null != resourceServerN) {
1371                 try {
1372                     resourceServerN.stopAutomation(autoId);
1373                 } catch (SimulatorException e) {
1374                     Activator
1375                             .getDefault()
1376                             .getLogManager()
1377                             .log(Level.ERROR.ordinal(),
1378                                     new Date(),
1379                                     "[" + e.getClass().getSimpleName() + "]"
1380                                             + e.code().toString() + "-"
1381                                             + e.message());
1382                     return;
1383                 }
1384                 // Change the automation status
1385                 att.setAutomationInProgress(false);
1386                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1387             }
1388         }
1389     }
1390
1391     private boolean isAnyAttributeInAutomation(SimulatorResource resource) {
1392         if (null == resource) {
1393             return false;
1394         }
1395         Map<String, LocalResourceAttribute> attMap = resource
1396                 .getResourceAttributesMap();
1397         if (null == attMap) {
1398             return false;
1399         }
1400         boolean status = false;
1401         Set<String> keySet = attMap.keySet();
1402         Iterator<String> attItr = keySet.iterator();
1403         while (attItr.hasNext()) {
1404             LocalResourceAttribute attribute = attMap.get(attItr.next());
1405             if (attribute.isAutomationInProgress()) {
1406                 status = true;
1407                 break;
1408             }
1409         }
1410         return status;
1411     }
1412
1413     private LocalResourceAttribute getAttributeWithGivenAutomationId(
1414             SimulatorResource resource, int automationId) {
1415         LocalResourceAttribute targetAttribute = null;
1416         if (null != resource) {
1417             Map<String, LocalResourceAttribute> attributeMap = resource
1418                     .getResourceAttributesMap();
1419             if (null != attributeMap) {
1420                 Set<String> attNameSet = attributeMap.keySet();
1421                 Iterator<String> attNameItr = attNameSet.iterator();
1422                 String attName;
1423                 LocalResourceAttribute attribute;
1424                 while (attNameItr.hasNext()) {
1425                     attName = attNameItr.next();
1426                     if (null != attName) {
1427                         attribute = attributeMap.get(attName);
1428                         if (null != attribute) {
1429                             if (attribute.isAutomationInProgress()
1430                                     && (attribute.getAutomationId() == automationId)) {
1431                                 targetAttribute = attribute;
1432                                 break;
1433                             }
1434                         }
1435                     }
1436                 }
1437             }
1438         }
1439         return targetAttribute;
1440     }
1441
1442     public boolean startResourceAutomationUIRequest(final String resourceURI) {
1443         if (null == resourceURI) {
1444             return false;
1445         }
1446         boolean status = false;
1447         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1448         if (null != resource) {
1449             changeResourceLevelAutomationStatus(resource, true);
1450
1451             // Invoke the native automation method
1452             SimulatorResourceServer resourceServer = resource
1453                     .getResourceServer();
1454             if (null != resourceServer) {
1455                 // TODO: Temporarily handling the normal one-time automation for
1456                 // resources
1457                 int autoId = -1;
1458                 try {
1459                     autoId = resourceServer.startResourceAutomation(
1460                             AutomationType.NORMAL, 500, automationListener);
1461                 } catch (SimulatorException e) {
1462                     Activator
1463                             .getDefault()
1464                             .getLogManager()
1465                             .log(Level.ERROR.ordinal(),
1466                                     new Date(),
1467                                     "[" + e.getClass().getSimpleName() + "]"
1468                                             + e.code().toString() + "-"
1469                                             + e.message());
1470                     autoId = -1;
1471                 }
1472                 if (-1 == autoId) {
1473                     // Automation request failed and hence status is being
1474                     // rolled back
1475                     changeResourceLevelAutomationStatus(resource, false);
1476                 } else {
1477                     // Automation request accepted.
1478                     resource.setAutomationId(autoId);
1479
1480                     // Notify the UI listeners in a different thread.
1481                     Thread notifyThread = new Thread() {
1482                         public void run() {
1483                             resourceAutomationStartedUINotification(resourceURI);
1484                         };
1485                     };
1486                     notifyThread.setPriority(Thread.MAX_PRIORITY);
1487                     notifyThread.start();
1488
1489                     status = true;
1490                 }
1491             }
1492         }
1493         return status;
1494     }
1495
1496     public boolean stopResourceAutomationUIRequest(final String resourceURI) {
1497         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1498         if (null == resource) {
1499             return false;
1500         }
1501         final int autoId = resource.getAutomationId();
1502         if (-1 == autoId) {
1503             return false;
1504         }
1505         SimulatorResourceServer resourceServer = resource.getResourceServer();
1506         if (null == resourceServer) {
1507             return false;
1508         }
1509         // Call native method
1510         try {
1511             resourceServer.stopAutomation(autoId);
1512         } catch (SimulatorException e) {
1513             Activator
1514                     .getDefault()
1515                     .getLogManager()
1516                     .log(Level.ERROR.ordinal(),
1517                             new Date(),
1518                             "[" + e.getClass().getSimpleName() + "]"
1519                                     + e.code().toString() + "-" + e.message());
1520             return false;
1521         }
1522
1523         // Notify the UI Listeners. Invoke the automation complete callback.
1524         Thread stopThread = new Thread() {
1525             public void run() {
1526                 automationListener.onAutomationComplete(resourceURI, autoId);
1527             }
1528         };
1529         stopThread.start();
1530         return true;
1531     }
1532
1533     // Changes the automation state of the resource and its attributes
1534     private void changeResourceLevelAutomationStatus(
1535             SimulatorResource resource, boolean status) {
1536
1537         Map<String, LocalResourceAttribute> attributeMap = resource
1538                 .getResourceAttributesMap();
1539         if (null != attributeMap) {
1540             Set<String> attrNameSet = attributeMap.keySet();
1541             Iterator<String> attrNameItr = attrNameSet.iterator();
1542             String attrName;
1543             LocalResourceAttribute attribute;
1544             while (attrNameItr.hasNext()) {
1545                 attrName = attrNameItr.next();
1546                 attribute = attributeMap.get(attrName);
1547                 if (null != attribute) {
1548                     attribute.setAutomationInProgress(status);
1549                 }
1550             }
1551         }
1552         resource.setResourceAutomationInProgress(status);
1553     }
1554
1555     public boolean isResourceAutomationStarted(String resourceURI) {
1556         boolean status = false;
1557         if (null == resourceURI) {
1558             return status;
1559         }
1560
1561         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1562         if (null != resource) {
1563             status = resource.isResourceAutomationInProgress();
1564         }
1565         return status;
1566     }
1567
1568     public boolean isAttributeAutomationStarted(String resourceURI) {
1569         boolean status = false;
1570         if (null == resourceURI) {
1571             return status;
1572         }
1573         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1574         if (null != resource) {
1575             status = resource.isAttributeAutomationInProgress();
1576         }
1577         return status;
1578     }
1579
1580     public LocalResourceAttribute getAttributeByResourceURI(String uri,
1581             String attName) {
1582         if (null == uri || null == attName) {
1583             return null;
1584         }
1585         SimulatorResource resource = getSimulatorResourceByURI(uri);
1586         if (null == resource) {
1587             return null;
1588         }
1589         Map<String, LocalResourceAttribute> attMap = resource
1590                 .getResourceAttributesMap();
1591         if (null == attMap) {
1592             return null;
1593         }
1594         return attMap.get(attName);
1595     }
1596
1597     public void notifyObserverRequest(SimulatorResource res, int observerId) {
1598         if (null == res) {
1599             return;
1600         }
1601         SimulatorResourceServer server = res.getResourceServer();
1602         if (null == server) {
1603             return;
1604         }
1605         try {
1606             server.notifyObserver(observerId);
1607         } catch (SimulatorException e) {
1608             Activator
1609                     .getDefault()
1610                     .getLogManager()
1611                     .log(Level.ERROR.ordinal(),
1612                             new Date(),
1613                             "[" + e.getClass().getSimpleName() + "]"
1614                                     + e.code().toString() + "-" + e.message());
1615         }
1616     }
1617
1618     public boolean isAttHasRangeOrAllowedValues(LocalResourceAttribute att) {
1619         if (null == att) {
1620             return false;
1621         }
1622         Object[] obj = att.getAllowedValues();
1623         if (null != obj && obj.length > 0) {
1624             return true;
1625         }
1626         Object min = att.getMinValue();
1627         Object max = att.getMaxValue();
1628         if (null != min && null != max) {
1629             return true;
1630         }
1631         return false;
1632     }
1633
1634     public boolean isResourceAutomationAllowed(String resourceURI) {
1635         if (null == resourceURI) {
1636             return false;
1637         }
1638         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1639         if (null == resource) {
1640             return false;
1641         }
1642         Map<String, LocalResourceAttribute> attributes = resource
1643                 .getResourceAttributesMap();
1644         if (null == attributes || attributes.size() < 1) {
1645             return false;
1646         }
1647         Set<String> keySet = attributes.keySet();
1648         Iterator<String> itr = keySet.iterator();
1649         String attName;
1650         boolean allowed = false;
1651         LocalResourceAttribute att;
1652         while (itr.hasNext()) {
1653             attName = itr.next();
1654             if (null != attName) {
1655                 att = attributes.get(attName);
1656                 if (null != att) {
1657                     allowed = allowed | isAttHasRangeOrAllowedValues(att);
1658                 }
1659             }
1660         }
1661         return allowed;
1662     }
1663
1664     public Image getImage(String resourceURI) {
1665         if (null == resourceURI) {
1666             return null;
1667         }
1668         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1669         if (null == resource) {
1670             return null;
1671         }
1672         return Activator.getDefault().getImageRegistry()
1673                 .get(resource.getResourceType());
1674     }
1675
1676     public void shutdown() {
1677         threadHandle.interrupt();
1678     }
1679 }