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.AttributeProperty;
42 import org.oic.simulator.AttributeProperty.Type;
43 import org.oic.simulator.AttributeValue;
44 import org.oic.simulator.AttributeValue.TypeInfo;
45 import org.oic.simulator.AttributeValue.ValueType;
46 import org.oic.simulator.DeviceInfo;
47 import org.oic.simulator.DeviceListener;
48 import org.oic.simulator.ILogger.Level;
49 import org.oic.simulator.PlatformInfo;
50 import org.oic.simulator.SimulatorException;
51 import org.oic.simulator.SimulatorManager;
52 import org.oic.simulator.SimulatorResourceAttribute;
53 import org.oic.simulator.SimulatorResourceModel;
54 import org.oic.simulator.server.Observer;
55 import org.oic.simulator.server.SimulatorResource;
56 import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
57 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
58 import org.oic.simulator.server.SimulatorResource.ObserverListener;
59 import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
60 import org.oic.simulator.server.SimulatorSingleResource;
63 * This class acts as an interface between the simulator java SDK and the
64 * various UI modules. It maintains all the details of resources and provides
65 * other UI modules with the information required. It also handles model change,
66 * automation, and observer related events from native layer and propagates
67 * those events to the registered UI listeners.
69 public class ResourceManager {
73 private Resource currentResourceInSelection;
75 private ResourceModelChangeListener resourceModelChangeListener;
77 private AutoUpdateListener automationListener;
79 private ObserverListener observer;
81 private DeviceListener deviceListener;
83 private NotificationSynchronizerThread synchronizerThread;
85 private Thread threadHandle;
87 private DeviceInfo deviceInfo;
88 private PlatformInfo platformInfo;
90 private String deviceName;
92 public ResourceManager() {
95 deviceListener = new DeviceListener() {
98 public void onDeviceFound(final String host,
99 final DeviceInfo deviceInfo) {
100 if (null != ResourceManager.this.deviceInfo
101 || null == deviceInfo || null == host) {
104 synchronizerThread.addToQueue(new Runnable() {
107 String rcvdDeviceName = deviceInfo.getName();
108 if (null == rcvdDeviceName) {
111 if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
112 ResourceManager.this.deviceInfo = deviceInfo;
114 // Notify the UI Listeners
115 UiListenerHandler.getInstance()
116 .deviceInfoReceivedNotification();
123 resourceModelChangeListener = new ResourceModelChangeListener() {
126 public void onResourceModelChanged(final String resourceURI,
127 final SimulatorResourceModel resourceModelN) {
128 synchronizerThread.addToQueue(new Runnable() {
132 if (null == resourceURI || null == resourceModelN) {
136 Display.getDefault().asyncExec(new Runnable() {
139 Resource resource = data
140 .getResourceByURI(resourceURI);
141 if (null != resource) {
143 resource.setResourceRepresentation(resourceModelN);
144 } catch (NumberFormatException e) {
148 .log(Level.ERROR.ordinal(),
150 "Error while trying to update the attributes.\n"
152 .getSimulatorErrorString(
164 automationListener = new AutoUpdateListener() {
167 public void onUpdateComplete(final String resourceURI,
168 final int automationId) {
169 synchronizerThread.addToQueue(new Runnable() {
173 SingleResource resource = data
174 .getSingleResourceByURI(resourceURI);
175 if (null == resource) {
178 // Checking whether this notification is for an
179 // attribute or a resource
180 if (resource.isResourceAutomationInProgress()) {
181 changeResourceLevelAutomationStatus(resource, false);
182 // Notify the UI listeners
183 UiListenerHandler.getInstance()
184 .automationCompleteUINotification(resource,
186 } else if (resource.isAttributeAutomationInProgress()) {
187 // Find the attribute with the given automation id
188 final AttributeElement attribute = getAttributeWithGivenAutomationId(
189 resource, automationId);
190 if (null != attribute) {
191 attribute.setAutoUpdateState(false);
192 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
194 // Setting the attribute automation status to
196 resource.setAttributeAutomationInProgress(false);
204 observer = new ObserverListener() {
206 public void onObserverChanged(final String resourceURI,
207 final int status, final Observer observer) {
211 if (null == resourceURI || null == observer) {
214 Resource resource = data.getResourceByURI(resourceURI);
215 if (null == resource) {
218 // Update the observers information
220 resource.addObserverInfo(observer);
222 resource.removeObserverInfo(observer);
224 // Notify the UI listeners
225 UiListenerHandler.getInstance()
226 .observerListChangedUINotification(resource);
232 public void onObserverAdded(String resourceURI, Observer observer) {
233 onObserverChanged(resourceURI, 0, observer);
237 public void onObserverRemoved(String resourceURI, Observer observer) {
238 onObserverChanged(resourceURI, 1, observer);
242 synchronizerThread = new NotificationSynchronizerThread();
243 threadHandle = new Thread(synchronizerThread);
244 threadHandle.setName("Simulator service provider event queue");
245 threadHandle.start();
247 // Set the default device name.
248 deviceName = "IoTivity Simulator";
250 SimulatorManager.setDeviceInfo(deviceName);
251 } catch (SimulatorException e) {
255 .log(Level.ERROR.ordinal(),
257 "Error while registering the device info.\n"
258 + Utility.getSimulatorErrorString(e, null));
261 // Set the default platform information.
262 platformInfo = new PlatformInfo();
263 platformInfo.setPlatformID("Samsung Platform Identifier");
264 platformInfo.setManufacturerName("Samsung");
265 platformInfo.setManufacturerUrl("www.samsung.com");
266 platformInfo.setModelNumber("Samsung Model Num01");
267 platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
268 platformInfo.setPlatformVersion("PlatformVersion01");
269 platformInfo.setOperationSystemVersion("OSVersion01");
270 platformInfo.setHardwareVersion("HardwareVersion01");
271 platformInfo.setFirmwareVersion("FirwareVersion01");
272 platformInfo.setSupportUrl("http://www.samsung.com/support");
273 platformInfo.setSystemTime("2015-09-10T11:10:30Z");
275 SimulatorManager.setPlatformInfo(platformInfo);
276 } catch (SimulatorException e) {
280 .log(Level.ERROR.ordinal(),
282 "Error while registering the platform info.\n"
283 + Utility.getSimulatorErrorString(e, null));
286 // Get the device information to show other details of the device in UI.
288 SimulatorManager.findDevices("", deviceListener);
289 } catch (SimulatorException e) {
293 .log(Level.ERROR.ordinal(),
295 "Failed to get the local device information.\n"
296 + Utility.getSimulatorErrorString(e, null));
300 private static class NotificationSynchronizerThread implements Runnable {
302 LinkedList<Runnable> notificationQueue = new LinkedList<Runnable>();
306 while (!Thread.interrupted()) {
307 synchronized (this) {
309 while (notificationQueue.isEmpty()) {
313 } catch (InterruptedException e) {
319 synchronized (this) {
320 thread = notificationQueue.pop();
324 } catch (Exception e) {
325 if (e instanceof InterruptedException) {
333 public void addToQueue(Runnable event) {
334 synchronized (this) {
335 notificationQueue.add(event);
341 public void setDeviceInfo(List<MetaProperty> metaProperties) {
342 if (null == metaProperties || metaProperties.size() < 1) {
345 Iterator<MetaProperty> itr = metaProperties.iterator();
349 boolean found = false;
350 while (itr.hasNext()) {
352 propName = prop.getPropName();
353 propValue = prop.getPropValue();
354 if (propName.equals(Constants.DEVICE_NAME)) {
355 this.deviceName = propValue;
366 SimulatorManager.setDeviceInfo(deviceName);
367 } catch (SimulatorException e) {
371 .log(Level.ERROR.ordinal(),
373 "Error while registering the device info.\n"
374 + Utility.getSimulatorErrorString(e, null));
378 public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
379 if (null == metaProperties || metaProperties.size() < 1) {
383 Iterator<MetaProperty> itr = metaProperties.iterator();
387 while (itr.hasNext()) {
389 propName = prop.getPropName();
390 propValue = prop.getPropValue();
391 if (propName.equals(Constants.DEVICE_NAME)) {
392 if (null == propValue || propValue.length() < 1) {
401 public List<MetaProperty> getDeviceInfo() {
402 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
403 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
404 if (null != deviceInfo) {
405 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
407 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
408 deviceInfo.getSpecVersion()));
409 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
410 deviceInfo.getDataModelVersion()));
412 return metaProperties;
415 public List<MetaProperty> getPlatformInfo() {
416 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
417 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
419 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
420 platformInfo.getManufacturerName()));
421 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
422 platformInfo.getManufacturerUrl()));
423 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
424 platformInfo.getModelNumber()));
425 metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
426 platformInfo.getDateOfManufacture()));
427 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
428 platformInfo.getPlatformVersion()));
429 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
430 platformInfo.getOperationSystemVersion()));
431 metaProperties.add(new MetaProperty(
432 Constants.PLATFORM_HARDWARE_VERSION, platformInfo
433 .getHardwareVersion()));
434 metaProperties.add(new MetaProperty(
435 Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
436 .getFirmwareVersion()));
437 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
438 platformInfo.getSupportUrl()));
439 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
440 platformInfo.getSystemTime()));
441 return metaProperties;
444 public void setPlatformInfo(List<MetaProperty> metaProperties) {
445 if (null == metaProperties || metaProperties.size() < 1) {
448 Iterator<MetaProperty> itr = metaProperties.iterator();
452 while (itr.hasNext()) {
454 propName = prop.getPropName();
455 propValue = prop.getPropValue();
456 if (propName.equals(Constants.PLATFORM_ID)) {
457 platformInfo.setPlatformID(propValue);
458 } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
459 platformInfo.setManufacturerName(propValue);
460 } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
461 platformInfo.setManufacturerUrl(propValue);
462 } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
463 platformInfo.setModelNumber(propValue);
464 } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
465 platformInfo.setDateOfManufacture(propValue);
466 } else if (propName.equals(Constants.PLATFORM_VERSION)) {
467 platformInfo.setPlatformVersion(propValue);
468 } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
469 platformInfo.setOperationSystemVersion(propValue);
470 } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
471 platformInfo.setHardwareVersion(propValue);
472 } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
473 platformInfo.setFirmwareVersion(propValue);
474 } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
475 platformInfo.setSupportUrl(propValue);
476 } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
477 platformInfo.setSystemTime(propValue);
481 SimulatorManager.setPlatformInfo(platformInfo);
482 } catch (SimulatorException e) {
486 .log(Level.ERROR.ordinal(),
488 "Error while registering the platform info.\n"
489 + Utility.getSimulatorErrorString(e, null));
493 public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
494 if (null == metaProperties || metaProperties.size() < 1) {
497 Iterator<MetaProperty> itr = metaProperties.iterator();
500 while (itr.hasNext()) {
502 propValue = prop.getPropValue();
503 if (null == propValue || propValue.length() < 1) {
510 public synchronized Resource getCurrentResourceInSelection() {
511 return currentResourceInSelection;
514 public synchronized void setCurrentResourceInSelection(Resource resource) {
515 this.currentResourceInSelection = resource;
518 public boolean isResourceExist(String resourceURI) {
519 return data.isResourceExist(resourceURI);
522 public boolean isAnyResourceExist() {
523 return data.isAnyResourceExist();
526 public boolean createSingleResource(SingleResource resource,
527 Map<String, SimulatorResourceAttribute> attributes)
528 throws SimulatorException {
529 if (null == resource) {
534 // Create the resource.
535 SimulatorResource jSimulatorResource = SimulatorManager
536 .createResource(SimulatorResource.Type.SINGLE,
537 resource.getResourceName(),
538 resource.getResourceURI(),
539 resource.getResourceType());
540 if (null == jSimulatorResource
541 || !(jSimulatorResource instanceof SimulatorSingleResource)) {
544 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
545 resource.setSimulatorResource(jSimulatorSingleResource);
547 // Cancel observable property if requested by user.
548 if (!resource.isObservable()) {
549 jSimulatorSingleResource.setObservable(false);
552 // Set the model change listener.
553 jSimulatorSingleResource
554 .setResourceModelChangeListener(resourceModelChangeListener);
556 // Set the observer listener if the resource is observable.
557 if (resource.isObservable()) {
558 jSimulatorSingleResource.setObserverListener(observer);
562 if (null != attributes && !attributes.isEmpty()) {
563 SimulatorResourceAttribute value;
564 for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
566 value = entry.getValue();
568 jSimulatorSingleResource.addAttribute(value);
571 // Get the resource model java object reference.
572 resource.setResourceModel(jSimulatorSingleResource
573 .getResourceModel());
575 resource.setResourceRepresentation(resource.getResourceModel());
578 // Register the resource with the platform.
579 jSimulatorSingleResource.start();
580 resource.setStarted(true);
582 // Add the resource interfaces
583 Set<String> newIfSet = resource.getResourceInterfaces();
584 // Get the default interface(s) if any configured by the platform.
585 // These interfaces will be overwritten by the new interfaces.
586 Set<String> ifSetFromPlatform = Utility
587 .convertVectorToSet(jSimulatorSingleResource.getInterface());
588 resource.setResourceInterfaces(ifSetFromPlatform);
589 updateResourceInterfaces(resource, newIfSet);
590 } catch (SimulatorException e) {
594 .log(Level.ERROR.ordinal(), new Date(),
595 Utility.getSimulatorErrorString(e, null));
599 // Add to local cache.
600 data.addResource(resource);
602 // Update UI listeners
603 UiListenerHandler.getInstance().resourceCreatedUINotification(
604 ResourceType.SINGLE);
609 public Resource createResourceByRAML(String configFilePath)
610 throws SimulatorException {
611 Resource resource = null;
613 // Create the resource
614 SimulatorResource jSimulatorResource = SimulatorManager
615 .createResource(configFilePath);
616 if (null == jSimulatorResource) {
619 if (jSimulatorResource instanceof SimulatorSingleResource) {
620 resource = new SingleResource();
624 resource.setSimulatorResource(jSimulatorResource);
626 // Fetch and locally store the resource name and uri.
627 String uri = jSimulatorResource.getURI();
628 if (null == uri || uri.trim().isEmpty()) {
631 resource.setResourceURI(uri.trim());
633 String name = jSimulatorResource.getName();
634 if (null == name || name.trim().isEmpty()) {
637 resource.setResourceName(name.trim());
638 } catch (SimulatorException e) {
642 .log(Level.ERROR.ordinal(), new Date(),
643 Utility.getSimulatorErrorString(e, null));
650 * This method can set/change the resource uri and name of an already
651 * created resource which is not yet registered with the platform. This
652 * method registers the model change and observer listeners, registers the
653 * resource, fetches the resource attributes, updates the local cache and
654 * notifies the UI listeners.
656 public boolean completeSingleResourceCreationByRAML(Resource resource,
657 String uri, String name, boolean multiInstance)
658 throws SimulatorException {
659 if (null == resource || !(resource instanceof SingleResource)) {
663 SingleResource singleRes = (SingleResource) resource;
665 SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
666 .getSimulatorResource();
667 if (null == jSimulatorSingleResource) {
671 // Update resource URI and Name if they are changed.
672 String newUri = uri.trim();
673 String newName = name.trim();
676 singleRes.setResourceURI(newUri);
677 singleRes.setResourceName(newName);
679 if (!singleRes.getResourceURI().equals(newUri)) {
680 jSimulatorSingleResource.setURI(newUri);
681 singleRes.setResourceURI(newUri);
683 if (!singleRes.getResourceName().equals(newName)) {
684 jSimulatorSingleResource.setName(newName);
685 singleRes.setResourceName(newName);
689 // Set the model change listener.
690 jSimulatorSingleResource
691 .setResourceModelChangeListener(resourceModelChangeListener);
693 // Set the observer listener if the resource is observable.
694 if (jSimulatorSingleResource.isObservable()) {
695 jSimulatorSingleResource.setObserverListener(observer);
696 singleRes.setObservable(true);
699 // Fetch the resource model.
700 SimulatorResourceModel jResModel = jSimulatorSingleResource
702 if (null == jResModel) {
705 singleRes.setResourceModel(jResModel);
707 // Fetch the basic details of the resource.
708 singleRes.setResourceType(jSimulatorSingleResource
711 .setResourceInterfaces(Utility
712 .convertVectorToSet(jSimulatorSingleResource
715 // Fetch the resource attributes.
716 singleRes.setResourceRepresentation(jResModel);
718 // Register the resource with the platform.
719 jSimulatorSingleResource.start();
720 singleRes.setStarted(true);
722 // Add to local cache.
723 data.addResource(singleRes);
725 // Update UI listeners for single instance creation
727 UiListenerHandler.getInstance().resourceCreatedUINotification(
728 ResourceType.SINGLE);
729 } catch (Exception e) {
733 .log(Level.ERROR.ordinal(), new Date(),
734 Utility.getSimulatorErrorString(e, null));
740 public Set<SingleResource> createSingleResourceMultiInstances(
741 String configFile, int count, IProgressMonitor progressMonitor)
742 throws SimulatorException {
743 Set<SingleResource> resultSet;
745 resultSet = new HashSet<SingleResource>();
746 Vector<SimulatorResource> jSimulatorResources = SimulatorManager
747 .createResource(configFile, count);
748 if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
751 SimulatorSingleResource jResource;
752 SingleResource resource;
754 for (SimulatorResource jSimulatorResource : jSimulatorResources) {
755 // If the resource creation progress is canceled, then stop the
756 // creation and stop/delete
757 // the resources created already.
758 if (progressMonitor.isCanceled()) {
759 removeSingleResources(resultSet);
762 jResource = (SimulatorSingleResource) jSimulatorResource;
763 resource = new SingleResource();
764 resource.setSimulatorResource(jResource);
766 result = completeSingleResourceCreationByRAML(resource,
767 jResource.getURI(), jResource.getName(), true);
769 resultSet.add(resource);
771 } catch (SimulatorException eInner) {
775 .log(Level.ERROR.ordinal(),
777 Utility.getSimulatorErrorString(eInner,
780 progressMonitor.worked(1);
782 } catch (SimulatorException eOuter) {
786 .log(Level.ERROR.ordinal(), new Date(),
787 Utility.getSimulatorErrorString(eOuter, null));
793 public List<Resource> getResourceList() {
794 List<Resource> resourceList = data.getResources();
795 if (null == resourceList) {
799 Collections.sort(resourceList, Utility.resourceComparator);
804 public List<SingleResource> getSingleResourceList() {
805 List<SingleResource> resourceList = data.getSingleResources();
806 if (null == resourceList) {
810 Collections.sort(resourceList, Utility.singleResourceComparator);
815 public void removeSingleResources(Set<SingleResource> resources)
816 throws SimulatorException {
817 if (null == resources) {
820 Iterator<SingleResource> itr = resources.iterator();
821 while (itr.hasNext()) {
822 removeResource(itr.next());
826 public void removeResource(Resource res) throws SimulatorException {
827 // Unregister the resource from the platform.
828 SimulatorResource simRes = res.getSimulatorResource();
831 } catch (SimulatorException e) {
835 .log(Level.ERROR.ordinal(), new Date(),
836 Utility.getSimulatorErrorString(e, null));
840 // Delete this resource
841 data.deleteResource(res);
844 public boolean isUriUnique(List<MetaProperty> properties) {
845 if (null == properties) {
849 Iterator<MetaProperty> itr = properties.iterator();
850 while (itr.hasNext()) {
852 if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
853 String uri = prop.getPropValue();
854 return !data.isResourceExist(uri);
860 public void resourceSelectionChanged(final Resource selectedResource) {
864 if (null != selectedResource) {
865 setCurrentResourceInSelection(selectedResource);
867 setCurrentResourceInSelection(null);
869 // Notify all observers for resource selection change event
870 UiListenerHandler.getInstance()
871 .resourceSelectionChangedUINotification(
877 public List<MetaProperty> getMetaProperties(Resource resource) {
878 if (null != resource) {
882 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
884 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
885 propName = Constants.META_PROPERTIES[index];
886 if (propName.equals(Constants.RESOURCE_NAME)) {
887 propValue = resource.getResourceName();
888 } else if (propName.equals(Constants.RESOURCE_URI)) {
889 propValue = resource.getResourceURI();
890 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
891 propValue = resource.getResourceType();
892 } else if (propName.equals(Constants.INTERFACE_TYPES)) {
893 Set<String> ifTypes = resource.getResourceInterfaces();
894 if (null != ifTypes && !ifTypes.isEmpty()) {
896 Iterator<String> itr = ifTypes.iterator();
897 while (itr.hasNext()) {
898 propValue += itr.next();
909 if (null != propValue) {
910 metaPropertyList.add(new MetaProperty(propName, propValue));
913 return metaPropertyList;
918 public boolean startResource(Resource resource) throws SimulatorException {
919 if (null == resource) {
922 SimulatorResource server = resource.getSimulatorResource();
923 if (null == server) {
928 resource.setStarted(true);
929 } catch (SimulatorException e) {
933 .log(Level.ERROR.ordinal(),
935 "There is an error while starting the resource.\n"
936 + Utility.getSimulatorErrorString(e, null));
942 public boolean stopResource(Resource resource) throws SimulatorException {
943 if (null == resource) {
946 SimulatorResource server = resource.getSimulatorResource();
947 if (null == server) {
952 resource.setStarted(false);
953 } catch (SimulatorException e) {
957 .log(Level.ERROR.ordinal(),
959 "There is an error while stopping the resource.\n"
960 + Utility.getSimulatorErrorString(e, null));
966 public boolean changeResourceName(Resource resource, String newName)
967 throws SimulatorException {
968 if (null == resource || null == newName) {
972 if (!stopResource(resource)) {
976 SimulatorResource server = resource.getSimulatorResource();
978 server.setName(newName);
979 resource.setResourceName(newName);
980 } catch (SimulatorException e) {
984 .log(Level.ERROR.ordinal(),
986 "There is an error while changing the resource name.\n"
987 + Utility.getSimulatorErrorString(e, null));
991 if (!startResource(resource)) {
998 public boolean changeResourceURI(Resource resource, String newURI)
999 throws SimulatorException {
1000 if (null == resource || null == newURI) {
1004 if (!stopResource(resource)) {
1008 String curURI = resource.getResourceURI();
1009 setResourceURI(resource, newURI);
1012 if (!startResource(resource)) {
1015 } catch (SimulatorException e) {
1016 setResourceURI(resource, curURI);
1022 public void setResourceURI(Resource resource, String newURI)
1023 throws SimulatorException {
1024 String curURI = resource.getResourceURI();
1025 SimulatorResource server = resource.getSimulatorResource();
1027 server.setURI(newURI);
1028 data.changeResourceURI(resource, curURI, newURI);
1029 } catch (SimulatorException e) {
1033 .log(Level.ERROR.ordinal(),
1035 "There is an error while changing the resource URI.\n"
1036 + Utility.getSimulatorErrorString(e, null));
1041 public boolean updateResourceProperties(Resource resource,
1042 List<MetaProperty> properties, boolean uriChanged,
1043 boolean nameChanged) throws SimulatorException {
1044 if (null == resource || null == properties) {
1048 // Updating the properties
1049 Iterator<MetaProperty> itr = properties.iterator();
1050 MetaProperty property;
1053 String resName = null;
1054 String resURI = null;
1055 while (itr.hasNext()) {
1056 property = itr.next();
1057 if (null == property) {
1060 propName = property.getPropName();
1061 propValue = property.getPropValue();
1062 if (propName.equals(Constants.RESOURCE_NAME)) {
1063 resName = propValue;
1064 } else if (propName.equals(Constants.RESOURCE_URI)) {
1070 if (!changeResourceName(resource, resName)) {
1074 // Notify UI Listeners
1075 UiListenerHandler.getInstance().propertiesChangedUINotification(
1080 if (!changeResourceURI(resource, resURI)) {
1088 public boolean updateResourceInterfaces(Resource resource,
1089 Set<String> newIfSet) throws SimulatorException {
1090 if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
1093 SimulatorResource jResource = resource.getSimulatorResource();
1094 if (null == jResource) {
1097 Set<String> curIfSet = resource.getResourceInterfaces();
1098 Iterator<String> itr = curIfSet.iterator();
1099 String interfaceType;
1100 boolean resourceRestartRequired = false;
1101 while (itr.hasNext()) {
1102 interfaceType = itr.next();
1103 if (newIfSet.contains(interfaceType)) {
1104 newIfSet.remove(interfaceType);
1106 // Remove this interface support from the resource.
1108 if (!resourceRestartRequired) {
1109 resourceRestartRequired = true;
1111 jResource.removeInterface(interfaceType);
1113 } catch (SimulatorException e) {
1117 .log(Level.ERROR.ordinal(),
1119 "There is an error while removing the interface type("
1122 + Utility.getSimulatorErrorString(
1129 // Add all remaining interfaces.
1130 itr = newIfSet.iterator();
1131 while (itr.hasNext()) {
1132 interfaceType = itr.next();
1133 // Add this interface support to the resource.
1135 jResource.addInterface(interfaceType);
1136 curIfSet.add(interfaceType);
1137 } catch (SimulatorException e) {
1141 .log(Level.ERROR.ordinal(),
1143 "There is an error while adding the interface type("
1146 + Utility.getSimulatorErrorString(e,
1152 // As there is no support from native layer for interface removal,
1153 // supporting it from the simulator requires restarting the resource if
1154 // any existing interfaces are removed.
1155 if (resourceRestartRequired) {
1156 stopResource(resource);
1157 startResource(resource);
1160 // Set the resource interfaces.
1161 resource.setResourceInterfaces(curIfSet);
1166 public boolean attributeValueUpdated(SingleResource resource,
1167 String attributeName, AttributeValue value) {
1168 if (null != resource && null != attributeName && null != value) {
1169 SimulatorSingleResource simRes = (SimulatorSingleResource) resource
1170 .getSimulatorResource();
1171 if (null != simRes) {
1173 simRes.updateAttribute(attributeName, value);
1175 } catch (SimulatorException e) {
1179 .log(Level.ERROR.ordinal(), new Date(),
1180 Utility.getSimulatorErrorString(e, null));
1187 public boolean isResourceStarted(Resource resource) {
1188 if (null == resource) {
1191 return resource.isStarted();
1194 public boolean isPropertyValueInvalid(Resource resource,
1195 List<MetaProperty> properties, String propName) {
1196 if (null == resource || null == properties || null == propName) {
1199 boolean invalid = false;
1201 Iterator<MetaProperty> itr = properties.iterator();
1202 while (itr.hasNext()) {
1204 if (prop.getPropName().equals(propName)) {
1205 String value = prop.getPropValue();
1206 if (propName.equals(Constants.RESOURCE_URI)) {
1207 if (!Utility.isUriValid(value)) {
1211 if (null == value || value.trim().isEmpty()) {
1220 public boolean isPropValueChanged(Resource resource,
1221 List<MetaProperty> properties, String propName) {
1222 if (null == resource || null == properties || null == propName) {
1225 boolean changed = false;
1228 Iterator<MetaProperty> itr = properties.iterator();
1229 while (itr.hasNext()) {
1231 if (prop.getPropName().equals(propName)) {
1232 oldValue = getPropertyValueFromResource(resource, propName);
1233 if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
1242 private String getPropertyValueFromResource(Resource resource,
1244 if (null == resource || null == propName) {
1247 if (propName.equals(Constants.RESOURCE_URI)) {
1248 return resource.getResourceURI();
1249 } else if (propName.equals(Constants.RESOURCE_NAME)) {
1250 return resource.getResourceName();
1251 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
1252 return resource.getResourceType();
1258 public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
1262 AttributeProperty prop = att.property();
1266 Type attProp = prop.type();
1267 if (attProp == Type.UNKNOWN) {
1273 public int startAutomation(SingleResource resource,
1274 AttributeElement attribute, AutoUpdateType autoType,
1275 int autoUpdateInterval) {
1277 if (null != resource && null != attribute) {
1278 SimulatorSingleResource server = (SimulatorSingleResource) resource
1279 .getSimulatorResource();
1280 if (null != server) {
1281 String attrName = attribute.getSimulatorResourceAttribute()
1284 autoId = server.startAttributeUpdation(attrName, autoType,
1285 autoUpdateInterval, automationListener);
1286 } catch (SimulatorException e) {
1290 .log(Level.ERROR.ordinal(),
1292 "[" + e.getClass().getSimpleName() + "]"
1293 + e.code().toString() + "-"
1298 attribute.setAutoUpdateId(autoId);
1299 attribute.setAutoUpdateType(autoType);
1300 attribute.setAutoUpdateInterval(autoUpdateInterval);
1301 attribute.setAutoUpdateState(true);
1302 resource.setAttributeAutomationInProgress(true);
1309 public void stopAutomation(SingleResource resource, AttributeElement att,
1311 if (null != resource) {
1312 SimulatorSingleResource server = (SimulatorSingleResource) resource
1313 .getSimulatorResource();
1314 if (null != server) {
1316 server.stopUpdation(autoId);
1317 } catch (SimulatorException e) {
1321 .log(Level.ERROR.ordinal(),
1323 "[" + e.getClass().getSimpleName() + "]"
1324 + e.code().toString() + "-"
1328 // Change the automation status
1329 att.setAutoUpdateState(false);
1330 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
1335 public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
1336 int autoUpdateInterval, final SingleResource resource) {
1337 if (null == resource) {
1340 boolean status = false;
1341 // Invoke the native automation method
1342 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1343 .getSimulatorResource();
1344 if (null != resourceServer) {
1347 autoId = resourceServer.startResourceUpdation(autoType,
1348 autoUpdateInterval, automationListener);
1349 } catch (SimulatorException e) {
1353 .log(Level.ERROR.ordinal(), new Date(),
1354 Utility.getSimulatorErrorString(e, null));
1358 // Automation request accepted.
1359 changeResourceLevelAutomationStatus(resource, true);
1361 resource.setAutomationId(autoId);
1363 // Notify the UI listeners in a different thread.
1364 Thread notifyThread = new Thread() {
1366 UiListenerHandler.getInstance()
1367 .resourceAutomationStartedUINotification(
1371 notifyThread.setPriority(Thread.MAX_PRIORITY);
1372 notifyThread.start();
1380 public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
1381 if (null == resource) {
1384 final int autoId = resource.getAutomationId();
1388 SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
1389 .getSimulatorResource();
1390 if (null == resourceServer) {
1393 // Call native method
1395 resourceServer.stopUpdation(autoId);
1396 } catch (SimulatorException e) {
1400 .log(Level.ERROR.ordinal(), new Date(),
1401 Utility.getSimulatorErrorString(e, null));
1405 // Notify the UI Listeners. Invoke the automation complete callback.
1406 Thread stopThread = new Thread() {
1408 automationListener.onUpdateComplete(resource.getResourceURI(),
1416 private boolean isAnyAttributeInAutomation(SingleResource resource) {
1417 if (null == resource || null == resource.getResourceRepresentation()) {
1421 Map<String, AttributeElement> attributes = resource
1422 .getResourceRepresentation().getAttributes();
1423 if (null == attributes || 0 == attributes.size())
1426 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1427 if (entry.getValue().isAutoUpdateInProgress())
1434 // Changes the automation state of the resource and its attributes
1435 private void changeResourceLevelAutomationStatus(SingleResource resource,
1438 if (null == resource || null == resource.getResourceRepresentation()) {
1442 Map<String, AttributeElement> attributes = resource
1443 .getResourceRepresentation().getAttributes();
1444 if (null == attributes || 0 == attributes.size())
1447 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1448 entry.getValue().setAutoUpdateState(status);
1451 resource.setResourceAutomationInProgress(status);
1454 private AttributeElement getAttributeWithGivenAutomationId(
1455 SingleResource resource, int automationId) {
1456 if (null == resource || null == resource.getResourceRepresentation()) {
1460 Map<String, AttributeElement> attributes = resource
1461 .getResourceRepresentation().getAttributes();
1462 if (null == attributes || 0 == attributes.size())
1465 for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
1466 if (automationId == entry.getValue().getAutoUpdateId())
1467 return entry.getValue();
1473 public boolean isResourceAutomationStarted(SingleResource resource) {
1474 boolean status = false;
1475 if (null != resource) {
1476 status = resource.isResourceAutomationInProgress();
1481 public boolean isAttributeAutomationStarted(SingleResource resource) {
1482 if (null == resource) {
1485 return resource.isAttributeAutomationInProgress();
1488 public void notifyObserverRequest(Resource resource, int observerId) {
1489 if (null == resource) {
1492 SimulatorResource simulatorResource = resource.getSimulatorResource();
1493 if (null == simulatorResource) {
1497 simulatorResource.notifyObserver(observerId);
1498 } catch (SimulatorException e) {
1502 .log(Level.ERROR.ordinal(), new Date(),
1503 Utility.getSimulatorErrorString(e, null));
1507 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1512 AttributeValue val = att.value();
1517 TypeInfo type = val.typeInfo();
1519 AttributeProperty prop = att.property();
1524 List<String> values = new ArrayList<String>();
1526 Type valuesType = prop.type();
1528 if (valuesType == Type.UNKNOWN) {
1529 // Adding the default value
1530 values.add(Utility.getAttributeValueAsString(val));
1534 if (type.mType != ValueType.RESOURCEMODEL) {
1535 if (type.mType == ValueType.ARRAY) {
1536 if (type.mDepth == 1) {
1537 AttributeProperty childProp = prop.getChildProperty();
1538 if (null != childProp) {
1539 valuesType = childProp.type();
1540 if (valuesType == Type.RANGE) {
1541 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1542 childProp, type.mBaseType);
1544 values.addAll(list);
1546 } else if (valuesType == Type.VALUESET) {
1547 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1548 childProp.valueSet(), type.mBaseType);
1550 values.addAll(list);
1556 if (valuesType == Type.RANGE) {
1557 List<String> list = getRangeForPrimitiveNonArrayAttributes(
1560 values.addAll(list);
1562 } else if (valuesType == Type.VALUESET) {
1563 List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
1564 prop.valueSet(), type.mType);
1566 values.addAll(list);
1575 public List<String> getRangeForPrimitiveNonArrayAttributes(
1576 AttributeProperty prop, ValueType type) {
1581 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1585 List<String> values = new ArrayList<String>();
1588 int min = (int) prop.min();
1589 int max = (int) prop.max();
1590 for (int iVal = min; iVal <= max; iVal++) {
1591 values.add(String.valueOf(iVal));
1595 double minD = (double) prop.min();
1596 double maxD = (double) prop.max();
1597 for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
1598 values.add(String.valueOf(iVal));
1606 public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
1607 AttributeValue[] attValues, ValueType type) {
1608 if (null == attValues || attValues.length < 1) {
1612 if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
1617 List<String> values = new ArrayList<String>();
1618 for (AttributeValue val : attValues) {
1626 values.add(String.valueOf(obj));
1631 public int getResourceCount() {
1632 return data.getResourceCount();
1635 public void shutdown() {
1636 threadHandle.interrupt();