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