Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / ResourceManager.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.serviceprovider.manager;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.swt.widgets.Display;
21
22 import java.text.NumberFormat;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.Vector;
33
34 import org.oic.simulator.ArrayProperty;
35 import org.oic.simulator.AttributeProperty;
36 import org.oic.simulator.AttributeProperty.Type;
37 import org.oic.simulator.AttributeValue;
38 import org.oic.simulator.AttributeValue.TypeInfo;
39 import org.oic.simulator.AttributeValue.ValueType;
40 import org.oic.simulator.BooleanProperty;
41 import org.oic.simulator.DeviceInfo;
42 import org.oic.simulator.DeviceListener;
43 import org.oic.simulator.DoubleProperty;
44 import org.oic.simulator.ILogger.Level;
45 import org.oic.simulator.IntegerProperty;
46 import org.oic.simulator.PlatformInfo;
47 import org.oic.simulator.SimulatorException;
48 import org.oic.simulator.SimulatorManager;
49 import org.oic.simulator.SimulatorResourceAttribute;
50 import org.oic.simulator.SimulatorResourceModel;
51 import org.oic.simulator.StringProperty;
52 import org.oic.simulator.server.Observer;
53 import org.oic.simulator.server.SimulatorResource;
54 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
55 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
56 import org.oic.simulator.server.SimulatorResource.ObserverListener;
57 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
58 import org.oic.simulator.server.SimulatorSingleResource;
59
60 import oic.simulator.serviceprovider.Activator;
61 import oic.simulator.serviceprovider.model.AttributeElement;
62 import oic.simulator.serviceprovider.model.MetaProperty;
63 import oic.simulator.serviceprovider.model.Resource;
64 import oic.simulator.serviceprovider.model.ResourceType;
65 import oic.simulator.serviceprovider.model.SingleResource;
66 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
67 import oic.simulator.serviceprovider.utils.Constants;
68 import oic.simulator.serviceprovider.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 model change,
74  * automation, and observer related events from native layer and propagates
75  * those events to the registered UI listeners.
76  */
77 public class ResourceManager {
78
79     private Data                           data;
80
81     private Resource                       currentResourceInSelection;
82
83     private ResourceModelChangeListener    resourceModelChangeListener;
84
85     private AutoUpdateListener             automationListener;
86
87     private ObserverListener               observer;
88
89     private DeviceListener                 deviceListener;
90
91     private NotificationSynchronizerThread synchronizerThread;
92
93     private Thread                         threadHandle;
94
95     private DeviceInfo                     deviceInfo;
96     private PlatformInfo                   platformInfo;
97
98     private String                         deviceName;
99
100     public ResourceManager() {
101         data = new Data();
102
103         deviceListener = new DeviceListener() {
104
105             @Override
106             public void onDeviceFound(final String host,
107                     final DeviceInfo deviceInfo) {
108                 if (null != ResourceManager.this.deviceInfo
109                         || null == deviceInfo || null == host) {
110                     return;
111                 }
112                 synchronizerThread.addToQueue(new Runnable() {
113                     @Override
114                     public void run() {
115                         String rcvdDeviceName = deviceInfo.getName();
116                         if (null == rcvdDeviceName) {
117                             return;
118                         }
119                         if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
120                             ResourceManager.this.deviceInfo = deviceInfo;
121
122                             // Notify the UI Listeners
123                             UiListenerHandler.getInstance()
124                                     .deviceInfoReceivedNotification();
125                         }
126                     }
127                 });
128             }
129         };
130
131         resourceModelChangeListener = new ResourceModelChangeListener() {
132
133             @Override
134             public void onResourceModelChanged(final String resourceURI,
135                     final SimulatorResourceModel resourceModelN) {
136                 synchronizerThread.addToQueue(new Runnable() {
137
138                     @Override
139                     public void run() {
140                         if (null == resourceURI || null == resourceModelN) {
141                             return;
142                         }
143
144                         Display.getDefault().asyncExec(new Runnable() {
145                             @Override
146                             public void run() {
147                                 Resource resource = data
148                                         .getResourceByURI(resourceURI);
149                                 if (null != resource) {
150                                     try {
151                                         resource.updateResourceRepresentation(resourceModelN);
152                                     } catch (NumberFormatException e) {
153                                         Activator
154                                                 .getDefault()
155                                                 .getLogManager()
156                                                 .log(Level.ERROR.ordinal(),
157                                                         new Date(),
158                                                         "Error while trying to update the attributes.\n"
159                                                                 + Utility
160                                                                         .getSimulatorErrorString(
161                                                                                 e,
162                                                                                 null));
163                                     }
164                                 }
165                             }
166                         });
167                     }
168                 });
169             }
170         };
171
172         automationListener = new AutoUpdateListener() {
173
174             @Override
175             public void onUpdateComplete(final String resourceURI,
176                     final int automationId) {
177                 synchronizerThread.addToQueue(new Runnable() {
178
179                     @Override
180                     public void run() {
181                         SingleResource resource = data
182                                 .getSingleResourceByURI(resourceURI);
183                         if (null == resource) {
184                             return;
185                         }
186                         // Checking whether this notification is for an
187                         // attribute or a resource
188                         if (resource.isResourceAutomationInProgress()) {
189                             changeResourceLevelAutomationStatus(resource, false);
190                             // Notify the UI listeners
191                             UiListenerHandler.getInstance()
192                                     .automationCompleteUINotification(resource,
193                                             null);
194                         } else if (resource.isAttributeAutomationInProgress()) {
195                             // Find the attribute with the given automation id
196                             final AttributeElement attribute = getAttributeWithGivenAutomationId(
197                                     resource, automationId);
198                             if (null != attribute) {
199                                 attribute.setAutoUpdateState(false);
200                                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
201                             } else {
202                                 // Setting the attribute automation status to
203                                 // false.
204                                 resource.setAttributeAutomationInProgress(false);
205                             }
206                         }
207                     }
208                 });
209             }
210         };
211
212         observer = new ObserverListener() {
213
214             public void onObserverChanged(final String resourceURI,
215                     final int status, final Observer observer) {
216                 new Thread() {
217                     @Override
218                     public void run() {
219                         if (null == resourceURI || null == observer) {
220                             return;
221                         }
222                         Resource resource = data.getResourceByURI(resourceURI);
223                         if (null == resource) {
224                             return;
225                         }
226                         // Update the observers information
227                         if (status == 0) {
228                             resource.addObserverInfo(observer);
229                         } else {
230                             resource.removeObserverInfo(observer);
231                         }
232                         // Notify the UI listeners
233                         UiListenerHandler.getInstance()
234                                 .observerListChangedUINotification(resource);
235                     }
236                 }.start();
237             }
238
239             @Override
240             public void onObserverAdded(String resourceURI, Observer observer) {
241                 onObserverChanged(resourceURI, 0, observer);
242             }
243
244             @Override
245             public void onObserverRemoved(String resourceURI, Observer observer) {
246                 onObserverChanged(resourceURI, 1, observer);
247             }
248         };
249
250         synchronizerThread = new NotificationSynchronizerThread();
251         threadHandle = new Thread(synchronizerThread);
252         threadHandle.setName("Simulator service provider event queue");
253         threadHandle.start();
254
255         // Set the default device name.
256         deviceName = "IoTivity Simulator";
257         try {
258             SimulatorManager.setDeviceInfo(deviceName);
259         } catch (SimulatorException e) {
260             Activator
261                     .getDefault()
262                     .getLogManager()
263                     .log(Level.ERROR.ordinal(),
264                             new Date(),
265                             "Error while registering the device info.\n"
266                                     + Utility.getSimulatorErrorString(e, null));
267         }
268
269         // Set the default platform information.
270         platformInfo = new PlatformInfo();
271         platformInfo.setPlatformID("Samsung Platform Identifier");
272         platformInfo.setManufacturerName("Samsung");
273         platformInfo.setManufacturerUrl("www.samsung.com");
274         platformInfo.setModelNumber("Samsung Model Num01");
275         platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
276         platformInfo.setPlatformVersion("PlatformVersion01");
277         platformInfo.setOperationSystemVersion("OSVersion01");
278         platformInfo.setHardwareVersion("HardwareVersion01");
279         platformInfo.setFirmwareVersion("FirwareVersion01");
280         platformInfo.setSupportUrl("http://www.samsung.com/support");
281         platformInfo.setSystemTime("2015-09-10T11:10:30Z");
282         try {
283             SimulatorManager.setPlatformInfo(platformInfo);
284         } catch (SimulatorException e) {
285             Activator
286                     .getDefault()
287                     .getLogManager()
288                     .log(Level.ERROR.ordinal(),
289                             new Date(),
290                             "Error while registering the platform info.\n"
291                                     + Utility.getSimulatorErrorString(e, null));
292         }
293
294         // Get the device information to show other details of the device in UI.
295         try {
296             SimulatorManager.findDevices("", deviceListener);
297         } catch (SimulatorException e) {
298             Activator
299                     .getDefault()
300                     .getLogManager()
301                     .log(Level.ERROR.ordinal(),
302                             new Date(),
303                             "Failed to get the local device information.\n"
304                                     + Utility.getSimulatorErrorString(e, null));
305         }
306     }
307
308     private static class NotificationSynchronizerThread implements Runnable {
309
310         LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
311
312         @Override
313         public void run() {
314             while (!Thread.interrupted()) {
315                 synchronized (this) {
316                     try {
317                         while (notificationQueue.isEmpty()) {
318                             this.wait();
319                             break;
320                         }
321                     } catch (InterruptedException e) {
322                         return;
323                     }
324                 }
325
326                 Runnable thread;
327                 synchronized (this) {
328                     thread = notificationQueue.pop();
329                 }
330                 try {
331                     thread.run();
332                 } catch (Exception e) {
333                     if (e instanceof InterruptedException) {
334                         return;
335                     }
336                     e.printStackTrace();
337                 }
338             }
339         }
340
341         public void addToQueue(Runnable event) {
342             synchronized (this) {
343                 notificationQueue.add(event);
344                 this.notify();
345             }
346         }
347     }
348
349     public void setDeviceInfo(List<MetaProperty> metaProperties) {
350         if (null == metaProperties || metaProperties.size() < 1) {
351             return;
352         }
353         Iterator<MetaProperty> itr = metaProperties.iterator();
354         MetaProperty prop;
355         String propName;
356         String propValue;
357         boolean found = false;
358         while (itr.hasNext()) {
359             prop = itr.next();
360             propName = prop.getPropName();
361             propValue = prop.getPropValue();
362             if (propName.equals(Constants.DEVICE_NAME)) {
363                 this.deviceName = propValue;
364                 found = true;
365                 break;
366             }
367         }
368
369         if (!found) {
370             return;
371         }
372
373         try {
374             SimulatorManager.setDeviceInfo(deviceName);
375         } catch (SimulatorException e) {
376             Activator
377                     .getDefault()
378                     .getLogManager()
379                     .log(Level.ERROR.ordinal(),
380                             new Date(),
381                             "Error while registering the device info.\n"
382                                     + Utility.getSimulatorErrorString(e, null));
383         }
384     }
385
386     public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
387         if (null == metaProperties || metaProperties.size() < 1) {
388             return false;
389         }
390
391         Iterator<MetaProperty> itr = metaProperties.iterator();
392         MetaProperty prop;
393         String propName;
394         String propValue;
395         while (itr.hasNext()) {
396             prop = itr.next();
397             propName = prop.getPropName();
398             propValue = prop.getPropValue();
399             if (propName.equals(Constants.DEVICE_NAME)) {
400                 if (null == propValue || propValue.length() < 1) {
401                     return false;
402                 }
403                 break;
404             }
405         }
406         return true;
407     }
408
409     public List<MetaProperty> getDeviceInfo() {
410         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
411         metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
412         if (null != deviceInfo) {
413             metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
414                     .getID()));
415             metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
416                     deviceInfo.getSpecVersion()));
417             metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
418                     deviceInfo.getDataModelVersion()));
419         }
420         return metaProperties;
421     }
422
423     public List<MetaProperty> getPlatformInfo() {
424         List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
425         metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
426                 .getPlatformID()));
427         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
428                 platformInfo.getManufacturerName()));
429         metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
430                 platformInfo.getManufacturerUrl()));
431         metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
432                 platformInfo.getModelNumber()));
433         metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
434                 platformInfo.getDateOfManufacture()));
435         metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
436                 platformInfo.getPlatformVersion()));
437         metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
438                 platformInfo.getOperationSystemVersion()));
439         metaProperties.add(new MetaProperty(
440                 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
441                         .getHardwareVersion()));
442         metaProperties.add(new MetaProperty(
443                 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
444                         .getFirmwareVersion()));
445         metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
446                 platformInfo.getSupportUrl()));
447         metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
448                 platformInfo.getSystemTime()));
449         return metaProperties;
450     }
451
452     public void setPlatformInfo(List<MetaProperty> metaProperties) {
453         if (null == metaProperties || metaProperties.size() < 1) {
454             return;
455         }
456         Iterator<MetaProperty> itr = metaProperties.iterator();
457         MetaProperty prop;
458         String propName;
459         String propValue;
460         while (itr.hasNext()) {
461             prop = itr.next();
462             propName = prop.getPropName();
463             propValue = prop.getPropValue();
464             if (propName.equals(Constants.PLATFORM_ID)) {
465                 platformInfo.setPlatformID(propValue);
466             } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
467                 platformInfo.setManufacturerName(propValue);
468             } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
469                 platformInfo.setManufacturerUrl(propValue);
470             } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
471                 platformInfo.setModelNumber(propValue);
472             } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
473                 platformInfo.setDateOfManufacture(propValue);
474             } else if (propName.equals(Constants.PLATFORM_VERSION)) {
475                 platformInfo.setPlatformVersion(propValue);
476             } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
477                 platformInfo.setOperationSystemVersion(propValue);
478             } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
479                 platformInfo.setHardwareVersion(propValue);
480             } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
481                 platformInfo.setFirmwareVersion(propValue);
482             } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
483                 platformInfo.setSupportUrl(propValue);
484             } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
485                 platformInfo.setSystemTime(propValue);
486             }
487         }
488         try {
489             SimulatorManager.setPlatformInfo(platformInfo);
490         } catch (SimulatorException e) {
491             Activator
492                     .getDefault()
493                     .getLogManager()
494                     .log(Level.ERROR.ordinal(),
495                             new Date(),
496                             "Error while registering the platform info.\n"
497                                     + Utility.getSimulatorErrorString(e, null));
498         }
499     }
500
501     public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
502         if (null == metaProperties || metaProperties.size() < 1) {
503             return false;
504         }
505         Iterator<MetaProperty> itr = metaProperties.iterator();
506         MetaProperty prop;
507         String propValue;
508         while (itr.hasNext()) {
509             prop = itr.next();
510             propValue = prop.getPropValue();
511             if (null == propValue || propValue.length() < 1) {
512                 return false;
513             }
514         }
515         return true;
516     }
517
518     public synchronized Resource getCurrentResourceInSelection() {
519         return currentResourceInSelection;
520     }
521
522     public synchronized void setCurrentResourceInSelection(Resource resource) {
523         this.currentResourceInSelection = resource;
524     }
525
526     public boolean isResourceExist(String resourceURI) {
527         return data.isResourceExist(resourceURI);
528     }
529
530     public boolean isAnyResourceExist() {
531         return data.isAnyResourceExist();
532     }
533
534     public boolean createSingleResource(SingleResource resource,
535             Map<String, SimulatorResourceAttribute> attributes)
536             throws SimulatorException {
537         if (null == resource) {
538             return false;
539         }
540
541         try {
542             // Create the resource.
543             SimulatorResource jSimulatorResource = SimulatorManager
544                     .createResource(SimulatorResource.Type.SINGLE,
545                             resource.getResourceName(),
546                             resource.getResourceURI(),
547                             resource.getResourceType());
548             if (null == jSimulatorResource
549                     || !(jSimulatorResource instanceof SimulatorSingleResource)) {
550                 return false;
551             }
552             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
553             resource.setSimulatorResource(jSimulatorSingleResource);
554
555             // Cancel discoverable property if requested by user.
556             if (!resource.isDiscoverable()) {
557                 jSimulatorSingleResource.setDiscoverable(false);
558             }
559
560             // Cancel observable property if requested by user.
561             if (!resource.isObservable()) {
562                 jSimulatorSingleResource.setObservable(false);
563             }
564
565             // Set the model change listener.
566             jSimulatorSingleResource
567                     .setResourceModelChangeListener(resourceModelChangeListener);
568
569             // Set the observer listener if the resource is observable.
570             if (resource.isObservable()) {
571                 jSimulatorSingleResource.setObserverListener(observer);
572             }
573
574             // Add attributes.
575             if (null != attributes && !attributes.isEmpty()) {
576                 SimulatorResourceAttribute value;
577                 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
578                         .entrySet()) {
579                     value = entry.getValue();
580                     if (null != value)
581                         jSimulatorSingleResource.addAttribute(value);
582                 }
583
584                 // Get the resource model java object reference.
585                 resource.setResourceModel(jSimulatorSingleResource
586                         .getResourceModel());
587
588                 resource.createResourceRepresentation(jSimulatorSingleResource
589                         .getAttributes());
590             }
591
592             // Set the resource interfaces.
593             Set<String> interfaces = resource.getResourceInterfaces();
594             if (null != interfaces && !interfaces.isEmpty()) {
595                 jSimulatorSingleResource.setInterface(Utility
596                         .convertSetToVectorString(resource
597                                 .getResourceInterfaces()));
598             }
599
600             // Register the resource with the platform.
601             jSimulatorSingleResource.start();
602
603             // Read the interfaces from the native layer and set it to the
604             // interface list.
605             resource.setResourceInterfaces(Utility
606                     .convertVectorToSet(jSimulatorSingleResource.getInterface()));
607
608             resource.setStarted(true);
609         } catch (SimulatorException e) {
610             Activator
611                     .getDefault()
612                     .getLogManager()
613                     .log(Level.ERROR.ordinal(), new Date(),
614                             Utility.getSimulatorErrorString(e, null));
615             throw e;
616         }
617
618         // Add to local cache.
619         data.addResource(resource);
620
621         // Update UI listeners
622         UiListenerHandler.getInstance().resourceCreatedUINotification(
623                 ResourceType.SINGLE);
624
625         Activator
626                 .getDefault()
627                 .getLogManager()
628                 .log(Level.INFO.ordinal(), new Date(),
629                         "Resource created [" + resource.getResourceURI() + "].");
630
631         return true;
632     }
633
634     public Resource createResourceByRAML(String configFilePath)
635             throws SimulatorException {
636         SimulatorResource jSimulatorResource;
637         try {
638             // Create the resource
639             jSimulatorResource = SimulatorManager
640                     .createResource(configFilePath);
641             if (null == jSimulatorResource) {
642                 return null;
643             }
644         } catch (SimulatorException e) {
645             Activator
646                     .getDefault()
647                     .getLogManager()
648                     .log(Level.ERROR.ordinal(), new Date(),
649                             Utility.getSimulatorErrorString(e, null));
650             throw e;
651         }
652
653         if (!(jSimulatorResource instanceof SimulatorSingleResource)) {
654             return null;
655         }
656
657         Resource resource = new SingleResource();
658         resource.setSimulatorResource(jSimulatorResource);
659
660         // Fetch and locally store the resource name and uri.
661         String uri = jSimulatorResource.getURI();
662         if (null == uri || uri.trim().isEmpty()) {
663             return null;
664         }
665         resource.setResourceURI(uri.trim());
666
667         String name = jSimulatorResource.getName();
668         if (null == name || name.trim().isEmpty()) {
669             return null;
670         }
671
672         resource.setResourceName(name.trim());
673
674         return resource;
675     }
676
677     /**
678      * This method can set/change the resource uri and name of an already
679      * created resource which is not yet registered with the platform. This
680      * method registers the model change and observer listeners, registers the
681      * resource, fetches the resource attributes, updates the local cache and
682      * notifies the UI listeners.
683      */
684     public boolean completeSingleResourceCreationByRAML(Resource resource,
685             String uri, String name, boolean multiInstance)
686             throws SimulatorException {
687         if (null == resource || !(resource instanceof SingleResource)) {
688             return false;
689         }
690         try {
691             SingleResource singleRes = (SingleResource) resource;
692
693             SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
694                     .getSimulatorResource();
695             if (null == jSimulatorSingleResource) {
696                 return false;
697             }
698
699             // Update resource URI and Name if they are changed.
700             String newUri = uri.trim();
701             String newName = name.trim();
702
703             if (multiInstance) {
704                 singleRes.setResourceURI(newUri);
705                 singleRes.setResourceName(newName);
706             } else {
707                 if (!singleRes.getResourceURI().equals(newUri)) {
708                     jSimulatorSingleResource.setURI(newUri);
709                     singleRes.setResourceURI(newUri);
710                 }
711                 if (!singleRes.getResourceName().equals(newName)) {
712                     jSimulatorSingleResource.setName(newName);
713                     singleRes.setResourceName(newName);
714                 }
715             }
716
717             // Set the model change listener.
718             jSimulatorSingleResource
719                     .setResourceModelChangeListener(resourceModelChangeListener);
720
721             // Set the observer listener if the resource is observable.
722             if (jSimulatorSingleResource.isObservable()) {
723                 jSimulatorSingleResource.setObserverListener(observer);
724                 singleRes.setObservable(true);
725             }
726
727             // Fetch the resource model.
728             SimulatorResourceModel jResModel = jSimulatorSingleResource
729                     .getResourceModel();
730             if (null == jResModel) {
731                 return false;
732             }
733             singleRes.setResourceModel(jResModel);
734
735             // Fetch the basic details of the resource.
736             singleRes.setResourceType(jSimulatorSingleResource
737                     .getResourceType());
738             singleRes
739                     .setResourceInterfaces(Utility
740                             .convertVectorToSet(jSimulatorSingleResource
741                                     .getInterface()));
742
743             // Fetch the resource attributes.
744             singleRes.createResourceRepresentation(jSimulatorSingleResource
745                     .getAttributes());
746
747             // Register the resource with the platform.
748             jSimulatorSingleResource.start();
749             singleRes.setStarted(true);
750
751             // Add to local cache.
752             data.addResource(singleRes);
753
754             // Update UI listeners for single instance creation
755             if (!multiInstance)
756                 UiListenerHandler.getInstance().resourceCreatedUINotification(
757                         ResourceType.SINGLE);
758         } catch (Exception e) {
759             Activator
760                     .getDefault()
761                     .getLogManager()
762                     .log(Level.ERROR.ordinal(), new Date(),
763                             Utility.getSimulatorErrorString(e, null));
764             throw e;
765         }
766
767         Activator
768                 .getDefault()
769                 .getLogManager()
770                 .log(Level.INFO.ordinal(), new Date(),
771                         "Resource created [" + resource.getResourceURI() + "].");
772         return true;
773     }
774
775     public Set<SingleResource> createSingleResourceMultiInstances(
776             String configFile, int count, IProgressMonitor progressMonitor)
777             throws SimulatorException {
778         Set<SingleResource> resultSet;
779         try {
780             resultSet = new HashSet<SingleResource>();
781             Vector<SimulatorResource> jSimulatorResources = SimulatorManager
782                     .createResource(configFile, count);
783             if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
784                 return null;
785             }
786             SimulatorSingleResource jResource;
787             SingleResource resource;
788             boolean result;
789             for (SimulatorResource jSimulatorResource : jSimulatorResources) {
790                 // If the resource creation progress is canceled, then stop the
791                 // creation and stop/delete
792                 // the resources created already.
793                 if (progressMonitor.isCanceled()) {
794                     removeSingleResources(resultSet);
795                     return null;
796                 }
797                 jResource = (SimulatorSingleResource) jSimulatorResource;
798                 resource = new SingleResource();
799                 resource.setSimulatorResource(jResource);
800                 try {
801                     result = completeSingleResourceCreationByRAML(resource,
802                             jResource.getURI(), jResource.getName(), true);
803                     if (result) {
804                         resultSet.add(resource);
805                     }
806                 } catch (SimulatorException eInner) {
807                     Activator
808                             .getDefault()
809                             .getLogManager()
810                             .log(Level.ERROR.ordinal(),
811                                     new Date(),
812                                     Utility.getSimulatorErrorString(eInner,
813                                             null));
814                 }
815                 progressMonitor.worked(1);
816             }
817         } catch (SimulatorException eOuter) {
818             Activator
819                     .getDefault()
820                     .getLogManager()
821                     .log(Level.ERROR.ordinal(), new Date(),
822                             Utility.getSimulatorErrorString(eOuter, null));
823             throw eOuter;
824         }
825         return resultSet;
826     }
827
828     public List<Resource> getResourceList() {
829         List<Resource> resourceList = data.getResources();
830         if (null == resourceList) {
831             return null;
832         }
833         // Sort the list
834         Collections.sort(resourceList, Utility.resourceComparator);
835
836         return resourceList;
837     }
838
839     public List<SingleResource> getSingleResourceList() {
840         List<SingleResource> resourceList = data.getSingleResources();
841         if (null == resourceList) {
842             return null;
843         }
844         // Sort the list
845         Collections.sort(resourceList, Utility.singleResourceComparator);
846
847         return resourceList;
848     }
849
850     public void removeSingleResources(Set<SingleResource> resources)
851             throws SimulatorException {
852         if (null == resources) {
853             return;
854         }
855         Iterator<SingleResource> itr = resources.iterator();
856         while (itr.hasNext()) {
857             removeResource(itr.next());
858         }
859     }
860
861     public void removeResource(Resource res) throws SimulatorException {
862         // Unregister the resource from the platform.
863         SimulatorResource simRes = res.getSimulatorResource();
864         try {
865             simRes.stop();
866         } catch (SimulatorException e) {
867             Activator
868                     .getDefault()
869                     .getLogManager()
870                     .log(Level.ERROR.ordinal(), new Date(),
871                             Utility.getSimulatorErrorString(e, null));
872             throw e;
873         }
874
875         // Delete this resource
876         data.deleteResource(res);
877
878         Activator
879                 .getDefault()
880                 .getLogManager()
881                 .log(Level.INFO.ordinal(), new Date(),
882                         "Resource deleted [" + res.getResourceURI() + "].");
883     }
884
885     public boolean isUriUnique(List<MetaProperty> properties) {
886         if (null == properties) {
887             return false;
888         }
889         MetaProperty prop;
890         Iterator<MetaProperty> itr = properties.iterator();
891         while (itr.hasNext()) {
892             prop = itr.next();
893             if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
894                 String uri = prop.getPropValue();
895                 return !data.isResourceExist(uri);
896             }
897         }
898         return false;
899     }
900
901     public void resourceSelectionChanged(final Resource selectedResource) {
902         new Thread() {
903             @Override
904             public void run() {
905                 if (null != selectedResource) {
906                     setCurrentResourceInSelection(selectedResource);
907                 } else {
908                     setCurrentResourceInSelection(null);
909                 }
910                 // Notify all observers for resource selection change event
911                 UiListenerHandler.getInstance()
912                         .resourceSelectionChangedUINotification(
913                                 selectedResource);
914             }
915         }.start();
916     }
917
918     public List<MetaProperty> getMetaProperties(Resource resource) {
919         if (null != resource) {
920             String propName;
921             StringBuilder propValue;
922             List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
923
924             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
925                 propName = Constants.META_PROPERTIES[index];
926                 propValue = new StringBuilder();
927                 if (propName.equals(Constants.RESOURCE_NAME)) {
928                     propValue.append(resource.getResourceName());
929                 } else if (propName.equals(Constants.RESOURCE_URI)) {
930                     propValue.append(resource.getResourceURI());
931                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
932                     propValue.append(resource.getResourceType());
933                 } else if (propName.equals(Constants.INTERFACE_TYPES)) {
934                     Set<String> ifTypes = resource.getResourceInterfaces();
935                     if (null != ifTypes && !ifTypes.isEmpty()) {
936                         Iterator<String> itr = ifTypes.iterator();
937                         while (itr.hasNext()) {
938                             propValue.append(itr.next());
939                             if (itr.hasNext()) {
940                                 propValue.append(", ");
941                             }
942                         }
943                     } else {
944                         propValue = null;
945                     }
946                 } else {
947                     propValue = null;
948                 }
949                 if (null != propValue) {
950                     metaPropertyList.add(new MetaProperty(propName, propValue
951                             .toString()));
952                 }
953             }
954             return metaPropertyList;
955         }
956         return null;
957     }
958
959     public boolean startResource(Resource resource) throws SimulatorException {
960         if (null == resource) {
961             return false;
962         }
963         SimulatorResource server = resource.getSimulatorResource();
964         if (null == server) {
965             return false;
966         }
967         try {
968             server.start();
969             resource.setStarted(true);
970         } catch (SimulatorException e) {
971             Activator
972                     .getDefault()
973                     .getLogManager()
974                     .log(Level.ERROR.ordinal(),
975                             new Date(),
976                             "There is an error while starting the resource.\n"
977                                     + Utility.getSimulatorErrorString(e, null));
978             throw e;
979         }
980         return true;
981     }
982
983     public boolean stopResource(Resource resource) throws SimulatorException {
984         if (null == resource) {
985             return false;
986         }
987         SimulatorResource server = resource.getSimulatorResource();
988         if (null == server) {
989             return false;
990         }
991         try {
992             server.stop();
993             resource.setStarted(false);
994         } catch (SimulatorException e) {
995             Activator
996                     .getDefault()
997                     .getLogManager()
998                     .log(Level.ERROR.ordinal(),
999                             new Date(),
1000                             "There is an error while stopping the resource.\n"
1001                                     + Utility.getSimulatorErrorString(e, null));
1002             throw e;
1003         }
1004         return true;
1005     }
1006
1007     public boolean changeResourceName(Resource resource, String newName)
1008             throws SimulatorException {
1009         if (null == resource || null == newName) {
1010             return false;
1011         }
1012
1013         if (!stopResource(resource)) {
1014             return false;
1015         }
1016
1017         SimulatorResource server = resource.getSimulatorResource();
1018         try {
1019             server.setName(newName);
1020             resource.setResourceName(newName);
1021         } catch (SimulatorException e) {
1022             Activator
1023                     .getDefault()
1024                     .getLogManager()
1025                     .log(Level.ERROR.ordinal(),
1026                             new Date(),
1027                             "There is an error while changing the resource name.\n"
1028                                     + Utility.getSimulatorErrorString(e, null));
1029             throw e;
1030         }
1031
1032         if (!startResource(resource)) {
1033             return false;
1034         }
1035
1036         return true;
1037     }
1038
1039     public boolean changeResourceURI(Resource resource, String newURI)
1040             throws SimulatorException {
1041         if (null == resource || null == newURI) {
1042             return false;
1043         }
1044
1045         if (!stopResource(resource)) {
1046             return false;
1047         }
1048
1049         String curURI = resource.getResourceURI();
1050         setResourceURI(resource, newURI);
1051
1052         try {
1053             if (!startResource(resource)) {
1054                 return false;
1055             }
1056         } catch (SimulatorException e) {
1057             setResourceURI(resource, curURI);
1058         }
1059
1060         return true;
1061     }
1062
1063     public boolean changeResourceType(Resource resource, String newResourceType)
1064             throws SimulatorException {
1065         if (null == resource || null == newResourceType) {
1066             return false;
1067         }
1068
1069         if (!stopResource(resource)) {
1070             return false;
1071         }
1072
1073         String curResourceType = resource.getResourceType();
1074         setResourceType(resource, newResourceType);
1075
1076         try {
1077             if (!startResource(resource)) {
1078                 return false;
1079             }
1080         } catch (SimulatorException e) {
1081             setResourceType(resource, curResourceType);
1082         }
1083
1084         return true;
1085     }
1086
1087     public void setResourceURI(Resource resource, String newURI)
1088             throws SimulatorException {
1089         String curURI = resource.getResourceURI();
1090         SimulatorResource server = resource.getSimulatorResource();
1091         try {
1092             server.setURI(newURI);
1093             data.changeResourceURI(resource, curURI, newURI);
1094         } catch (SimulatorException e) {
1095             Activator
1096                     .getDefault()
1097                     .getLogManager()
1098                     .log(Level.ERROR.ordinal(),
1099                             new Date(),
1100                             "There is an error while changing the resource URI.\n"
1101                                     + Utility.getSimulatorErrorString(e, null));
1102             throw e;
1103         }
1104     }
1105
1106     public void setResourceType(Resource resource, String newResourceType)
1107             throws SimulatorException {
1108         SimulatorResource server = resource.getSimulatorResource();
1109         try {
1110             server.setResourceType(newResourceType);
1111             resource.setResourceType(newResourceType);
1112         } catch (SimulatorException e) {
1113             Activator
1114                     .getDefault()
1115                     .getLogManager()
1116                     .log(Level.ERROR.ordinal(),
1117                             new Date(),
1118                             "There is an error while changing the resource Type.\n"
1119                                     + Utility.getSimulatorErrorString(e, null));
1120             throw e;
1121         }
1122     }
1123
1124     public boolean updateResourceProperties(Resource resource,
1125             List<MetaProperty> properties, boolean uriChanged,
1126             boolean nameChanged, boolean resTypeChanged)
1127             throws SimulatorException {
1128         if (null == resource || null == properties) {
1129             return false;
1130         }
1131
1132         // Updating the properties
1133         Iterator<MetaProperty> itr = properties.iterator();
1134         MetaProperty property;
1135         String propName;
1136         String propValue;
1137         String resName = null;
1138         String resURI = null;
1139         String resType = null;
1140         while (itr.hasNext()) {
1141             property = itr.next();
1142             if (null == property) {
1143                 continue;
1144             }
1145             propName = property.getPropName();
1146             propValue = property.getPropValue();
1147             if (propName.equals(Constants.RESOURCE_NAME)) {
1148                 resName = propValue;
1149             } else if (propName.equals(Constants.RESOURCE_URI)) {
1150                 resURI = propValue;
1151             } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1152                 resType = propValue;
1153             }
1154         }
1155
1156         if (nameChanged) {
1157             if (!changeResourceName(resource, resName)) {
1158                 return false;
1159             }
1160
1161             // Notify UI Listeners
1162             UiListenerHandler.getInstance().propertiesChangedUINotification(
1163                     Resource.class);
1164         }
1165
1166         if (uriChanged) {
1167             if (!changeResourceURI(resource, resURI)) {
1168                 return false;
1169             }
1170         }
1171
1172         if (resTypeChanged) {
1173             if (!changeResourceType(resource, resType)) {
1174                 return false;
1175             }
1176         }
1177
1178         return true;
1179     }
1180
1181     public boolean updateResourceInterfaces(Resource resource,
1182             Set<String> newIfSet) throws SimulatorException {
1183         if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1184             return false;
1185         }
1186
1187         SimulatorResource jResource = resource.getSimulatorResource();
1188         if (null == jResource) {
1189             return false;
1190         }
1191
1192         Set<String> curIfSet = resource.getResourceInterfaces();
1193         if (null == curIfSet || curIfSet.isEmpty()) {
1194             return false;
1195         }
1196
1197         Iterator<String> itr = curIfSet.iterator();
1198         String interfaceType;
1199         boolean resourceRestartRequired = false;
1200         while (itr.hasNext()) {
1201             interfaceType = itr.next();
1202             if (!newIfSet.contains(interfaceType)) {
1203                 resourceRestartRequired = true;
1204                 break;
1205             }
1206         }
1207
1208         try {
1209             // As there is no support from native layer for interface removal,
1210             // supporting it from the simulator requires restarting the resource
1211             // if any existing interfaces are removed.
1212
1213             if (resourceRestartRequired) {
1214                 stopResource(resource);
1215                 jResource.setInterface(Utility
1216                         .convertSetToVectorString(newIfSet));
1217                 startResource(resource);
1218             } else {
1219                 // Existing interfaces are not removed.
1220                 itr = newIfSet.iterator();
1221                 while (itr.hasNext()) {
1222                     interfaceType = itr.next();
1223                     if (!curIfSet.contains(interfaceType)) {
1224                         jResource.addInterface(interfaceType);
1225                     }
1226                 }
1227             }
1228         } catch (SimulatorException e) {
1229             Activator
1230                     .getDefault()
1231                     .getLogManager()
1232                     .log(Level.ERROR.ordinal(),
1233                             new Date(),
1234                             "There is an error while changing the interface types."
1235                                     + Utility.getSimulatorErrorString(e, null));
1236             throw e;
1237         }
1238
1239         // Set the resource interfaces.
1240         resource.setResourceInterfaces(newIfSet);
1241
1242         return true;
1243     }
1244
1245     public boolean attributeValueUpdated(SingleResource resource,
1246             String attributeName, AttributeValue value) {
1247         if (null != resource && null != attributeName && null != value) {
1248             SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1249                     .getSimulatorResource();
1250             if (null != simRes) {
1251                 try {
1252                     simRes.updateAttribute(attributeName, value);
1253                     return true;
1254                 } catch (SimulatorException e) {
1255                     Activator
1256                             .getDefault()
1257                             .getLogManager()
1258                             .log(Level.ERROR.ordinal(), new Date(),
1259                                     Utility.getSimulatorErrorString(e, null));
1260                 }
1261             }
1262         }
1263         return false;
1264     }
1265
1266     public boolean isResourceStarted(Resource resource) {
1267         if (null == resource) {
1268             return false;
1269         }
1270         return resource.isStarted();
1271     }
1272
1273     public boolean isPropertyValueInvalid(Resource resource,
1274             List<MetaProperty> properties, String propName) {
1275         if (null == resource || null == properties || null == propName) {
1276             return false;
1277         }
1278         boolean invalid = false;
1279         MetaProperty prop;
1280         Iterator<MetaProperty> itr = properties.iterator();
1281         while (itr.hasNext()) {
1282             prop = itr.next();
1283             if (prop.getPropName().equals(propName)) {
1284                 String value = prop.getPropValue();
1285                 if (propName.equals(Constants.RESOURCE_URI)) {
1286                     if (!Utility.isUriValid(value)) {
1287                         invalid = true;
1288                     }
1289                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1290                     if (!Utility.isResourceTypeValid(value)) {
1291                         invalid = true;
1292                     }
1293                 } else {
1294                     if (null == value || value.trim().isEmpty()) {
1295                         invalid = true;
1296                     }
1297                 }
1298             }
1299         }
1300         return invalid;
1301     }
1302
1303     public boolean isPropValueChanged(Resource resource,
1304             List<MetaProperty> properties, String propName) {
1305         if (null == resource || null == properties || null == propName) {
1306             return false;
1307         }
1308         boolean changed = false;
1309         MetaProperty prop;
1310         String oldValue;
1311         Iterator<MetaProperty> itr = properties.iterator();
1312         while (itr.hasNext()) {
1313             prop = itr.next();
1314             if (prop.getPropName().equals(propName)) {
1315                 oldValue = getPropertyValueFromResource(resource, propName);
1316                 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1317                     changed = true;
1318                 }
1319                 break;
1320             }
1321         }
1322         return changed;
1323     }
1324
1325     private String getPropertyValueFromResource(Resource resource,
1326             String propName) {
1327         if (null == resource || null == propName) {
1328             return null;
1329         }
1330         if (propName.equals(Constants.RESOURCE_URI)) {
1331             return resource.getResourceURI();
1332         } else if (propName.equals(Constants.RESOURCE_NAME)) {
1333             return resource.getResourceName();
1334         } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1335             return resource.getResourceType();
1336         } else {
1337             return null;
1338         }
1339     }
1340
1341     public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1342         if (null == att) {
1343             return false;
1344         }
1345         AttributeProperty prop = att.property();
1346         if (null == prop) {
1347             return false;
1348         }
1349
1350         if (prop.getType() == Type.INTEGER) {
1351             IntegerProperty intProperty = prop.asInteger();
1352             if (null != intProperty) {
1353                 return (intProperty.hasRange() || intProperty.hasValues());
1354             } else {
1355                 return false;
1356             }
1357         } else if (prop.getType() == Type.DOUBLE) {
1358             DoubleProperty dblProperty = prop.asDouble();
1359             if (null != dblProperty) {
1360                 return (dblProperty.hasRange() || dblProperty.hasValues());
1361             } else {
1362                 return false;
1363             }
1364         } else if (prop.getType() == Type.STRING) {
1365             StringProperty stringProperty = prop.asString();
1366             if (null != stringProperty) {
1367                 return stringProperty.hasValues();
1368             } else {
1369                 return false;
1370             }
1371         }
1372
1373         return true;
1374     }
1375
1376     public int startAutomation(SingleResource resource,
1377             AttributeElement attribute, AutoUpdateType autoType,
1378             int autoUpdateInterval) {
1379         int autoId = -1;
1380         if (null != resource && null != attribute) {
1381             SimulatorSingleResource server = (SimulatorSingleResource) resource
1382                     .getSimulatorResource();
1383             if (null != server) {
1384                 String attrName = attribute.getSimulatorResourceAttribute()
1385                         .name();
1386                 try {
1387                     autoId = server.startAttributeUpdation(attrName, autoType,
1388                             autoUpdateInterval, automationListener);
1389                 } catch (SimulatorException e) {
1390                     Activator
1391                             .getDefault()
1392                             .getLogManager()
1393                             .log(Level.ERROR.ordinal(),
1394                                     new Date(),
1395                                     "[" + e.getClass().getSimpleName() + "]"
1396                                             + e.code().toString() + "-"
1397                                             + e.message());
1398                     return -1;
1399                 }
1400                 if (-1 != autoId) {
1401                     attribute.setAutoUpdateId(autoId);
1402                     attribute.setAutoUpdateType(autoType);
1403                     attribute.setAutoUpdateInterval(autoUpdateInterval);
1404                     attribute.setAutoUpdateState(true);
1405                     resource.setAttributeAutomationInProgress(true);
1406                 }
1407             }
1408         }
1409         return autoId;
1410     }
1411
1412     public void stopAutomation(SingleResource resource, AttributeElement att,
1413             int autoId) {
1414         if (null != resource) {
1415             SimulatorSingleResource server = (SimulatorSingleResource) resource
1416                     .getSimulatorResource();
1417             if (null != server) {
1418                 try {
1419                     server.stopUpdation(autoId);
1420                 } catch (SimulatorException e) {
1421                     Activator
1422                             .getDefault()
1423                             .getLogManager()
1424                             .log(Level.ERROR.ordinal(),
1425                                     new Date(),
1426                                     "[" + e.getClass().getSimpleName() + "]"
1427                                             + e.code().toString() + "-"
1428                                             + e.message());
1429                     return;
1430                 }
1431                 // Change the automation status
1432                 att.setAutoUpdateState(false);
1433                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1434             }
1435         }
1436     }
1437
1438     public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1439             int autoUpdateInterval, final SingleResource resource) {
1440         if (null == resource) {
1441             return false;
1442         }
1443         boolean status = false;
1444         // Invoke the native automation method
1445         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1446                 .getSimulatorResource();
1447         if (null != resourceServer) {
1448             int autoId = -1;
1449             try {
1450                 autoId = resourceServer.startResourceUpdation(autoType,
1451                         autoUpdateInterval, automationListener);
1452             } catch (SimulatorException e) {
1453                 Activator
1454                         .getDefault()
1455                         .getLogManager()
1456                         .log(Level.ERROR.ordinal(), new Date(),
1457                                 Utility.getSimulatorErrorString(e, null));
1458                 autoId = -1;
1459             }
1460             if (-1 != autoId) {
1461                 // Automation request accepted.
1462                 changeResourceLevelAutomationStatus(resource, true);
1463
1464                 resource.setAutomationId(autoId);
1465
1466                 // Notify the UI listeners in a different thread.
1467                 Thread notifyThread = new Thread() {
1468                     public void run() {
1469                         UiListenerHandler.getInstance()
1470                                 .resourceAutomationStartedUINotification(
1471                                         resource);
1472                     };
1473                 };
1474                 notifyThread.setPriority(Thread.MAX_PRIORITY);
1475                 notifyThread.start();
1476
1477                 status = true;
1478             }
1479         }
1480         return status;
1481     }
1482
1483     public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1484         if (null == resource) {
1485             return false;
1486         }
1487         final int autoId = resource.getAutomationId();
1488         if (-1 == autoId) {
1489             return false;
1490         }
1491         SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1492                 .getSimulatorResource();
1493         if (null == resourceServer) {
1494             return false;
1495         }
1496         // Call native method
1497         try {
1498             resourceServer.stopUpdation(autoId);
1499         } catch (SimulatorException e) {
1500             Activator
1501                     .getDefault()
1502                     .getLogManager()
1503                     .log(Level.ERROR.ordinal(), new Date(),
1504                             Utility.getSimulatorErrorString(e, null));
1505             return false;
1506         }
1507
1508         // Notify the UI Listeners. Invoke the automation complete callback.
1509         Thread stopThread = new Thread() {
1510             public void run() {
1511                 automationListener.onUpdateComplete(resource.getResourceURI(),
1512                         autoId);
1513             }
1514         };
1515         stopThread.start();
1516         return true;
1517     }
1518
1519     private boolean isAnyAttributeInAutomation(SingleResource resource) {
1520         if (null == resource || null == resource.getResourceRepresentation()) {
1521             return false;
1522         }
1523
1524         Map<String, AttributeElement> attributes = resource
1525                 .getResourceRepresentation().getAttributes();
1526         if (null == attributes || 0 == attributes.size())
1527             return false;
1528
1529         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1530             if (entry.getValue().isAutoUpdateInProgress())
1531                 return true;
1532         }
1533
1534         return false;
1535     }
1536
1537     // Changes the automation state of the resource and its attributes
1538     private void changeResourceLevelAutomationStatus(SingleResource resource,
1539             boolean status) {
1540
1541         if (null == resource || null == resource.getResourceRepresentation()) {
1542             return;
1543         }
1544
1545         Map<String, AttributeElement> attributes = resource
1546                 .getResourceRepresentation().getAttributes();
1547         if (null == attributes || 0 == attributes.size())
1548             return;
1549
1550         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1551             entry.getValue().setAutoUpdateState(status);
1552         }
1553
1554         resource.setResourceAutomationInProgress(status);
1555     }
1556
1557     private AttributeElement getAttributeWithGivenAutomationId(
1558             SingleResource resource, int automationId) {
1559         if (null == resource || null == resource.getResourceRepresentation()) {
1560             return null;
1561         }
1562
1563         Map<String, AttributeElement> attributes = resource
1564                 .getResourceRepresentation().getAttributes();
1565         if (null == attributes || 0 == attributes.size())
1566             return null;
1567
1568         for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1569             if (automationId == entry.getValue().getAutoUpdateId())
1570                 return entry.getValue();
1571         }
1572
1573         return null;
1574     }
1575
1576     public boolean isResourceAutomationStarted(SingleResource resource) {
1577         boolean status = false;
1578         if (null != resource) {
1579             status = resource.isResourceAutomationInProgress();
1580         }
1581         return status;
1582     }
1583
1584     public boolean isAttributeAutomationStarted(SingleResource resource) {
1585         if (null == resource) {
1586             return false;
1587         }
1588         return resource.isAttributeAutomationInProgress();
1589     }
1590
1591     public void notifyObserverRequest(Resource resource, int observerId) {
1592         if (null == resource) {
1593             return;
1594         }
1595         SimulatorResource simulatorResource = resource.getSimulatorResource();
1596         if (null == simulatorResource) {
1597             return;
1598         }
1599         try {
1600             simulatorResource.notifyObserver(observerId);
1601         } catch (SimulatorException e) {
1602             Activator
1603                     .getDefault()
1604                     .getLogManager()
1605                     .log(Level.ERROR.ordinal(), new Date(),
1606                             Utility.getSimulatorErrorString(e, null));
1607         }
1608     }
1609
1610     public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1611         if (null == att) {
1612             return null;
1613         }
1614
1615         AttributeValue value = att.value();
1616         if (null == value || null == value.get()) {
1617             return null;
1618         }
1619
1620         TypeInfo type = value.typeInfo();
1621
1622         if (type.mBaseType == ValueType.RESOURCEMODEL) {
1623             return null;
1624         }
1625
1626         List<String> values = new ArrayList<String>();
1627
1628         AttributeProperty prop = att.property();
1629         if (null == prop) {
1630             // Adding the current value of the attribute.
1631             values.add(new AttributeValueStringConverter(value).toString());
1632             return values;
1633         }
1634
1635         if (type.mType == ValueType.ARRAY) {
1636             if (type.mDepth == 1) {
1637                 ArrayProperty arrayProperty = prop.asArray();
1638                 if (null != arrayProperty) {
1639                     AttributeProperty childProp = arrayProperty
1640                             .getElementProperty();
1641                     switch (childProp.getType()) {
1642                         case INTEGER:
1643                             IntegerProperty intProperty = childProp.asInteger();
1644                             if (null != intProperty) {
1645                                 values.addAll(getAllValues(att, intProperty,
1646                                         Type.INTEGER));
1647                             }
1648                             break;
1649                         case DOUBLE:
1650                             DoubleProperty dblProperty = childProp.asDouble();
1651                             if (null != dblProperty) {
1652                                 values.addAll(getAllValues(att, dblProperty,
1653                                         Type.DOUBLE));
1654                             }
1655                             break;
1656                         case BOOLEAN:
1657                             BooleanProperty boolProperty = childProp
1658                                     .asBoolean();
1659                             if (null != boolProperty) {
1660                                 values.addAll(getAllValues(att, boolProperty,
1661                                         Type.BOOLEAN));
1662                             }
1663                             break;
1664                         case STRING:
1665                             StringProperty stringProperty = childProp
1666                                     .asString();
1667                             if (null != stringProperty) {
1668                                 values.addAll(getAllValues(att, stringProperty,
1669                                         Type.STRING));
1670                             }
1671                             break;
1672                         default:
1673                             break;
1674                     }
1675                 }
1676             }
1677         } else {
1678             switch (prop.getType()) {
1679                 case INTEGER:
1680                     IntegerProperty intProperty = prop.asInteger();
1681                     if (null != intProperty) {
1682                         values.addAll(getAllValues(att, intProperty,
1683                                 Type.INTEGER));
1684                     }
1685                     break;
1686                 case DOUBLE:
1687                     DoubleProperty dblProperty = prop.asDouble();
1688                     if (null != dblProperty) {
1689                         values.addAll(getAllValues(att, dblProperty,
1690                                 Type.DOUBLE));
1691                     }
1692                     break;
1693                 case BOOLEAN:
1694                     BooleanProperty boolProperty = prop.asBoolean();
1695                     if (null != boolProperty) {
1696                         values.addAll(getAllValues(att, boolProperty,
1697                                 Type.BOOLEAN));
1698                     }
1699                     break;
1700                 case STRING:
1701                     StringProperty stringProperty = prop.asString();
1702                     if (null != stringProperty) {
1703                         values.addAll(getAllValues(att, stringProperty,
1704                                 Type.STRING));
1705                     }
1706                     break;
1707                 default:
1708                     break;
1709             }
1710         }
1711
1712         return values;
1713     }
1714
1715     public List<String> getAllValues(SimulatorResourceAttribute attribute,
1716             IntegerProperty intProperty, AttributeProperty.Type type) {
1717         List<String> values = new ArrayList<String>();
1718
1719         if (intProperty.hasRange()) {
1720             int min = (int) intProperty.min();
1721             int max = (int) intProperty.max();
1722             for (int iVal = min; iVal <= max; iVal++) {
1723                 values.add(String.valueOf(iVal));
1724             }
1725         } else if (intProperty.hasValues()) {
1726             for (Integer val : intProperty.getValues()) {
1727                 values.add(String.valueOf(val));
1728             }
1729         } else {
1730             AttributeValue value = attribute.value();
1731             if (null != value && null != value.get()) {
1732                 // Adding the current value of the attribute.
1733                 values.add(String.valueOf(value.get()));
1734             }
1735         }
1736         return values;
1737     }
1738
1739     public List<String> getAllValues(SimulatorResourceAttribute attribute,
1740             DoubleProperty dblProperty, AttributeProperty.Type type) {
1741         NumberFormat formatter = NumberFormat.getInstance();
1742         List<String> values = new ArrayList<String>();
1743
1744         if (dblProperty.hasRange()) {
1745             double min = (double) dblProperty.min();
1746             double max = (double) dblProperty.max();
1747             for (double val = min; val <= max; val += 0.1) {
1748                 formatter.setMaximumFractionDigits(1);
1749                 formatter.setMinimumFractionDigits(1);
1750                 values.add(formatter.format(val));
1751             }
1752         } else if (dblProperty.hasValues()) {
1753             for (Double val : dblProperty.getValues()) {
1754                 values.add(String.valueOf(val));
1755             }
1756         } else {
1757             AttributeValue value = attribute.value();
1758             if (null != value && null != value.get()) {
1759                 // Adding the current value of the attribute.
1760                 values.add(String.valueOf(value.get()));
1761             }
1762         }
1763         return values;
1764     }
1765
1766     public List<String> getAllValues(SimulatorResourceAttribute attribute,
1767             BooleanProperty boolProperty, AttributeProperty.Type type) {
1768         List<String> values = new ArrayList<String>();
1769         values.add("true");
1770         values.add("false");
1771         return values;
1772     }
1773
1774     public List<String> getAllValues(SimulatorResourceAttribute attribute,
1775             StringProperty stringProperty, AttributeProperty.Type type) {
1776         List<String> values = new ArrayList<String>();
1777
1778         if (stringProperty.hasValues()) {
1779             for (String val : stringProperty.getValues()) {
1780                 values.add(String.valueOf(val));
1781             }
1782         } else {
1783             AttributeValue value = attribute.value();
1784             if (null != value && null != value.get()) {
1785                 // Adding the current value of the attribute.
1786                 values.add(String.valueOf(value.get()));
1787             }
1788         }
1789         return values;
1790     }
1791
1792     public int getResourceCount() {
1793         return data.getResourceCount();
1794     }
1795
1796     public void shutdown() {
1797         threadHandle.interrupt();
1798     }
1799 }