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