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.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;
31 import java.util.Vector;
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;
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;
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.
75 public class ResourceManager {
79 private Resource currentResourceInSelection;
81 private ResourceModelChangeListener resourceModelChangeListener;
83 private AutoUpdateListener automationListener;
85 private ObserverListener observer;
87 private DeviceListener deviceListener;
89 private NotificationSynchronizerThread synchronizerThread;
91 private Thread threadHandle;
93 private DeviceInfo deviceInfo;
94 private PlatformInfo platformInfo;
96 private String deviceName;
98 public ResourceManager() {
101 deviceListener = new DeviceListener() {
104 public void onDeviceFound(final String host,
105 final DeviceInfo deviceInfo) {
106 if (null != ResourceManager.this.deviceInfo
107 || null == deviceInfo || null == host) {
110 synchronizerThread.addToQueue(new Runnable() {
113 String rcvdDeviceName = deviceInfo.getName();
114 if (null == rcvdDeviceName) {
117 if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
118 ResourceManager.this.deviceInfo = deviceInfo;
120 // Notify the UI Listeners
121 UiListenerHandler.getInstance()
122 .deviceInfoReceivedNotification();
129 resourceModelChangeListener = new ResourceModelChangeListener() {
132 public void onResourceModelChanged(final String resourceURI,
133 final SimulatorResourceModel resourceModelN) {
134 synchronizerThread.addToQueue(new Runnable() {
138 if (null == resourceURI || null == resourceModelN) {
142 Display.getDefault().asyncExec(new Runnable() {
145 Resource resource = data
146 .getResourceByURI(resourceURI);
147 if (null != resource) {
149 resource.updateResourceRepresentation(resourceModelN);
150 } catch (NumberFormatException e) {
154 .log(Level.ERROR.ordinal(),
156 "Error while trying to update the attributes.\n"
158 .getSimulatorErrorString(
170 automationListener = new AutoUpdateListener() {
173 public void onUpdateComplete(final String resourceURI,
174 final int automationId) {
175 synchronizerThread.addToQueue(new Runnable() {
179 SingleResource resource = data
180 .getSingleResourceByURI(resourceURI);
181 if (null == resource) {
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,
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));
200 // Setting the attribute automation status to
202 resource.setAttributeAutomationInProgress(false);
210 observer = new ObserverListener() {
212 public void onObserverChanged(final String resourceURI,
213 final int status, final Observer observer) {
217 if (null == resourceURI || null == observer) {
220 Resource resource = data.getResourceByURI(resourceURI);
221 if (null == resource) {
224 // Update the observers information
226 resource.addObserverInfo(observer);
228 resource.removeObserverInfo(observer);
230 // Notify the UI listeners
231 UiListenerHandler.getInstance()
232 .observerListChangedUINotification(resource);
238 public void onObserverAdded(String resourceURI, Observer observer) {
239 onObserverChanged(resourceURI, 0, observer);
243 public void onObserverRemoved(String resourceURI, Observer observer) {
244 onObserverChanged(resourceURI, 1, observer);
248 synchronizerThread = new NotificationSynchronizerThread();
249 threadHandle = new Thread(synchronizerThread);
250 threadHandle.setName("Simulator service provider event queue");
251 threadHandle.start();
253 // Set the default device name.
254 deviceName = "IoTivity Simulator";
256 SimulatorManager.setDeviceInfo(deviceName);
257 } catch (SimulatorException e) {
261 .log(Level.ERROR.ordinal(),
263 "Error while registering the device info.\n"
264 + Utility.getSimulatorErrorString(e, null));
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");
281 SimulatorManager.setPlatformInfo(platformInfo);
282 } catch (SimulatorException e) {
286 .log(Level.ERROR.ordinal(),
288 "Error while registering the platform info.\n"
289 + Utility.getSimulatorErrorString(e, null));
292 // Get the device information to show other details of the device in UI.
294 SimulatorManager.findDevices("", deviceListener);
295 } catch (SimulatorException e) {
299 .log(Level.ERROR.ordinal(),
301 "Failed to get the local device information.\n"
302 + Utility.getSimulatorErrorString(e, null));
306 private static class NotificationSynchronizerThread implements Runnable {
308 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
312 while (!Thread.interrupted()) {
313 synchronized (this) {
315 while (notificationQueue.isEmpty()) {
319 } catch (InterruptedException e) {
325 synchronized (this) {
326 thread = notificationQueue.pop();
330 } catch (Exception e) {
331 if (e instanceof InterruptedException) {
339 public void addToQueue(Runnable event) {
340 synchronized (this) {
341 notificationQueue.add(event);
347 public void setDeviceInfo(List<MetaProperty> metaProperties) {
348 if (null == metaProperties || metaProperties.size() < 1) {
351 Iterator<MetaProperty> itr = metaProperties.iterator();
355 boolean found = false;
356 while (itr.hasNext()) {
358 propName = prop.getPropName();
359 propValue = prop.getPropValue();
360 if (propName.equals(Constants.DEVICE_NAME)) {
361 this.deviceName = propValue;
372 SimulatorManager.setDeviceInfo(deviceName);
373 } catch (SimulatorException e) {
377 .log(Level.ERROR.ordinal(),
379 "Error while registering the device info.\n"
380 + Utility.getSimulatorErrorString(e, null));
384 public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
385 if (null == metaProperties || metaProperties.size() < 1) {
389 Iterator<MetaProperty> itr = metaProperties.iterator();
393 while (itr.hasNext()) {
395 propName = prop.getPropName();
396 propValue = prop.getPropValue();
397 if (propName.equals(Constants.DEVICE_NAME)) {
398 if (null == propValue || propValue.length() < 1) {
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
413 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
414 deviceInfo.getSpecVersion()));
415 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
416 deviceInfo.getDataModelVersion()));
418 return metaProperties;
421 public List<MetaProperty> getPlatformInfo() {
422 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
423 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
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;
450 public void setPlatformInfo(List<MetaProperty> metaProperties) {
451 if (null == metaProperties || metaProperties.size() < 1) {
454 Iterator<MetaProperty> itr = metaProperties.iterator();
458 while (itr.hasNext()) {
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);
487 SimulatorManager.setPlatformInfo(platformInfo);
488 } catch (SimulatorException e) {
492 .log(Level.ERROR.ordinal(),
494 "Error while registering the platform info.\n"
495 + Utility.getSimulatorErrorString(e, null));
499 public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
500 if (null == metaProperties || metaProperties.size() < 1) {
503 Iterator<MetaProperty> itr = metaProperties.iterator();
506 while (itr.hasNext()) {
508 propValue = prop.getPropValue();
509 if (null == propValue || propValue.length() < 1) {
516 public synchronized Resource getCurrentResourceInSelection() {
517 return currentResourceInSelection;
520 public synchronized void setCurrentResourceInSelection(Resource resource) {
521 this.currentResourceInSelection = resource;
524 public boolean isResourceExist(String resourceURI) {
525 return data.isResourceExist(resourceURI);
528 public boolean isAnyResourceExist() {
529 return data.isAnyResourceExist();
532 public boolean createSingleResource(SingleResource resource,
533 Map<String, SimulatorResourceAttribute> attributes)
534 throws SimulatorException {
535 if (null == resource) {
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)) {
550 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
551 resource.setSimulatorResource(jSimulatorSingleResource);
553 // Cancel discoverable property if requested by user.
554 if (!resource.isDiscoverable()) {
555 jSimulatorSingleResource.setDiscoverable(false);
558 // Cancel observable property if requested by user.
559 if (!resource.isObservable()) {
560 jSimulatorSingleResource.setObservable(false);
563 // Set the model change listener.
564 jSimulatorSingleResource
565 .setResourceModelChangeListener(resourceModelChangeListener);
567 // Set the observer listener if the resource is observable.
568 if (resource.isObservable()) {
569 jSimulatorSingleResource.setObserverListener(observer);
573 if (null != attributes && !attributes.isEmpty()) {
574 SimulatorResourceAttribute value;
575 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
577 value = entry.getValue();
579 jSimulatorSingleResource.addAttribute(value);
582 // Get the resource model java object reference.
583 resource.setResourceModel(jSimulatorSingleResource
584 .getResourceModel());
586 resource.createResourceRepresentation(jSimulatorSingleResource
590 // Set the resource interfaces.
591 jSimulatorSingleResource
592 .setInterface(Utility.convertSetToVectorString(resource
593 .getResourceInterfaces()));
595 // Register the resource with the platform.
596 jSimulatorSingleResource.start();
597 resource.setStarted(true);
598 } catch (SimulatorException e) {
602 .log(Level.ERROR.ordinal(), new Date(),
603 Utility.getSimulatorErrorString(e, null));
607 // Add to local cache.
608 data.addResource(resource);
610 // Update UI listeners
611 UiListenerHandler.getInstance().resourceCreatedUINotification(
612 ResourceType.SINGLE);
617 public Resource createResourceByRAML(String configFilePath)
618 throws SimulatorException {
619 Resource resource = null;
621 // Create the resource
622 SimulatorResource jSimulatorResource = SimulatorManager
623 .createResource(configFilePath);
624 if (null == jSimulatorResource) {
627 if (jSimulatorResource instanceof SimulatorSingleResource) {
628 resource = new SingleResource();
632 resource.setSimulatorResource(jSimulatorResource);
634 // Fetch and locally store the resource name and uri.
635 String uri = jSimulatorResource.getURI();
636 if (null == uri || uri.trim().isEmpty()) {
639 resource.setResourceURI(uri.trim());
641 String name = jSimulatorResource.getName();
642 if (null == name || name.trim().isEmpty()) {
645 resource.setResourceName(name.trim());
646 } catch (SimulatorException e) {
650 .log(Level.ERROR.ordinal(), new Date(),
651 Utility.getSimulatorErrorString(e, null));
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.
664 public boolean completeSingleResourceCreationByRAML(Resource resource,
665 String uri, String name, boolean multiInstance)
666 throws SimulatorException {
667 if (null == resource || !(resource instanceof SingleResource)) {
671 SingleResource singleRes = (SingleResource) resource;
673 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
674 .getSimulatorResource();
675 if (null == jSimulatorSingleResource) {
679 // Update resource URI and Name if they are changed.
680 String newUri = uri.trim();
681 String newName = name.trim();
684 singleRes.setResourceURI(newUri);
685 singleRes.setResourceName(newName);
687 if (!singleRes.getResourceURI().equals(newUri)) {
688 jSimulatorSingleResource.setURI(newUri);
689 singleRes.setResourceURI(newUri);
691 if (!singleRes.getResourceName().equals(newName)) {
692 jSimulatorSingleResource.setName(newName);
693 singleRes.setResourceName(newName);
697 // Set the model change listener.
698 jSimulatorSingleResource
699 .setResourceModelChangeListener(resourceModelChangeListener);
701 // Set the observer listener if the resource is observable.
702 if (jSimulatorSingleResource.isObservable()) {
703 jSimulatorSingleResource.setObserverListener(observer);
704 singleRes.setObservable(true);
707 // Fetch the resource model.
708 SimulatorResourceModel jResModel = jSimulatorSingleResource
710 if (null == jResModel) {
713 singleRes.setResourceModel(jResModel);
715 // Fetch the basic details of the resource.
716 singleRes.setResourceType(jSimulatorSingleResource
719 .setResourceInterfaces(Utility
720 .convertVectorToSet(jSimulatorSingleResource
723 // Fetch the resource attributes.
724 singleRes.createResourceRepresentation(jSimulatorSingleResource
727 // Register the resource with the platform.
728 jSimulatorSingleResource.start();
729 singleRes.setStarted(true);
731 // Add to local cache.
732 data.addResource(singleRes);
734 // Update UI listeners for single instance creation
736 UiListenerHandler.getInstance().resourceCreatedUINotification(
737 ResourceType.SINGLE);
738 } catch (Exception e) {
742 .log(Level.ERROR.ordinal(), new Date(),
743 Utility.getSimulatorErrorString(e, null));
749 public Set<SingleResource> createSingleResourceMultiInstances(
750 String configFile, int count, IProgressMonitor progressMonitor)
751 throws SimulatorException {
752 Set<SingleResource> resultSet;
754 resultSet = new HashSet<SingleResource>();
755 Vector<SimulatorResource> jSimulatorResources = SimulatorManager
756 .createResource(configFile, count);
757 if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
760 SimulatorSingleResource jResource;
761 SingleResource resource;
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);
771 jResource = (SimulatorSingleResource) jSimulatorResource;
772 resource = new SingleResource();
773 resource.setSimulatorResource(jResource);
775 result = completeSingleResourceCreationByRAML(resource,
776 jResource.getURI(), jResource.getName(), true);
778 resultSet.add(resource);
780 } catch (SimulatorException eInner) {
784 .log(Level.ERROR.ordinal(),
786 Utility.getSimulatorErrorString(eInner,
789 progressMonitor.worked(1);
791 } catch (SimulatorException eOuter) {
795 .log(Level.ERROR.ordinal(), new Date(),
796 Utility.getSimulatorErrorString(eOuter, null));
802 public List<Resource> getResourceList() {
803 List<Resource> resourceList = data.getResources();
804 if (null == resourceList) {
808 Collections.sort(resourceList, Utility.resourceComparator);
813 public List<SingleResource> getSingleResourceList() {
814 List<SingleResource> resourceList = data.getSingleResources();
815 if (null == resourceList) {
819 Collections.sort(resourceList, Utility.singleResourceComparator);
824 public void removeSingleResources(Set<SingleResource> resources)
825 throws SimulatorException {
826 if (null == resources) {
829 Iterator<SingleResource> itr = resources.iterator();
830 while (itr.hasNext()) {
831 removeResource(itr.next());
835 public void removeResource(Resource res) throws SimulatorException {
836 // Unregister the resource from the platform.
837 SimulatorResource simRes = res.getSimulatorResource();
840 } catch (SimulatorException e) {
844 .log(Level.ERROR.ordinal(), new Date(),
845 Utility.getSimulatorErrorString(e, null));
849 // Delete this resource
850 data.deleteResource(res);
853 public boolean isUriUnique(List<MetaProperty> properties) {
854 if (null == properties) {
858 Iterator<MetaProperty> itr = properties.iterator();
859 while (itr.hasNext()) {
861 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
862 String uri = prop.getPropValue();
863 return !data.isResourceExist(uri);
869 public void resourceSelectionChanged(final Resource selectedResource) {
873 if (null != selectedResource) {
874 setCurrentResourceInSelection(selectedResource);
876 setCurrentResourceInSelection(null);
878 // Notify all observers for resource selection change event
879 UiListenerHandler.getInstance()
880 .resourceSelectionChangedUINotification(
886 public List<MetaProperty> getMetaProperties(Resource resource) {
887 if (null != resource) {
891 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
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()) {
905 Iterator<String> itr = ifTypes.iterator();
906 while (itr.hasNext()) {
907 propValue += itr.next();
918 if (null != propValue) {
919 metaPropertyList.add(new MetaProperty(propName, propValue));
922 return metaPropertyList;
927 public boolean startResource(Resource resource) throws SimulatorException {
928 if (null == resource) {
931 SimulatorResource server = resource.getSimulatorResource();
932 if (null == server) {
937 resource.setStarted(true);
938 } catch (SimulatorException e) {
942 .log(Level.ERROR.ordinal(),
944 "There is an error while starting the resource.\n"
945 + Utility.getSimulatorErrorString(e, null));
951 public boolean stopResource(Resource resource) throws SimulatorException {
952 if (null == resource) {
955 SimulatorResource server = resource.getSimulatorResource();
956 if (null == server) {
961 resource.setStarted(false);
962 } catch (SimulatorException e) {
966 .log(Level.ERROR.ordinal(),
968 "There is an error while stopping the resource.\n"
969 + Utility.getSimulatorErrorString(e, null));
975 public boolean changeResourceName(Resource resource, String newName)
976 throws SimulatorException {
977 if (null == resource || null == newName) {
981 if (!stopResource(resource)) {
985 SimulatorResource server = resource.getSimulatorResource();
987 server.setName(newName);
988 resource.setResourceName(newName);
989 } catch (SimulatorException e) {
993 .log(Level.ERROR.ordinal(),
995 "There is an error while changing the resource name.\n"
996 + Utility.getSimulatorErrorString(e, null));
1000 if (!startResource(resource)) {
1007 public boolean changeResourceURI(Resource resource, String newURI)
1008 throws SimulatorException {
1009 if (null == resource || null == newURI) {
1013 if (!stopResource(resource)) {
1017 String curURI = resource.getResourceURI();
1018 setResourceURI(resource, newURI);
1021 if (!startResource(resource)) {
1024 } catch (SimulatorException e) {
1025 setResourceURI(resource, curURI);
1031 public void setResourceURI(Resource resource, String newURI)
1032 throws SimulatorException {
1033 String curURI = resource.getResourceURI();
1034 SimulatorResource server = resource.getSimulatorResource();
1036 server.setURI(newURI);
1037 data.changeResourceURI(resource, curURI, newURI);
1038 } catch (SimulatorException e) {
1042 .log(Level.ERROR.ordinal(),
1044 "There is an error while changing the resource URI.\n"
1045 + Utility.getSimulatorErrorString(e, null));
1050 public boolean updateResourceProperties(Resource resource,
1051 List<MetaProperty> properties, boolean uriChanged,
1052 boolean nameChanged) throws SimulatorException {
1053 if (null == resource || null == properties) {
1057 // Updating the properties
1058 Iterator<MetaProperty> itr = properties.iterator();
1059 MetaProperty property;
1062 String resName = null;
1063 String resURI = null;
1064 while (itr.hasNext()) {
1065 property = itr.next();
1066 if (null == property) {
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)) {
1079 if (!changeResourceName(resource, resName)) {
1083 // Notify UI Listeners
1084 UiListenerHandler.getInstance().propertiesChangedUINotification(
1089 if (!changeResourceURI(resource, resURI)) {
1097 public boolean updateResourceInterfaces(Resource resource,
1098 Set<String> newIfSet) throws SimulatorException {
1099 if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1102 SimulatorResource jResource = resource.getSimulatorResource();
1103 if (null == jResource) {
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;
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.
1123 if (resourceRestartRequired) {
1124 stopResource(resource);
1125 jResource.setInterface(Utility
1126 .convertSetToVectorString(newIfSet));
1127 startResource(resource);
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);
1138 } catch (SimulatorException e) {
1142 .log(Level.ERROR.ordinal(),
1144 "There is an error while changing the interface types."
1145 + Utility.getSimulatorErrorString(e, null));
1149 // Set the resource interfaces.
1150 resource.setResourceInterfaces(newIfSet);
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) {
1162 simRes.updateAttribute(attributeName, value);
1164 } catch (SimulatorException e) {
1168 .log(Level.ERROR.ordinal(), new Date(),
1169 Utility.getSimulatorErrorString(e, null));
1176 public boolean isResourceStarted(Resource resource) {
1177 if (null == resource) {
1180 return resource.isStarted();
1183 public boolean isPropertyValueInvalid(Resource resource,
1184 List<MetaProperty> properties, String propName) {
1185 if (null == resource || null == properties || null == propName) {
1188 boolean invalid = false;
1190 Iterator<MetaProperty> itr = properties.iterator();
1191 while (itr.hasNext()) {
1193 if (prop.getPropName().equals(propName)) {
1194 String value = prop.getPropValue();
1195 if (propName.equals(Constants.RESOURCE_URI)) {
1196 if (!Utility.isUriValid(value)) {
1200 if (null == value || value.trim().isEmpty()) {
1209 public boolean isPropValueChanged(Resource resource,
1210 List<MetaProperty> properties, String propName) {
1211 if (null == resource || null == properties || null == propName) {
1214 boolean changed = false;
1217 Iterator<MetaProperty> itr = properties.iterator();
1218 while (itr.hasNext()) {
1220 if (prop.getPropName().equals(propName)) {
1221 oldValue = getPropertyValueFromResource(resource, propName);
1222 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1231 private String getPropertyValueFromResource(Resource resource,
1233 if (null == resource || null == propName) {
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();
1247 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1251 AttributeProperty prop = att.property();
1256 if (prop.getType() == Type.INTEGER) {
1257 IntegerProperty intProperty = prop.asInteger();
1258 if (null != intProperty) {
1259 return (intProperty.hasRange() || intProperty.hasValues());
1263 } else if (prop.getType() == Type.DOUBLE) {
1264 DoubleProperty dblProperty = prop.asDouble();
1265 if (null != dblProperty) {
1266 return (dblProperty.hasRange() || dblProperty.hasValues());
1270 } else if (prop.getType() == Type.STRING) {
1271 StringProperty stringProperty = prop.asString();
1272 if (null != stringProperty) {
1273 return stringProperty.hasValues();
1282 public int startAutomation(SingleResource resource,
1283 AttributeElement attribute, AutoUpdateType autoType,
1284 int autoUpdateInterval) {
1286 if (null != resource && null != attribute) {
1287 SimulatorSingleResource server = (SimulatorSingleResource) resource
1288 .getSimulatorResource();
1289 if (null != server) {
1290 String attrName = attribute.getSimulatorResourceAttribute()
1293 autoId = server.startAttributeUpdation(attrName, autoType,
1294 autoUpdateInterval, automationListener);
1295 } catch (SimulatorException e) {
1299 .log(Level.ERROR.ordinal(),
1301 "[" + e.getClass().getSimpleName() + "]"
1302 + e.code().toString() + "-"
1307 attribute.setAutoUpdateId(autoId);
1308 attribute.setAutoUpdateType(autoType);
1309 attribute.setAutoUpdateInterval(autoUpdateInterval);
1310 attribute.setAutoUpdateState(true);
1311 resource.setAttributeAutomationInProgress(true);
1318 public void stopAutomation(SingleResource resource, AttributeElement att,
1320 if (null != resource) {
1321 SimulatorSingleResource server = (SimulatorSingleResource) resource
1322 .getSimulatorResource();
1323 if (null != server) {
1325 server.stopUpdation(autoId);
1326 } catch (SimulatorException e) {
1330 .log(Level.ERROR.ordinal(),
1332 "[" + e.getClass().getSimpleName() + "]"
1333 + e.code().toString() + "-"
1337 // Change the automation status
1338 att.setAutoUpdateState(false);
1339 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1344 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1345 int autoUpdateInterval, final SingleResource resource) {
1346 if (null == resource) {
1349 boolean status = false;
1350 // Invoke the native automation method
1351 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1352 .getSimulatorResource();
1353 if (null != resourceServer) {
1356 autoId = resourceServer.startResourceUpdation(autoType,
1357 autoUpdateInterval, automationListener);
1358 } catch (SimulatorException e) {
1362 .log(Level.ERROR.ordinal(), new Date(),
1363 Utility.getSimulatorErrorString(e, null));
1367 // Automation request accepted.
1368 changeResourceLevelAutomationStatus(resource, true);
1370 resource.setAutomationId(autoId);
1372 // Notify the UI listeners in a different thread.
1373 Thread notifyThread = new Thread() {
1375 UiListenerHandler.getInstance()
1376 .resourceAutomationStartedUINotification(
1380 notifyThread.setPriority(Thread.MAX_PRIORITY);
1381 notifyThread.start();
1389 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1390 if (null == resource) {
1393 final int autoId = resource.getAutomationId();
1397 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1398 .getSimulatorResource();
1399 if (null == resourceServer) {
1402 // Call native method
1404 resourceServer.stopUpdation(autoId);
1405 } catch (SimulatorException e) {
1409 .log(Level.ERROR.ordinal(), new Date(),
1410 Utility.getSimulatorErrorString(e, null));
1414 // Notify the UI Listeners. Invoke the automation complete callback.
1415 Thread stopThread = new Thread() {
1417 automationListener.onUpdateComplete(resource.getResourceURI(),
1425 private boolean isAnyAttributeInAutomation(SingleResource resource) {
1426 if (null == resource || null == resource.getResourceRepresentation()) {
1430 Map<String, AttributeElement> attributes = resource
1431 .getResourceRepresentation().getAttributes();
1432 if (null == attributes || 0 == attributes.size())
1435 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1436 if (entry.getValue().isAutoUpdateInProgress())
1443 // Changes the automation state of the resource and its attributes
1444 private void changeResourceLevelAutomationStatus(SingleResource resource,
1447 if (null == resource || null == resource.getResourceRepresentation()) {
1451 Map<String, AttributeElement> attributes = resource
1452 .getResourceRepresentation().getAttributes();
1453 if (null == attributes || 0 == attributes.size())
1456 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1457 entry.getValue().setAutoUpdateState(status);
1460 resource.setResourceAutomationInProgress(status);
1463 private AttributeElement getAttributeWithGivenAutomationId(
1464 SingleResource resource, int automationId) {
1465 if (null == resource || null == resource.getResourceRepresentation()) {
1469 Map<String, AttributeElement> attributes = resource
1470 .getResourceRepresentation().getAttributes();
1471 if (null == attributes || 0 == attributes.size())
1474 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1475 if (automationId == entry.getValue().getAutoUpdateId())
1476 return entry.getValue();
1482 public boolean isResourceAutomationStarted(SingleResource resource) {
1483 boolean status = false;
1484 if (null != resource) {
1485 status = resource.isResourceAutomationInProgress();
1490 public boolean isAttributeAutomationStarted(SingleResource resource) {
1491 if (null == resource) {
1494 return resource.isAttributeAutomationInProgress();
1497 public void notifyObserverRequest(Resource resource, int observerId) {
1498 if (null == resource) {
1501 SimulatorResource simulatorResource = resource.getSimulatorResource();
1502 if (null == simulatorResource) {
1506 simulatorResource.notifyObserver(observerId);
1507 } catch (SimulatorException e) {
1511 .log(Level.ERROR.ordinal(), new Date(),
1512 Utility.getSimulatorErrorString(e, null));
1516 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1521 AttributeValue val = att.value();
1526 TypeInfo type = val.typeInfo();
1528 AttributeProperty prop = att.property();
1533 List<String> values = new ArrayList<String>();
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()) {
1544 IntegerProperty intProperty = childProp
1546 if (null != intProperty) {
1547 values.addAll(getAllValues(intProperty,
1552 DoubleProperty dblProperty = childProp
1554 if (null != dblProperty) {
1555 values.addAll(getAllValues(dblProperty,
1560 BooleanProperty boolProperty = childProp
1562 if (null != boolProperty) {
1563 values.addAll(getAllValues(boolProperty,
1568 StringProperty stringProperty = childProp
1570 if (null != stringProperty) {
1571 values.addAll(getAllValues(stringProperty,
1581 switch (prop.getType()) {
1583 IntegerProperty intProperty = prop.asInteger();
1584 if (null != intProperty) {
1585 values.addAll(getAllValues(intProperty,
1590 DoubleProperty dblProperty = prop.asDouble();
1591 if (null != dblProperty) {
1592 values.addAll(getAllValues(dblProperty, Type.DOUBLE));
1596 BooleanProperty boolProperty = prop.asBoolean();
1597 if (null != boolProperty) {
1598 values.addAll(getAllValues(boolProperty,
1603 StringProperty stringProperty = prop.asString();
1604 if (null != stringProperty) {
1605 values.addAll(getAllValues(stringProperty,
1618 public List<String> getAllValues(IntegerProperty intProperty,
1619 AttributeProperty.Type type) {
1620 List<String> values = new ArrayList<String>();
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));
1628 } else if (intProperty.hasValues()) {
1629 for (Integer val : intProperty.getValues()) {
1630 values.add(String.valueOf(val));
1633 // Adding the default value.
1634 values.add(String.valueOf(intProperty.getDefaultValue()));
1639 public List<String> getAllValues(DoubleProperty dblProperty,
1640 AttributeProperty.Type type) {
1641 List<String> values = new ArrayList<String>();
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));
1649 } else if (dblProperty.hasValues()) {
1650 for (Double val : dblProperty.getValues()) {
1651 values.add(String.valueOf(val));
1654 // Adding the default value.
1655 values.add(String.valueOf(dblProperty.getDefaultValue()));
1660 public List<String> getAllValues(BooleanProperty boolProperty,
1661 AttributeProperty.Type type) {
1662 List<String> values = new ArrayList<String>();
1664 values.add("false");
1668 public List<String> getAllValues(StringProperty stringProperty,
1669 AttributeProperty.Type type) {
1670 List<String> values = new ArrayList<String>();
1672 if (stringProperty.hasValues()) {
1673 for (String val : stringProperty.getValues()) {
1674 values.add(String.valueOf(val));
1677 // Adding the default value.
1678 values.add(String.valueOf(stringProperty.getDefaultValue()));
1683 public int getResourceCount() {
1684 return data.getResourceCount();
1687 public void shutdown() {
1688 threadHandle.interrupt();