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 java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.List;
28 import java.util.Vector;
30 import oic.simulator.serviceprovider.Activator;
31 import oic.simulator.serviceprovider.model.AttributeElement;
32 import oic.simulator.serviceprovider.model.MetaProperty;
33 import oic.simulator.serviceprovider.model.Resource;
34 import oic.simulator.serviceprovider.model.ResourceType;
35 import oic.simulator.serviceprovider.model.SingleResource;
36 import oic.simulator.serviceprovider.utils.Constants;
37 import oic.simulator.serviceprovider.utils.Utility;
39 import org.eclipse.core.runtime.IProgressMonitor;
40 import org.eclipse.swt.widgets.Display;
41 import org.oic.simulator.ArrayProperty;
42 import org.oic.simulator.AttributeProperty;
43 import org.oic.simulator.AttributeProperty.Type;
44 import org.oic.simulator.AttributeValue;
45 import org.oic.simulator.AttributeValue.TypeInfo;
46 import org.oic.simulator.AttributeValue.ValueType;
47 import org.oic.simulator.BooleanProperty;
48 import org.oic.simulator.DeviceInfo;
49 import org.oic.simulator.DeviceListener;
50 import org.oic.simulator.DoubleProperty;
51 import org.oic.simulator.ILogger.Level;
52 import org.oic.simulator.IntegerProperty;
53 import org.oic.simulator.PlatformInfo;
54 import org.oic.simulator.SimulatorException;
55 import org.oic.simulator.SimulatorManager;
56 import org.oic.simulator.SimulatorResourceAttribute;
57 import org.oic.simulator.SimulatorResourceModel;
58 import org.oic.simulator.StringProperty;
59 import org.oic.simulator.server.Observer;
60 import org.oic.simulator.server.SimulatorResource;
61 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
62 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
63 import org.oic.simulator.server.SimulatorResource.ObserverListener;
64 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
65 import org.oic.simulator.server.SimulatorSingleResource;
68 * This class acts as an interface between the simulator java SDK and the
69 * various UI modules. It maintains all the details of resources and provides
70 * other UI modules with the information required. It also handles model change,
71 * automation, and observer related events from native layer and propagates
72 * those events to the registered UI listeners.
74 public class ResourceManager {
78 private Resource currentResourceInSelection;
80 private ResourceModelChangeListener resourceModelChangeListener;
82 private AutoUpdateListener automationListener;
84 private ObserverListener observer;
86 private DeviceListener deviceListener;
88 private NotificationSynchronizerThread synchronizerThread;
90 private Thread threadHandle;
92 private DeviceInfo deviceInfo;
93 private PlatformInfo platformInfo;
95 private String deviceName;
97 public ResourceManager() {
100 deviceListener = new DeviceListener() {
103 public void onDeviceFound(final String host,
104 final DeviceInfo deviceInfo) {
105 if (null != ResourceManager.this.deviceInfo
106 || null == deviceInfo || null == host) {
109 synchronizerThread.addToQueue(new Runnable() {
112 String rcvdDeviceName = deviceInfo.getName();
113 if (null == rcvdDeviceName) {
116 if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
117 ResourceManager.this.deviceInfo = deviceInfo;
119 // Notify the UI Listeners
120 UiListenerHandler.getInstance()
121 .deviceInfoReceivedNotification();
128 resourceModelChangeListener = new ResourceModelChangeListener() {
131 public void onResourceModelChanged(final String resourceURI,
132 final SimulatorResourceModel resourceModelN) {
133 synchronizerThread.addToQueue(new Runnable() {
137 if (null == resourceURI || null == resourceModelN) {
141 Display.getDefault().asyncExec(new Runnable() {
144 Resource resource = data
145 .getResourceByURI(resourceURI);
146 if (null != resource) {
148 resource.updateResourceRepresentation(resourceModelN);
149 } catch (NumberFormatException e) {
153 .log(Level.ERROR.ordinal(),
155 "Error while trying to update the attributes.\n"
157 .getSimulatorErrorString(
169 automationListener = new AutoUpdateListener() {
172 public void onUpdateComplete(final String resourceURI,
173 final int automationId) {
174 synchronizerThread.addToQueue(new Runnable() {
178 SingleResource resource = data
179 .getSingleResourceByURI(resourceURI);
180 if (null == resource) {
183 // Checking whether this notification is for an
184 // attribute or a resource
185 if (resource.isResourceAutomationInProgress()) {
186 changeResourceLevelAutomationStatus(resource, false);
187 // Notify the UI listeners
188 UiListenerHandler.getInstance()
189 .automationCompleteUINotification(resource,
191 } else if (resource.isAttributeAutomationInProgress()) {
192 // Find the attribute with the given automation id
193 final AttributeElement attribute = getAttributeWithGivenAutomationId(
194 resource, automationId);
195 if (null != attribute) {
196 attribute.setAutoUpdateState(false);
197 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
199 // Setting the attribute automation status to
201 resource.setAttributeAutomationInProgress(false);
209 observer = new ObserverListener() {
211 public void onObserverChanged(final String resourceURI,
212 final int status, final Observer observer) {
216 if (null == resourceURI || null == observer) {
219 Resource resource = data.getResourceByURI(resourceURI);
220 if (null == resource) {
223 // Update the observers information
225 resource.addObserverInfo(observer);
227 resource.removeObserverInfo(observer);
229 // Notify the UI listeners
230 UiListenerHandler.getInstance()
231 .observerListChangedUINotification(resource);
237 public void onObserverAdded(String resourceURI, Observer observer) {
238 onObserverChanged(resourceURI, 0, observer);
242 public void onObserverRemoved(String resourceURI, Observer observer) {
243 onObserverChanged(resourceURI, 1, observer);
247 synchronizerThread = new NotificationSynchronizerThread();
248 threadHandle = new Thread(synchronizerThread);
249 threadHandle.setName("Simulator service provider event queue");
250 threadHandle.start();
252 // Set the default device name.
253 deviceName = "IoTivity Simulator";
255 SimulatorManager.setDeviceInfo(deviceName);
256 } catch (SimulatorException e) {
260 .log(Level.ERROR.ordinal(),
262 "Error while registering the device info.\n"
263 + Utility.getSimulatorErrorString(e, null));
266 // Set the default platform information.
267 platformInfo = new PlatformInfo();
268 platformInfo.setPlatformID("Samsung Platform Identifier");
269 platformInfo.setManufacturerName("Samsung");
270 platformInfo.setManufacturerUrl("www.samsung.com");
271 platformInfo.setModelNumber("Samsung Model Num01");
272 platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
273 platformInfo.setPlatformVersion("PlatformVersion01");
274 platformInfo.setOperationSystemVersion("OSVersion01");
275 platformInfo.setHardwareVersion("HardwareVersion01");
276 platformInfo.setFirmwareVersion("FirwareVersion01");
277 platformInfo.setSupportUrl("http://www.samsung.com/support");
278 platformInfo.setSystemTime("2015-09-10T11:10:30Z");
280 SimulatorManager.setPlatformInfo(platformInfo);
281 } catch (SimulatorException e) {
285 .log(Level.ERROR.ordinal(),
287 "Error while registering the platform info.\n"
288 + Utility.getSimulatorErrorString(e, null));
291 // Get the device information to show other details of the device in UI.
293 SimulatorManager.findDevices("", deviceListener);
294 } catch (SimulatorException e) {
298 .log(Level.ERROR.ordinal(),
300 "Failed to get the local device information.\n"
301 + Utility.getSimulatorErrorString(e, null));
305 private static class NotificationSynchronizerThread implements Runnable {
307 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
311 while (!Thread.interrupted()) {
312 synchronized (this) {
314 while (notificationQueue.isEmpty()) {
318 } catch (InterruptedException e) {
324 synchronized (this) {
325 thread = notificationQueue.pop();
329 } catch (Exception e) {
330 if (e instanceof InterruptedException) {
338 public void addToQueue(Runnable event) {
339 synchronized (this) {
340 notificationQueue.add(event);
346 public void setDeviceInfo(List<MetaProperty> metaProperties) {
347 if (null == metaProperties || metaProperties.size() < 1) {
350 Iterator<MetaProperty> itr = metaProperties.iterator();
354 boolean found = false;
355 while (itr.hasNext()) {
357 propName = prop.getPropName();
358 propValue = prop.getPropValue();
359 if (propName.equals(Constants.DEVICE_NAME)) {
360 this.deviceName = propValue;
371 SimulatorManager.setDeviceInfo(deviceName);
372 } catch (SimulatorException e) {
376 .log(Level.ERROR.ordinal(),
378 "Error while registering the device info.\n"
379 + Utility.getSimulatorErrorString(e, null));
383 public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
384 if (null == metaProperties || metaProperties.size() < 1) {
388 Iterator<MetaProperty> itr = metaProperties.iterator();
392 while (itr.hasNext()) {
394 propName = prop.getPropName();
395 propValue = prop.getPropValue();
396 if (propName.equals(Constants.DEVICE_NAME)) {
397 if (null == propValue || propValue.length() < 1) {
406 public List<MetaProperty> getDeviceInfo() {
407 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
408 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
409 if (null != deviceInfo) {
410 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
412 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
413 deviceInfo.getSpecVersion()));
414 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
415 deviceInfo.getDataModelVersion()));
417 return metaProperties;
420 public List<MetaProperty> getPlatformInfo() {
421 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
422 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
424 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
425 platformInfo.getManufacturerName()));
426 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
427 platformInfo.getManufacturerUrl()));
428 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
429 platformInfo.getModelNumber()));
430 metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
431 platformInfo.getDateOfManufacture()));
432 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
433 platformInfo.getPlatformVersion()));
434 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
435 platformInfo.getOperationSystemVersion()));
436 metaProperties.add(new MetaProperty(
437 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
438 .getHardwareVersion()));
439 metaProperties.add(new MetaProperty(
440 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
441 .getFirmwareVersion()));
442 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
443 platformInfo.getSupportUrl()));
444 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
445 platformInfo.getSystemTime()));
446 return metaProperties;
449 public void setPlatformInfo(List<MetaProperty> metaProperties) {
450 if (null == metaProperties || metaProperties.size() < 1) {
453 Iterator<MetaProperty> itr = metaProperties.iterator();
457 while (itr.hasNext()) {
459 propName = prop.getPropName();
460 propValue = prop.getPropValue();
461 if (propName.equals(Constants.PLATFORM_ID)) {
462 platformInfo.setPlatformID(propValue);
463 } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
464 platformInfo.setManufacturerName(propValue);
465 } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
466 platformInfo.setManufacturerUrl(propValue);
467 } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
468 platformInfo.setModelNumber(propValue);
469 } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
470 platformInfo.setDateOfManufacture(propValue);
471 } else if (propName.equals(Constants.PLATFORM_VERSION)) {
472 platformInfo.setPlatformVersion(propValue);
473 } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
474 platformInfo.setOperationSystemVersion(propValue);
475 } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
476 platformInfo.setHardwareVersion(propValue);
477 } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
478 platformInfo.setFirmwareVersion(propValue);
479 } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
480 platformInfo.setSupportUrl(propValue);
481 } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
482 platformInfo.setSystemTime(propValue);
486 SimulatorManager.setPlatformInfo(platformInfo);
487 } catch (SimulatorException e) {
491 .log(Level.ERROR.ordinal(),
493 "Error while registering the platform info.\n"
494 + Utility.getSimulatorErrorString(e, null));
498 public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
499 if (null == metaProperties || metaProperties.size() < 1) {
502 Iterator<MetaProperty> itr = metaProperties.iterator();
505 while (itr.hasNext()) {
507 propValue = prop.getPropValue();
508 if (null == propValue || propValue.length() < 1) {
515 public synchronized Resource getCurrentResourceInSelection() {
516 return currentResourceInSelection;
519 public synchronized void setCurrentResourceInSelection(Resource resource) {
520 this.currentResourceInSelection = resource;
523 public boolean isResourceExist(String resourceURI) {
524 return data.isResourceExist(resourceURI);
527 public boolean isAnyResourceExist() {
528 return data.isAnyResourceExist();
531 public boolean createSingleResource(SingleResource resource,
532 Map<String, SimulatorResourceAttribute> attributes)
533 throws SimulatorException {
534 if (null == resource) {
539 // Create the resource.
540 SimulatorResource jSimulatorResource = SimulatorManager
541 .createResource(SimulatorResource.Type.SINGLE,
542 resource.getResourceName(),
543 resource.getResourceURI(),
544 resource.getResourceType());
545 if (null == jSimulatorResource
546 || !(jSimulatorResource instanceof SimulatorSingleResource)) {
549 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
550 resource.setSimulatorResource(jSimulatorSingleResource);
552 // Cancel discoverable property if requested by user.
553 if (!resource.isDiscoverable()) {
554 jSimulatorSingleResource.setDiscoverable(false);
557 // Cancel observable property if requested by user.
558 if (!resource.isObservable()) {
559 jSimulatorSingleResource.setObservable(false);
562 // Set the model change listener.
563 jSimulatorSingleResource
564 .setResourceModelChangeListener(resourceModelChangeListener);
566 // Set the observer listener if the resource is observable.
567 if (resource.isObservable()) {
568 jSimulatorSingleResource.setObserverListener(observer);
572 if (null != attributes && !attributes.isEmpty()) {
573 SimulatorResourceAttribute value;
574 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
576 value = entry.getValue();
578 jSimulatorSingleResource.addAttribute(value);
581 // Get the resource model java object reference.
582 resource.setResourceModel(jSimulatorSingleResource
583 .getResourceModel());
585 resource.createResourceRepresentation(jSimulatorSingleResource
589 // Set the resource interfaces.
590 jSimulatorSingleResource
591 .setInterface(Utility.convertSetToVectorString(resource
592 .getResourceInterfaces()));
594 // Register the resource with the platform.
595 jSimulatorSingleResource.start();
596 resource.setStarted(true);
597 } catch (SimulatorException e) {
601 .log(Level.ERROR.ordinal(), new Date(),
602 Utility.getSimulatorErrorString(e, null));
606 // Add to local cache.
607 data.addResource(resource);
609 // Update UI listeners
610 UiListenerHandler.getInstance().resourceCreatedUINotification(
611 ResourceType.SINGLE);
616 public Resource createResourceByRAML(String configFilePath)
617 throws SimulatorException {
618 Resource resource = null;
620 // Create the resource
621 SimulatorResource jSimulatorResource = SimulatorManager
622 .createResource(configFilePath);
623 if (null == jSimulatorResource) {
626 if (jSimulatorResource instanceof SimulatorSingleResource) {
627 resource = new SingleResource();
631 resource.setSimulatorResource(jSimulatorResource);
633 // Fetch and locally store the resource name and uri.
634 String uri = jSimulatorResource.getURI();
635 if (null == uri || uri.trim().isEmpty()) {
638 resource.setResourceURI(uri.trim());
640 String name = jSimulatorResource.getName();
641 if (null == name || name.trim().isEmpty()) {
644 resource.setResourceName(name.trim());
645 } catch (SimulatorException e) {
649 .log(Level.ERROR.ordinal(), new Date(),
650 Utility.getSimulatorErrorString(e, null));
657 * This method can set/change the resource uri and name of an already
658 * created resource which is not yet registered with the platform. This
659 * method registers the model change and observer listeners, registers the
660 * resource, fetches the resource attributes, updates the local cache and
661 * notifies the UI listeners.
663 public boolean completeSingleResourceCreationByRAML(Resource resource,
664 String uri, String name, boolean multiInstance)
665 throws SimulatorException {
666 if (null == resource || !(resource instanceof SingleResource)) {
670 SingleResource singleRes = (SingleResource) resource;
672 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
673 .getSimulatorResource();
674 if (null == jSimulatorSingleResource) {
678 // Update resource URI and Name if they are changed.
679 String newUri = uri.trim();
680 String newName = name.trim();
683 singleRes.setResourceURI(newUri);
684 singleRes.setResourceName(newName);
686 if (!singleRes.getResourceURI().equals(newUri)) {
687 jSimulatorSingleResource.setURI(newUri);
688 singleRes.setResourceURI(newUri);
690 if (!singleRes.getResourceName().equals(newName)) {
691 jSimulatorSingleResource.setName(newName);
692 singleRes.setResourceName(newName);
696 // Set the model change listener.
697 jSimulatorSingleResource
698 .setResourceModelChangeListener(resourceModelChangeListener);
700 // Set the observer listener if the resource is observable.
701 if (jSimulatorSingleResource.isObservable()) {
702 jSimulatorSingleResource.setObserverListener(observer);
703 singleRes.setObservable(true);
706 // Fetch the resource model.
707 SimulatorResourceModel jResModel = jSimulatorSingleResource
709 if (null == jResModel) {
712 singleRes.setResourceModel(jResModel);
714 // Fetch the basic details of the resource.
715 singleRes.setResourceType(jSimulatorSingleResource
718 .setResourceInterfaces(Utility
719 .convertVectorToSet(jSimulatorSingleResource
722 // Fetch the resource attributes.
723 singleRes.createResourceRepresentation(jSimulatorSingleResource
726 // Register the resource with the platform.
727 jSimulatorSingleResource.start();
728 singleRes.setStarted(true);
730 // Add to local cache.
731 data.addResource(singleRes);
733 // Update UI listeners for single instance creation
735 UiListenerHandler.getInstance().resourceCreatedUINotification(
736 ResourceType.SINGLE);
737 } catch (Exception e) {
741 .log(Level.ERROR.ordinal(), new Date(),
742 Utility.getSimulatorErrorString(e, null));
748 public Set<SingleResource> createSingleResourceMultiInstances(
749 String configFile, int count, IProgressMonitor progressMonitor)
750 throws SimulatorException {
751 Set<SingleResource> resultSet;
753 resultSet = new HashSet<SingleResource>();
754 Vector<SimulatorResource> jSimulatorResources = SimulatorManager
755 .createResource(configFile, count);
756 if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
759 SimulatorSingleResource jResource;
760 SingleResource resource;
762 for (SimulatorResource jSimulatorResource : jSimulatorResources) {
763 // If the resource creation progress is canceled, then stop the
764 // creation and stop/delete
765 // the resources created already.
766 if (progressMonitor.isCanceled()) {
767 removeSingleResources(resultSet);
770 jResource = (SimulatorSingleResource) jSimulatorResource;
771 resource = new SingleResource();
772 resource.setSimulatorResource(jResource);
774 result = completeSingleResourceCreationByRAML(resource,
775 jResource.getURI(), jResource.getName(), true);
777 resultSet.add(resource);
779 } catch (SimulatorException eInner) {
783 .log(Level.ERROR.ordinal(),
785 Utility.getSimulatorErrorString(eInner,
788 progressMonitor.worked(1);
790 } catch (SimulatorException eOuter) {
794 .log(Level.ERROR.ordinal(), new Date(),
795 Utility.getSimulatorErrorString(eOuter, null));
801 public List<Resource> getResourceList() {
802 List<Resource> resourceList = data.getResources();
803 if (null == resourceList) {
807 Collections.sort(resourceList, Utility.resourceComparator);
812 public List<SingleResource> getSingleResourceList() {
813 List<SingleResource> resourceList = data.getSingleResources();
814 if (null == resourceList) {
818 Collections.sort(resourceList, Utility.singleResourceComparator);
823 public void removeSingleResources(Set<SingleResource> resources)
824 throws SimulatorException {
825 if (null == resources) {
828 Iterator<SingleResource> itr = resources.iterator();
829 while (itr.hasNext()) {
830 removeResource(itr.next());
834 public void removeResource(Resource res) throws SimulatorException {
835 // Unregister the resource from the platform.
836 SimulatorResource simRes = res.getSimulatorResource();
839 } catch (SimulatorException e) {
843 .log(Level.ERROR.ordinal(), new Date(),
844 Utility.getSimulatorErrorString(e, null));
848 // Delete this resource
849 data.deleteResource(res);
852 public boolean isUriUnique(List<MetaProperty> properties) {
853 if (null == properties) {
857 Iterator<MetaProperty> itr = properties.iterator();
858 while (itr.hasNext()) {
860 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
861 String uri = prop.getPropValue();
862 return !data.isResourceExist(uri);
868 public void resourceSelectionChanged(final Resource selectedResource) {
872 if (null != selectedResource) {
873 setCurrentResourceInSelection(selectedResource);
875 setCurrentResourceInSelection(null);
877 // Notify all observers for resource selection change event
878 UiListenerHandler.getInstance()
879 .resourceSelectionChangedUINotification(
885 public List<MetaProperty> getMetaProperties(Resource resource) {
886 if (null != resource) {
890 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
892 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
893 propName = Constants.META_PROPERTIES[index];
894 if (propName.equals(Constants.RESOURCE_NAME)) {
895 propValue = resource.getResourceName();
896 } else if (propName.equals(Constants.RESOURCE_URI)) {
897 propValue = resource.getResourceURI();
898 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
899 propValue = resource.getResourceType();
900 } else if (propName.equals(Constants.INTERFACE_TYPES)) {
901 Set<String> ifTypes = resource.getResourceInterfaces();
902 if (null != ifTypes && !ifTypes.isEmpty()) {
904 Iterator<String> itr = ifTypes.iterator();
905 while (itr.hasNext()) {
906 propValue += itr.next();
917 if (null != propValue) {
918 metaPropertyList.add(new MetaProperty(propName, propValue));
921 return metaPropertyList;
926 public boolean startResource(Resource resource) throws SimulatorException {
927 if (null == resource) {
930 SimulatorResource server = resource.getSimulatorResource();
931 if (null == server) {
936 resource.setStarted(true);
937 } catch (SimulatorException e) {
941 .log(Level.ERROR.ordinal(),
943 "There is an error while starting the resource.\n"
944 + Utility.getSimulatorErrorString(e, null));
950 public boolean stopResource(Resource resource) throws SimulatorException {
951 if (null == resource) {
954 SimulatorResource server = resource.getSimulatorResource();
955 if (null == server) {
960 resource.setStarted(false);
961 } catch (SimulatorException e) {
965 .log(Level.ERROR.ordinal(),
967 "There is an error while stopping the resource.\n"
968 + Utility.getSimulatorErrorString(e, null));
974 public boolean changeResourceName(Resource resource, String newName)
975 throws SimulatorException {
976 if (null == resource || null == newName) {
980 if (!stopResource(resource)) {
984 SimulatorResource server = resource.getSimulatorResource();
986 server.setName(newName);
987 resource.setResourceName(newName);
988 } catch (SimulatorException e) {
992 .log(Level.ERROR.ordinal(),
994 "There is an error while changing the resource name.\n"
995 + Utility.getSimulatorErrorString(e, null));
999 if (!startResource(resource)) {
1006 public boolean changeResourceURI(Resource resource, String newURI)
1007 throws SimulatorException {
1008 if (null == resource || null == newURI) {
1012 if (!stopResource(resource)) {
1016 String curURI = resource.getResourceURI();
1017 setResourceURI(resource, newURI);
1020 if (!startResource(resource)) {
1023 } catch (SimulatorException e) {
1024 setResourceURI(resource, curURI);
1030 public void setResourceURI(Resource resource, String newURI)
1031 throws SimulatorException {
1032 String curURI = resource.getResourceURI();
1033 SimulatorResource server = resource.getSimulatorResource();
1035 server.setURI(newURI);
1036 data.changeResourceURI(resource, curURI, newURI);
1037 } catch (SimulatorException e) {
1041 .log(Level.ERROR.ordinal(),
1043 "There is an error while changing the resource URI.\n"
1044 + Utility.getSimulatorErrorString(e, null));
1049 public boolean updateResourceProperties(Resource resource,
1050 List<MetaProperty> properties, boolean uriChanged,
1051 boolean nameChanged) throws SimulatorException {
1052 if (null == resource || null == properties) {
1056 // Updating the properties
1057 Iterator<MetaProperty> itr = properties.iterator();
1058 MetaProperty property;
1061 String resName = null;
1062 String resURI = null;
1063 while (itr.hasNext()) {
1064 property = itr.next();
1065 if (null == property) {
1068 propName = property.getPropName();
1069 propValue = property.getPropValue();
1070 if (propName.equals(Constants.RESOURCE_NAME)) {
1071 resName = propValue;
1072 } else if (propName.equals(Constants.RESOURCE_URI)) {
1078 if (!changeResourceName(resource, resName)) {
1082 // Notify UI Listeners
1083 UiListenerHandler.getInstance().propertiesChangedUINotification(
1088 if (!changeResourceURI(resource, resURI)) {
1096 public boolean updateResourceInterfaces(Resource resource,
1097 Set<String> newIfSet) throws SimulatorException {
1098 if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1101 SimulatorResource jResource = resource.getSimulatorResource();
1102 if (null == jResource) {
1105 Set<String> curIfSet = resource.getResourceInterfaces();
1106 Iterator<String> itr = curIfSet.iterator();
1107 String interfaceType;
1108 boolean resourceRestartRequired = false;
1109 while (itr.hasNext()) {
1110 interfaceType = itr.next();
1111 if (!newIfSet.contains(interfaceType)) {
1112 resourceRestartRequired = true;
1118 // As there is no support from native layer for interface removal,
1119 // supporting it from the simulator requires restarting the resource
1120 // if any existing interfaces are removed.
1122 if (resourceRestartRequired) {
1123 stopResource(resource);
1124 jResource.setInterface(Utility
1125 .convertSetToVectorString(newIfSet));
1126 startResource(resource);
1128 // Existing interfaces are not removed.
1129 itr = newIfSet.iterator();
1130 while (itr.hasNext()) {
1131 interfaceType = itr.next();
1132 if (!curIfSet.contains(interfaceType)) {
1133 jResource.addInterface(interfaceType);
1137 } catch (SimulatorException e) {
1141 .log(Level.ERROR.ordinal(),
1143 "There is an error while changing the interface types."
1144 + Utility.getSimulatorErrorString(e, null));
1148 // Set the resource interfaces.
1149 resource.setResourceInterfaces(newIfSet);
1154 public boolean attributeValueUpdated(SingleResource resource,
1155 String attributeName, AttributeValue value) {
1156 if (null != resource && null != attributeName && null != value) {
1157 SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1158 .getSimulatorResource();
1159 if (null != simRes) {
1161 simRes.updateAttribute(attributeName, value);
1163 } catch (SimulatorException e) {
1167 .log(Level.ERROR.ordinal(), new Date(),
1168 Utility.getSimulatorErrorString(e, null));
1175 public boolean isResourceStarted(Resource resource) {
1176 if (null == resource) {
1179 return resource.isStarted();
1182 public boolean isPropertyValueInvalid(Resource resource,
1183 List<MetaProperty> properties, String propName) {
1184 if (null == resource || null == properties || null == propName) {
1187 boolean invalid = false;
1189 Iterator<MetaProperty> itr = properties.iterator();
1190 while (itr.hasNext()) {
1192 if (prop.getPropName().equals(propName)) {
1193 String value = prop.getPropValue();
1194 if (propName.equals(Constants.RESOURCE_URI)) {
1195 if (!Utility.isUriValid(value)) {
1199 if (null == value || value.trim().isEmpty()) {
1208 public boolean isPropValueChanged(Resource resource,
1209 List<MetaProperty> properties, String propName) {
1210 if (null == resource || null == properties || null == propName) {
1213 boolean changed = false;
1216 Iterator<MetaProperty> itr = properties.iterator();
1217 while (itr.hasNext()) {
1219 if (prop.getPropName().equals(propName)) {
1220 oldValue = getPropertyValueFromResource(resource, propName);
1221 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1230 private String getPropertyValueFromResource(Resource resource,
1232 if (null == resource || null == propName) {
1235 if (propName.equals(Constants.RESOURCE_URI)) {
1236 return resource.getResourceURI();
1237 } else if (propName.equals(Constants.RESOURCE_NAME)) {
1238 return resource.getResourceName();
1239 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1240 return resource.getResourceType();
1246 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1250 AttributeProperty prop = att.property();
1255 if (prop.getType() == Type.INTEGER) {
1256 IntegerProperty intProperty = prop.asInteger();
1257 if (null != intProperty) {
1258 return (intProperty.hasRange() || intProperty.hasValues());
1262 } else if (prop.getType() == Type.DOUBLE) {
1263 DoubleProperty dblProperty = prop.asDouble();
1264 if (null != dblProperty) {
1265 return (dblProperty.hasRange() || dblProperty.hasValues());
1269 } else if (prop.getType() == Type.STRING) {
1270 StringProperty stringProperty = prop.asString();
1271 if (null != stringProperty) {
1272 return stringProperty.hasValues();
1281 public int startAutomation(SingleResource resource,
1282 AttributeElement attribute, AutoUpdateType autoType,
1283 int autoUpdateInterval) {
1285 if (null != resource && null != attribute) {
1286 SimulatorSingleResource server = (SimulatorSingleResource) resource
1287 .getSimulatorResource();
1288 if (null != server) {
1289 String attrName = attribute.getSimulatorResourceAttribute()
1292 autoId = server.startAttributeUpdation(attrName, autoType,
1293 autoUpdateInterval, automationListener);
1294 } catch (SimulatorException e) {
1298 .log(Level.ERROR.ordinal(),
1300 "[" + e.getClass().getSimpleName() + "]"
1301 + e.code().toString() + "-"
1306 attribute.setAutoUpdateId(autoId);
1307 attribute.setAutoUpdateType(autoType);
1308 attribute.setAutoUpdateInterval(autoUpdateInterval);
1309 attribute.setAutoUpdateState(true);
1310 resource.setAttributeAutomationInProgress(true);
1317 public void stopAutomation(SingleResource resource, AttributeElement att,
1319 if (null != resource) {
1320 SimulatorSingleResource server = (SimulatorSingleResource) resource
1321 .getSimulatorResource();
1322 if (null != server) {
1324 server.stopUpdation(autoId);
1325 } catch (SimulatorException e) {
1329 .log(Level.ERROR.ordinal(),
1331 "[" + e.getClass().getSimpleName() + "]"
1332 + e.code().toString() + "-"
1336 // Change the automation status
1337 att.setAutoUpdateState(false);
1338 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1343 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1344 int autoUpdateInterval, final SingleResource resource) {
1345 if (null == resource) {
1348 boolean status = false;
1349 // Invoke the native automation method
1350 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1351 .getSimulatorResource();
1352 if (null != resourceServer) {
1355 autoId = resourceServer.startResourceUpdation(autoType,
1356 autoUpdateInterval, automationListener);
1357 } catch (SimulatorException e) {
1361 .log(Level.ERROR.ordinal(), new Date(),
1362 Utility.getSimulatorErrorString(e, null));
1366 // Automation request accepted.
1367 changeResourceLevelAutomationStatus(resource, true);
1369 resource.setAutomationId(autoId);
1371 // Notify the UI listeners in a different thread.
1372 Thread notifyThread = new Thread() {
1374 UiListenerHandler.getInstance()
1375 .resourceAutomationStartedUINotification(
1379 notifyThread.setPriority(Thread.MAX_PRIORITY);
1380 notifyThread.start();
1388 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1389 if (null == resource) {
1392 final int autoId = resource.getAutomationId();
1396 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1397 .getSimulatorResource();
1398 if (null == resourceServer) {
1401 // Call native method
1403 resourceServer.stopUpdation(autoId);
1404 } catch (SimulatorException e) {
1408 .log(Level.ERROR.ordinal(), new Date(),
1409 Utility.getSimulatorErrorString(e, null));
1413 // Notify the UI Listeners. Invoke the automation complete callback.
1414 Thread stopThread = new Thread() {
1416 automationListener.onUpdateComplete(resource.getResourceURI(),
1424 private boolean isAnyAttributeInAutomation(SingleResource resource) {
1425 if (null == resource || null == resource.getResourceRepresentation()) {
1429 Map<String, AttributeElement> attributes = resource
1430 .getResourceRepresentation().getAttributes();
1431 if (null == attributes || 0 == attributes.size())
1434 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1435 if (entry.getValue().isAutoUpdateInProgress())
1442 // Changes the automation state of the resource and its attributes
1443 private void changeResourceLevelAutomationStatus(SingleResource resource,
1446 if (null == resource || null == resource.getResourceRepresentation()) {
1450 Map<String, AttributeElement> attributes = resource
1451 .getResourceRepresentation().getAttributes();
1452 if (null == attributes || 0 == attributes.size())
1455 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1456 entry.getValue().setAutoUpdateState(status);
1459 resource.setResourceAutomationInProgress(status);
1462 private AttributeElement getAttributeWithGivenAutomationId(
1463 SingleResource resource, int automationId) {
1464 if (null == resource || null == resource.getResourceRepresentation()) {
1468 Map<String, AttributeElement> attributes = resource
1469 .getResourceRepresentation().getAttributes();
1470 if (null == attributes || 0 == attributes.size())
1473 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1474 if (automationId == entry.getValue().getAutoUpdateId())
1475 return entry.getValue();
1481 public boolean isResourceAutomationStarted(SingleResource resource) {
1482 boolean status = false;
1483 if (null != resource) {
1484 status = resource.isResourceAutomationInProgress();
1489 public boolean isAttributeAutomationStarted(SingleResource resource) {
1490 if (null == resource) {
1493 return resource.isAttributeAutomationInProgress();
1496 public void notifyObserverRequest(Resource resource, int observerId) {
1497 if (null == resource) {
1500 SimulatorResource simulatorResource = resource.getSimulatorResource();
1501 if (null == simulatorResource) {
1505 simulatorResource.notifyObserver(observerId);
1506 } catch (SimulatorException e) {
1510 .log(Level.ERROR.ordinal(), new Date(),
1511 Utility.getSimulatorErrorString(e, null));
1515 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1520 AttributeValue val = att.value();
1525 TypeInfo type = val.typeInfo();
1527 AttributeProperty prop = att.property();
1532 List<String> values = new ArrayList<String>();
1534 if (type.mType != ValueType.RESOURCEMODEL) {
1535 if (type.mType == ValueType.ARRAY) {
1536 if (type.mDepth == 1) {
1537 ArrayProperty arrayProperty = prop.asArray();
1538 if (null != arrayProperty) {
1539 AttributeProperty childProp = arrayProperty
1540 .getElementProperty();
1541 switch (childProp.getType()) {
1543 IntegerProperty intProperty = childProp
1545 if (null != intProperty) {
1546 values.addAll(getAllValues(intProperty,
1551 DoubleProperty dblProperty = childProp
1553 if (null != dblProperty) {
1554 values.addAll(getAllValues(dblProperty,
1559 BooleanProperty boolProperty = childProp
1561 if (null != boolProperty) {
1562 values.addAll(getAllValues(boolProperty,
1567 StringProperty stringProperty = childProp
1569 if (null != stringProperty) {
1570 values.addAll(getAllValues(stringProperty,
1580 switch (prop.getType()) {
1582 IntegerProperty intProperty = prop.asInteger();
1583 if (null != intProperty) {
1584 values.addAll(getAllValues(intProperty,
1589 DoubleProperty dblProperty = prop.asDouble();
1590 if (null != dblProperty) {
1591 values.addAll(getAllValues(dblProperty, Type.DOUBLE));
1595 BooleanProperty boolProperty = prop.asBoolean();
1596 if (null != boolProperty) {
1597 values.addAll(getAllValues(boolProperty,
1602 StringProperty stringProperty = prop.asString();
1603 if (null != stringProperty) {
1604 values.addAll(getAllValues(stringProperty,
1617 public List<String> getAllValues(IntegerProperty intProperty,
1618 AttributeProperty.Type type) {
1619 List<String> values = new ArrayList<String>();
1621 if (intProperty.hasRange()) {
1622 int min = (int) intProperty.min();
1623 int max = (int) intProperty.max();
1624 for (int iVal = min; iVal <= max; iVal++) {
1625 values.add(String.valueOf(iVal));
1627 } else if (intProperty.hasValues()) {
1628 for (Integer val : intProperty.getValues()) {
1629 values.add(String.valueOf(val));
1632 // Adding the default value.
1633 values.add(String.valueOf(intProperty.getDefaultValue()));
1638 public List<String> getAllValues(DoubleProperty dblProperty,
1639 AttributeProperty.Type type) {
1640 List<String> values = new ArrayList<String>();
1642 if (dblProperty.hasRange()) {
1643 double min = (double) dblProperty.min();
1644 double max = (double) dblProperty.max();
1645 for (double iVal = min; iVal <= max; iVal = iVal + 1) {
1646 values.add(String.valueOf(iVal));
1648 } else if (dblProperty.hasValues()) {
1649 for (Double val : dblProperty.getValues()) {
1650 values.add(String.valueOf(val));
1653 // Adding the default value.
1654 values.add(String.valueOf(dblProperty.getDefaultValue()));
1659 public List<String> getAllValues(BooleanProperty boolProperty,
1660 AttributeProperty.Type type) {
1661 List<String> values = new ArrayList<String>();
1663 values.add("false");
1667 public List<String> getAllValues(StringProperty stringProperty,
1668 AttributeProperty.Type type) {
1669 List<String> values = new ArrayList<String>();
1671 if (stringProperty.hasValues()) {
1672 for (String val : stringProperty.getValues()) {
1673 values.add(String.valueOf(val));
1676 // Adding the default value.
1677 values.add(String.valueOf(stringProperty.getDefaultValue()));
1682 public int getResourceCount() {
1683 return data.getResourceCount();
1686 public void shutdown() {
1687 threadHandle.interrupt();