2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.manager;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.swt.widgets.Display;
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;
32 import java.util.Vector;
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;
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.Constants;
67 import oic.simulator.serviceprovider.utils.Utility;
70 * This class acts as an interface between the simulator java SDK and the
71 * various UI modules. It maintains all the details of resources and provides
72 * other UI modules with the information required. It also handles model change,
73 * automation, and observer related events from native layer and propagates
74 * those events to the registered UI listeners.
76 public class ResourceManager {
80 private Resource currentResourceInSelection;
82 private ResourceModelChangeListener resourceModelChangeListener;
84 private AutoUpdateListener automationListener;
86 private ObserverListener observer;
88 private DeviceListener deviceListener;
90 private NotificationSynchronizerThread synchronizerThread;
92 private Thread threadHandle;
94 private DeviceInfo deviceInfo;
95 private PlatformInfo platformInfo;
97 private String deviceName;
99 public ResourceManager() {
102 deviceListener = new DeviceListener() {
105 public void onDeviceFound(final String host,
106 final DeviceInfo deviceInfo) {
107 if (null != ResourceManager.this.deviceInfo
108 || null == deviceInfo || null == host) {
111 synchronizerThread.addToQueue(new Runnable() {
114 String rcvdDeviceName = deviceInfo.getName();
115 if (null == rcvdDeviceName) {
118 if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
119 ResourceManager.this.deviceInfo = deviceInfo;
121 // Notify the UI Listeners
122 UiListenerHandler.getInstance()
123 .deviceInfoReceivedNotification();
130 resourceModelChangeListener = new ResourceModelChangeListener() {
133 public void onResourceModelChanged(final String resourceURI,
134 final SimulatorResourceModel resourceModelN) {
135 synchronizerThread.addToQueue(new Runnable() {
139 if (null == resourceURI || null == resourceModelN) {
143 Display.getDefault().asyncExec(new Runnable() {
146 Resource resource = data
147 .getResourceByURI(resourceURI);
148 if (null != resource) {
150 resource.updateResourceRepresentation(resourceModelN);
151 } catch (NumberFormatException e) {
155 .log(Level.ERROR.ordinal(),
157 "Error while trying to update the attributes.\n"
159 .getSimulatorErrorString(
171 automationListener = new AutoUpdateListener() {
174 public void onUpdateComplete(final String resourceURI,
175 final int automationId) {
176 synchronizerThread.addToQueue(new Runnable() {
180 SingleResource resource = data
181 .getSingleResourceByURI(resourceURI);
182 if (null == resource) {
185 // Checking whether this notification is for an
186 // attribute or a resource
187 if (resource.isResourceAutomationInProgress()) {
188 changeResourceLevelAutomationStatus(resource, false);
189 // Notify the UI listeners
190 UiListenerHandler.getInstance()
191 .automationCompleteUINotification(resource,
193 } else if (resource.isAttributeAutomationInProgress()) {
194 // Find the attribute with the given automation id
195 final AttributeElement attribute = getAttributeWithGivenAutomationId(
196 resource, automationId);
197 if (null != attribute) {
198 attribute.setAutoUpdateState(false);
199 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
201 // Setting the attribute automation status to
203 resource.setAttributeAutomationInProgress(false);
211 observer = new ObserverListener() {
213 public void onObserverChanged(final String resourceURI,
214 final int status, final Observer observer) {
218 if (null == resourceURI || null == observer) {
221 Resource resource = data.getResourceByURI(resourceURI);
222 if (null == resource) {
225 // Update the observers information
227 resource.addObserverInfo(observer);
229 resource.removeObserverInfo(observer);
231 // Notify the UI listeners
232 UiListenerHandler.getInstance()
233 .observerListChangedUINotification(resource);
239 public void onObserverAdded(String resourceURI, Observer observer) {
240 onObserverChanged(resourceURI, 0, observer);
244 public void onObserverRemoved(String resourceURI, Observer observer) {
245 onObserverChanged(resourceURI, 1, observer);
249 synchronizerThread = new NotificationSynchronizerThread();
250 threadHandle = new Thread(synchronizerThread);
251 threadHandle.setName("Simulator service provider event queue");
252 threadHandle.start();
254 // Set the default device name.
255 deviceName = "IoTivity Simulator";
257 SimulatorManager.setDeviceInfo(deviceName);
258 } catch (SimulatorException e) {
262 .log(Level.ERROR.ordinal(),
264 "Error while registering the device info.\n"
265 + Utility.getSimulatorErrorString(e, null));
268 // Set the default platform information.
269 platformInfo = new PlatformInfo();
270 platformInfo.setPlatformID("Samsung Platform Identifier");
271 platformInfo.setManufacturerName("Samsung");
272 platformInfo.setManufacturerUrl("www.samsung.com");
273 platformInfo.setModelNumber("Samsung Model Num01");
274 platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
275 platformInfo.setPlatformVersion("PlatformVersion01");
276 platformInfo.setOperationSystemVersion("OSVersion01");
277 platformInfo.setHardwareVersion("HardwareVersion01");
278 platformInfo.setFirmwareVersion("FirwareVersion01");
279 platformInfo.setSupportUrl("http://www.samsung.com/support");
280 platformInfo.setSystemTime("2015-09-10T11:10:30Z");
282 SimulatorManager.setPlatformInfo(platformInfo);
283 } catch (SimulatorException e) {
287 .log(Level.ERROR.ordinal(),
289 "Error while registering the platform info.\n"
290 + Utility.getSimulatorErrorString(e, null));
293 // Get the device information to show other details of the device in UI.
295 SimulatorManager.findDevices("", deviceListener);
296 } catch (SimulatorException e) {
300 .log(Level.ERROR.ordinal(),
302 "Failed to get the local device information.\n"
303 + Utility.getSimulatorErrorString(e, null));
307 private static class NotificationSynchronizerThread implements Runnable {
309 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
313 while (!Thread.interrupted()) {
314 synchronized (this) {
316 while (notificationQueue.isEmpty()) {
320 } catch (InterruptedException e) {
326 synchronized (this) {
327 thread = notificationQueue.pop();
331 } catch (Exception e) {
332 if (e instanceof InterruptedException) {
340 public void addToQueue(Runnable event) {
341 synchronized (this) {
342 notificationQueue.add(event);
348 public void setDeviceInfo(List<MetaProperty> metaProperties) {
349 if (null == metaProperties || metaProperties.size() < 1) {
352 Iterator<MetaProperty> itr = metaProperties.iterator();
356 boolean found = false;
357 while (itr.hasNext()) {
359 propName = prop.getPropName();
360 propValue = prop.getPropValue();
361 if (propName.equals(Constants.DEVICE_NAME)) {
362 this.deviceName = propValue;
373 SimulatorManager.setDeviceInfo(deviceName);
374 } catch (SimulatorException e) {
378 .log(Level.ERROR.ordinal(),
380 "Error while registering the device info.\n"
381 + Utility.getSimulatorErrorString(e, null));
385 public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
386 if (null == metaProperties || metaProperties.size() < 1) {
390 Iterator<MetaProperty> itr = metaProperties.iterator();
394 while (itr.hasNext()) {
396 propName = prop.getPropName();
397 propValue = prop.getPropValue();
398 if (propName.equals(Constants.DEVICE_NAME)) {
399 if (null == propValue || propValue.length() < 1) {
408 public List<MetaProperty> getDeviceInfo() {
409 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
410 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
411 if (null != deviceInfo) {
412 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
414 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
415 deviceInfo.getSpecVersion()));
416 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
417 deviceInfo.getDataModelVersion()));
419 return metaProperties;
422 public List<MetaProperty> getPlatformInfo() {
423 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
424 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
426 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
427 platformInfo.getManufacturerName()));
428 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
429 platformInfo.getManufacturerUrl()));
430 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
431 platformInfo.getModelNumber()));
432 metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
433 platformInfo.getDateOfManufacture()));
434 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
435 platformInfo.getPlatformVersion()));
436 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
437 platformInfo.getOperationSystemVersion()));
438 metaProperties.add(new MetaProperty(
439 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
440 .getHardwareVersion()));
441 metaProperties.add(new MetaProperty(
442 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
443 .getFirmwareVersion()));
444 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
445 platformInfo.getSupportUrl()));
446 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
447 platformInfo.getSystemTime()));
448 return metaProperties;
451 public void setPlatformInfo(List<MetaProperty> metaProperties) {
452 if (null == metaProperties || metaProperties.size() < 1) {
455 Iterator<MetaProperty> itr = metaProperties.iterator();
459 while (itr.hasNext()) {
461 propName = prop.getPropName();
462 propValue = prop.getPropValue();
463 if (propName.equals(Constants.PLATFORM_ID)) {
464 platformInfo.setPlatformID(propValue);
465 } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
466 platformInfo.setManufacturerName(propValue);
467 } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
468 platformInfo.setManufacturerUrl(propValue);
469 } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
470 platformInfo.setModelNumber(propValue);
471 } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
472 platformInfo.setDateOfManufacture(propValue);
473 } else if (propName.equals(Constants.PLATFORM_VERSION)) {
474 platformInfo.setPlatformVersion(propValue);
475 } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
476 platformInfo.setOperationSystemVersion(propValue);
477 } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
478 platformInfo.setHardwareVersion(propValue);
479 } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
480 platformInfo.setFirmwareVersion(propValue);
481 } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
482 platformInfo.setSupportUrl(propValue);
483 } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
484 platformInfo.setSystemTime(propValue);
488 SimulatorManager.setPlatformInfo(platformInfo);
489 } catch (SimulatorException e) {
493 .log(Level.ERROR.ordinal(),
495 "Error while registering the platform info.\n"
496 + Utility.getSimulatorErrorString(e, null));
500 public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
501 if (null == metaProperties || metaProperties.size() < 1) {
504 Iterator<MetaProperty> itr = metaProperties.iterator();
507 while (itr.hasNext()) {
509 propValue = prop.getPropValue();
510 if (null == propValue || propValue.length() < 1) {
517 public synchronized Resource getCurrentResourceInSelection() {
518 return currentResourceInSelection;
521 public synchronized void setCurrentResourceInSelection(Resource resource) {
522 this.currentResourceInSelection = resource;
525 public boolean isResourceExist(String resourceURI) {
526 return data.isResourceExist(resourceURI);
529 public boolean isAnyResourceExist() {
530 return data.isAnyResourceExist();
533 public boolean createSingleResource(SingleResource resource,
534 Map<String, SimulatorResourceAttribute> attributes)
535 throws SimulatorException {
536 if (null == resource) {
541 // Create the resource.
542 SimulatorResource jSimulatorResource = SimulatorManager
543 .createResource(SimulatorResource.Type.SINGLE,
544 resource.getResourceName(),
545 resource.getResourceURI(),
546 resource.getResourceType());
547 if (null == jSimulatorResource
548 || !(jSimulatorResource instanceof SimulatorSingleResource)) {
551 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
552 resource.setSimulatorResource(jSimulatorSingleResource);
554 // Cancel discoverable property if requested by user.
555 if (!resource.isDiscoverable()) {
556 jSimulatorSingleResource.setDiscoverable(false);
559 // Cancel observable property if requested by user.
560 if (!resource.isObservable()) {
561 jSimulatorSingleResource.setObservable(false);
564 // Set the model change listener.
565 jSimulatorSingleResource
566 .setResourceModelChangeListener(resourceModelChangeListener);
568 // Set the observer listener if the resource is observable.
569 if (resource.isObservable()) {
570 jSimulatorSingleResource.setObserverListener(observer);
574 if (null != attributes && !attributes.isEmpty()) {
575 SimulatorResourceAttribute value;
576 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
578 value = entry.getValue();
580 jSimulatorSingleResource.addAttribute(value);
583 // Get the resource model java object reference.
584 resource.setResourceModel(jSimulatorSingleResource
585 .getResourceModel());
587 resource.createResourceRepresentation(jSimulatorSingleResource
591 // Set the resource interfaces.
592 jSimulatorSingleResource
593 .setInterface(Utility.convertSetToVectorString(resource
594 .getResourceInterfaces()));
596 // Register the resource with the platform.
597 jSimulatorSingleResource.start();
598 resource.setStarted(true);
599 } catch (SimulatorException e) {
603 .log(Level.ERROR.ordinal(), new Date(),
604 Utility.getSimulatorErrorString(e, null));
608 // Add to local cache.
609 data.addResource(resource);
611 // Update UI listeners
612 UiListenerHandler.getInstance().resourceCreatedUINotification(
613 ResourceType.SINGLE);
618 public Resource createResourceByRAML(String configFilePath)
619 throws SimulatorException {
620 Resource resource = null;
622 // Create the resource
623 SimulatorResource jSimulatorResource = SimulatorManager
624 .createResource(configFilePath);
625 if (null == jSimulatorResource) {
628 if (jSimulatorResource instanceof SimulatorSingleResource) {
629 resource = new SingleResource();
633 resource.setSimulatorResource(jSimulatorResource);
635 // Fetch and locally store the resource name and uri.
636 String uri = jSimulatorResource.getURI();
637 if (null == uri || uri.trim().isEmpty()) {
640 resource.setResourceURI(uri.trim());
642 String name = jSimulatorResource.getName();
643 if (null == name || name.trim().isEmpty()) {
646 resource.setResourceName(name.trim());
647 } catch (SimulatorException e) {
651 .log(Level.ERROR.ordinal(), new Date(),
652 Utility.getSimulatorErrorString(e, null));
659 * This method can set/change the resource uri and name of an already
660 * created resource which is not yet registered with the platform. This
661 * method registers the model change and observer listeners, registers the
662 * resource, fetches the resource attributes, updates the local cache and
663 * notifies the UI listeners.
665 public boolean completeSingleResourceCreationByRAML(Resource resource,
666 String uri, String name, boolean multiInstance)
667 throws SimulatorException {
668 if (null == resource || !(resource instanceof SingleResource)) {
672 SingleResource singleRes = (SingleResource) resource;
674 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
675 .getSimulatorResource();
676 if (null == jSimulatorSingleResource) {
680 // Update resource URI and Name if they are changed.
681 String newUri = uri.trim();
682 String newName = name.trim();
685 singleRes.setResourceURI(newUri);
686 singleRes.setResourceName(newName);
688 if (!singleRes.getResourceURI().equals(newUri)) {
689 jSimulatorSingleResource.setURI(newUri);
690 singleRes.setResourceURI(newUri);
692 if (!singleRes.getResourceName().equals(newName)) {
693 jSimulatorSingleResource.setName(newName);
694 singleRes.setResourceName(newName);
698 // Set the model change listener.
699 jSimulatorSingleResource
700 .setResourceModelChangeListener(resourceModelChangeListener);
702 // Set the observer listener if the resource is observable.
703 if (jSimulatorSingleResource.isObservable()) {
704 jSimulatorSingleResource.setObserverListener(observer);
705 singleRes.setObservable(true);
708 // Fetch the resource model.
709 SimulatorResourceModel jResModel = jSimulatorSingleResource
711 if (null == jResModel) {
714 singleRes.setResourceModel(jResModel);
716 // Fetch the basic details of the resource.
717 singleRes.setResourceType(jSimulatorSingleResource
720 .setResourceInterfaces(Utility
721 .convertVectorToSet(jSimulatorSingleResource
724 // Fetch the resource attributes.
725 singleRes.createResourceRepresentation(jSimulatorSingleResource
728 // Register the resource with the platform.
729 jSimulatorSingleResource.start();
730 singleRes.setStarted(true);
732 // Add to local cache.
733 data.addResource(singleRes);
735 // Update UI listeners for single instance creation
737 UiListenerHandler.getInstance().resourceCreatedUINotification(
738 ResourceType.SINGLE);
739 } catch (Exception e) {
743 .log(Level.ERROR.ordinal(), new Date(),
744 Utility.getSimulatorErrorString(e, null));
750 public Set<SingleResource> createSingleResourceMultiInstances(
751 String configFile, int count, IProgressMonitor progressMonitor)
752 throws SimulatorException {
753 Set<SingleResource> resultSet;
755 resultSet = new HashSet<SingleResource>();
756 Vector<SimulatorResource> jSimulatorResources = SimulatorManager
757 .createResource(configFile, count);
758 if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
761 SimulatorSingleResource jResource;
762 SingleResource resource;
764 for (SimulatorResource jSimulatorResource : jSimulatorResources) {
765 // If the resource creation progress is canceled, then stop the
766 // creation and stop/delete
767 // the resources created already.
768 if (progressMonitor.isCanceled()) {
769 removeSingleResources(resultSet);
772 jResource = (SimulatorSingleResource) jSimulatorResource;
773 resource = new SingleResource();
774 resource.setSimulatorResource(jResource);
776 result = completeSingleResourceCreationByRAML(resource,
777 jResource.getURI(), jResource.getName(), true);
779 resultSet.add(resource);
781 } catch (SimulatorException eInner) {
785 .log(Level.ERROR.ordinal(),
787 Utility.getSimulatorErrorString(eInner,
790 progressMonitor.worked(1);
792 } catch (SimulatorException eOuter) {
796 .log(Level.ERROR.ordinal(), new Date(),
797 Utility.getSimulatorErrorString(eOuter, null));
803 public List<Resource> getResourceList() {
804 List<Resource> resourceList = data.getResources();
805 if (null == resourceList) {
809 Collections.sort(resourceList, Utility.resourceComparator);
814 public List<SingleResource> getSingleResourceList() {
815 List<SingleResource> resourceList = data.getSingleResources();
816 if (null == resourceList) {
820 Collections.sort(resourceList, Utility.singleResourceComparator);
825 public void removeSingleResources(Set<SingleResource> resources)
826 throws SimulatorException {
827 if (null == resources) {
830 Iterator<SingleResource> itr = resources.iterator();
831 while (itr.hasNext()) {
832 removeResource(itr.next());
836 public void removeResource(Resource res) throws SimulatorException {
837 // Unregister the resource from the platform.
838 SimulatorResource simRes = res.getSimulatorResource();
841 } catch (SimulatorException e) {
845 .log(Level.ERROR.ordinal(), new Date(),
846 Utility.getSimulatorErrorString(e, null));
850 // Delete this resource
851 data.deleteResource(res);
854 public boolean isUriUnique(List<MetaProperty> properties) {
855 if (null == properties) {
859 Iterator<MetaProperty> itr = properties.iterator();
860 while (itr.hasNext()) {
862 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
863 String uri = prop.getPropValue();
864 return !data.isResourceExist(uri);
870 public void resourceSelectionChanged(final Resource selectedResource) {
874 if (null != selectedResource) {
875 setCurrentResourceInSelection(selectedResource);
877 setCurrentResourceInSelection(null);
879 // Notify all observers for resource selection change event
880 UiListenerHandler.getInstance()
881 .resourceSelectionChangedUINotification(
887 public List<MetaProperty> getMetaProperties(Resource resource) {
888 if (null != resource) {
892 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
894 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
895 propName = Constants.META_PROPERTIES[index];
896 if (propName.equals(Constants.RESOURCE_NAME)) {
897 propValue = resource.getResourceName();
898 } else if (propName.equals(Constants.RESOURCE_URI)) {
899 propValue = resource.getResourceURI();
900 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
901 propValue = resource.getResourceType();
902 } else if (propName.equals(Constants.INTERFACE_TYPES)) {
903 Set<String> ifTypes = resource.getResourceInterfaces();
904 if (null != ifTypes && !ifTypes.isEmpty()) {
906 Iterator<String> itr = ifTypes.iterator();
907 while (itr.hasNext()) {
908 propValue += itr.next();
919 if (null != propValue) {
920 metaPropertyList.add(new MetaProperty(propName, propValue));
923 return metaPropertyList;
928 public boolean startResource(Resource resource) throws SimulatorException {
929 if (null == resource) {
932 SimulatorResource server = resource.getSimulatorResource();
933 if (null == server) {
938 resource.setStarted(true);
939 } catch (SimulatorException e) {
943 .log(Level.ERROR.ordinal(),
945 "There is an error while starting the resource.\n"
946 + Utility.getSimulatorErrorString(e, null));
952 public boolean stopResource(Resource resource) throws SimulatorException {
953 if (null == resource) {
956 SimulatorResource server = resource.getSimulatorResource();
957 if (null == server) {
962 resource.setStarted(false);
963 } catch (SimulatorException e) {
967 .log(Level.ERROR.ordinal(),
969 "There is an error while stopping the resource.\n"
970 + Utility.getSimulatorErrorString(e, null));
976 public boolean changeResourceName(Resource resource, String newName)
977 throws SimulatorException {
978 if (null == resource || null == newName) {
982 if (!stopResource(resource)) {
986 SimulatorResource server = resource.getSimulatorResource();
988 server.setName(newName);
989 resource.setResourceName(newName);
990 } catch (SimulatorException e) {
994 .log(Level.ERROR.ordinal(),
996 "There is an error while changing the resource name.\n"
997 + Utility.getSimulatorErrorString(e, null));
1001 if (!startResource(resource)) {
1008 public boolean changeResourceURI(Resource resource, String newURI)
1009 throws SimulatorException {
1010 if (null == resource || null == newURI) {
1014 if (!stopResource(resource)) {
1018 String curURI = resource.getResourceURI();
1019 setResourceURI(resource, newURI);
1022 if (!startResource(resource)) {
1025 } catch (SimulatorException e) {
1026 setResourceURI(resource, curURI);
1032 public void setResourceURI(Resource resource, String newURI)
1033 throws SimulatorException {
1034 String curURI = resource.getResourceURI();
1035 SimulatorResource server = resource.getSimulatorResource();
1037 server.setURI(newURI);
1038 data.changeResourceURI(resource, curURI, newURI);
1039 } catch (SimulatorException e) {
1043 .log(Level.ERROR.ordinal(),
1045 "There is an error while changing the resource URI.\n"
1046 + Utility.getSimulatorErrorString(e, null));
1051 public boolean updateResourceProperties(Resource resource,
1052 List<MetaProperty> properties, boolean uriChanged,
1053 boolean nameChanged) throws SimulatorException {
1054 if (null == resource || null == properties) {
1058 // Updating the properties
1059 Iterator<MetaProperty> itr = properties.iterator();
1060 MetaProperty property;
1063 String resName = null;
1064 String resURI = null;
1065 while (itr.hasNext()) {
1066 property = itr.next();
1067 if (null == property) {
1070 propName = property.getPropName();
1071 propValue = property.getPropValue();
1072 if (propName.equals(Constants.RESOURCE_NAME)) {
1073 resName = propValue;
1074 } else if (propName.equals(Constants.RESOURCE_URI)) {
1080 if (!changeResourceName(resource, resName)) {
1084 // Notify UI Listeners
1085 UiListenerHandler.getInstance().propertiesChangedUINotification(
1090 if (!changeResourceURI(resource, resURI)) {
1098 public boolean updateResourceInterfaces(Resource resource,
1099 Set<String> newIfSet) throws SimulatorException {
1100 if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1103 SimulatorResource jResource = resource.getSimulatorResource();
1104 if (null == jResource) {
1107 Set<String> curIfSet = resource.getResourceInterfaces();
1108 Iterator<String> itr = curIfSet.iterator();
1109 String interfaceType;
1110 boolean resourceRestartRequired = false;
1111 while (itr.hasNext()) {
1112 interfaceType = itr.next();
1113 if (!newIfSet.contains(interfaceType)) {
1114 resourceRestartRequired = true;
1120 // As there is no support from native layer for interface removal,
1121 // supporting it from the simulator requires restarting the resource
1122 // if any existing interfaces are removed.
1124 if (resourceRestartRequired) {
1125 stopResource(resource);
1126 jResource.setInterface(Utility
1127 .convertSetToVectorString(newIfSet));
1128 startResource(resource);
1130 // Existing interfaces are not removed.
1131 itr = newIfSet.iterator();
1132 while (itr.hasNext()) {
1133 interfaceType = itr.next();
1134 if (!curIfSet.contains(interfaceType)) {
1135 jResource.addInterface(interfaceType);
1139 } catch (SimulatorException e) {
1143 .log(Level.ERROR.ordinal(),
1145 "There is an error while changing the interface types."
1146 + Utility.getSimulatorErrorString(e, null));
1150 // Set the resource interfaces.
1151 resource.setResourceInterfaces(newIfSet);
1156 public boolean attributeValueUpdated(SingleResource resource,
1157 String attributeName, AttributeValue value) {
1158 if (null != resource && null != attributeName && null != value) {
1159 SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1160 .getSimulatorResource();
1161 if (null != simRes) {
1163 simRes.updateAttribute(attributeName, value);
1165 } catch (SimulatorException e) {
1169 .log(Level.ERROR.ordinal(), new Date(),
1170 Utility.getSimulatorErrorString(e, null));
1177 public boolean isResourceStarted(Resource resource) {
1178 if (null == resource) {
1181 return resource.isStarted();
1184 public boolean isPropertyValueInvalid(Resource resource,
1185 List<MetaProperty> properties, String propName) {
1186 if (null == resource || null == properties || null == propName) {
1189 boolean invalid = false;
1191 Iterator<MetaProperty> itr = properties.iterator();
1192 while (itr.hasNext()) {
1194 if (prop.getPropName().equals(propName)) {
1195 String value = prop.getPropValue();
1196 if (propName.equals(Constants.RESOURCE_URI)) {
1197 if (!Utility.isUriValid(value)) {
1201 if (null == value || value.trim().isEmpty()) {
1210 public boolean isPropValueChanged(Resource resource,
1211 List<MetaProperty> properties, String propName) {
1212 if (null == resource || null == properties || null == propName) {
1215 boolean changed = false;
1218 Iterator<MetaProperty> itr = properties.iterator();
1219 while (itr.hasNext()) {
1221 if (prop.getPropName().equals(propName)) {
1222 oldValue = getPropertyValueFromResource(resource, propName);
1223 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1232 private String getPropertyValueFromResource(Resource resource,
1234 if (null == resource || null == propName) {
1237 if (propName.equals(Constants.RESOURCE_URI)) {
1238 return resource.getResourceURI();
1239 } else if (propName.equals(Constants.RESOURCE_NAME)) {
1240 return resource.getResourceName();
1241 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1242 return resource.getResourceType();
1248 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1252 AttributeProperty prop = att.property();
1257 if (prop.getType() == Type.INTEGER) {
1258 IntegerProperty intProperty = prop.asInteger();
1259 if (null != intProperty) {
1260 return (intProperty.hasRange() || intProperty.hasValues());
1264 } else if (prop.getType() == Type.DOUBLE) {
1265 DoubleProperty dblProperty = prop.asDouble();
1266 if (null != dblProperty) {
1267 return (dblProperty.hasRange() || dblProperty.hasValues());
1271 } else if (prop.getType() == Type.STRING) {
1272 StringProperty stringProperty = prop.asString();
1273 if (null != stringProperty) {
1274 return stringProperty.hasValues();
1283 public int startAutomation(SingleResource resource,
1284 AttributeElement attribute, AutoUpdateType autoType,
1285 int autoUpdateInterval) {
1287 if (null != resource && null != attribute) {
1288 SimulatorSingleResource server = (SimulatorSingleResource) resource
1289 .getSimulatorResource();
1290 if (null != server) {
1291 String attrName = attribute.getSimulatorResourceAttribute()
1294 autoId = server.startAttributeUpdation(attrName, autoType,
1295 autoUpdateInterval, automationListener);
1296 } catch (SimulatorException e) {
1300 .log(Level.ERROR.ordinal(),
1302 "[" + e.getClass().getSimpleName() + "]"
1303 + e.code().toString() + "-"
1308 attribute.setAutoUpdateId(autoId);
1309 attribute.setAutoUpdateType(autoType);
1310 attribute.setAutoUpdateInterval(autoUpdateInterval);
1311 attribute.setAutoUpdateState(true);
1312 resource.setAttributeAutomationInProgress(true);
1319 public void stopAutomation(SingleResource resource, AttributeElement att,
1321 if (null != resource) {
1322 SimulatorSingleResource server = (SimulatorSingleResource) resource
1323 .getSimulatorResource();
1324 if (null != server) {
1326 server.stopUpdation(autoId);
1327 } catch (SimulatorException e) {
1331 .log(Level.ERROR.ordinal(),
1333 "[" + e.getClass().getSimpleName() + "]"
1334 + e.code().toString() + "-"
1338 // Change the automation status
1339 att.setAutoUpdateState(false);
1340 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1345 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1346 int autoUpdateInterval, final SingleResource resource) {
1347 if (null == resource) {
1350 boolean status = false;
1351 // Invoke the native automation method
1352 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1353 .getSimulatorResource();
1354 if (null != resourceServer) {
1357 autoId = resourceServer.startResourceUpdation(autoType,
1358 autoUpdateInterval, automationListener);
1359 } catch (SimulatorException e) {
1363 .log(Level.ERROR.ordinal(), new Date(),
1364 Utility.getSimulatorErrorString(e, null));
1368 // Automation request accepted.
1369 changeResourceLevelAutomationStatus(resource, true);
1371 resource.setAutomationId(autoId);
1373 // Notify the UI listeners in a different thread.
1374 Thread notifyThread = new Thread() {
1376 UiListenerHandler.getInstance()
1377 .resourceAutomationStartedUINotification(
1381 notifyThread.setPriority(Thread.MAX_PRIORITY);
1382 notifyThread.start();
1390 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1391 if (null == resource) {
1394 final int autoId = resource.getAutomationId();
1398 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1399 .getSimulatorResource();
1400 if (null == resourceServer) {
1403 // Call native method
1405 resourceServer.stopUpdation(autoId);
1406 } catch (SimulatorException e) {
1410 .log(Level.ERROR.ordinal(), new Date(),
1411 Utility.getSimulatorErrorString(e, null));
1415 // Notify the UI Listeners. Invoke the automation complete callback.
1416 Thread stopThread = new Thread() {
1418 automationListener.onUpdateComplete(resource.getResourceURI(),
1426 private boolean isAnyAttributeInAutomation(SingleResource resource) {
1427 if (null == resource || null == resource.getResourceRepresentation()) {
1431 Map<String, AttributeElement> attributes = resource
1432 .getResourceRepresentation().getAttributes();
1433 if (null == attributes || 0 == attributes.size())
1436 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1437 if (entry.getValue().isAutoUpdateInProgress())
1444 // Changes the automation state of the resource and its attributes
1445 private void changeResourceLevelAutomationStatus(SingleResource resource,
1448 if (null == resource || null == resource.getResourceRepresentation()) {
1452 Map<String, AttributeElement> attributes = resource
1453 .getResourceRepresentation().getAttributes();
1454 if (null == attributes || 0 == attributes.size())
1457 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1458 entry.getValue().setAutoUpdateState(status);
1461 resource.setResourceAutomationInProgress(status);
1464 private AttributeElement getAttributeWithGivenAutomationId(
1465 SingleResource resource, int automationId) {
1466 if (null == resource || null == resource.getResourceRepresentation()) {
1470 Map<String, AttributeElement> attributes = resource
1471 .getResourceRepresentation().getAttributes();
1472 if (null == attributes || 0 == attributes.size())
1475 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1476 if (automationId == entry.getValue().getAutoUpdateId())
1477 return entry.getValue();
1483 public boolean isResourceAutomationStarted(SingleResource resource) {
1484 boolean status = false;
1485 if (null != resource) {
1486 status = resource.isResourceAutomationInProgress();
1491 public boolean isAttributeAutomationStarted(SingleResource resource) {
1492 if (null == resource) {
1495 return resource.isAttributeAutomationInProgress();
1498 public void notifyObserverRequest(Resource resource, int observerId) {
1499 if (null == resource) {
1502 SimulatorResource simulatorResource = resource.getSimulatorResource();
1503 if (null == simulatorResource) {
1507 simulatorResource.notifyObserver(observerId);
1508 } catch (SimulatorException e) {
1512 .log(Level.ERROR.ordinal(), new Date(),
1513 Utility.getSimulatorErrorString(e, null));
1517 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1522 AttributeValue val = att.value();
1527 TypeInfo type = val.typeInfo();
1529 AttributeProperty prop = att.property();
1534 List<String> values = new ArrayList<String>();
1536 if (type.mType != ValueType.RESOURCEMODEL) {
1537 if (type.mType == ValueType.ARRAY) {
1538 if (type.mDepth == 1) {
1539 ArrayProperty arrayProperty = prop.asArray();
1540 if (null != arrayProperty) {
1541 AttributeProperty childProp = arrayProperty
1542 .getElementProperty();
1543 switch (childProp.getType()) {
1545 IntegerProperty intProperty = childProp
1547 if (null != intProperty) {
1548 values.addAll(getAllValues(intProperty,
1553 DoubleProperty dblProperty = childProp
1555 if (null != dblProperty) {
1556 values.addAll(getAllValues(dblProperty,
1561 BooleanProperty boolProperty = childProp
1563 if (null != boolProperty) {
1564 values.addAll(getAllValues(boolProperty,
1569 StringProperty stringProperty = childProp
1571 if (null != stringProperty) {
1572 values.addAll(getAllValues(stringProperty,
1582 switch (prop.getType()) {
1584 IntegerProperty intProperty = prop.asInteger();
1585 if (null != intProperty) {
1586 values.addAll(getAllValues(intProperty,
1591 DoubleProperty dblProperty = prop.asDouble();
1592 if (null != dblProperty) {
1593 values.addAll(getAllValues(dblProperty, Type.DOUBLE));
1597 BooleanProperty boolProperty = prop.asBoolean();
1598 if (null != boolProperty) {
1599 values.addAll(getAllValues(boolProperty,
1604 StringProperty stringProperty = prop.asString();
1605 if (null != stringProperty) {
1606 values.addAll(getAllValues(stringProperty,
1619 public List<String> getAllValues(IntegerProperty intProperty,
1620 AttributeProperty.Type type) {
1621 List<String> values = new ArrayList<String>();
1623 if (intProperty.hasRange()) {
1624 int min = (int) intProperty.min();
1625 int max = (int) intProperty.max();
1626 for (int iVal = min; iVal <= max; iVal++) {
1627 values.add(String.valueOf(iVal));
1629 } else if (intProperty.hasValues()) {
1630 for (Integer val : intProperty.getValues()) {
1631 values.add(String.valueOf(val));
1634 // Adding the default value.
1635 values.add(String.valueOf(intProperty.getDefaultValue()));
1640 public List<String> getAllValues(DoubleProperty dblProperty,
1641 AttributeProperty.Type type) {
1642 NumberFormat formatter = NumberFormat.getInstance();
1643 List<String> values = new ArrayList<String>();
1645 if (dblProperty.hasRange()) {
1646 double min = (double) dblProperty.min();
1647 double max = (double) dblProperty.max();
1648 for (double val = min; val <= max; val += 0.1) {
1649 formatter.setMaximumFractionDigits(1);
1650 formatter.setMinimumFractionDigits(1);
1651 values.add(formatter.format(val));
1653 } else if (dblProperty.hasValues()) {
1654 for (Double val : dblProperty.getValues()) {
1655 values.add(String.valueOf(val));
1658 // Adding the default value.
1659 values.add(String.valueOf(dblProperty.getDefaultValue()));
1664 public List<String> getAllValues(BooleanProperty boolProperty,
1665 AttributeProperty.Type type) {
1666 List<String> values = new ArrayList<String>();
1668 values.add("false");
1672 public List<String> getAllValues(StringProperty stringProperty,
1673 AttributeProperty.Type type) {
1674 List<String> values = new ArrayList<String>();
1676 if (stringProperty.hasValues()) {
1677 for (String val : stringProperty.getValues()) {
1678 values.add(String.valueOf(val));
1681 // Adding the default value.
1682 values.add(String.valueOf(stringProperty.getDefaultValue()));
1687 public int getResourceCount() {
1688 return data.getResourceCount();
1691 public void shutdown() {
1692 threadHandle.interrupt();