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 boolean changeResourceType(Resource resource, String newResourceType)
1033 throws SimulatorException {
1034 if (null == resource || null == newResourceType) {
1038 if (!stopResource(resource)) {
1042 String curResourceType = resource.getResourceType();
1043 setResourceType(resource, newResourceType);
1046 if (!startResource(resource)) {
1049 } catch (SimulatorException e) {
1050 setResourceType(resource, curResourceType);
1056 public void setResourceURI(Resource resource, String newURI)
1057 throws SimulatorException {
1058 String curURI = resource.getResourceURI();
1059 SimulatorResource server = resource.getSimulatorResource();
1061 server.setURI(newURI);
1062 data.changeResourceURI(resource, curURI, newURI);
1063 } catch (SimulatorException e) {
1067 .log(Level.ERROR.ordinal(),
1069 "There is an error while changing the resource URI.\n"
1070 + Utility.getSimulatorErrorString(e, null));
1075 public void setResourceType(Resource resource, String newResourceType)
1076 throws SimulatorException {
1077 SimulatorResource server = resource.getSimulatorResource();
1079 server.setResourceType(newResourceType);
1080 resource.setResourceType(newResourceType);
1081 } catch (SimulatorException e) {
1085 .log(Level.ERROR.ordinal(),
1087 "There is an error while changing the resource Type.\n"
1088 + Utility.getSimulatorErrorString(e, null));
1093 public boolean updateResourceProperties(Resource resource,
1094 List<MetaProperty> properties, boolean uriChanged,
1095 boolean nameChanged, boolean resTypeChanged)
1096 throws SimulatorException {
1097 if (null == resource || null == properties) {
1101 // Updating the properties
1102 Iterator<MetaProperty> itr = properties.iterator();
1103 MetaProperty property;
1106 String resName = null;
1107 String resURI = null;
1108 String resType = null;
1109 while (itr.hasNext()) {
1110 property = itr.next();
1111 if (null == property) {
1114 propName = property.getPropName();
1115 propValue = property.getPropValue();
1116 if (propName.equals(Constants.RESOURCE_NAME)) {
1117 resName = propValue;
1118 } else if (propName.equals(Constants.RESOURCE_URI)) {
1120 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1121 resType = propValue;
1126 if (!changeResourceName(resource, resName)) {
1130 // Notify UI Listeners
1131 UiListenerHandler.getInstance().propertiesChangedUINotification(
1136 if (!changeResourceURI(resource, resURI)) {
1141 if (resTypeChanged) {
1142 if (!changeResourceType(resource, resType)) {
1150 public boolean updateResourceInterfaces(Resource resource,
1151 Set<String> newIfSet) throws SimulatorException {
1152 if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1155 SimulatorResource jResource = resource.getSimulatorResource();
1156 if (null == jResource) {
1159 Set<String> curIfSet = resource.getResourceInterfaces();
1160 Iterator<String> itr = curIfSet.iterator();
1161 String interfaceType;
1162 boolean resourceRestartRequired = false;
1163 while (itr.hasNext()) {
1164 interfaceType = itr.next();
1165 if (!newIfSet.contains(interfaceType)) {
1166 resourceRestartRequired = true;
1172 // As there is no support from native layer for interface removal,
1173 // supporting it from the simulator requires restarting the resource
1174 // if any existing interfaces are removed.
1176 if (resourceRestartRequired) {
1177 stopResource(resource);
1178 jResource.setInterface(Utility
1179 .convertSetToVectorString(newIfSet));
1180 startResource(resource);
1182 // Existing interfaces are not removed.
1183 itr = newIfSet.iterator();
1184 while (itr.hasNext()) {
1185 interfaceType = itr.next();
1186 if (!curIfSet.contains(interfaceType)) {
1187 jResource.addInterface(interfaceType);
1191 } catch (SimulatorException e) {
1195 .log(Level.ERROR.ordinal(),
1197 "There is an error while changing the interface types."
1198 + Utility.getSimulatorErrorString(e, null));
1202 // Set the resource interfaces.
1203 resource.setResourceInterfaces(newIfSet);
1208 public boolean attributeValueUpdated(SingleResource resource,
1209 String attributeName, AttributeValue value) {
1210 if (null != resource && null != attributeName && null != value) {
1211 SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1212 .getSimulatorResource();
1213 if (null != simRes) {
1215 simRes.updateAttribute(attributeName, value);
1217 } catch (SimulatorException e) {
1221 .log(Level.ERROR.ordinal(), new Date(),
1222 Utility.getSimulatorErrorString(e, null));
1229 public boolean isResourceStarted(Resource resource) {
1230 if (null == resource) {
1233 return resource.isStarted();
1236 public boolean isPropertyValueInvalid(Resource resource,
1237 List<MetaProperty> properties, String propName) {
1238 if (null == resource || null == properties || null == propName) {
1241 boolean invalid = false;
1243 Iterator<MetaProperty> itr = properties.iterator();
1244 while (itr.hasNext()) {
1246 if (prop.getPropName().equals(propName)) {
1247 String value = prop.getPropValue();
1248 if (propName.equals(Constants.RESOURCE_URI)) {
1249 if (!Utility.isUriValid(value)) {
1252 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1253 if (!Utility.isResourceTypeValid(value)) {
1257 if (null == value || value.trim().isEmpty()) {
1266 public boolean isPropValueChanged(Resource resource,
1267 List<MetaProperty> properties, String propName) {
1268 if (null == resource || null == properties || null == propName) {
1271 boolean changed = false;
1274 Iterator<MetaProperty> itr = properties.iterator();
1275 while (itr.hasNext()) {
1277 if (prop.getPropName().equals(propName)) {
1278 oldValue = getPropertyValueFromResource(resource, propName);
1279 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1288 private String getPropertyValueFromResource(Resource resource,
1290 if (null == resource || null == propName) {
1293 if (propName.equals(Constants.RESOURCE_URI)) {
1294 return resource.getResourceURI();
1295 } else if (propName.equals(Constants.RESOURCE_NAME)) {
1296 return resource.getResourceName();
1297 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1298 return resource.getResourceType();
1304 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1308 AttributeProperty prop = att.property();
1313 if (prop.getType() == Type.INTEGER) {
1314 IntegerProperty intProperty = prop.asInteger();
1315 if (null != intProperty) {
1316 return (intProperty.hasRange() || intProperty.hasValues());
1320 } else if (prop.getType() == Type.DOUBLE) {
1321 DoubleProperty dblProperty = prop.asDouble();
1322 if (null != dblProperty) {
1323 return (dblProperty.hasRange() || dblProperty.hasValues());
1327 } else if (prop.getType() == Type.STRING) {
1328 StringProperty stringProperty = prop.asString();
1329 if (null != stringProperty) {
1330 return stringProperty.hasValues();
1339 public int startAutomation(SingleResource resource,
1340 AttributeElement attribute, AutoUpdateType autoType,
1341 int autoUpdateInterval) {
1343 if (null != resource && null != attribute) {
1344 SimulatorSingleResource server = (SimulatorSingleResource) resource
1345 .getSimulatorResource();
1346 if (null != server) {
1347 String attrName = attribute.getSimulatorResourceAttribute()
1350 autoId = server.startAttributeUpdation(attrName, autoType,
1351 autoUpdateInterval, automationListener);
1352 } catch (SimulatorException e) {
1356 .log(Level.ERROR.ordinal(),
1358 "[" + e.getClass().getSimpleName() + "]"
1359 + e.code().toString() + "-"
1364 attribute.setAutoUpdateId(autoId);
1365 attribute.setAutoUpdateType(autoType);
1366 attribute.setAutoUpdateInterval(autoUpdateInterval);
1367 attribute.setAutoUpdateState(true);
1368 resource.setAttributeAutomationInProgress(true);
1375 public void stopAutomation(SingleResource resource, AttributeElement att,
1377 if (null != resource) {
1378 SimulatorSingleResource server = (SimulatorSingleResource) resource
1379 .getSimulatorResource();
1380 if (null != server) {
1382 server.stopUpdation(autoId);
1383 } catch (SimulatorException e) {
1387 .log(Level.ERROR.ordinal(),
1389 "[" + e.getClass().getSimpleName() + "]"
1390 + e.code().toString() + "-"
1394 // Change the automation status
1395 att.setAutoUpdateState(false);
1396 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1401 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1402 int autoUpdateInterval, final SingleResource resource) {
1403 if (null == resource) {
1406 boolean status = false;
1407 // Invoke the native automation method
1408 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1409 .getSimulatorResource();
1410 if (null != resourceServer) {
1413 autoId = resourceServer.startResourceUpdation(autoType,
1414 autoUpdateInterval, automationListener);
1415 } catch (SimulatorException e) {
1419 .log(Level.ERROR.ordinal(), new Date(),
1420 Utility.getSimulatorErrorString(e, null));
1424 // Automation request accepted.
1425 changeResourceLevelAutomationStatus(resource, true);
1427 resource.setAutomationId(autoId);
1429 // Notify the UI listeners in a different thread.
1430 Thread notifyThread = new Thread() {
1432 UiListenerHandler.getInstance()
1433 .resourceAutomationStartedUINotification(
1437 notifyThread.setPriority(Thread.MAX_PRIORITY);
1438 notifyThread.start();
1446 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1447 if (null == resource) {
1450 final int autoId = resource.getAutomationId();
1454 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1455 .getSimulatorResource();
1456 if (null == resourceServer) {
1459 // Call native method
1461 resourceServer.stopUpdation(autoId);
1462 } catch (SimulatorException e) {
1466 .log(Level.ERROR.ordinal(), new Date(),
1467 Utility.getSimulatorErrorString(e, null));
1471 // Notify the UI Listeners. Invoke the automation complete callback.
1472 Thread stopThread = new Thread() {
1474 automationListener.onUpdateComplete(resource.getResourceURI(),
1482 private boolean isAnyAttributeInAutomation(SingleResource resource) {
1483 if (null == resource || null == resource.getResourceRepresentation()) {
1487 Map<String, AttributeElement> attributes = resource
1488 .getResourceRepresentation().getAttributes();
1489 if (null == attributes || 0 == attributes.size())
1492 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1493 if (entry.getValue().isAutoUpdateInProgress())
1500 // Changes the automation state of the resource and its attributes
1501 private void changeResourceLevelAutomationStatus(SingleResource resource,
1504 if (null == resource || null == resource.getResourceRepresentation()) {
1508 Map<String, AttributeElement> attributes = resource
1509 .getResourceRepresentation().getAttributes();
1510 if (null == attributes || 0 == attributes.size())
1513 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1514 entry.getValue().setAutoUpdateState(status);
1517 resource.setResourceAutomationInProgress(status);
1520 private AttributeElement getAttributeWithGivenAutomationId(
1521 SingleResource resource, int automationId) {
1522 if (null == resource || null == resource.getResourceRepresentation()) {
1526 Map<String, AttributeElement> attributes = resource
1527 .getResourceRepresentation().getAttributes();
1528 if (null == attributes || 0 == attributes.size())
1531 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1532 if (automationId == entry.getValue().getAutoUpdateId())
1533 return entry.getValue();
1539 public boolean isResourceAutomationStarted(SingleResource resource) {
1540 boolean status = false;
1541 if (null != resource) {
1542 status = resource.isResourceAutomationInProgress();
1547 public boolean isAttributeAutomationStarted(SingleResource resource) {
1548 if (null == resource) {
1551 return resource.isAttributeAutomationInProgress();
1554 public void notifyObserverRequest(Resource resource, int observerId) {
1555 if (null == resource) {
1558 SimulatorResource simulatorResource = resource.getSimulatorResource();
1559 if (null == simulatorResource) {
1563 simulatorResource.notifyObserver(observerId);
1564 } catch (SimulatorException e) {
1568 .log(Level.ERROR.ordinal(), new Date(),
1569 Utility.getSimulatorErrorString(e, null));
1573 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1578 AttributeValue val = att.value();
1583 TypeInfo type = val.typeInfo();
1585 AttributeProperty prop = att.property();
1590 List<String> values = new ArrayList<String>();
1592 if (type.mType != ValueType.RESOURCEMODEL) {
1593 if (type.mType == ValueType.ARRAY) {
1594 if (type.mDepth == 1) {
1595 ArrayProperty arrayProperty = prop.asArray();
1596 if (null != arrayProperty) {
1597 AttributeProperty childProp = arrayProperty
1598 .getElementProperty();
1599 switch (childProp.getType()) {
1601 IntegerProperty intProperty = childProp
1603 if (null != intProperty) {
1604 values.addAll(getAllValues(intProperty,
1609 DoubleProperty dblProperty = childProp
1611 if (null != dblProperty) {
1612 values.addAll(getAllValues(dblProperty,
1617 BooleanProperty boolProperty = childProp
1619 if (null != boolProperty) {
1620 values.addAll(getAllValues(boolProperty,
1625 StringProperty stringProperty = childProp
1627 if (null != stringProperty) {
1628 values.addAll(getAllValues(stringProperty,
1638 switch (prop.getType()) {
1640 IntegerProperty intProperty = prop.asInteger();
1641 if (null != intProperty) {
1642 values.addAll(getAllValues(intProperty,
1647 DoubleProperty dblProperty = prop.asDouble();
1648 if (null != dblProperty) {
1649 values.addAll(getAllValues(dblProperty, Type.DOUBLE));
1653 BooleanProperty boolProperty = prop.asBoolean();
1654 if (null != boolProperty) {
1655 values.addAll(getAllValues(boolProperty,
1660 StringProperty stringProperty = prop.asString();
1661 if (null != stringProperty) {
1662 values.addAll(getAllValues(stringProperty,
1675 public List<String> getAllValues(IntegerProperty intProperty,
1676 AttributeProperty.Type type) {
1677 List<String> values = new ArrayList<String>();
1679 if (intProperty.hasRange()) {
1680 int min = (int) intProperty.min();
1681 int max = (int) intProperty.max();
1682 for (int iVal = min; iVal <= max; iVal++) {
1683 values.add(String.valueOf(iVal));
1685 } else if (intProperty.hasValues()) {
1686 for (Integer val : intProperty.getValues()) {
1687 values.add(String.valueOf(val));
1690 // Adding the default value.
1691 values.add(String.valueOf(intProperty.getDefaultValue()));
1696 public List<String> getAllValues(DoubleProperty dblProperty,
1697 AttributeProperty.Type type) {
1698 NumberFormat formatter = NumberFormat.getInstance();
1699 List<String> values = new ArrayList<String>();
1701 if (dblProperty.hasRange()) {
1702 double min = (double) dblProperty.min();
1703 double max = (double) dblProperty.max();
1704 for (double val = min; val <= max; val += 0.1) {
1705 formatter.setMaximumFractionDigits(1);
1706 formatter.setMinimumFractionDigits(1);
1707 values.add(formatter.format(val));
1709 } else if (dblProperty.hasValues()) {
1710 for (Double val : dblProperty.getValues()) {
1711 values.add(String.valueOf(val));
1714 // Adding the default value.
1715 values.add(String.valueOf(dblProperty.getDefaultValue()));
1720 public List<String> getAllValues(BooleanProperty boolProperty,
1721 AttributeProperty.Type type) {
1722 List<String> values = new ArrayList<String>();
1724 values.add("false");
1728 public List<String> getAllValues(StringProperty stringProperty,
1729 AttributeProperty.Type type) {
1730 List<String> values = new ArrayList<String>();
1732 if (stringProperty.hasValues()) {
1733 for (String val : stringProperty.getValues()) {
1734 values.add(String.valueOf(val));
1737 // Adding the default value.
1738 values.add(String.valueOf(stringProperty.getDefaultValue()));
1743 public int getResourceCount() {
1744 return data.getResourceCount();
1747 public void shutdown() {
1748 threadHandle.interrupt();