63b22c6b925e3642e9acf78f8a926c8ecc6b469d
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / 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.clientcontroller.manager;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Date;
23 import java.util.HashMap;
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 import java.util.Vector;
30
31 import oic.simulator.clientcontroller.Activator;
32 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
33 import oic.simulator.clientcontroller.listener.IDevicePlatformInfoUIListener;
34 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
35 import oic.simulator.clientcontroller.listener.IGetUIListener;
36 import oic.simulator.clientcontroller.listener.IObserveUIListener;
37 import oic.simulator.clientcontroller.listener.IPostUIListener;
38 import oic.simulator.clientcontroller.listener.IPutUIListener;
39 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
40 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
41 import oic.simulator.clientcontroller.remoteresource.DeviceAndPlatformInfo;
42 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
43 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
44 import oic.simulator.clientcontroller.utils.Constants;
45 import oic.simulator.clientcontroller.utils.Utility;
46
47 import org.oic.simulator.AttributeProperty;
48 import org.oic.simulator.AttributeProperty.Type;
49 import org.oic.simulator.AttributeValue;
50 import org.oic.simulator.AttributeValue.TypeInfo;
51 import org.oic.simulator.AttributeValue.ValueType;
52 import org.oic.simulator.DeviceInfo;
53 import org.oic.simulator.DeviceListener;
54 import org.oic.simulator.ILogger.Level;
55 import org.oic.simulator.PlatformInfo;
56 import org.oic.simulator.PlatformListener;
57 import org.oic.simulator.SimulatorException;
58 import org.oic.simulator.SimulatorManager;
59 import org.oic.simulator.SimulatorResourceAttribute;
60 import org.oic.simulator.SimulatorResourceModel;
61 import org.oic.simulator.SimulatorResult;
62 import org.oic.simulator.client.FindResourceListener;
63 import org.oic.simulator.client.SimulatorRemoteResource;
64 import org.oic.simulator.client.SimulatorRemoteResource.GetResponseListener;
65 import org.oic.simulator.client.SimulatorRemoteResource.ObserveNotificationListener;
66 import org.oic.simulator.client.SimulatorRemoteResource.PostResponseListener;
67 import org.oic.simulator.client.SimulatorRemoteResource.PutResponseListener;
68 import org.oic.simulator.client.SimulatorRemoteResource.VerificationListener;
69 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
70
71 /**
72  * This class acts as an interface between the simulator java SDK and the
73  * various UI modules. It maintains all the details of resources and provides
74  * other UI modules with the information required. It also handles responses for
75  * find, GET, PUT, POST, Observe and automatic verification operations from
76  * native layer and propagates those events to the registered UI listeners.
77  */
78 public class ResourceManager {
79
80     private Set<String>                               lastKnownSearchTypes;
81
82     private RemoteResource                            currentResourceInSelection;
83
84     private FindResourceListener                      findResourceListener;
85     private GetResponseListener                       getListener;
86     private PutResponseListener                       putListener;
87     private PostResponseListener                      postListener;
88     private ObserveNotificationListener               observeListener;
89     private VerificationListener                      verifyListener;
90     private DeviceListener                            deviceListener;
91     private PlatformListener                          platformListener;
92
93     private ResponseSynchronizerThread                synchronizerThread;
94
95     private Thread                                    threadHandle;
96
97     private List<IFindResourceUIListener>             findResourceUIListeners;
98     private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
99     private List<IGetUIListener>                      getUIListeners;
100     private List<IPutUIListener>                      putUIListeners;
101     private List<IPostUIListener>                     postUIListeners;
102     private List<IObserveUIListener>                  observeUIListeners;
103     private List<IVerificationUIListener>             verificationUIListeners;
104     private List<IConfigurationUpload>                configUploadUIListeners;
105     private List<IDevicePlatformInfoUIListener>       devicePlatformInfoUIListeners;
106
107     // Map with Server ID as key and the complete object as the value
108     private Map<String, RemoteResource>               resourceMap;
109     private List<RemoteResource>                      favoriteResources;
110     // Maintaining a list of resource URIs for favorite resources feature.
111     private List<String>                              favoriteURIList;
112
113     // Maintaining a list of observed resource URIs.
114     private List<String>                              observedResourceURIList;
115
116     private Map<String, DeviceAndPlatformInfo>        hostDeviceAndPlatformMap;
117
118     public ResourceManager() {
119         resourceMap = new HashMap<String, RemoteResource>();
120         favoriteResources = new ArrayList<RemoteResource>();
121         favoriteURIList = new ArrayList<String>();
122         observedResourceURIList = new ArrayList<String>();
123         hostDeviceAndPlatformMap = new HashMap<String, DeviceAndPlatformInfo>();
124
125         findResourceUIListeners = new ArrayList<IFindResourceUIListener>();
126         resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
127         getUIListeners = new ArrayList<IGetUIListener>();
128         putUIListeners = new ArrayList<IPutUIListener>();
129         postUIListeners = new ArrayList<IPostUIListener>();
130         observeUIListeners = new ArrayList<IObserveUIListener>();
131         verificationUIListeners = new ArrayList<IVerificationUIListener>();
132         configUploadUIListeners = new ArrayList<IConfigurationUpload>();
133         devicePlatformInfoUIListeners = new ArrayList<IDevicePlatformInfoUIListener>();
134
135         findResourceListener = new FindResourceListener() {
136
137             @Override
138             public void onResourceFound(final SimulatorRemoteResource resourceN) {
139                 synchronizerThread.addToQueue(new Runnable() {
140                     @Override
141                     public void run() {
142                         if (null == resourceN) {
143                             return;
144                         }
145
146                         // If id is not available, then it cannot be added to
147                         // the local map as null value should not be allowed as
148                         // key.
149                         String uid = resourceN.getId();
150                         if (null == uid) {
151                             return;
152                         }
153
154                         // If resource already exist, then ignore it.
155                         boolean exist = isUidExist(uid);
156                         if (exist) {
157                             return;
158                         }
159
160                         // Fetch the resource data
161                         // RemoteResource resource =
162                         // fetchResourceDetails(resourceN);
163                         RemoteResource resource = new RemoteResource();
164                         resource.setRemoteResourceRef(resourceN);
165
166                         String uri = resourceN.getURI();
167                         if (null != uri && uri.trim().length() > 0) {
168                             // Add resource to favorite list if it was in
169                             // favorites list during find/refresh operation.
170                             if (favoriteURIList.contains(uri)) {
171                                 addResourcetoFavorites(resource);
172                             }
173                             // Add resource to observed resources list if it was
174                             // in observe list during find/refresh operation.
175                             if (observedResourceURIList.contains(uri)) {
176                                 sendObserveRequest(resource);
177                             }
178                         } else {
179                             Activator
180                                     .getDefault()
181                                     .getLogManager()
182                                     .log(Level.INFO.ordinal(), new Date(),
183                                             "Found a resource without URI. Ignoring it.");
184                             return;
185                         }
186
187                         // Add the resource in local data structure
188                         addResourceDetails(resource);
189
190                         // Notify the UI listener
191                         newResourceFoundNotification(resource);
192
193                         Activator
194                                 .getDefault()
195                                 .getLogManager()
196                                 .log(Level.INFO.ordinal(),
197                                         new Date(),
198                                         "Resource Found [" + resourceN.getURI()
199                                                 + "].");
200
201                         // Send an initial GET request to get the resource
202                         // attributes
203                         try {
204                             resourceN.get(null, getListener);
205                         } catch (SimulatorException e) {
206                             Activator
207                                     .getDefault()
208                                     .getLogManager()
209                                     .log(Level.ERROR.ordinal(),
210                                             new Date(),
211                                             Utility.getSimulatorErrorString(e,
212                                                     null));
213                         }
214
215                         // Get the device information
216                         if (!isDeviceInfoExist(resourceN.getHost())) {
217                             try {
218                                 SimulatorManager.findDevices(resource
219                                         .getRemoteResourceRef().getHost(),
220                                         deviceListener);
221                             } catch (SimulatorException e) {
222                                 Activator
223                                         .getDefault()
224                                         .getLogManager()
225                                         .log(Level.ERROR.ordinal(),
226                                                 new Date(),
227                                                 Utility.getSimulatorErrorString(
228                                                         e, null));
229                             }
230                         }
231
232                         // Get the platform information
233                         if (!isPlatformInfoExist(resourceN.getHost())) {
234                             try {
235                                 SimulatorManager.getPlatformInformation(
236                                         resource.getRemoteResourceRef()
237                                                 .getHost(), platformListener);
238                             } catch (SimulatorException e) {
239                                 Activator
240                                         .getDefault()
241                                         .getLogManager()
242                                         .log(Level.ERROR.ordinal(),
243                                                 new Date(),
244                                                 Utility.getSimulatorErrorString(
245                                                         e, null));
246                             }
247                         }
248                     }
249                 });
250             }
251         };
252
253         // TODO: Listeners for device and platform information.
254         deviceListener = new DeviceListener() {
255
256             @Override
257             public void onDeviceFound(final String host,
258                     final DeviceInfo deviceInfo) {
259                 if (null == deviceInfo || null == host) {
260                     return;
261                 }
262                 synchronizerThread.addToQueue(new Runnable() {
263                     @Override
264                     public void run() {
265                         synchronized (hostDeviceAndPlatformMap) {
266                             DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
267                                     .get(host);
268                             if (null == info) {
269                                 info = new DeviceAndPlatformInfo();
270                                 info.setHost(host);
271                                 hostDeviceAndPlatformMap.put(host, info);
272                             }
273                             info.setDeviceInfo(deviceInfo);
274                         }
275
276                         // Notify UI listeners
277                         deviceInfoReceivedNotification();
278                     }
279                 });
280             }
281         };
282
283         platformListener = new PlatformListener() {
284
285             @Override
286             public void onPlatformFound(final String host,
287                     final PlatformInfo platformInfo) {
288                 if (null == platformInfo || null == host) {
289                     return;
290                 }
291                 synchronizerThread.addToQueue(new Runnable() {
292                     @Override
293                     public void run() {
294                         synchronized (hostDeviceAndPlatformMap) {
295                             DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
296                                     .get(host);
297                             if (null == info) {
298                                 info = new DeviceAndPlatformInfo();
299                                 info.setHost(host);
300                                 hostDeviceAndPlatformMap.put(host, info);
301                             }
302                             info.setPlatformInfo(platformInfo);
303                         }
304
305                         // Notify UI listeners
306                         platformInfoReceivedNotification();
307                     }
308                 });
309             }
310         };
311
312         getListener = new GetResponseListener() {
313             @Override
314             public void onGetResponse(final String uid,
315                     final SimulatorResult result,
316                     final SimulatorResourceModel resourceModelN) {
317                 System.out.println(result);
318                 if (result != SimulatorResult.SIMULATOR_OK) {
319                     Activator
320                             .getDefault()
321                             .getLogManager()
322                             .log(Level.ERROR.ordinal(),
323                                     new Date(),
324                                     "["
325                                             + result.toString()
326                                             + "] Received error response for GET request.");
327                     return;
328                 }
329                 synchronizerThread.addToQueue(new Runnable() {
330                     @Override
331                     public void run() {
332                         // Handling the response which includes retrieving the
333                         // attributes and updating the local model.
334                         RemoteResource resource = handleResponse(uid,
335                                 resourceModelN);
336                         if (null != resource) {
337                             // Notify the UI listeners
338                             getCompleteNotification(resource);
339                         }
340                     }
341                 });
342             }
343         };
344
345         putListener = new PutResponseListener() {
346
347             @Override
348             public void onPutResponse(final String uid,
349                     final SimulatorResult result,
350                     final SimulatorResourceModel resourceModelN) {
351                 if (result != SimulatorResult.SIMULATOR_OK) {
352                     Activator
353                             .getDefault()
354                             .getLogManager()
355                             .log(Level.ERROR.ordinal(),
356                                     new Date(),
357                                     "["
358                                             + result.toString()
359                                             + "] Received error response for PUT request.");
360                     return;
361                 }
362                 synchronizerThread.addToQueue(new Thread() {
363                     @Override
364                     public void run() {
365                         // Handling the response which includes retrieving the
366                         // attributes and updating the local model.
367                         RemoteResource resource = handleResponse(uid,
368                                 resourceModelN);
369                         if (null != resource) {
370                             // Notify the UI listeners
371                             putCompleteNotification(resource);
372                         }
373                     }
374                 });
375             }
376         };
377
378         postListener = new PostResponseListener() {
379             @Override
380             public void onPostResponse(final String uid,
381                     final SimulatorResult result,
382                     final SimulatorResourceModel resourceModelN) {
383                 if (result != SimulatorResult.SIMULATOR_OK) {
384                     Activator
385                             .getDefault()
386                             .getLogManager()
387                             .log(Level.ERROR.ordinal(),
388                                     new Date(),
389                                     "["
390                                             + result.toString()
391                                             + "] Received error response for POST request.");
392                     return;
393                 }
394                 synchronizerThread.addToQueue(new Runnable() {
395                     @Override
396                     public void run() {
397                         // Handling the response which includes retrieving the
398                         // attributes and updating the local model.
399                         RemoteResource resource = handleResponse(uid,
400                                 resourceModelN);
401                         if (null != resource) {
402                             // Notify the UI listeners
403                             postCompleteNotification(resource);
404                         }
405                     }
406                 });
407             }
408         };
409
410         observeListener = new ObserveNotificationListener() {
411
412             @Override
413             public void onObserveNotification(final String uid,
414                     final SimulatorResourceModel resourceModelN, final int seq) {
415                 synchronizerThread.addToQueue(new Runnable() {
416                     @Override
417                     public void run() {
418                         // Handling the response which includes retrieving the
419                         // attributes and updating the local model.
420                         RemoteResource resource = handleResponse(uid,
421                                 resourceModelN);
422                         if (null != resource) {
423                             // Notify the UI listeners
424                             observeCompleteNotification(resource);
425                         }
426                     }
427                 });
428             }
429         };
430
431         verifyListener = new VerificationListener() {
432
433             @Override
434             public void onVerificationStarted(final String uid, final int autoId) {
435                 synchronizerThread.addToQueue(new Runnable() {
436                     @Override
437                     public void run() {
438                         RemoteResource resource = getResource(uid);
439                         if (null == resource) {
440                             return;
441                         }
442                         // Update the automation status.
443                         resource.updateAutomationStatus(autoId, true);
444
445                         int autoType = resource.getAutomationtype(autoId);
446
447                         // Notify the listeners.
448                         verificationStartedNotification(resource, autoType);
449                     }
450                 });
451             }
452
453             @Override
454             public void onVerificationCompleted(final String uid,
455                     final int autoId) {
456                 synchronizerThread.addToQueue(new Runnable() {
457                     @Override
458                     public void run() {
459                         RemoteResource resource = getResource(uid);
460                         if (null == resource) {
461                             return;
462                         }
463                         // Update the automation status.
464                         resource.updateAutomationStatus(autoId, false);
465
466                         int autoType = resource.getAutomationtype(autoId);
467
468                         // Notify the listeners.
469                         verificationCompletedNotification(resource, autoType);
470                     }
471                 });
472             }
473
474             @Override
475             public void onVerificationAborted(final String uid, final int autoId) {
476                 synchronizerThread.addToQueue(new Runnable() {
477                     @Override
478                     public void run() {
479                         RemoteResource resource = getResource(uid);
480                         if (null == resource) {
481                             return;
482                         }
483                         // Update the automation status.
484                         resource.updateAutomationStatus(autoId, false);
485
486                         int autoType = resource.getAutomationtype(autoId);
487
488                         // Notify the listeners.
489                         verificationAbortedNotification(resource, autoType);
490                     }
491                 });
492             }
493         };
494
495         synchronizerThread = new ResponseSynchronizerThread();
496         threadHandle = new Thread(synchronizerThread);
497         threadHandle.setName("Simulator Client Controller Event Queue");
498         threadHandle.start();
499     }
500
501     private RemoteResource handleResponse(String uid,
502             SimulatorResourceModel resourceModelN) {
503         if (null == uid || null == resourceModelN) {
504             return null;
505         }
506
507         // Update the local model
508         RemoteResource resource;
509         resource = getResource(uid);
510         if (null == resource) {
511             return null;
512         }
513
514         // if(!resource.isConfigUploaded() || null ==
515         // resource.getResourceModelRef())
516         SimulatorResourceModel resourceModel = resource.getResourceModelRef();
517         if (null == resourceModel) {
518             resource.setResourceModelRef(resourceModelN);
519         } else {
520             resourceModel.update(resourceModelN);
521         }
522
523         resource.setResourceRepresentation(resourceModelN, false);
524
525         return resource;
526     }
527
528     public synchronized boolean isDeviceInfoExist(String host) {
529         DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
530         if (null == info) {
531             return false;
532         }
533         if (null == info.getDeviceInfo()) {
534             return false;
535         }
536         return true;
537     }
538
539     public synchronized boolean isPlatformInfoExist(String host) {
540         DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
541         if (null == info) {
542             return false;
543         }
544         if (null == info.getPlatformInfo()) {
545             return false;
546         }
547         return true;
548     }
549
550     private static class ResponseSynchronizerThread implements Runnable {
551
552         LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
553
554         @Override
555         public void run() {
556             while (!Thread.interrupted()) {
557                 synchronized (this) {
558                     try {
559                         while (responseQueue.isEmpty()) {
560                             this.wait();
561                             break;
562                         }
563                     } catch (InterruptedException e) {
564                         return;
565                     }
566                 }
567
568                 Runnable thread;
569                 synchronized (this) {
570                     thread = responseQueue.pop();
571                 }
572                 try {
573                     thread.run();
574                 } catch (Exception e) {
575                     if (e instanceof InterruptedException) {
576                         return;
577                     }
578                     e.printStackTrace();
579                 }
580             }
581         }
582
583         public void addToQueue(Runnable event) {
584             synchronized (this) {
585                 responseQueue.add(event);
586                 this.notify();
587             }
588         }
589     }
590
591     public void addResourceSelectionChangedUIListener(
592             IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
593         synchronized (resourceSelectionChangedUIListeners) {
594             resourceSelectionChangedUIListeners
595                     .add(resourceSelectionChangedUIListener);
596         }
597     }
598
599     public void addGetUIListener(IGetUIListener getUIListener) {
600         synchronized (getUIListeners) {
601             getUIListeners.add(getUIListener);
602         }
603     }
604
605     public void addPutUIListener(IPutUIListener putUIListener) {
606         synchronized (putUIListeners) {
607             putUIListeners.add(putUIListener);
608         }
609     }
610
611     public void addPostUIListener(IPostUIListener postUIListener) {
612         synchronized (postUIListeners) {
613             postUIListeners.add(postUIListener);
614         }
615     }
616
617     public void addObserveUIListener(IObserveUIListener observeUIListener) {
618         synchronized (observeUIListeners) {
619             observeUIListeners.add(observeUIListener);
620         }
621     }
622
623     public void addVerificationUIListener(
624             IVerificationUIListener verificationUIListener) {
625         synchronized (verificationUIListeners) {
626             verificationUIListeners.add(verificationUIListener);
627         }
628     }
629
630     public void addConfigUploadUIListener(IConfigurationUpload configListener) {
631         synchronized (configUploadUIListeners) {
632             configUploadUIListeners.add(configListener);
633         }
634     }
635
636     public void addDevicePlatformInfoUIListener(
637             IDevicePlatformInfoUIListener deviceUIListener) {
638         synchronized (devicePlatformInfoUIListeners) {
639             devicePlatformInfoUIListeners.add(deviceUIListener);
640         }
641     }
642
643     public void removeDevicePlatformInfoUIListener(
644             IDevicePlatformInfoUIListener platformUIListener) {
645         synchronized (devicePlatformInfoUIListeners) {
646             devicePlatformInfoUIListeners.remove(platformUIListener);
647         }
648     }
649
650     public void removeResourceSelectionChangedUIListener(
651             IResourceSelectionChangedUIListener listener) {
652         synchronized (resourceSelectionChangedUIListeners) {
653             if (null != listener
654                     && resourceSelectionChangedUIListeners.size() > 0) {
655                 resourceSelectionChangedUIListeners.remove(listener);
656             }
657         }
658     }
659
660     public void removeGetUIListener(IGetUIListener getUIListener) {
661         synchronized (getUIListeners) {
662             getUIListeners.remove(getUIListener);
663         }
664     }
665
666     public void removePutUIListener(IPutUIListener putUIListener) {
667         synchronized (putUIListeners) {
668             putUIListeners.remove(putUIListener);
669         }
670     }
671
672     public void removePostUIListener(IPostUIListener postUIListener) {
673         synchronized (postUIListeners) {
674             postUIListeners.remove(postUIListener);
675         }
676     }
677
678     public void removeObserveUIListener(IObserveUIListener observeUIListener) {
679         synchronized (observeUIListeners) {
680             observeUIListeners.remove(observeUIListener);
681         }
682     }
683
684     public void removeVerificationUIListener(
685             IVerificationUIListener verificationUIListener) {
686         synchronized (verificationUIListeners) {
687             verificationUIListeners.remove(verificationUIListener);
688         }
689     }
690
691     public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
692         synchronized (configUploadUIListeners) {
693             configUploadUIListeners.remove(configListener);
694         }
695     }
696
697     public void addResourcetoFavorites(RemoteResource resource) {
698         if (null == resource) {
699             return;
700         }
701         resource.setFavorite(true);
702         synchronized (favoriteResources) {
703             favoriteResources.add(resource);
704         }
705     }
706
707     public void removeResourceFromFavorites(RemoteResource resource) {
708         if (null == resource) {
709             return;
710         }
711         resource.setFavorite(false);
712         synchronized (favoriteResources) {
713             favoriteResources.remove(resource);
714         }
715     }
716
717     public void addResourceURItoFavorites(RemoteResource resource) {
718         if (null == resource) {
719             return;
720         }
721         synchronized (favoriteURIList) {
722             favoriteURIList.add(resource.getRemoteResourceRef().getURI());
723         }
724     }
725
726     public void removeResourceURIFromFavorites(RemoteResource resource) {
727         if (null == resource) {
728             return;
729         }
730         synchronized (favoriteURIList) {
731             favoriteURIList.remove(resource.getRemoteResourceRef().getURI());
732         }
733     }
734
735     public void addObservedResourceURI(String resourceURI) {
736         synchronized (observedResourceURIList) {
737             if (!observedResourceURIList.contains(resourceURI))
738                 observedResourceURIList.add(resourceURI);
739         }
740     }
741
742     public void removeObservedResourceURI(String resourceURI) {
743         synchronized (observedResourceURIList) {
744             observedResourceURIList.remove(resourceURI);
745         }
746     }
747
748     public boolean isResourceObserved(String resourceURI) {
749         boolean observed = false;
750         synchronized (observedResourceURIList) {
751             observed = observedResourceURIList.contains(resourceURI);
752         }
753         return observed;
754     }
755
756     public synchronized RemoteResource getCurrentResourceInSelection() {
757         return currentResourceInSelection;
758     }
759
760     public synchronized void setCurrentResourceInSelection(
761             RemoteResource resource) {
762         this.currentResourceInSelection = resource;
763     }
764
765     private void addResourceDetails(RemoteResource remoteResource) {
766         if (null != remoteResource) {
767             synchronized (resourceMap) {
768                 resourceMap.put(remoteResource.getRemoteResourceRef().getId(),
769                         remoteResource);
770             }
771         }
772     }
773
774     public void addFindresourceUIListener(IFindResourceUIListener listener) {
775         if (null == listener) {
776             return;
777         }
778         synchronized (findResourceUIListeners) {
779             findResourceUIListeners.add(listener);
780         }
781     }
782
783     public void removeFindresourceUIListener(IFindResourceUIListener listener) {
784         if (null == listener) {
785             return;
786         }
787         synchronized (findResourceUIListeners) {
788             findResourceUIListeners.remove(listener);
789         }
790     }
791
792     private boolean isUidExist(String uid) {
793         boolean exist;
794         synchronized (resourceMap) {
795             exist = resourceMap.containsKey(uid);
796         }
797         return exist;
798     }
799
800     private RemoteResource getResource(String uid) {
801         if (null == uid) {
802             return null;
803         }
804         RemoteResource resource;
805         synchronized (resourceMap) {
806             resource = resourceMap.get(uid);
807         }
808         return resource;
809     }
810
811     private void newResourceFoundNotification(RemoteResource resource) {
812         synchronized (findResourceUIListeners) {
813             if (findResourceUIListeners.size() > 0) {
814                 IFindResourceUIListener listener;
815                 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
816                         .iterator();
817                 while (listenerItr.hasNext()) {
818                     listener = listenerItr.next();
819                     if (null != listener) {
820                         listener.onNewResourceFound(resource);
821                     }
822                 }
823             }
824         }
825     }
826
827     private void resourceSelectionChangedUINotification(RemoteResource resource) {
828         synchronized (resourceSelectionChangedUIListeners) {
829             if (resourceSelectionChangedUIListeners.size() > 0) {
830                 IResourceSelectionChangedUIListener listener;
831                 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
832                         .iterator();
833                 while (listenerItr.hasNext()) {
834                     listener = listenerItr.next();
835                     if (null != listener) {
836                         listener.onResourceSelectionChange(resource);
837                     }
838                 }
839             }
840         }
841     }
842
843     private void getCompleteNotification(RemoteResource resource) {
844         synchronized (getUIListeners) {
845             if (getUIListeners.size() > 0) {
846                 IGetUIListener listener;
847                 Iterator<IGetUIListener> listenerItr = getUIListeners
848                         .iterator();
849                 while (listenerItr.hasNext()) {
850                     listener = listenerItr.next();
851                     if (null != listener) {
852                         listener.onGetCompleted(resource);
853                     }
854                 }
855             }
856         }
857     }
858
859     private void putCompleteNotification(RemoteResource resource) {
860         synchronized (putUIListeners) {
861             if (putUIListeners.size() > 0) {
862                 IPutUIListener listener;
863                 Iterator<IPutUIListener> listenerItr = putUIListeners
864                         .iterator();
865                 while (listenerItr.hasNext()) {
866                     listener = listenerItr.next();
867                     if (null != listener) {
868                         listener.onPutCompleted(resource);
869                     }
870                 }
871             }
872         }
873     }
874
875     private void postCompleteNotification(RemoteResource resource) {
876         synchronized (postUIListeners) {
877             if (postUIListeners.size() > 0) {
878                 IPostUIListener listener;
879                 Iterator<IPostUIListener> listenerItr = postUIListeners
880                         .iterator();
881                 while (listenerItr.hasNext()) {
882                     listener = listenerItr.next();
883                     if (null != listener) {
884                         listener.onPostCompleted(resource);
885                     }
886                 }
887             }
888         }
889     }
890
891     private void observeCompleteNotification(RemoteResource resource) {
892         synchronized (observeUIListeners) {
893             if (observeUIListeners.size() > 0) {
894                 IObserveUIListener listener;
895                 Iterator<IObserveUIListener> listenerItr = observeUIListeners
896                         .iterator();
897                 while (listenerItr.hasNext()) {
898                     listener = listenerItr.next();
899                     if (null != listener) {
900                         listener.onObserveCompleted(resource);
901                     }
902                 }
903             }
904         }
905     }
906
907     private void verificationStartedNotification(RemoteResource resource,
908             int autoType) {
909         synchronized (verificationUIListeners) {
910             if (verificationUIListeners.size() > 0) {
911                 IVerificationUIListener listener;
912                 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
913                         .iterator();
914                 while (listenerItr.hasNext()) {
915                     listener = listenerItr.next();
916                     if (null != listener) {
917                         listener.onVerificationStarted(resource, autoType);
918                     }
919                 }
920             }
921         }
922     }
923
924     private void verificationAbortedNotification(RemoteResource resource,
925             int autoType) {
926         synchronized (verificationUIListeners) {
927             if (verificationUIListeners.size() > 0) {
928                 IVerificationUIListener listener;
929                 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
930                         .iterator();
931                 while (listenerItr.hasNext()) {
932                     listener = listenerItr.next();
933                     if (null != listener) {
934                         listener.onVerificationAborted(resource, autoType);
935                     }
936                 }
937             }
938         }
939     }
940
941     private void verificationCompletedNotification(RemoteResource resource,
942             int autoType) {
943         synchronized (verificationUIListeners) {
944             if (verificationUIListeners.size() > 0) {
945                 IVerificationUIListener listener;
946                 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
947                         .iterator();
948                 while (listenerItr.hasNext()) {
949                     listener = listenerItr.next();
950                     if (null != listener) {
951                         listener.onVerificationCompleted(resource, autoType);
952                     }
953                 }
954             }
955         }
956     }
957
958     private void configUploadedNotification(RemoteResource resource) {
959         synchronized (configUploadUIListeners) {
960             if (configUploadUIListeners.size() > 0) {
961                 IConfigurationUpload listener;
962                 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
963                         .iterator();
964                 while (listenerItr.hasNext()) {
965                     listener = listenerItr.next();
966                     if (null != listener) {
967                         listener.onConfigurationUploaded(resource);
968                     }
969                 }
970             }
971         }
972     }
973
974     private void deviceInfoReceivedNotification() {
975         synchronized (devicePlatformInfoUIListeners) {
976             if (devicePlatformInfoUIListeners.size() > 0) {
977                 IDevicePlatformInfoUIListener listener;
978                 Iterator<IDevicePlatformInfoUIListener> listenerItr = devicePlatformInfoUIListeners
979                         .iterator();
980                 while (listenerItr.hasNext()) {
981                     listener = listenerItr.next();
982                     if (null != listener) {
983                         listener.onDeviceInfoFound();
984                     }
985                 }
986             }
987         }
988     }
989
990     private void platformInfoReceivedNotification() {
991         synchronized (devicePlatformInfoUIListeners) {
992             if (devicePlatformInfoUIListeners.size() > 0) {
993                 IDevicePlatformInfoUIListener listener;
994                 Iterator<IDevicePlatformInfoUIListener> listenerItr = devicePlatformInfoUIListeners
995                         .iterator();
996                 while (listenerItr.hasNext()) {
997                     listener = listenerItr.next();
998                     if (null != listener) {
999                         listener.onPlatformInfoFound();
1000                     }
1001                 }
1002             }
1003         }
1004     }
1005
1006     public synchronized Set<String> getLastKnownSearchTypes() {
1007         return lastKnownSearchTypes;
1008     }
1009
1010     public synchronized void setLastKnownSearchTypes(
1011             Set<String> lastKnownSearchTypes) {
1012         this.lastKnownSearchTypes = lastKnownSearchTypes;
1013     }
1014
1015     public boolean findResourceRequest(Set<String> searchTypes) {
1016         boolean result = false;
1017         if (null == searchTypes || searchTypes.size() < 1) {
1018             try {
1019                 SimulatorManager.findResource(findResourceListener);
1020                 result = true;
1021             } catch (SimulatorException e) {
1022                 Activator
1023                         .getDefault()
1024                         .getLogManager()
1025                         .log(Level.ERROR.ordinal(), new Date(),
1026                                 Utility.getSimulatorErrorString(e, null));
1027             }
1028         } else {
1029             Iterator<String> searchItr = searchTypes.iterator();
1030             String rType;
1031             while (searchItr.hasNext()) {
1032                 rType = searchItr.next();
1033                 try {
1034                     SimulatorManager.findResource(rType, findResourceListener);
1035                     result = true;
1036                 } catch (SimulatorException e) {
1037                     Activator
1038                             .getDefault()
1039                             .getLogManager()
1040                             .log(Level.ERROR.ordinal(), new Date(),
1041                                     Utility.getSimulatorErrorString(e, null));
1042                 }
1043             }
1044         }
1045         return result;
1046     }
1047
1048     public void deleteResources(final Set<String> searchTypes) {
1049         synchronized (resourceMap) {
1050             if (null == resourceMap && resourceMap.isEmpty()) {
1051                 return;
1052             }
1053         }
1054         new Thread() {
1055             public void run() {
1056                 if (null == searchTypes || searchTypes.size() < 1) {
1057                     synchronized (resourceMap) {
1058                         // Stop observing all the resources
1059                         Iterator<String> itr = resourceMap.keySet().iterator();
1060                         while (itr.hasNext()) {
1061                             sendCancelObserveRequest(
1062                                     resourceMap.get(itr.next()), false);
1063                         }
1064                         // Delete all cached details of resources
1065                         resourceMap.clear();
1066                         favoriteResources.clear();
1067
1068                         // Clearing the device and platform information
1069                         hostDeviceAndPlatformMap.clear();
1070                     }
1071                     // Change the current resource in selection
1072                     setCurrentResourceInSelection(null);
1073                     resourceSelectionChangedUINotification(null);
1074                 } else {
1075                     Iterator<String> typeItr = searchTypes.iterator();
1076                     String resType;
1077                     while (typeItr.hasNext()) {
1078                         resType = typeItr.next();
1079                         deleteResourcesByType(resType);
1080
1081                         // Change the current resource in selection
1082                         updateCurrentResourceInSelection(searchTypes);
1083                     }
1084                 }
1085             }
1086         }.start();
1087     }
1088
1089     private void updateCurrentResourceInSelection(Set<String> searchTypes) {
1090         if (null == searchTypes || searchTypes.size() < 1) {
1091             return;
1092         }
1093         RemoteResource resourceInSelection = getCurrentResourceInSelection();
1094         if (null == resourceInSelection) {
1095             return;
1096         }
1097         List<String> typesOfSelection = resourceInSelection
1098                 .getRemoteResourceRef().getResourceTypes();
1099         if (null == typesOfSelection || typesOfSelection.size() < 1) {
1100             return;
1101         }
1102         Iterator<String> itr = typesOfSelection.iterator();
1103         String type;
1104         while (itr.hasNext()) {
1105             type = itr.next();
1106             if (searchTypes.contains(type)) {
1107                 setCurrentResourceInSelection(null);
1108                 resourceSelectionChangedUINotification(null);
1109                 break;
1110             }
1111         }
1112     }
1113
1114     private void deleteResourcesByType(String resourceType) {
1115         if (null == resourceType) {
1116             return;
1117         }
1118         synchronized (resourceMap) {
1119             Set<String> keySet = resourceMap.keySet();
1120             if (null == keySet) {
1121                 return;
1122             }
1123             Iterator<String> keyItr = keySet.iterator();
1124             String uId;
1125             RemoteResource resource;
1126             boolean exist;
1127             List<String> types;
1128             while (keyItr.hasNext()) {
1129                 uId = keyItr.next();
1130                 resource = resourceMap.get(uId);
1131                 if (null == resource) {
1132                     continue;
1133                 }
1134                 types = resource.getRemoteResourceRef().getResourceTypes();
1135                 if (null != types) {
1136                     exist = types.contains(resourceType);
1137                     if (exist) {
1138                         // Cancel observing the resource.
1139                         sendCancelObserveRequest(resource, false);
1140                         // Remove the resource from favorites list.
1141                         removeResourceFromFavorites(resource);
1142                         // Remove the resource
1143                         keyItr.remove();
1144                         // Remove the device and platform information
1145                         synchronized (hostDeviceAndPlatformMap) {
1146                             hostDeviceAndPlatformMap.remove(resource
1147                                     .getRemoteResourceRef().getHost());
1148                         }
1149                     }
1150                 }
1151             }
1152         }
1153     }
1154
1155     public void resourceSelectionChanged(final RemoteResource resource) {
1156         new Thread() {
1157             @Override
1158             public void run() {
1159                 setCurrentResourceInSelection(resource);
1160                 // Notify all observers for resource selection change event
1161                 resourceSelectionChangedUINotification(resource);
1162             }
1163         }.start();
1164     }
1165
1166     public List<MetaProperty> getDefaultProperties(RemoteResource resource) {
1167         if (null != resource) {
1168             String propName;
1169             String propValue;
1170
1171             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1172
1173             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1174                 propName = Constants.META_PROPERTIES[index];
1175                 if (propName.equals(Constants.RESOURCE_URI)) {
1176                     propValue = resource.getRemoteResourceRef().getURI();
1177                 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1178                     propValue = resource.getRemoteResourceRef()
1179                             .getConnectivityType().toString();
1180                 } else if (propName.equals(Constants.ADDRESS)) {
1181                     propValue = resource.getRemoteResourceRef().getHost();
1182                 } else if (propName.equals(Constants.OBSERVABLE)) {
1183                     propValue = Utility.getObservableInString(resource
1184                             .getRemoteResourceRef().isObservable());
1185                 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1186                     Vector<String> types = resource.getRemoteResourceRef()
1187                             .getResourceTypes();
1188                     if (null != types) {
1189                         propValue = types.toString();
1190                     } else {
1191                         propValue = Constants.NOT_AVAILABLE;
1192                     }
1193                 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1194                     Vector<String> interfaces = resource.getRemoteResourceRef()
1195                             .getResourceInterfaces();
1196                     if (null != interfaces) {
1197                         propValue = interfaces.toString();
1198                     } else {
1199                         propValue = Constants.NOT_AVAILABLE;
1200                     }
1201                 } else {
1202                     propValue = null;
1203                 }
1204                 if (null != propValue) {
1205                     metaPropertyList.add(new MetaProperty(propName, propValue));
1206                 }
1207             }
1208
1209             return metaPropertyList;
1210         }
1211         return null;
1212     }
1213
1214     public List<MetaProperty> getDeviceProperties() {
1215         if (null == currentResourceInSelection) {
1216             return null;
1217         }
1218
1219         SimulatorRemoteResource remoteResource = currentResourceInSelection
1220                 .getRemoteResourceRef();
1221         if (null == remoteResource) {
1222             return null;
1223         }
1224
1225         String host = remoteResource.getHost();
1226         if (null == host) {
1227             return null;
1228         }
1229
1230         if (!isDeviceInfoExist(host)) {
1231             // Device Information
1232             try {
1233                 SimulatorManager.findDevices(host, deviceListener);
1234             } catch (SimulatorException e) {
1235                 Activator
1236                         .getDefault()
1237                         .getLogManager()
1238                         .log(Level.ERROR.ordinal(), new Date(),
1239                                 Utility.getSimulatorErrorString(e, null));
1240             }
1241             return null;
1242         }
1243
1244         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
1245         synchronized (hostDeviceAndPlatformMap) {
1246             DeviceInfo devInfo = hostDeviceAndPlatformMap.get(host)
1247                     .getDeviceInfo();
1248             metaProperties.add(new MetaProperty(Constants.DEVICE_ID, devInfo
1249                     .getID()));
1250             metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, devInfo
1251                     .getName()));
1252             metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
1253                     devInfo.getSpecVersion()));
1254             metaProperties.add(new MetaProperty(Constants.DEVICE_DMV_VERSION,
1255                     devInfo.getDataModelVersion()));
1256         }
1257
1258         /*
1259          * metaProperties.add(new MetaProperty(Constants.DEVICE_ID, ""));
1260          * metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, ""));
1261          * metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
1262          * "")); metaProperties.add(new
1263          * MetaProperty(Constants.DEVICE_DMV_VERSION, ""));
1264          */
1265         return metaProperties;
1266     }
1267
1268     public List<MetaProperty> getPlatformProperties() {
1269         if (null == currentResourceInSelection) {
1270             return null;
1271         }
1272
1273         SimulatorRemoteResource remoteResource = currentResourceInSelection
1274                 .getRemoteResourceRef();
1275         if (null == remoteResource) {
1276             return null;
1277         }
1278
1279         String host = remoteResource.getHost();
1280         if (null == host) {
1281             return null;
1282         }
1283
1284         if (!isPlatformInfoExist(host)) {
1285             // Platform Information
1286             try {
1287                 SimulatorManager.getPlatformInformation(host, platformListener);
1288             } catch (SimulatorException e) {
1289                 Activator
1290                         .getDefault()
1291                         .getLogManager()
1292                         .log(Level.ERROR.ordinal(), new Date(),
1293                                 Utility.getSimulatorErrorString(e, null));
1294             }
1295             return null;
1296         }
1297
1298         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
1299         synchronized (hostDeviceAndPlatformMap) {
1300             PlatformInfo platInfo = hostDeviceAndPlatformMap.get(host)
1301                     .getPlatformInfo();
1302             metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platInfo
1303                     .getPlatformID()));
1304             metaProperties.add(new MetaProperty(
1305                     Constants.PLATFORM_MANUFAC_NAME, platInfo
1306                             .getManufacturerName()));
1307             metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
1308                     platInfo.getManufacturerUrl()));
1309             metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
1310                     platInfo.getModelNumber()));
1311             metaProperties.add(new MetaProperty(
1312                     Constants.PLATFORM_DATE_OF_MANUFAC, platInfo
1313                             .getDateOfManufacture()));
1314             metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
1315                     platInfo.getPlatformVersion()));
1316             metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1317                     platInfo.getOperationSystemVersion()));
1318             metaProperties.add(new MetaProperty(
1319                     Constants.PLATFORM_HARDWARE_VERSION, platInfo
1320                             .getHardwareVersion()));
1321             metaProperties.add(new MetaProperty(
1322                     Constants.PLATFORM_FIRMWARE_VERSION, platInfo
1323                             .getFirmwareVersion()));
1324             metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1325                     platInfo.getSupportUrl()));
1326             metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
1327                     platInfo.getSystemTime()));
1328         }
1329         /*
1330          * metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, ""));
1331          * metaProperties .add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
1332          * "")); metaProperties .add(new
1333          * MetaProperty(Constants.PLATFORM_MANUFAC_URL, ""));
1334          * metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
1335          * "")); metaProperties.add(new
1336          * MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC, ""));
1337          * metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION, ""));
1338          * metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1339          * "")); metaProperties.add(new MetaProperty(
1340          * Constants.PLATFORM_HARDWARE_VERSION, "")); metaProperties.add(new
1341          * MetaProperty( Constants.PLATFORM_FIRMWARE_VERSION, ""));
1342          * metaProperties .add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1343          * "")); metaProperties .add(new
1344          * MetaProperty(Constants.PLATFORM_SYSTEM_TIME, ""));
1345          */
1346         return metaProperties;
1347     }
1348
1349     public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1350         if (null == resource) {
1351             return null;
1352         }
1353         Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1354         autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1355         autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1356         autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1357         return autoStatus;
1358     }
1359
1360     public List<RemoteResource> getResourceList() {
1361         List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1362         synchronized (resourceMap) {
1363             Set<String> idSet = resourceMap.keySet();
1364             Iterator<String> idItr = idSet.iterator();
1365             RemoteResource resource;
1366             while (idItr.hasNext()) {
1367                 resource = resourceMap.get(idItr.next());
1368                 if (null != resource) {
1369                     resourceList.add(resource);
1370                 }
1371             }
1372         }
1373         // Sort the list
1374         Collections.sort(resourceList, new Comparator<RemoteResource>() {
1375             public int compare(RemoteResource res1, RemoteResource res2) {
1376                 String s1 = res1.getRemoteResourceRef().getURI();
1377                 String s2 = res2.getRemoteResourceRef().getURI();
1378
1379                 String s1Part = s1.replaceAll("\\d", "");
1380                 String s2Part = s2.replaceAll("\\d", "");
1381
1382                 if (s1Part.equalsIgnoreCase(s2Part)) {
1383                     return extractInt(s1) - extractInt(s2);
1384                 }
1385                 return s1.compareTo(s2);
1386             }
1387
1388             int extractInt(String s) {
1389                 String num = s.replaceAll("\\D", "");
1390                 // return 0 if no digits found
1391                 return num.isEmpty() ? 0 : Integer.parseInt(num);
1392             }
1393         });
1394
1395         return resourceList;
1396     }
1397
1398     public List<RemoteResource> getFavResourceList() {
1399         List<RemoteResource> resourceList;
1400         synchronized (favoriteResources) {
1401             resourceList = new ArrayList<RemoteResource>(favoriteResources);
1402         }
1403         return resourceList;
1404     }
1405
1406     public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1407         if (null == att) {
1408             return null;
1409         }
1410
1411         AttributeValue val = att.value();
1412         if (null == val) {
1413             return null;
1414         }
1415
1416         List<String> values = new ArrayList<String>();
1417
1418         TypeInfo type = val.typeInfo();
1419
1420         AttributeProperty prop = att.property();
1421         if (null == prop || prop.type().ordinal() == Type.UNKNOWN.ordinal()) {
1422             values.add(Utility.getAttributeValueAsString(val));
1423             return values;
1424         }
1425
1426         Type valuesType = prop.type();
1427
1428         if (type.mType != ValueType.RESOURCEMODEL) {
1429             if (type.mType == ValueType.ARRAY) {
1430                 if (type.mDepth == 1) {
1431                     AttributeProperty childProp = prop.getChildProperty();
1432                     if (null != childProp) {
1433                         valuesType = childProp.type();
1434                         if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1435                             List<String> list = getRangeForPrimitiveNonArrayAttributes(
1436                                     childProp, type.mBaseType);
1437                             if (null != list) {
1438                                 values.addAll(list);
1439                             }
1440                         } else if (valuesType.ordinal() == Type.VALUESET
1441                                 .ordinal()) {
1442                             List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1443                                     childProp.valueSet(), type.mBaseType);
1444                             if (null != list) {
1445                                 values.addAll(list);
1446                             }
1447                         }
1448                     }
1449                 }
1450             } else {
1451                 if (valuesType.ordinal() == Type.RANGE.ordinal()) {
1452                     List<String> list = getRangeForPrimitiveNonArrayAttributes(
1453                             prop, type.mType);
1454                     if (null != list) {
1455                         values.addAll(list);
1456                     }
1457                 } else if (valuesType.ordinal() == Type.VALUESET.ordinal()) {
1458                     List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1459                             prop.valueSet(), type.mType);
1460                     if (null != list) {
1461                         values.addAll(list);
1462                     }
1463                 }
1464             }
1465         }
1466
1467         if (values.isEmpty()) {
1468             values.add(Utility.getAttributeValueAsString(val));
1469         }
1470
1471         return values;
1472     }
1473
1474     public List<String> getRangeForPrimitiveNonArrayAttributes(
1475             AttributeProperty prop, ValueType type) {
1476         if (null == prop) {
1477             return null;
1478         }
1479
1480         if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1481             return null;
1482         }
1483
1484         List<String> values = new ArrayList<String>();
1485         switch (type) {
1486             case INTEGER:
1487                 int min = (int) prop.min();
1488                 int max = (int) prop.max();
1489                 for (int iVal = min; iVal <= max; iVal++) {
1490                     values.add(String.valueOf(iVal));
1491                 }
1492                 break;
1493             case DOUBLE:
1494                 double minD = (double) prop.min();
1495                 double maxD = (double) prop.max();
1496                 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
1497                     values.add(String.valueOf(iVal));
1498                 }
1499                 break;
1500             default:
1501         }
1502         return values;
1503     }
1504
1505     public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
1506             AttributeValue[] attValues, ValueType type) {
1507         if (null == attValues || attValues.length < 1) {
1508             return null;
1509         }
1510
1511         if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1512             return null;
1513         }
1514
1515         Object obj;
1516         List<String> values = new ArrayList<String>();
1517         for (AttributeValue val : attValues) {
1518             if (null == val) {
1519                 continue;
1520             }
1521             obj = val.get();
1522             if (null == obj) {
1523                 continue;
1524             }
1525             values.add(String.valueOf(obj));
1526         }
1527         return values;
1528     }
1529
1530     /*
1531      * public String getAttributeValue(RemoteResource res, String attName) { if
1532      * (null == res || null == attName) { return null; } return
1533      * res.getAttributeValue(attName); }
1534      */
1535     public void sendGetRequest(RemoteResource resource) {
1536         if (null == resource) {
1537             return;
1538         }
1539         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1540         if (null == resourceN) {
1541             return;
1542         }
1543         try {
1544             resourceN.get(null, getListener);
1545         } catch (SimulatorException e) {
1546             Activator
1547                     .getDefault()
1548                     .getLogManager()
1549                     .log(Level.ERROR.ordinal(), new Date(),
1550                             Utility.getSimulatorErrorString(e, null));
1551         }
1552     }
1553
1554     public void sendPutRequest(RemoteResource resource,
1555             SimulatorResourceModel model) {
1556         if (null == resource || null == model) {
1557             return;
1558         }
1559         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1560         if (null == resourceN) {
1561             return;
1562         }
1563         try {
1564             resourceN.put(null, model, putListener);
1565         } catch (Exception e) {
1566             String addlInfo;
1567             addlInfo = "Invalid Attribute Value. Cannot send PUT request.";
1568             Activator
1569                     .getDefault()
1570                     .getLogManager()
1571                     .log(Level.ERROR.ordinal(), new Date(),
1572                             Utility.getSimulatorErrorString(e, addlInfo));
1573         }
1574     }
1575
1576     public void sendPostRequest(RemoteResource resource,
1577             SimulatorResourceModel model) {
1578         if (null == resource || null == model) {
1579             return;
1580         }
1581         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1582         if (null == resourceN) {
1583             return;
1584         }
1585         try {
1586             resourceN.post(null, model, postListener);
1587         } catch (Exception e) {
1588             String addlInfo;
1589             addlInfo = "Invalid Attribute Value. Cannot send POST request.";
1590             Activator
1591                     .getDefault()
1592                     .getLogManager()
1593                     .log(Level.ERROR.ordinal(), new Date(),
1594                             Utility.getSimulatorErrorString(e, addlInfo));
1595         }
1596     }
1597
1598     public boolean sendObserveRequest(RemoteResource resource) {
1599         if (null == resource) {
1600             return false;
1601         }
1602         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1603         if (null == resourceN) {
1604             return false;
1605         }
1606         try {
1607             resourceN.startObserve(null, observeListener);
1608             resource.setObserved(true);
1609             // Add observed resource URI to show the proper status after every
1610             // find/refresh operations.
1611             addObservedResourceURI(resource.getRemoteResourceRef().getURI());
1612         } catch (SimulatorException e) {
1613             Activator
1614                     .getDefault()
1615                     .getLogManager()
1616                     .log(Level.ERROR.ordinal(), new Date(),
1617                             Utility.getSimulatorErrorString(e, null));
1618             return false;
1619         }
1620         return true;
1621     }
1622
1623     public boolean sendCancelObserveRequest(RemoteResource resource,
1624             boolean removeEntry) {
1625         if (null == resource || !resource.isObserved()) {
1626             return false;
1627         }
1628         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1629         if (null == resourceN) {
1630             return false;
1631         }
1632         try {
1633             resourceN.stopObserve();
1634             resource.setObserved(false);
1635             // Remove observed resource URI to show the proper status after
1636             // every find/refresh operations.
1637             if (removeEntry)
1638                 removeObservedResourceURI(resource.getRemoteResourceRef()
1639                         .getURI());
1640         } catch (SimulatorException e) {
1641             Activator
1642                     .getDefault()
1643                     .getLogManager()
1644                     .log(Level.ERROR.ordinal(), new Date(),
1645                             Utility.getSimulatorErrorString(e, null));
1646             return false;
1647         }
1648         return true;
1649     }
1650
1651     public void startAutomationRequest(VerificationType reqType,
1652             RemoteResource resource) {
1653         if (null == resource) {
1654             return;
1655         }
1656         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1657         if (null == resourceN) {
1658             return;
1659         }
1660         if (null == reqType) {
1661             return;
1662         }
1663         int autoId;
1664         try {
1665             autoId = resourceN.startVerification(reqType, verifyListener);
1666             if (autoId != -1) {
1667                 if (reqType == VerificationType.GET) {
1668                     // resource.setGetAutomtnInProgress(true);
1669                     resource.setGetAutomtnId(autoId);
1670                 } else if (reqType == VerificationType.PUT) {
1671                     // resource.setPutAutomtnInProgress(true);
1672                     resource.setPutAutomtnId(autoId);
1673                 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1674                     // resource.setPostAutomtnInProgress(true);
1675                     resource.setPostAutomtnId(autoId);
1676                 }
1677             }
1678         } catch (SimulatorException e) {
1679             Activator
1680                     .getDefault()
1681                     .getLogManager()
1682                     .log(Level.ERROR.ordinal(), new Date(),
1683                             Utility.getSimulatorErrorString(e, null));
1684         }
1685     }
1686
1687     public void stopAutomationRequest(VerificationType reqType,
1688             RemoteResource resource) {
1689         if (null == resource) {
1690             return;
1691         }
1692         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1693         if (null == resourceN) {
1694             return;
1695         }
1696         int autoId;
1697         if (reqType == VerificationType.GET) {
1698             resource.setGetAutomtnInProgress(false);
1699             autoId = resource.getGetAutomtnId();
1700         } else if (reqType == VerificationType.PUT) {
1701             resource.setPutAutomtnInProgress(false);
1702             autoId = resource.getPutAutomtnId();
1703         } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1704             resource.setPostAutomtnInProgress(false);
1705             autoId = resource.getPostAutomtnId();
1706         }
1707         try {
1708             resourceN.stopVerification(autoId);
1709         } catch (SimulatorException e) {
1710             Activator
1711                     .getDefault()
1712                     .getLogManager()
1713                     .log(Level.ERROR.ordinal(), new Date(),
1714                             Utility.getSimulatorErrorString(e, null));
1715         }
1716     }
1717
1718     public boolean setConfigFilePath(RemoteResource resource,
1719             String configFilePath) throws SimulatorException {
1720         if (null == resource) {
1721             return false;
1722         }
1723         SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1724         if (null == resourceN) {
1725             return false;
1726         }
1727         try {
1728             SimulatorResourceModel configuredResourceModel;
1729             configuredResourceModel = resourceN.setConfigInfo(configFilePath);
1730             if (null == configuredResourceModel) {
1731                 return false;
1732             }
1733
1734             // Store the resource model in the local cache
1735             SimulatorResourceModel resourceModel = resource
1736                     .getResourceModelRef();
1737             if (null != resourceModel) {
1738                 configuredResourceModel.update(resourceModel);
1739             }
1740             resource.setResourceModelRef(configuredResourceModel);
1741         } catch (SimulatorException e) {
1742             Activator
1743                     .getDefault()
1744                     .getLogManager()
1745                     .log(Level.ERROR.ordinal(), new Date(),
1746                             Utility.getSimulatorErrorString(e, null));
1747             throw e;
1748         }
1749         // Update the status
1750         resource.setConfigUploaded(true);
1751
1752         // Notify the UI listeners
1753         configUploadedNotification(resource);
1754
1755         return true;
1756     }
1757
1758     public void shutdown() {
1759     }
1760 }