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