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