Eclipse plug-in for service provider.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / ResourceManager.java
1 package oic.simulator.serviceprovider.manager;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.LinkedList;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Set;
10
11 import oic.simulator.serviceprovider.listener.IAutomationUIListener;
12 import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
13 import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
14 import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
15 import oic.simulator.serviceprovider.resource.MetaProperty;
16 import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
17 import oic.simulator.serviceprovider.resource.ResourceAttribute;
18 import oic.simulator.serviceprovider.resource.SimulatorResource;
19 import oic.simulator.serviceprovider.resource.StandardConfiguration;
20 import oic.simulator.serviceprovider.utils.Constants;
21
22 import org.oic.simulator.AutomationType;
23 import org.oic.simulator.IAutomation;
24 import org.oic.simulator.SimulatorManager;
25 import org.oic.simulator.SimulatorResourceAttribute;
26 import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
27 import org.oic.simulator.serviceprovider.SimulatorResourceModel;
28 import org.oic.simulator.serviceprovider.SimulatorResourceServer;
29
30 public class ResourceManager {
31
32     private Map<String, Map<String, SimulatorResource>> resourceMap;
33
34     private StandardConfiguration                       stdConfig;
35
36     private SimulatorResource                           currentResourceInSelection;
37
38     private List<IResourceListChangedUIListener>        resourceListChangedUIListeners;
39
40     private List<IResourceSelectionChangedUIListener>   resourceSelectionChangedUIListeners;
41
42     private List<IResourceModelChangedUIListener>       resourceModelChangedUIListeners;
43
44     private List<IAutomationUIListener>                 automationUIListeners;
45
46     private IResourceModelChangedListener               resourceModelChangeListener;
47
48     private IAutomation                                 automationListener;
49
50     private NotificationSynchronizerThread              synchronizerThread;
51
52     private Thread                                      threadHandle;
53
54     static {
55         System.loadLibrary("SimulatorManager");
56     }
57
58     public ResourceManager() {
59         resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
60         stdConfig = new StandardConfiguration();
61
62         resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
63         resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
64         resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
65         automationUIListeners = new ArrayList<IAutomationUIListener>();
66
67         // Populate standard configuration file list
68         populateStandardConfigurationList();
69
70         resourceModelChangeListener = new IResourceModelChangedListener() {
71
72             @Override
73             public void onResourceModelChanged(final String resourceURI,
74                     final SimulatorResourceModel resourceModelN) {
75                 synchronizerThread.addToQueue(new Runnable() {
76
77                     @Override
78                     public void run() {
79                         if (null == resourceURI || null == resourceModelN) {
80                             return;
81                         }
82                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
83                         if (null == resource) {
84                             return;
85                         }
86                         // Fetch the resource attributes
87                         Map<String, ResourceAttribute> resourceAttributeMapNew;
88                         resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
89                         if (null == resourceAttributeMapNew) {
90                             return;
91                         }
92                         // Update the resource with new model data
93                         Map<String, ResourceAttribute> resourceAttributeMapOld;
94                         resourceAttributeMapOld = resource
95                                 .getResourceAttributesMap();
96                         if (null == resourceAttributeMapOld) {
97                             return;
98                         }
99                         ModelChangeNotificationType notificationType;
100                         notificationType = compareAndUpdateLocalAttributes(
101                                 resourceAttributeMapOld,
102                                 resourceAttributeMapNew);
103                         if (notificationType != ModelChangeNotificationType.NONE) {
104                             // Update the UI listeners
105                             resourceModelChangedUINotification(
106                                     notificationType, resourceURI);
107                         }
108                     }
109                 });
110             }
111         };
112
113         automationListener = new IAutomation() {
114
115             @Override
116             public void onAutomationComplete(final String resourceURI,
117                     final int automationId) {
118                 synchronizerThread.addToQueue(new Runnable() {
119
120                     @Override
121                     public void run() {
122                         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
123                         if (null == resource) {
124                             return;
125                         }
126                         // Checking whether this notification is for an
127                         // attribute or a resource
128                         if (resource.isResourceAutomationInProgress()) {
129                             changeResourceLevelAutomationStatus(resource, false);
130                             // Notify the UI listeners
131                             automationCompleteUINotification(resourceURI, null);
132                         } else if (resource.isAttributeAutomationInProgress()) {
133                             // Find the attribute with the given automation id
134                             ResourceAttribute attribute;
135                             attribute = getAttributeWithGivenAutomationId(
136                                     resource, automationId);
137                             if (null != attribute) {
138                                 attribute.setAutomationInProgress(false);
139                                 resource.setAttributeAutomationInProgress(false);
140                                 // Notify the UI listeners
141                                 automationCompleteUINotification(resourceURI,
142                                         attribute.getAttributeName());
143                             }
144                         } else {
145                             // Ignoring the notification as there are no
146                             // known automation for the current resource.
147                         }
148                     }
149                 });
150             }
151         };
152
153         synchronizerThread = new NotificationSynchronizerThread();
154         threadHandle = new Thread(synchronizerThread);
155         threadHandle.setName("Simulator service provider event queue");
156         threadHandle.start();
157     }
158
159     private static class NotificationSynchronizerThread implements Runnable {
160
161         LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
162
163         @Override
164         public void run() {
165             while (!Thread.interrupted()) {
166                 synchronized (this) {
167                     try {
168                         while (notificationQueue.isEmpty()) {
169                             this.wait();
170                             break;
171                         }
172                     } catch (InterruptedException e) {
173                         return;
174                     }
175                 }
176
177                 Runnable thread;
178                 synchronized (this) {
179                     thread = notificationQueue.pop();
180                 }
181                 try {
182                     thread.run();
183                 } catch (Exception e) {
184                     if (e instanceof InterruptedException) {
185                         return;
186                     }
187                     e.printStackTrace();
188                 }
189             }
190         }
191
192         public void addToQueue(Runnable event) {
193             synchronized (this) {
194                 notificationQueue.add(event);
195                 this.notify();
196             }
197         }
198     }
199
200     private void populateStandardConfigurationList() {
201         // TODO: Add all the standard configuration files
202         // Ex: stdConfig.addResourceConfiguration(LIGHT, LIGHT_FILE);
203     }
204
205     // This method gives a list of RAML resource configurations available.
206     public List<String> getResourceConfigurationList() {
207         List<String> resourceConfigurationList = new ArrayList<String>();
208         synchronized (stdConfig) {
209             Map<String, String> configMap = stdConfig
210                     .getStandardResourceConfigurationList();
211             if (null != configMap) {
212                 Set<String> keySet = configMap.keySet();
213                 Iterator<String> keyItr = keySet.iterator();
214                 while (keyItr.hasNext()) {
215                     resourceConfigurationList.add(keyItr.next());
216                 }
217             }
218         }
219         return resourceConfigurationList;
220     }
221
222     public String getConfigFilePath(String resourceType) {
223         String path = null;
224         if (null != resourceType) {
225             synchronized (stdConfig) {
226                 path = stdConfig.getResourceConfigFilePath(resourceType);
227             }
228         }
229         return path;
230     }
231
232     public void addResourceListChangedUIListener(
233             IResourceListChangedUIListener resourceListChangedUIListener) {
234         synchronized (resourceListChangedUIListeners) {
235             resourceListChangedUIListeners.add(resourceListChangedUIListener);
236         }
237     }
238
239     public void addResourceSelectionChangedUIListener(
240             IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
241         synchronized (resourceSelectionChangedUIListeners) {
242             resourceSelectionChangedUIListeners
243                     .add(resourceSelectionChangedUIListener);
244         }
245     }
246
247     public void addResourceModelChangedUIListener(
248             IResourceModelChangedUIListener resourceModelChangedUIListener) {
249         synchronized (resourceModelChangedUIListeners) {
250             resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
251         }
252     }
253
254     public void addAutomationUIListener(
255             IAutomationUIListener automationUIListener) {
256         synchronized (automationUIListeners) {
257             automationUIListeners.add(automationUIListener);
258         }
259     }
260
261     public void removeResourceListChangedUIListener(
262             IResourceListChangedUIListener listener) {
263         synchronized (resourceListChangedUIListeners) {
264             if (null != listener && resourceListChangedUIListeners.size() > 0) {
265                 resourceListChangedUIListeners.remove(listener);
266             }
267         }
268     }
269
270     public void removeResourceSelectionChangedUIListener(
271             IResourceSelectionChangedUIListener listener) {
272         synchronized (resourceSelectionChangedUIListeners) {
273             if (null != listener
274                     && resourceSelectionChangedUIListeners.size() > 0) {
275                 resourceSelectionChangedUIListeners.remove(listener);
276             }
277         }
278     }
279
280     public void removeResourceModelChangedUIListener(
281             IResourceModelChangedUIListener listener) {
282         synchronized (resourceModelChangedUIListeners) {
283             if (null != listener && resourceModelChangedUIListeners.size() > 0) {
284                 resourceModelChangedUIListeners.remove(listener);
285             }
286         }
287     }
288
289     public void removeAutomationUIListener(IAutomationUIListener listener) {
290         synchronized (automationUIListeners) {
291             if (null != listener && automationUIListeners.size() > 0) {
292                 automationUIListeners.remove(listener);
293             }
294         }
295     }
296
297     public synchronized SimulatorResource getCurrentResourceInSelection() {
298         return currentResourceInSelection;
299     }
300
301     public synchronized void setCurrentResourceInSelection(
302             SimulatorResource resource) {
303         this.currentResourceInSelection = resource;
304     }
305
306     private void addResourceToMap(SimulatorResource simulatorResource) {
307         if (null != simulatorResource) {
308             synchronized (resourceMap) {
309                 Map<String, SimulatorResource> resourceTypeMap;
310                 resourceTypeMap = resourceMap.get(simulatorResource
311                         .getResourceType());
312                 if (null == resourceTypeMap) {
313                     resourceTypeMap = new HashMap<String, SimulatorResource>();
314                     resourceMap.put(simulatorResource.getResourceType(),
315                             resourceTypeMap);
316                 }
317                 resourceTypeMap.put(simulatorResource.getResourceURI(),
318                         simulatorResource);
319             }
320         }
321     }
322
323     private void addResourceToMap(String resourceType,
324             Map<String, SimulatorResource> newResourceTypeMap) {
325         if (null != resourceType && null != newResourceTypeMap) {
326             synchronized (resourceMap) {
327                 Map<String, SimulatorResource> resourceTypeMap = resourceMap
328                         .get(resourceType);
329                 if (null != resourceTypeMap) {
330                     resourceTypeMap.putAll(newResourceTypeMap);
331                 } else {
332                     resourceMap.put(resourceType, newResourceTypeMap);
333                 }
334             }
335         }
336     }
337
338     private void removeResourceFromMap(String resourceType, String resourceURI) {
339         if (null != resourceURI && null != resourceType) {
340             synchronized (resourceMap) {
341                 Map<String, SimulatorResource> resourceTypeMap = resourceMap
342                         .get(resourceType);
343                 if (null != resourceTypeMap) {
344                     resourceTypeMap.remove(resourceURI);
345                     if (resourceTypeMap.size() < 1) {
346                         resourceMap.remove(resourceType);
347                     }
348                 }
349             }
350         }
351     }
352
353     public boolean isResourceExist(String resourceURI) {
354         boolean result = false;
355         if (null != resourceURI) {
356             SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
357             if (null != resource) {
358                 result = true;
359             }
360         }
361         return result;
362     }
363
364     public void createResource(final String configFilePath) {
365         new Thread() {
366             @Override
367             public void run() {
368                 SimulatorResourceServer resourceServerN;
369                 resourceServerN = SimulatorManager.createResource(
370                         configFilePath, resourceModelChangeListener);
371
372                 SimulatorResource simulatorResource;
373                 simulatorResource = fetchResourceData(resourceServerN);
374                 if (null != simulatorResource) {
375                     addResourceToMap(simulatorResource);
376
377                     resourceCreatedUINotification();
378                 }
379             }
380         }.start();
381     }
382
383     public void createResource(final String configFilePath,
384             final int noOfInstances) {
385         new Thread() {
386             @Override
387             public void run() {
388                 Map<String, SimulatorResource> resourceTypeMap;
389                 SimulatorResourceServer[] simulatorResourceServers = null;
390                 simulatorResourceServers = SimulatorManager.createResource(
391                         configFilePath, noOfInstances,
392                         resourceModelChangeListener);
393                 if (null == simulatorResourceServers) {
394                     return;
395                 }
396                 resourceTypeMap = new HashMap<String, SimulatorResource>();
397                 SimulatorResource resource;
398                 String uri;
399                 for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
400                     resource = fetchResourceData(resourceServerN);
401                     if (null != resource) {
402                         uri = resource.getResourceURI();
403                         resourceTypeMap.put(uri, resource);
404                     }
405                 }
406
407                 // Find the resourceType and add it to the local data
408                 // structure and notify UI Listeners
409                 if (resourceTypeMap.size() > 0) {
410                     String resourceType;
411                     Set<String> uriSet = resourceTypeMap.keySet();
412                     Iterator<String> itr = uriSet.iterator();
413                     if (itr.hasNext()) {
414                         SimulatorResource simResource = resourceTypeMap.get(itr
415                                 .next());
416                         if (null != simResource) {
417                             resourceType = simResource.getResourceType();
418
419                             addResourceToMap(resourceType, resourceTypeMap);
420                             resourceCreatedUINotification();
421                         }
422                     }
423                 }
424             }
425         }.start();
426     }
427
428     private SimulatorResource fetchResourceData(
429             SimulatorResourceServer resourceServerN) {
430         SimulatorResource simulatorResource = null;
431         if (null != resourceServerN) {
432             simulatorResource = new SimulatorResource();
433             simulatorResource.setResourceServer(resourceServerN);
434             simulatorResource.setResourceURI(resourceServerN.getURI());
435             simulatorResource
436                     .setResourceType(resourceServerN.getResourceType());
437             simulatorResource.setResourceName(resourceServerN.getName());
438             simulatorResource.setResourceInterface(resourceServerN
439                     .getInterfaceType());
440
441             SimulatorResourceModel resourceModelN = resourceServerN.getModel();
442             if (null != resourceModelN) {
443                 simulatorResource.setResourceModel(resourceModelN);
444
445                 // Fetch the resource attributes
446                 Map<String, ResourceAttribute> resourceAttributeMap;
447                 resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
448                 if (null != resourceAttributeMap) {
449                     simulatorResource
450                             .setResourceAttributesMap(resourceAttributeMap);
451                 }
452             }
453         }
454         return simulatorResource;
455     }
456
457     private Map<String, ResourceAttribute> fetchResourceAttributesFromModel(
458             SimulatorResourceModel resourceModelN) {
459         Map<String, ResourceAttribute> resourceAttributeMap = null;
460         if (null != resourceModelN) {
461             Map<String, SimulatorResourceAttribute> attributeMapN;
462             attributeMapN = resourceModelN.getAttributes();
463             if (null != attributeMapN) {
464                 resourceAttributeMap = new HashMap<String, ResourceAttribute>();
465
466                 Set<String> attNameSet = attributeMapN.keySet();
467                 String attName;
468                 Object attValueObj;
469                 SimulatorResourceAttribute attributeN;
470                 ResourceAttribute attribute;
471                 Iterator<String> attNameItr = attNameSet.iterator();
472                 while (attNameItr.hasNext()) {
473                     attName = attNameItr.next();
474                     attributeN = attributeMapN.get(attName);
475                     if (null != attributeN) {
476                         attribute = new ResourceAttribute();
477                         attribute.setResourceAttribute(attributeN);
478                         attribute.setAttributeName(attName);
479
480                         attValueObj = attributeN.getValue();
481                         if (null != attValueObj) {
482                             attribute.setAttributeValue(attValueObj);
483                         }
484
485                         // Read allowed values or min-max values of the
486                         // attribute
487                         // TODO: Temporarily reading the allowed values
488                         // as string
489                         // If attribute type is known, then appropriate
490                         // get method for that type will be called.
491                         String[] allowedValues = resourceModelN
492                                 .getAllowedValues(attName);
493                         attribute.setAllowedValues(allowedValues);
494                         if (null == allowedValues || allowedValues.length < 1) {
495                             // TODO: Get the range(min-max) of the attribute
496                             // Implementation of GetRange is in progress
497                         }
498
499                         // Initially disabling the automation
500                         attribute.setAutomationInProgress(false);
501
502                         // TODO: Temporarily setting the interval to 500.
503                         // This value should come from the native layer.
504                         // Native implementation is in progress.
505                         attribute
506                                 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
507
508                         // Setting the default automation type
509                         attribute
510                                 .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
511
512                         resourceAttributeMap.put(attName, attribute);
513                     }
514                 }
515             }
516         }
517         return resourceAttributeMap;
518     }
519
520     public void deleteResourceByURI(final String resourceURI) {
521         if (null != resourceURI) {
522             new Thread() {
523                 @Override
524                 public void run() {
525                     SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
526                     if (null != resource) {
527                         String resourceType = resource.getResourceType();
528
529                         // Unregister the resource from the platform
530                         deleteResource(resource);
531
532                         // Delete from the local data structure
533                         deleteLocalResourceDetails(resourceType, resourceURI);
534
535                         // Notify the UI listener for removing this resource
536                         // from UI
537                         resourceDeletedUINotification();
538
539                         if (null != currentResourceInSelection
540                                 && resource == currentResourceInSelection) {
541                             // Listeners might query the resource being deleted
542                             // if exists. So set the currently selection to
543                             // null.
544                             setCurrentResourceInSelection(null);
545
546                             // Notify all observers for resource selection
547                             // change event
548                             resourceSelectionChangedUINotification();
549                         }
550                     }
551                 }
552             }.start();
553         }
554     }
555
556     private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
557         SimulatorResource resource = null;
558         if (null != resourceURI) {
559             synchronized (resourceMap) {
560                 Set<String> typeSet = resourceMap.keySet();
561                 Iterator<String> typeItr = typeSet.iterator();
562                 String resourceType;
563                 Map<String, SimulatorResource> resourceTypeMap;
564                 while (typeItr.hasNext()) {
565                     resourceType = typeItr.next();
566                     resourceTypeMap = resourceMap.get(resourceType);
567                     if (null != resourceTypeMap) {
568                         resource = resourceTypeMap.get(resourceURI);
569                         if (null != resource) {
570                             break;
571                         }
572                     }
573                 }
574             }
575         }
576         return resource;
577     }
578
579     private void deleteResource(SimulatorResource resource) {
580         if (null != resource) {
581             SimulatorResourceServer resourceServerN = resource
582                     .getResourceServer();
583             if (null != resourceServerN) {
584                 SimulatorManager.deleteResource(resourceServerN);
585             }
586         }
587     }
588
589     public void deleteResourceByType(final String resourceType) {
590         if (null != resourceType) {
591             new Thread() {
592                 @Override
593                 public void run() {
594                     // Unregister the resources from the platform
595                     deleteResource(resourceType);
596
597                     // Delete from the local data structure
598                     deleteLocalResourceDetails(resourceType, null);
599
600                     // Notify the UI listener for removing this resource from UI
601                     resourceDeletedUINotification();
602
603                     if (null != currentResourceInSelection
604                             && resourceType.equals(currentResourceInSelection
605                                     .getResourceType())) {
606                         // Listeners might query the resource being deleted if
607                         // exists. So set the currently selection to null.
608                         setCurrentResourceInSelection(null);
609
610                         // Notify all observers for resource selection change
611                         // event
612                         resourceSelectionChangedUINotification();
613                     }
614                 }
615             }.start();
616         }
617     }
618
619     private void deleteResource(String resourceType) {
620         if (null != resourceType) {
621             SimulatorManager.deleteResources(resourceType);
622         }
623     }
624
625     public void deleteAllResources() {
626         new Thread() {
627             @Override
628             public void run() {
629                 // Unregister the resources from the platform
630                 deleteResource();
631
632                 // Delete from the local data structure
633                 deleteLocalResourceDetails(null, null);
634
635                 // Notify the UI listener for removing this resource from UI
636                 resourceDeletedUINotification();
637
638                 // Listeners might query the resource being deleted if exists.
639                 // So set the currently selection to null.
640                 setCurrentResourceInSelection(null);
641
642                 // Notify all observers for resource selection change event
643                 resourceSelectionChangedUINotification();
644             }
645         }.start();
646     }
647
648     private void deleteResource() {
649         SimulatorManager.deleteResources(null);
650     }
651
652     private void deleteLocalResourceDetails(String resourceType,
653             String resourceURI) {
654         if (null != resourceType && null != resourceURI) {
655             removeResourceFromMap(resourceType, resourceURI);
656         } else {
657             synchronized (resourceMap) {
658                 if (null != resourceType) {
659                     resourceMap.remove(resourceType);
660                 } else {
661                     resourceMap.clear();
662                 }
663             }
664         }
665     }
666
667     private void resourceCreatedUINotification() {
668         synchronized (resourceListChangedUIListeners) {
669             if (resourceListChangedUIListeners.size() > 0) {
670                 IResourceListChangedUIListener listener;
671                 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
672                         .iterator();
673                 while (listenerItr.hasNext()) {
674                     listener = listenerItr.next();
675                     if (null != listener) {
676                         listener.onResourceCreation();
677                     }
678                 }
679             }
680         }
681     }
682
683     private void resourceDeletedUINotification() {
684         synchronized (resourceListChangedUIListeners) {
685             if (resourceListChangedUIListeners.size() > 0) {
686                 IResourceListChangedUIListener listener;
687                 Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
688                         .iterator();
689                 while (listenerItr.hasNext()) {
690                     listener = listenerItr.next();
691                     if (null != listener) {
692                         listener.onResourceDeletion();
693                     }
694                 }
695             }
696         }
697     }
698
699     private void resourceSelectionChangedUINotification() {
700         synchronized (resourceSelectionChangedUIListeners) {
701             if (resourceSelectionChangedUIListeners.size() > 0) {
702                 IResourceSelectionChangedUIListener listener;
703                 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
704                         .iterator();
705                 while (listenerItr.hasNext()) {
706                     listener = listenerItr.next();
707                     if (null != listener) {
708                         listener.onResourceSelectionChange();
709                     }
710                 }
711             }
712         }
713     }
714
715     private void resourceModelChangedUINotification(
716             ModelChangeNotificationType notificationType, String resourceURI) {
717         synchronized (resourceModelChangedUIListeners) {
718             if (resourceModelChangedUIListeners.size() > 0
719                     && notificationType != ModelChangeNotificationType.NONE
720                     && null != resourceURI) {
721                 IResourceModelChangedUIListener listener;
722                 Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
723                         .iterator();
724                 while (listenerItr.hasNext()) {
725                     listener = listenerItr.next();
726                     if (null != listener) {
727                         listener.onResourceModelChange(notificationType,
728                                 resourceURI);
729                     }
730                 }
731             }
732         }
733     }
734
735     private void resourceAutomationStartedUINotification(String resourceURI) {
736         synchronized (automationUIListeners) {
737             if (automationUIListeners.size() > 0 && null != resourceURI) {
738                 IAutomationUIListener listener;
739                 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
740                         .iterator();
741                 while (listenerItr.hasNext()) {
742                     listener = listenerItr.next();
743                     if (null != listener) {
744                         listener.onResourceAutomationStart(resourceURI);
745                     }
746                 }
747             }
748         }
749     }
750
751     private void automationCompleteUINotification(String resourceURI,
752             String attName) {
753         synchronized (automationUIListeners) {
754             if (automationUIListeners.size() > 0 && null != resourceURI) {
755                 IAutomationUIListener listener;
756                 Iterator<IAutomationUIListener> listenerItr = automationUIListeners
757                         .iterator();
758                 while (listenerItr.hasNext()) {
759                     listener = listenerItr.next();
760                     if (null != listener) {
761                         listener.onAutomationComplete(resourceURI, attName);
762                     }
763                 }
764             }
765         }
766     }
767
768     public List<String> getResourceTypeList() {
769         List<String> typeList = null;
770         synchronized (resourceMap) {
771             if (resourceMap.size() > 0) {
772                 typeList = new ArrayList<String>();
773                 Set<String> typeSet = resourceMap.keySet();
774                 Iterator<String> typeItr = typeSet.iterator();
775                 while (typeItr.hasNext()) {
776                     typeList.add(typeItr.next());
777                 }
778             }
779         }
780         return typeList;
781     }
782
783     public boolean isTypeExist(String resType) {
784         synchronized (resourceMap) {
785             if (resourceMap.containsKey(resType)) {
786                 return true;
787             }
788         }
789         return false;
790     }
791
792     public List<String> getURIListOfResourceType(String type) {
793         List<String> uriList = null;
794         synchronized (resourceMap) {
795             if (null != type) {
796                 Map<String, SimulatorResource> typeMap = resourceMap.get(type);
797                 if (null != typeMap) {
798                     Set<String> keySet = typeMap.keySet();
799                     uriList = new ArrayList<String>();
800                     Iterator<String> keyItr = keySet.iterator();
801                     while (keyItr.hasNext()) {
802                         uriList.add(keyItr.next());
803                     }
804                 }
805             }
806         }
807         return uriList;
808     }
809
810     public void resourceSelectionChanged(final String selectedItem) {
811         new Thread() {
812             @Override
813             public void run() {
814                 // Check whether the item selected is a resource or resource
815                 // category
816                 if (isTypeExist(selectedItem)) {
817                     // Given item is a resource Type
818                     setCurrentResourceInSelection(null);
819                 } else {
820                     // Given item is a resource URI
821                     SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
822                     if (null != resource) {
823                         setCurrentResourceInSelection(resource);
824                     } else {
825                         setCurrentResourceInSelection(null);
826                     }
827                 }
828                 // Notify all observers for resource selection change event
829                 resourceSelectionChangedUINotification();
830             }
831         }.start();
832     }
833
834     public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
835         if (null != resource) {
836             String propName;
837             String propValue;
838
839             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
840
841             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
842                 propName = Constants.META_PROPERTIES[index];
843                 if (propName.equals(Constants.RESOURCE_URI)) {
844                     propValue = resource.getResourceURI();
845                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
846                     propValue = resource.getResourceType();
847                 } else if (propName.equals(Constants.RESOURCE_UID)) {
848                     // propValue = resource.getResourceUID();
849                     propValue = "Dummy123"; // Temporarily adding dummy value to
850                     // see in UI
851                 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
852                     // propValue = resource.getConnectivityType();
853                     propValue = "IP"; // Temporarily adding dummy value to see
854                     // in UI
855                 } else {
856                     propValue = null;
857                 }
858                 if (null != propValue) {
859                     metaPropertyList.add(new MetaProperty(propName, propValue));
860                 }
861             }
862
863             return metaPropertyList;
864         }
865         return null;
866     }
867
868     public List<ResourceAttribute> getAttributes(SimulatorResource resource) {
869         List<ResourceAttribute> attList = null;
870         if (null != resource) {
871             Map<String, ResourceAttribute> attMap = resource
872                     .getResourceAttributesMap();
873             if (null != attMap && attMap.size() > 0) {
874                 attList = new ArrayList<ResourceAttribute>();
875                 Set<String> attNameSet = attMap.keySet();
876                 String attName;
877                 ResourceAttribute attribute;
878                 // ResourceAttribute attributeClone;
879                 Iterator<String> attNameItr = attNameSet.iterator();
880                 while (attNameItr.hasNext()) {
881                     attName = attNameItr.next();
882                     attribute = attMap.get(attName);
883                     if (null != attribute) {
884                         // attributeClone =
885                         // ResourceAttribute.clone(attribute);
886                         attList.add(attribute);
887                     }
888                 }
889             }
890         }
891         return attList;
892     }
893
894     public void attributeValueUpdated(SimulatorResource resource,
895             String attributeName, String value) {
896         if (null != resource && null != attributeName && null != value) {
897             SimulatorResourceServer server = resource.getResourceServer();
898             if (null != server) {
899                 server.updateAttributeStringN(attributeName, value);
900             }
901         }
902     }
903
904     private ModelChangeNotificationType compareAndUpdateLocalAttributes(
905             Map<String, ResourceAttribute> resourceAttributeMapOld,
906             Map<String, ResourceAttribute> resourceAttributeMapNew) {
907         ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
908         if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
909             Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
910             Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
911             String attName;
912             ResourceAttribute attributeOld;
913             ResourceAttribute attributeNew;
914             Object attValueOld;
915             Object attValueNew;
916             String oldValueStr;
917             String newValueStr;
918             while (attributeMapOldItr.hasNext()) {
919                 attName = attributeMapOldItr.next();
920                 if (resourceAttributeMapNew.containsKey(attName)) {
921                     attributeOld = resourceAttributeMapOld.get(attName);
922                     attributeNew = resourceAttributeMapNew.get(attName);
923                     // Copy the attribute value from new to old if the value
924                     // has been changed
925                     // Comparing only the attribute's value considering the
926                     // fact that only the value can be changed
927                     if (null != attributeOld && null != attributeNew) {
928                         attValueOld = attributeOld.getAttributeValue();
929                         attValueNew = attributeNew.getAttributeValue();
930
931                         oldValueStr = String.valueOf(attValueOld);
932                         newValueStr = String.valueOf(attValueNew);
933
934                         if (null != oldValueStr && null != newValueStr) {
935                             if (!oldValueStr.equals(newValueStr)) {
936                                 attributeOld.setAttributeValue(attValueNew);
937                                 notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
938                             }
939                         }
940                     }
941                     resourceAttributeMapNew.remove(attName);
942                 } else {
943                     // Attribute doesn't exist in the new model. Hence
944                     // removing it from the model.
945                     resourceAttributeMapOld.remove(attName);
946                     notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
947                 }
948             }
949             // Check for new attributes in the new model
950             if (resourceAttributeMapNew.size() > 0) {
951                 Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
952                 Iterator<String> remainingAttItr = remainingAttSet.iterator();
953                 ResourceAttribute attribute;
954                 while (remainingAttItr.hasNext()) {
955                     attName = remainingAttItr.next();
956                     if (null != attName) {
957                         attribute = resourceAttributeMapNew.get(attName);
958                         if (null != attribute) {
959                             resourceAttributeMapOld.put(attName, attribute);
960                         }
961                     }
962                 }
963                 notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
964             }
965         }
966         return notificationType;
967     }
968
969     public int startAutomation(SimulatorResource resource,
970             ResourceAttribute attribute, AutomationType autoType,
971             int autoUpdateInterval) {
972         int autoId = -1;
973         if (null != resource && null != attribute) {
974             SimulatorResourceServer resourceServerN = resource
975                     .getResourceServer();
976             if (null != resourceServerN) {
977                 String attrName = attribute.getAttributeName();
978                 autoId = resourceServerN.startAttributeAutomation(attrName,
979                         autoType.ordinal(), automationListener);
980                 if (-1 != autoId) {
981                     attribute.setAutomationId(autoId);
982                 } else {
983                     attribute.setAutomationInProgress(false);
984                     resource.setAttributeAutomationInProgress(false);
985                 }
986             }
987         }
988         return autoId;
989     }
990
991     public void stopAutomation(SimulatorResource resource, int autoId) {
992         if (null != resource) {
993             SimulatorResourceServer resourceServerN = resource
994                     .getResourceServer();
995             if (null != resourceServerN) {
996                 resourceServerN.stopAutomation(autoId);
997             }
998         }
999     }
1000
1001     private ResourceAttribute getAttributeWithGivenAutomationId(
1002             SimulatorResource resource, int automationId) {
1003         ResourceAttribute targetAttribute = null;
1004         if (null != resource) {
1005             Map<String, ResourceAttribute> attributeMap = resource
1006                     .getResourceAttributesMap();
1007             if (null != attributeMap) {
1008                 Set<String> attNameSet = attributeMap.keySet();
1009                 Iterator<String> attNameItr = attNameSet.iterator();
1010                 String attName;
1011                 ResourceAttribute attribute;
1012                 while (attNameItr.hasNext()) {
1013                     attName = attNameItr.next();
1014                     if (null != attName) {
1015                         attribute = attributeMap.get(attName);
1016                         if (null != attribute) {
1017                             if (attribute.isAutomationInProgress()
1018                                     && (attribute.getAutomationId() == automationId)) {
1019                                 targetAttribute = attribute;
1020                                 break;
1021                             }
1022                         }
1023                     }
1024                 }
1025             }
1026         }
1027         return targetAttribute;
1028     }
1029
1030     public boolean startResourceAutomationUIRequest(final String resourceURI) {
1031         if (null == resourceURI) {
1032             return false;
1033         }
1034         boolean status = false;
1035         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1036         if (null != resource) {
1037             changeResourceLevelAutomationStatus(resource, true);
1038
1039             // Invoke the native automation method
1040             SimulatorResourceServer resourceServer = resource
1041                     .getResourceServer();
1042             if (null != resourceServer) {
1043                 // TODO: Temporarily handling the normal one-time automation for
1044                 // resources
1045                 int autoId = resourceServer.startResourceAutomation(
1046                         AutomationType.NORMAL.ordinal(), automationListener);
1047                 if (-1 == autoId) {
1048                     // Automation request failed and hence status is being
1049                     // rolled back
1050                     changeResourceLevelAutomationStatus(resource, false);
1051                 } else {
1052                     // Automation request accepted.
1053                     resource.setAutomationId(autoId);
1054
1055                     // Notify the UI listeners in a different thread.
1056                     Thread notifyThread = new Thread() {
1057                         public void run() {
1058                             resourceAutomationStartedUINotification(resourceURI);
1059                         };
1060                     };
1061                     notifyThread.setPriority(Thread.MAX_PRIORITY);
1062                     notifyThread.start();
1063
1064                     status = true;
1065                 }
1066             }
1067         }
1068         return status;
1069     }
1070
1071     public void stopResourceAutomationUIRequest(final String resourceURI) {
1072         Thread stopThread = new Thread() {
1073             public void run() {
1074                 SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1075                 if (null == resource) {
1076                     return;
1077                 }
1078                 int autoId = resource.getAutomationId();
1079                 if (-1 == autoId) {
1080                     return;
1081                 }
1082                 SimulatorResourceServer resourceServer = resource
1083                         .getResourceServer();
1084                 if (null == resourceServer) {
1085                     return;
1086                 }
1087                 // Call native method
1088                 resourceServer.stopAutomation(autoId);
1089
1090                 // Invoke the automation complete callback
1091                 automationListener.onAutomationComplete(resourceURI, autoId);
1092             }
1093         };
1094         stopThread.start();
1095     }
1096
1097     // Changes the automation state of the resource and its attributes
1098     private void changeResourceLevelAutomationStatus(
1099             SimulatorResource resource, boolean status) {
1100
1101         Map<String, ResourceAttribute> attributeMap = resource
1102                 .getResourceAttributesMap();
1103         if (null != attributeMap) {
1104             Set<String> attrNameSet = attributeMap.keySet();
1105             Iterator<String> attrNameItr = attrNameSet.iterator();
1106             String attrName;
1107             ResourceAttribute attribute;
1108             while (attrNameItr.hasNext()) {
1109                 attrName = attrNameItr.next();
1110                 attribute = attributeMap.get(attrName);
1111                 if (null != attribute) {
1112                     attribute.setAutomationInProgress(status);
1113                 }
1114             }
1115         }
1116         resource.setResourceAutomationInProgress(status);
1117     }
1118
1119     public boolean isResourceAutomationStarted(String resourceURI) {
1120         boolean status = false;
1121         if (null == resourceURI) {
1122             return status;
1123         }
1124
1125         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1126         if (null != resource) {
1127             status = resource.isResourceAutomationInProgress();
1128         }
1129         return status;
1130     }
1131
1132     public boolean isAttributeAutomationStarted(String resourceURI) {
1133         boolean status = false;
1134         if (null == resourceURI) {
1135             return status;
1136         }
1137         SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
1138         if (null != resource) {
1139             status = resource.isAttributeAutomationInProgress();
1140         }
1141         return status;
1142     }
1143
1144     public void shutdown() {
1145         threadHandle.interrupt();
1146     }
1147 }