Displaying and editing the complex value types for attributes.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / manager / ResourceManager.java
index 2a0473b..d7a8e20 100644 (file)
@@ -20,40 +20,46 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
-import oic.simulator.serviceprovider.Activator;
-import oic.simulator.serviceprovider.listener.IAutomationUIListener;
-import oic.simulator.serviceprovider.listener.IObserverListChangedUIListener;
-import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
-import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
-import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
-import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
-import oic.simulator.serviceprovider.resource.MetaProperty;
-import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
-import oic.simulator.serviceprovider.resource.SimulatorResource;
-import oic.simulator.serviceprovider.utils.Constants;
-import oic.simulator.serviceprovider.utils.Utility;
-
-import org.eclipse.swt.graphics.Image;
-import org.oic.simulator.IAutomation;
+import java.util.Vector;
+
+import org.eclipse.swt.widgets.Display;
+import org.oic.simulator.AttributeProperty;
+import org.oic.simulator.AttributeProperty.Type;
+import org.oic.simulator.AttributeValue;
+import org.oic.simulator.AttributeValue.TypeInfo;
+import org.oic.simulator.AttributeValue.ValueType;
 import org.oic.simulator.ILogger.Level;
-import org.oic.simulator.ResourceAttribute;
-import org.oic.simulator.ResourceAttribute.Range;
-import org.oic.simulator.ResourceAttribute.Type;
+import org.oic.simulator.PlatformInfo;
 import org.oic.simulator.SimulatorException;
 import org.oic.simulator.SimulatorManager;
+import org.oic.simulator.SimulatorResourceAttribute;
 import org.oic.simulator.SimulatorResourceModel;
-import org.oic.simulator.serviceprovider.AutomationType;
-import org.oic.simulator.serviceprovider.IObserver;
-import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
-import org.oic.simulator.serviceprovider.ObserverInfo;
-import org.oic.simulator.serviceprovider.SimulatorResourceServer;
+import org.oic.simulator.server.Observer;
+import org.oic.simulator.server.SimulatorCollectionResource;
+import org.oic.simulator.server.SimulatorResource;
+import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
+import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
+import org.oic.simulator.server.SimulatorResource.ObserverListener;
+import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
+import org.oic.simulator.server.SimulatorSingleResource;
+
+import oic.simulator.serviceprovider.Activator;
+import oic.simulator.serviceprovider.model.AttributeElement;
+import oic.simulator.serviceprovider.model.CollectionResource;
+import oic.simulator.serviceprovider.model.Device;
+import oic.simulator.serviceprovider.model.LocalResourceAttribute;
+import oic.simulator.serviceprovider.model.MetaProperty;
+import oic.simulator.serviceprovider.model.Resource;
+import oic.simulator.serviceprovider.model.ResourceType;
+import oic.simulator.serviceprovider.model.SRMItem;
+import oic.simulator.serviceprovider.model.SingleResource;
+import oic.simulator.serviceprovider.utils.Constants;
+import oic.simulator.serviceprovider.utils.Utility;
 
 /**
  * This class acts as an interface between the simulator java SDK and the
@@ -64,46 +70,67 @@ import org.oic.simulator.serviceprovider.SimulatorResourceServer;
  */
 public class ResourceManager {
 
-    private Map<String, Map<String, SimulatorResource>> resourceMap;
-
-    private Map<String, ArrayList<String>>              orderedResourceUriMap;
-
-    private Map<String, String>                         displayNameMap;
-
-    private SimulatorResource                           currentResourceInSelection;
+    private Data                           data;
 
-    private List<IResourceListChangedUIListener>        resourceListChangedUIListeners;
+    private Resource                       currentResourceInSelection;
 
-    private List<IResourceSelectionChangedUIListener>   resourceSelectionChangedUIListeners;
+    private Device                         currentDeviceInSelection;
 
-    private List<IResourceModelChangedUIListener>       resourceModelChangedUIListeners;
+    private ResourceModelChangeListener    resourceModelChangeListener;
 
-    private List<IAutomationUIListener>                 automationUIListeners;
+    private AutoUpdateListener             automationListener;
 
-    private List<IObserverListChangedUIListener>        observerUIListeners;
+    private ObserverListener               observer;
 
-    private IResourceModelChangedListener               resourceModelChangeListener;
+    private NotificationSynchronizerThread synchronizerThread;
 
-    private IAutomation                                 automationListener;
+    private Thread                         threadHandle;
 
-    private IObserver                                   observer;
-
-    private NotificationSynchronizerThread              synchronizerThread;
-
-    private Thread                                      threadHandle;
+    private String                         deviceName;
+    private PlatformInfo                   platformInfo;
 
     public ResourceManager() {
-        resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
-        orderedResourceUriMap = new HashMap<String, ArrayList<String>>();
-        displayNameMap = new HashMap<String, String>();
+        data = new Data();
 
-        resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
-        resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
-        resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
-        automationUIListeners = new ArrayList<IAutomationUIListener>();
-        observerUIListeners = new ArrayList<IObserverListChangedUIListener>();
+        // Set the default device and platform information
+        deviceName = "IoTivity Simulator";
+        try {
+            SimulatorManager.setDeviceInfo(deviceName);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "Error while registering the device info.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+        }
+
+        platformInfo = new PlatformInfo();
+        platformInfo.setPlatformID("Samsung Platform Identifier");
+        platformInfo.setManufacturerName("Samsung");
+        platformInfo.setManufacturerUrl("www.samsung.com");
+        platformInfo.setModelNumber("Samsung Model Num01");
+        platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
+        platformInfo.setPlatformVersion("PlatformVersion01");
+        platformInfo.setOperationSystemVersion("OSVersion01");
+        platformInfo.setHardwareVersion("HardwareVersion01");
+        platformInfo.setFirmwareVersion("FirwareVersion01");
+        platformInfo.setSupportUrl("http://www.samsung.com/support");
+        platformInfo.setSystemTime("2015-09-10T11:10:30Z");
+        try {
+            SimulatorManager.setPlatformInfo(platformInfo);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "Error while registering the platform info.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+        }
 
-        resourceModelChangeListener = new IResourceModelChangedListener() {
+        resourceModelChangeListener = new ResourceModelChangeListener() {
 
             @Override
             public void onResourceModelChanged(final String resourceURI,
@@ -115,59 +142,33 @@ public class ResourceManager {
                         if (null == resourceURI || null == resourceModelN) {
                             return;
                         }
-                        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
-                        if (null == resource) {
-                            return;
-                        }
 
-                        ModelChangeNotificationType notificationType;
-                        // Fetch the resource attributes
-                        Map<String, LocalResourceAttribute> resourceAttributeMapNew;
-                        resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
-                        if (null == resourceAttributeMapNew) {
-                            resource.setResourceAttributesMap(null);
-                            resourceModelChangedUINotification(
-                                    ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL,
-                                    resourceURI, null);
-                            return;
-                        }
-
-                        // Update the resource with new model data
-                        Map<String, LocalResourceAttribute> resourceAttributeMapOld;
-                        resourceAttributeMapOld = resource
-                                .getResourceAttributesMap();
-                        if (null == resourceAttributeMapOld) {
-                            resource.setResourceAttributesMap(resourceAttributeMapNew);
-                            resourceModelChangedUINotification(
-                                    ModelChangeNotificationType.ATTRIBUTE_ADDED,
-                                    resourceURI, null);
-                            return;
-                        }
-                        Set<LocalResourceAttribute> valueChangeSet = new HashSet<LocalResourceAttribute>();
-                        notificationType = compareAndUpdateLocalAttributes(
-                                resourceAttributeMapOld,
-                                resourceAttributeMapNew, valueChangeSet);
-                        if (notificationType != ModelChangeNotificationType.NONE) {
-                            // Update the UI listeners
-                            resourceModelChangedUINotification(
-                                    notificationType, resourceURI,
-                                    valueChangeSet);
-                        }
+                        Display.getDefault().asyncExec(new Runnable() {
+                            @Override
+                            public void run() {
+                                Resource resource = data
+                                        .getResourceByURI(resourceURI);
+                                if (null != resource) {
+                                    resource.setResourceRepresentation(resourceModelN);
+                                }
+                            }
+                        });
                     }
                 });
             }
         };
 
-        automationListener = new IAutomation() {
+        automationListener = new AutoUpdateListener() {
 
             @Override
-            public void onAutomationComplete(final String resourceURI,
+            public void onUpdateComplete(final String resourceURI,
                     final int automationId) {
                 synchronizerThread.addToQueue(new Runnable() {
 
                     @Override
                     public void run() {
-                        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
+                        SingleResource resource = data
+                                .getSingleResourceByURI(resourceURI);
                         if (null == resource) {
                             return;
                         }
@@ -176,40 +177,53 @@ public class ResourceManager {
                         if (resource.isResourceAutomationInProgress()) {
                             changeResourceLevelAutomationStatus(resource, false);
                             // Notify the UI listeners
-                            automationCompleteUINotification(resourceURI, null);
+                            UiListenerHandler.getInstance()
+                                    .automationCompleteUINotification(resource,
+                                            null);
                         } else if (resource.isAttributeAutomationInProgress()) {
                             // Find the attribute with the given automation id
-                            LocalResourceAttribute attribute;
-                            attribute = getAttributeWithGivenAutomationId(
+                            final AttributeElement attribute = getAttributeWithGivenAutomationId(
                                     resource, automationId);
                             if (null != attribute) {
-                                attribute.setAutomationInProgress(false);
+                                // Display.getDefault().asyncExec(new Runnable()
+                                // {
+                                // @Override
+                                // public void run() {
+                                attribute.setAutoUpdateState(false);
+                                // }
+                                // });
                                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
                                 // Notify the UI listeners
-                                automationCompleteUINotification(resourceURI,
-                                        attribute.getAttributeName());
+                                /*
+                                 * UiListenerHandler .getInstance()
+                                 * .automationCompleteUINotification( resource,
+                                 * attribute
+                                 * .getSimulatorResourceAttribute().name());
+                                 */
+                            } else {
+                                // TODO: Temporarily reset the attribute
+                                // automation status to false for making
+                                // resource-level automation work after
+                                // attribute-level automations.
+                                resource.setAttributeAutomationInProgress(false);
                             }
-                        } else {
-                            // Ignoring the notification as there are no
-                            // known automation for the current resource.
                         }
                     }
                 });
             }
         };
 
-        observer = new IObserver() {
+        observer = new ObserverListener() {
 
-            @Override
             public void onObserverChanged(final String resourceURI,
-                    final int status, final ObserverInfo observer) {
+                    final int status, final Observer observer) {
                 new Thread() {
                     @Override
                     public void run() {
                         if (null == resourceURI || null == observer) {
                             return;
                         }
-                        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
+                        Resource resource = data.getResourceByURI(resourceURI);
                         if (null == resource) {
                             return;
                         }
@@ -220,10 +234,21 @@ public class ResourceManager {
                             resource.removeObserverInfo(observer);
                         }
                         // Notify the UI listeners
-                        observerListChangedUINotification(resourceURI);
+                        UiListenerHandler.getInstance()
+                                .observerListChangedUINotification(resource);
                     }
                 }.start();
             }
+
+            @Override
+            public void onObserverAdded(String resourceURI, Observer observer) {
+                onObserverChanged(resourceURI, 0, observer);
+            }
+
+            @Override
+            public void onObserverRemoved(String resourceURI, Observer observer) {
+                onObserverChanged(resourceURI, 1, observer);
+            }
         };
 
         synchronizerThread = new NotificationSynchronizerThread();
@@ -273,891 +298,1228 @@ public class ResourceManager {
         }
     }
 
-    public void addResourceListChangedUIListener(
-            IResourceListChangedUIListener resourceListChangedUIListener) {
-        synchronized (resourceListChangedUIListeners) {
-            resourceListChangedUIListeners.add(resourceListChangedUIListener);
-        }
+    public String getDeviceName() {
+        return deviceName;
     }
 
-    public void addResourceSelectionChangedUIListener(
-            IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
-        synchronized (resourceSelectionChangedUIListeners) {
-            resourceSelectionChangedUIListeners
-                    .add(resourceSelectionChangedUIListener);
+    public void setDeviceName(String deviceName) {
+        this.deviceName = deviceName;
+        try {
+            SimulatorManager.setDeviceInfo(deviceName);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "Error while registering the device info.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+        }
+    }
+
+    public List<MetaProperty> getPlatformInfo() {
+        List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
+                .getPlatformID()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
+                platformInfo.getManufacturerName()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
+                platformInfo.getManufacturerUrl()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
+                platformInfo.getModelNumber()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
+                platformInfo.getDateOfManufacture()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
+                platformInfo.getPlatformVersion()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
+                platformInfo.getOperationSystemVersion()));
+        metaProperties.add(new MetaProperty(
+                Constants.PLATFORM_HARDWARE_VERSION, platformInfo
+                        .getHardwareVersion()));
+        metaProperties.add(new MetaProperty(
+                Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
+                        .getFirmwareVersion()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
+                platformInfo.getSupportUrl()));
+        metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
+                platformInfo.getSystemTime()));
+        return metaProperties;
+    }
+
+    public void setPlatformInfo(List<MetaProperty> metaProperties) {
+        if (null == metaProperties || metaProperties.size() < 1) {
+            return;
+        }
+        Iterator<MetaProperty> itr = metaProperties.iterator();
+        MetaProperty prop;
+        String propName;
+        String propValue;
+        while (itr.hasNext()) {
+            prop = itr.next();
+            propName = prop.getPropName();
+            propValue = prop.getPropValue();
+            if (propName.equals(Constants.PLATFORM_ID)) {
+                platformInfo.setPlatformID(propValue);
+            } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
+                platformInfo.setManufacturerName(propValue);
+            } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
+                platformInfo.setManufacturerUrl(propValue);
+            } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
+                platformInfo.setModelNumber(propValue);
+            } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
+                platformInfo.setDateOfManufacture(propValue);
+            } else if (propName.equals(Constants.PLATFORM_VERSION)) {
+                platformInfo.setPlatformVersion(propValue);
+            } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
+                platformInfo.setOperationSystemVersion(propValue);
+            } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
+                platformInfo.setHardwareVersion(propValue);
+            } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
+                platformInfo.setFirmwareVersion(propValue);
+            } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
+                platformInfo.setSupportUrl(propValue);
+            } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
+                platformInfo.setSystemTime(propValue);
+            }
+        }
+        try {
+            SimulatorManager.setPlatformInfo(platformInfo);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "Error while registering the platform info.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
         }
     }
 
-    public void addResourceModelChangedUIListener(
-            IResourceModelChangedUIListener resourceModelChangedUIListener) {
-        synchronized (resourceModelChangedUIListeners) {
-            resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
-        }
+    public synchronized Resource getCurrentResourceInSelection() {
+        return currentResourceInSelection;
     }
 
-    public void addAutomationUIListener(
-            IAutomationUIListener automationUIListener) {
-        synchronized (automationUIListeners) {
-            automationUIListeners.add(automationUIListener);
-        }
+    public synchronized void setCurrentResourceInSelection(Resource resource) {
+        this.currentResourceInSelection = resource;
     }
 
-    public void addObserverListChangedUIListener(
-            IObserverListChangedUIListener observerListChangedUIListener) {
-        synchronized (observerUIListeners) {
-            observerUIListeners.add(observerListChangedUIListener);
-        }
+    public synchronized Device getCurrentDeviceInSelection() {
+        return currentDeviceInSelection;
     }
 
-    public void removeResourceListChangedUIListener(
-            IResourceListChangedUIListener listener) {
-        synchronized (resourceListChangedUIListeners) {
-            if (null != listener && resourceListChangedUIListeners.size() > 0) {
-                resourceListChangedUIListeners.remove(listener);
-            }
-        }
+    public synchronized void setCurrentDeviceInSelection(Device dev) {
+        this.currentDeviceInSelection = dev;
     }
 
-    public void removeResourceSelectionChangedUIListener(
-            IResourceSelectionChangedUIListener listener) {
-        synchronized (resourceSelectionChangedUIListeners) {
-            if (null != listener
-                    && resourceSelectionChangedUIListeners.size() > 0) {
-                resourceSelectionChangedUIListeners.remove(listener);
-            }
-        }
+    public boolean isResourceExist(String resourceURI) {
+        return data.isResourceExist(resourceURI);
+    }
+
+    public boolean isAnyResourceExist() {
+        return data.isAnyResourceExist();
     }
 
-    public void removeResourceModelChangedUIListener(
-            IResourceModelChangedUIListener listener) {
-        synchronized (resourceModelChangedUIListeners) {
-            if (null != listener && resourceModelChangedUIListeners.size() > 0) {
-                resourceModelChangedUIListeners.remove(listener);
+    public boolean createSingleResource(SingleResource resource)
+            throws SimulatorException {
+        if (null == resource) {
+            return false;
+        }
+        String resType = (String) resource.getResourceTypes().toArray()[0];
+        try {
+            // 1. Create the resource.
+            SimulatorResource jSimulatorResource = SimulatorManager
+                    .createResource(SimulatorResource.Type.SINGLE,
+                            resource.getResourceName(),
+                            resource.getResourceURI(), resType);
+            if (null == jSimulatorResource
+                    || !(jSimulatorResource instanceof SimulatorSingleResource)) {
+                return false;
+            }
+            SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
+            resource.setSimulatorResource(jSimulatorSingleResource);
+
+            // 2. Cancel observable property if requested by user.
+            if (!resource.isObservable()) {
+                jSimulatorSingleResource.setObservable(false);
+            }
+
+            // 3. Set the model change listener.
+            jSimulatorSingleResource
+                    .setResourceModelChangeListener(resourceModelChangeListener);
+
+            // 4. Set the observer listener if the resource is observable.
+            if (resource.isObservable()) {
+                jSimulatorSingleResource.setObserverListener(observer);
+            }
+
+            // 5. Add attributes.
+            Map<String, LocalResourceAttribute> attributes = resource
+                    .getResourceAttributes();
+            if (null != attributes && !attributes.isEmpty()) {
+                Set<String> keySet = attributes.keySet();
+                Iterator<String> itr = keySet.iterator();
+
+                String attName;
+                LocalResourceAttribute localAtt;
+                SimulatorResourceAttribute simResAtt;
+
+                while (itr.hasNext()) {
+                    attName = itr.next();
+                    localAtt = attributes.get(attName);
+                    if (null == localAtt) {
+                        continue;
+                    }
+                    simResAtt = localAtt.getResourceAttributeRef();
+                    if (null == simResAtt) {
+                        continue;
+                    }
+                    jSimulatorSingleResource.addAttribute(simResAtt);
+                }
+
+                // 6. Get the resource model java object reference.
+                resource.setResourceModel(jSimulatorSingleResource
+                        .getResourceModel());
+
+                resource.setResourceRepresentation(resource.getResourceModel());
             }
+
+            // 7. Register the resource with the platform.
+            jSimulatorSingleResource.start();
+            resource.setStarted(true);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
         }
+
+        // 8. Add to local cache.
+        data.addResource(resource);
+
+        // 9. Update UI listeners
+        UiListenerHandler.getInstance().resourceCreatedUINotification(
+                ResourceType.SINGLE);
+
+        return true;
     }
 
-    public void removeAutomationUIListener(IAutomationUIListener listener) {
-        synchronized (automationUIListeners) {
-            if (null != listener && automationUIListeners.size() > 0) {
-                automationUIListeners.remove(listener);
+    public boolean createCollectionResource(CollectionResource resource)
+            throws SimulatorException {
+        if (null == resource) {
+            return false;
+        }
+        String resType = (String) resource.getResourceTypes().toArray()[0];
+        try {
+            // 1. Create the resource.
+            SimulatorResource jSimulatorResource = SimulatorManager
+                    .createResource(SimulatorResource.Type.COLLECTION,
+                            resource.getResourceName(),
+                            resource.getResourceURI(), resType);
+            if (null == jSimulatorResource
+                    || !(jSimulatorResource instanceof SimulatorCollectionResource)) {
+                return false;
+            }
+            SimulatorCollectionResource jSimulatorCollectionResource = (SimulatorCollectionResource) jSimulatorResource;
+            resource.setSimulatorResource(jSimulatorCollectionResource);
+
+            // 2. Cancel observable property if requested by user.
+            if (!resource.isObservable()) {
+                jSimulatorCollectionResource.setObservable(false);
             }
+
+            // 3. Set the observer listener if the resource is observable.
+            if (resource.isObservable()) {
+                jSimulatorCollectionResource.setObserverListener(observer);
+            }
+
+            // 4. Set the model change listener.
+            jSimulatorCollectionResource
+                    .setResourceModelChangeListener(resourceModelChangeListener);
+
+            // set resource model
+            resource.setResourceRepresentation(jSimulatorCollectionResource
+                    .getResourceModel());;
+
+            // 5. Register the resource with the platform.
+            jSimulatorCollectionResource.start();
+            resource.setStarted(true);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
         }
+
+        // 6. Add to local cache.
+        data.addResource(resource);
+
+        // 7. Update UI listeners
+        UiListenerHandler.getInstance().resourceCreatedUINotification(
+                ResourceType.COLLECTION);
+
+        return true;
     }
 
-    public void removeObserverListChangedUIListener(
-            IObserverListChangedUIListener listener) {
-        synchronized (observerUIListeners) {
-            if (null != listener && observerUIListeners.size() > 0) {
-                observerUIListeners.remove(listener);
+    public Resource createResourceByRAML(String configFilePath)
+            throws SimulatorException {
+        Resource resource = null;
+        try {
+            // 1. Create the resource
+            SimulatorResource jSimulatorResource = SimulatorManager
+                    .createResource(configFilePath);
+            if (null == jSimulatorResource) {
+                return null;
+            }
+            if (jSimulatorResource instanceof SimulatorSingleResource) {
+                resource = new SingleResource();
+            } else {
+                resource = new CollectionResource();
+            }
+            resource.setSimulatorResource(jSimulatorResource);
+
+            // 2. Fetch and locally store the resource name and uri.
+            String uri = jSimulatorResource.getURI();
+            if (null == uri || uri.trim().isEmpty()) {
+                return null;
+            }
+            resource.setResourceURI(uri.trim());
+
+            String name = jSimulatorResource.getName();
+            if (null == name || name.trim().isEmpty()) {
+                return null;
             }
+            resource.setResourceName(name.trim());
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
         }
+        return resource;
     }
 
-    public synchronized SimulatorResource getCurrentResourceInSelection() {
-        return currentResourceInSelection;
-    }
+    /**
+     * This method can set/change the resource uri and name of an already
+     * created resource which is not yet registered with the platform. This
+     * method registers the model change and observer listeners, registers the
+     * resource, fetches the resource attributes, updates the local cache and
+     * notifies the UI listeners.
+     */
+    public boolean completeSingleResourceCreationByRAML(Resource resource,
+            String uri, String name, boolean multiInstance)
+            throws SimulatorException {
+        if (null == resource || !(resource instanceof SingleResource)) {
+            return false;
+        }
+        try {
+            SingleResource singleRes = (SingleResource) resource;
 
-    public synchronized void setCurrentResourceInSelection(
-            SimulatorResource resource) {
-        this.currentResourceInSelection = resource;
-    }
+            SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
+                    .getSimulatorResource();
+            if (null == jSimulatorSingleResource) {
+                return false;
+            }
 
-    private void addResourceUriToOrderedMap(String resourceType,
-            String resourceURI) {
-        if (null != resourceURI && null != resourceType) {
-            synchronized (orderedResourceUriMap) {
-                ArrayList<String> uriListForType = orderedResourceUriMap
-                        .get(resourceType);
-                if (null == uriListForType) {
-                    uriListForType = new ArrayList<String>();
-                    orderedResourceUriMap.put(resourceType, uriListForType);
+            // 1. Update resource URI and Name if they are changed.
+            String newUri = uri.trim();
+            String newName = name.trim();
+
+            if (multiInstance) {
+                singleRes.setResourceURI(newUri);
+                singleRes.setResourceName(newName);
+            } else {
+                if (!singleRes.getResourceURI().equals(newUri)) {
+                    jSimulatorSingleResource.setURI(newUri);
+                    singleRes.setResourceURI(newUri);
                 }
-                uriListForType.add(resourceURI);
+                if (!singleRes.getResourceName().equals(newName)) {
+                    jSimulatorSingleResource.setName(newName);
+                    singleRes.setResourceName(newName);
+                }
+            }
+
+            // 2. Set the model change listener.
+            jSimulatorSingleResource
+                    .setResourceModelChangeListener(resourceModelChangeListener);
+
+            // 3. Set the observer listener if the resource is observable.
+            if (jSimulatorSingleResource.isObservable()) {
+                jSimulatorSingleResource.setObserverListener(observer);
+                singleRes.setObservable(true);
+            }
+
+            // 4. Fetch the resource model.
+            SimulatorResourceModel jResModel = jSimulatorSingleResource
+                    .getResourceModel();
+            if (null == jResModel) {
+                return false;
             }
+            singleRes.setResourceModel(jResModel);
+
+            // 5. Fetch the basic details of the resource.
+            singleRes.addResourceType(jSimulatorSingleResource
+                    .getResourceType());
+            singleRes
+                    .setResourceInterfaces(Utility
+                            .convertVectorToSet(jSimulatorSingleResource
+                                    .getInterface()));
+
+            // 6. Register the resource with the platform.
+            jSimulatorSingleResource.start();
+            singleRes.setStarted(true);
+
+            singleRes.setResourceRepresentation(jResModel);
+
+            // 7. Fetch the resource attributes.
+            Map<String, LocalResourceAttribute> resourceAttributeMap;
+            resourceAttributeMap = fetchResourceAttributesFromModel(jResModel);
+            if (null != resourceAttributeMap) {
+                singleRes.setResourceAttributes(resourceAttributeMap);
+            }
+
+            // 8. Add to local cache.
+            data.addResource(singleRes);
+
+            // 9. Update UI listeners for single instance creation
+            if (!multiInstance)
+                UiListenerHandler.getInstance().resourceCreatedUINotification(
+                        ResourceType.SINGLE);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
         }
+        return true;
     }
 
-    private void removeResourceUriFromOrderedMap(String resourceType,
-            String resourceURI) {
-        synchronized (orderedResourceUriMap) {
-            if (null != resourceURI && null != resourceType) {
-                ArrayList<String> uriListForType = orderedResourceUriMap
-                        .get(resourceType);
-                if (null != uriListForType) {
-                    uriListForType.remove(resourceURI);
-                    if (uriListForType.size() < 1) {
-                        orderedResourceUriMap.remove(resourceType);
-                    }
-                }
-            } else if (null != resourceType) {
-                orderedResourceUriMap.remove(resourceType);
-            } else {
-                orderedResourceUriMap.clear();
+    /**
+     * This method can set/change the resource uri and name of an already
+     * created resource which is not yet registered with the platform. This
+     * method registers the model change and observer listeners, registers the
+     * resource, fetches the resource attributes, updates the local cache and
+     * notifies the UI listeners.
+     */
+    public boolean completeCollectionResourceCreationByRAML(Resource resource,
+            String uri, String name) throws SimulatorException {
+        if (null == resource || !(resource instanceof CollectionResource)) {
+            return false;
+        }
+        try {
+            CollectionResource collectionRes = (CollectionResource) resource;
+
+            SimulatorCollectionResource jSimulatorCollectionResource = (SimulatorCollectionResource) resource
+                    .getSimulatorResource();
+            if (null == jSimulatorCollectionResource) {
+                return false;
+            }
+
+            // 1. Update resource URI and Name if they are changed.
+            String newUri = uri.trim();
+            String newName = name.trim();
+
+            if (!collectionRes.getResourceURI().equals(newUri)) {
+                jSimulatorCollectionResource.setURI(newUri);
+                collectionRes.setResourceURI(newUri);
+            }
+            if (!collectionRes.getResourceName().equals(newName)) {
+                jSimulatorCollectionResource.setName(newName);
+                collectionRes.setResourceName(newName);
+            }
+
+            // 2. Set the model change listener.
+            jSimulatorCollectionResource
+                    .setResourceModelChangeListener(resourceModelChangeListener);
+
+            // 3. Fetch the resource model.
+            SimulatorResourceModel jResModel = jSimulatorCollectionResource
+                    .getResourceModel();
+            if (null == jResModel) {
+                return false;
+            }
+            collectionRes.setResourceModel(jResModel);
+
+            // 4. Fetch the basic details of the resource.
+            collectionRes.addResourceType(jSimulatorCollectionResource
+                    .getResourceType());
+            collectionRes.setResourceInterfaces(Utility
+                    .convertVectorToSet(jSimulatorCollectionResource
+                            .getInterface()));
+
+            // 5. Set the observer listener if the resource is observable.
+            if (jSimulatorCollectionResource.isObservable()) {
+                jSimulatorCollectionResource.setObserverListener(observer);
+                collectionRes.setObservable(true);
+            }
+
+            // 6. Register the resource with the platform.
+            jSimulatorCollectionResource.start();
+            collectionRes.setStarted(true);
+
+            collectionRes.setResourceRepresentation(jResModel);
+
+            // 7. Fetch the resource attributes.
+            Map<String, LocalResourceAttribute> resourceAttributeMap;
+            resourceAttributeMap = fetchResourceAttributesFromModel(jResModel);
+            if (null != resourceAttributeMap) {
+                collectionRes.setResourceAttributes(resourceAttributeMap);
             }
+
+            // 6. Add to local cache.
+            data.addResource(collectionRes);
+
+            // 7. Update UI listeners for single instance creation
+            UiListenerHandler.getInstance().resourceCreatedUINotification(
+                    ResourceType.COLLECTION);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
         }
+        return true;
     }
 
-    private void addResourceToMap(SimulatorResource simulatorResource) {
-        if (null != simulatorResource) {
-            synchronized (resourceMap) {
-                Map<String, SimulatorResource> resourceTypeMap;
-                resourceTypeMap = resourceMap.get(simulatorResource
-                        .getResourceType());
-                if (null == resourceTypeMap) {
-                    resourceTypeMap = new HashMap<String, SimulatorResource>();
-                    resourceMap.put(simulatorResource.getResourceType(),
-                            resourceTypeMap);
+    public int createSingleResourceMultiInstances(String configFile, int count)
+            throws SimulatorException {
+        int createCount = 0;
+        try {
+            Vector<SimulatorResource> jSimulatorResources = SimulatorManager
+                    .createResource(configFile, count);
+            if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
+                return 0;
+            }
+            SimulatorSingleResource jResource;
+            SingleResource resource;
+            boolean result;
+            for (SimulatorResource jSimulatorResource : jSimulatorResources) {
+                jResource = (SimulatorSingleResource) jSimulatorResource;
+                resource = new SingleResource();
+                resource.setSimulatorResource(jResource);
+                try {
+                    result = completeSingleResourceCreationByRAML(resource,
+                            jResource.getURI(), jResource.getName(), true);
+                    if (result) {
+                        createCount++;
+                    }
+                } catch (SimulatorException eInner) {
+                    Activator
+                            .getDefault()
+                            .getLogManager()
+                            .log(Level.ERROR.ordinal(),
+                                    new Date(),
+                                    Utility.getSimulatorErrorString(eInner,
+                                            null));
                 }
-                resourceTypeMap.put(simulatorResource.getResourceURI(),
-                        simulatorResource);
             }
+            if (createCount > 0) {
+                UiListenerHandler.getInstance().resourceCreatedUINotification(
+                        ResourceType.SINGLE);
+            }
+        } catch (SimulatorException eOuter) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(eOuter, null));
+            throw eOuter;
         }
+        return createCount;
     }
 
-    private void addResourceToMap(String resourceType,
-            Map<String, SimulatorResource> newResourceTypeMap) {
-        if (null != resourceType && null != newResourceTypeMap) {
-            synchronized (resourceMap) {
-                Map<String, SimulatorResource> resourceTypeMap = resourceMap
-                        .get(resourceType);
-                if (null != resourceTypeMap) {
-                    resourceTypeMap.putAll(newResourceTypeMap);
-                } else {
-                    resourceMap.put(resourceType, newResourceTypeMap);
+    public void createDevice(String deviceName, Set<Resource> childs) {
+        // 1. Create device
+        Device dev = new Device();
+        dev.setDeviceName(deviceName);
+        data.addDevice(dev);
+
+        // 2. Add children to device
+        if (null != childs && !childs.isEmpty())
+            addResourceToDevice(dev, childs);
+
+        // 3. Update ui listeners
+        UiListenerHandler.getInstance().resourceListUpdateUINotification(
+                ResourceType.DEVICE);
+    }
+
+    private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
+            SimulatorResourceModel jResModel) throws SimulatorException {
+        Map<String, LocalResourceAttribute> resourceAttributeMap = null;
+        if (null != jResModel) {
+            Map<String, SimulatorResourceAttribute> jAttributeMap;
+            jAttributeMap = jResModel.getAttributes();
+            if (null != jAttributeMap) {
+                resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
+                Iterator<String> itr = jAttributeMap.keySet().iterator();
+                String attName;
+                SimulatorResourceAttribute jResAtt;
+                LocalResourceAttribute localAtt;
+                while (itr.hasNext()) {
+                    attName = itr.next();
+                    if (null != attName) {
+                        jResAtt = jAttributeMap.get(attName);
+                        if (null != jResAtt) {
+                            localAtt = new LocalResourceAttribute();
+
+                            localAtt.setResourceAttributeRef(jResAtt);
+
+                            // Initially disabling the automation
+                            localAtt.setAutomationInProgress(false);
+
+                            // Assigning the default automation interval
+                            localAtt.setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
+
+                            // Setting the default automation type
+                            localAtt.setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
+
+                            resourceAttributeMap.put(attName, localAtt);
+                        }
+                    }
                 }
             }
         }
+        return resourceAttributeMap;
+
     }
 
-    private void removeResourceFromMap(String resourceType, String resourceURI) {
-        if (null != resourceURI && null != resourceType) {
-            synchronized (resourceMap) {
-                Map<String, SimulatorResource> resourceTypeMap = resourceMap
-                        .get(resourceType);
-                if (null != resourceTypeMap) {
-                    resourceTypeMap.remove(resourceURI);
-                    if (resourceTypeMap.size() < 1) {
-                        resourceMap.remove(resourceType);
+    // This method gives all known possible values of the attribute in string
+    // format. It takes allowed values or range of values whichever is available
+    private List<String> getValueList(SimulatorResourceAttribute attributeN) {
+        AttributeProperty attProp = attributeN.property();
+        if (null == attProp) {
+            return null;
+        }
+        List<String> valueList = new ArrayList<String>();
+        Type valuesType = attProp.type();
+        if (valuesType == Type.VALUESET) {
+            Object[] allowedValues = attProp.valueSet();
+            if (null != allowedValues && allowedValues.length > 0) {
+                for (Object value : allowedValues) {
+                    if (null != value) {
+                        valueList.add(String.valueOf(((AttributeValue) value)
+                                .get()));
                     }
                 }
             }
+        } else if (valuesType == Type.RANGE) {
+            double minD = attProp.min();
+            double maxD = attProp.max();
+            for (double value = minD; value <= maxD; value++) {
+                valueList.add(String.valueOf(value));
+            }
         }
+        Object attValue = attributeN.value().get();
+        if (valueList.size() < 1 && null != attValue) {
+            valueList.add(String.valueOf(attValue));
+        }
+        return valueList;
     }
 
-    public boolean isResourceExist(String resourceURI) {
-        boolean result = false;
-        if (null != resourceURI) {
-            SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
-            if (null != resource) {
-                result = true;
-            }
+    public List<Resource> getResourceList() {
+        List<Resource> resourceList = data.getResources();
+        if (null == resourceList) {
+            return null;
         }
-        return result;
+        // Sort the list
+        Collections.sort(resourceList, Utility.resourceComparator);
+
+        return resourceList;
     }
 
-    public void addResourceDisplayName(String displayName, String completeURI) {
-        synchronized (displayNameMap) {
-            displayNameMap.put(displayName, completeURI);
+    public List<SingleResource> getSingleResourceList() {
+        List<SingleResource> resourceList = data.getSingleResources();
+        if (null == resourceList) {
+            return null;
         }
+        // Sort the list
+        Collections.sort(resourceList, Utility.singleResourceComparator);
+
+        return resourceList;
     }
 
-    public void removeResourceDisplayName(String displayName) {
-        synchronized (displayNameMap) {
-            displayNameMap.remove(displayName);
+    public List<CollectionResource> getCollectionResourceList() {
+        List<CollectionResource> resourceList = data.getCollectionResources();
+        if (null == resourceList) {
+            return null;
         }
+        // Sort the list
+        Collections.sort(resourceList, Utility.collectionResourceComparator);
+
+        return resourceList;
     }
 
-    public boolean isDisplayName(String displayName) {
-        boolean exist = false;
-        synchronized (displayNameMap) {
-            exist = displayNameMap.containsKey(displayName);
+    public List<Device> getDeviceList() {
+        List<Device> deviceList = data.getDevices();
+        if (null == deviceList) {
+            return null;
         }
-        return exist;
+        // Sort the list
+        Collections.sort(deviceList, Utility.deviceComparator);
+        return deviceList;
     }
 
-    public String getCompleteUriFromDisplayName(String displayName) {
-        String completeURI = null;
-        synchronized (displayNameMap) {
-            completeURI = displayNameMap.get(displayName);
+    // Returns the number of resources which are added properly to the
+    // collection.
+    public int addResourceToCollection(CollectionResource collectionParent,
+            Set<Resource> childs) {
+        if (null == collectionParent || null == childs || childs.isEmpty()) {
+            return -1;
+        }
+        Iterator<Resource> itr = childs.iterator();
+        Resource res;
+        int count = childs.size();
+        while (itr.hasNext()) {
+            res = itr.next();
+            try {
+                addResourceToCollection(collectionParent, res);
+            } catch (SimulatorException e) {
+                count--;
+            }
         }
-        return completeURI;
+        return count;
     }
 
-    public void createResource(final String configFilePath) {
-        new Thread() {
-            @Override
-            public void run() {
-                SimulatorResourceServer resourceServerN;
-                try {
-                    resourceServerN = SimulatorManager.createResource(
-                            configFilePath, resourceModelChangeListener);
-                } catch (SimulatorException e) {
-                    Activator
-                            .getDefault()
-                            .getLogManager()
-                            .log(Level.ERROR.ordinal(),
-                                    new Date(),
-                                    "[" + e.getClass().getSimpleName() + "]"
-                                            + e.code().toString() + "-"
-                                            + e.message());
-                    return;
-                }
-                SimulatorResource simulatorResource;
-                simulatorResource = fetchResourceData(resourceServerN);
-                if (null != simulatorResource) {
-                    String uri = simulatorResource.getResourceURI();
-                    addResourceToMap(simulatorResource);
-                    addResourceUriToOrderedMap(
-                            simulatorResource.getResourceType(), uri);
-                    resourceCreatedUINotification();
-
-                    // Form the display URI
-                    String displayName = Utility.uriToDisplayName(uri);
-                    if (null != displayName) {
-                        addResourceDisplayName(displayName, uri);
-                    } else {
-                        Activator
-                                .getDefault()
-                                .getLogManager()
-                                .log(Level.ERROR.ordinal(),
-                                        new Date(),
-                                        "Converting the URI to display format for "
-                                                + uri + "failed!");
-                    }
+    public void addResourceToCollection(CollectionResource collectionParent,
+            Resource child) throws SimulatorException {
+        if (null == collectionParent || null == child) {
+            return;
+        }
+        try {
+            // 1. Add child to collection
+            collectionParent.addChildResource(child);
 
-                    // Set the observer for the created resource
-                    try {
-                        resourceServerN.setObserverCallback(observer);
-                    } catch (SimulatorException e) {
-                        Activator
-                                .getDefault()
-                                .getLogManager()
-                                .log(Level.ERROR.ordinal(),
-                                        new Date(),
-                                        "[" + e.getClass().getSimpleName()
-                                                + "]" + e.code().toString()
-                                                + "-" + e.message());
-                    }
+            // 2. Add a reference to the collection in the child
+            if (child instanceof SingleResource) {
+                ((SingleResource) child)
+                        .addCollectionMembership(collectionParent);
+            } else {
+                ((CollectionResource) child).addMembership(collectionParent);
+            }
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
+    }
 
-                    // Print the resource data
-                    simulatorResource.printResourceInfo();
-                }
+    public int addResourceToCollection(Set<CollectionResource> collections,
+            Resource child) {
+        if (null == collections || collections.isEmpty() || null == child) {
+            return -1;
+        }
+        Iterator<CollectionResource> itr = collections.iterator();
+        CollectionResource res;
+        int count = collections.size();
+        while (itr.hasNext()) {
+            res = itr.next();
+            try {
+                addResourceToCollection(res, child);
+            } catch (SimulatorException e) {
+                count--;
             }
-        }.start();
+        }
+        return count;
     }
 
-    public void createResource(final String configFilePath,
-            final int noOfInstances) {
-        new Thread() {
-            @Override
-            public void run() {
-                Map<String, SimulatorResource> resourceTypeMap;
-                SimulatorResourceServer[] simulatorResourceServers = null;
-                try {
-                    simulatorResourceServers = SimulatorManager.createResource(
-                            configFilePath, noOfInstances,
-                            resourceModelChangeListener);
-                } catch (SimulatorException e) {
-                    Activator
-                            .getDefault()
-                            .getLogManager()
-                            .log(Level.ERROR.ordinal(),
-                                    new Date(),
-                                    "[" + e.getClass().getSimpleName() + "]"
-                                            + e.code().toString() + "-"
-                                            + e.message());
-                    return;
-                }
-                if (null == simulatorResourceServers) {
-                    return;
-                }
-                resourceTypeMap = new HashMap<String, SimulatorResource>();
-                SimulatorResource resource;
-                String uri;
-                String displayName;
-                for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
-                    resource = fetchResourceData(resourceServerN);
-                    if (null != resource) {
-                        uri = resource.getResourceURI();
-                        resourceTypeMap.put(uri, resource);
-                        addResourceUriToOrderedMap(resource.getResourceType(),
-                                uri);
-
-                        // Form the display URI
-                        displayName = Utility.uriToDisplayName(uri);
-                        if (null != displayName) {
-                            addResourceDisplayName(displayName, uri);
-                        } else {
-                            Activator
-                                    .getDefault()
-                                    .getLogManager()
-                                    .log(Level.ERROR.ordinal(),
-                                            new Date(),
-                                            "Converting the URI to display format for "
-                                                    + uri + "failed!");
-                        }
-                    }
-                    // Set the observer for the created resource
-                    try {
-                        resourceServerN.setObserverCallback(observer);
-                    } catch (SimulatorException e) {
-                        Activator
-                                .getDefault()
-                                .getLogManager()
-                                .log(Level.ERROR.ordinal(),
-                                        new Date(),
-                                        "[" + e.getClass().getSimpleName()
-                                                + "]" + e.code().toString()
-                                                + "-" + e.message());
-                    }
-                }
+    public void addResourceToDevice(Device dev, Set<Resource> childs) {
+        // 1. Add children to the device.
+        dev.addChildResource(childs);
 
-                // Find the resourceType and add it to the local data
-                // structure and notify UI Listeners
-                if (resourceTypeMap.size() > 0) {
-                    String resourceType;
-                    Set<String> uriSet = resourceTypeMap.keySet();
-                    Iterator<String> itr = uriSet.iterator();
-                    if (itr.hasNext()) {
-                        SimulatorResource simResource = resourceTypeMap.get(itr
-                                .next());
-                        if (null != simResource) {
-                            resourceType = simResource.getResourceType();
-
-                            addResourceToMap(resourceType, resourceTypeMap);
-                            resourceCreatedUINotification();
-                        }
-                    }
-                }
+        // 2. Add a reference to the device in all children.
+        Iterator<Resource> itr = childs.iterator();
+        Resource res;
+        while (itr.hasNext()) {
+            res = itr.next();
+            if (res instanceof SingleResource) {
+                ((SingleResource) res).addDeviceMembership(dev);
+            } else {
+                ((CollectionResource) res).addDeviceMembership(dev);
             }
-        }.start();
+        }
+    }
+
+    public void addResourceToDevice(Device dev, Resource child) {
+        // 1. Add child to the device.
+        dev.addChildResource(child);
+
+        // 2. Add a reference to the device in the child.
+        if (child instanceof SingleResource) {
+            ((SingleResource) child).addDeviceMembership(dev);
+        } else {
+            ((CollectionResource) child).addDeviceMembership(dev);
+        }
     }
 
-    private SimulatorResource fetchResourceData(
-            SimulatorResourceServer resourceServerN) {
-        SimulatorResource simulatorResource = null;
-        if (null != resourceServerN) {
-            simulatorResource = new SimulatorResource();
-            simulatorResource.setResourceServer(resourceServerN);
-            simulatorResource.setResourceURI(resourceServerN.getURI());
-            simulatorResource
-                    .setResourceType(resourceServerN.getResourceType());
-            simulatorResource.setResourceName(resourceServerN.getName());
-            simulatorResource.setResourceInterface(resourceServerN
-                    .getInterfaceType());
-
-            SimulatorResourceModel resourceModelN;
+    public void addResourceToDevice(Set<Device> devices, Resource child) {
+        // 1. Add device reference in child.
+        if (child instanceof SingleResource)
+            ((SingleResource) child).addDeviceMembership(devices);
+        else
+            ((CollectionResource) child).addDeviceMembership(devices);
+
+        // 2. Add a reference to the child in all devices.
+        Iterator<Device> itr = devices.iterator();
+        Device dev;
+        while (itr.hasNext()) {
+            dev = itr.next();
+            dev.addChildResource(child);
+        }
+    }
+
+    public int removeResourceFromCollection(
+            Set<CollectionResource> collections, Resource resource) {
+        // 1. Remove the reference of resource from all the collections.
+        Iterator<CollectionResource> itr = collections.iterator();
+        CollectionResource colRes;
+        int count = collections.size();
+        while (itr.hasNext()) {
+            colRes = itr.next();
             try {
-                resourceModelN = resourceServerN.getModel();
+                removeResourceFromCollection(colRes, resource);
             } catch (SimulatorException e) {
-                Activator
-                        .getDefault()
-                        .getLogManager()
-                        .log(Level.ERROR.ordinal(),
-                                new Date(),
-                                "[" + e.getClass().getSimpleName() + "]"
-                                        + e.code().toString() + "-"
-                                        + e.message());
-                return null;
+                count--;
             }
-            if (null != resourceModelN) {
-                simulatorResource.setResourceModel(resourceModelN);
+        }
+        return count;
 
-                // Fetch the resource attributes
-                Map<String, LocalResourceAttribute> resourceAttributeMap;
-                resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
-                if (null != resourceAttributeMap) {
-                    simulatorResource
-                            .setResourceAttributesMap(resourceAttributeMap);
-                }
-            }
+    }
+
+    public void removeResourceFromDevice(Set<Device> devices, Resource resource) {
+        // 1. Remove the reference of resource from all the devices.
+        Iterator<Device> itr = devices.iterator();
+        Device dev;
+        while (itr.hasNext()) {
+            dev = itr.next();
+            dev.removeChildResource(resource);
         }
-        return simulatorResource;
+
+        // 2. Remove the reference of devices from the resource.
+        resource.removeDeviceMembership(devices);
     }
 
-    private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
-            SimulatorResourceModel resourceModelN) {
-        Map<String, LocalResourceAttribute> resourceAttributeMap = null;
-        if (null != resourceModelN) {
-            Map<String, ResourceAttribute> attributeMapN;
+    // Returns the count of resources removed from the collection
+    public int removeResourcesFromCollection(CollectionResource colRes,
+            Set<Resource> resources) {
+        Iterator<Resource> itr = resources.iterator();
+        Resource res;
+        int count = resources.size();
+        while (itr.hasNext()) {
+            res = itr.next();
             try {
-                attributeMapN = resourceModelN.getAttributes();
+                removeResourceFromCollection(colRes, res);
             } catch (SimulatorException e) {
-                Activator
-                        .getDefault()
-                        .getLogManager()
-                        .log(Level.ERROR.ordinal(),
-                                new Date(),
-                                "[" + e.getClass().getSimpleName() + "]"
-                                        + e.code().toString() + "-"
-                                        + e.message());
-                return null;
+                count--;
             }
-            if (null != attributeMapN) {
-                resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
+        }
+        return count;
+    }
 
-                Set<String> attNameSet = attributeMapN.keySet();
-                String attName;
-                ResourceAttribute attributeN;
-                LocalResourceAttribute attribute;
-                Iterator<String> attNameItr = attNameSet.iterator();
-                while (attNameItr.hasNext()) {
-                    attName = attNameItr.next();
-                    attributeN = attributeMapN.get(attName);
-                    if (null != attributeN) {
-                        attribute = new LocalResourceAttribute();
-                        attribute.setResourceAttribute(attributeN);
-
-                        // Set the attribute value
-                        Object valueObj = attributeN.getValue();
-                        if (null != valueObj) {
-                            attribute.setAttributeValue(valueObj);
-                        }
+    public void removeResourcesFromDevice(Device dev, Set<Resource> resources) {
+        Iterator<Resource> itr = resources.iterator();
+        Resource res;
+        while (itr.hasNext()) {
+            res = itr.next();
+            res.removeDeviceMembership(dev);
+        }
+        dev.removeChildResource(resources);
+    }
 
-                        // Set the attribute value list.
-                        attribute.setAttValues(getValueList(attributeN));
+    public void removeResourceFromCollection(CollectionResource parent,
+            Resource child) throws SimulatorException {
+        try {
+            // 1. Remove the child from the parent
+            parent.removeChildResource(child);
 
-                        // Initially disabling the automation
-                        attribute.setAutomationInProgress(false);
+            // 2. Remove the reference to parent from child
+            if (child instanceof SingleResource) {
+                ((SingleResource) child).removeCollectionMembership(parent);
+            } else {
+                ((CollectionResource) child).removeMembership(parent);
+            }
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
+    }
 
-                        // TODO: Temporarily setting the interval to 500.
-                        // This value should come from the native layer.
-                        // Native implementation is in progress.
-                        attribute
-                                .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
+    public void removeResourceFromDevice(Device parent, Resource child) {
+        // 1. Remove the reference to parent from child
+        child.removeDeviceMembership(parent);
 
-                        // Setting the default automation type
-                        attribute
-                                .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
+        // 2. Remove the child from the parent
+        parent.removeChildResource(child);
+    }
 
-                        resourceAttributeMap.put(attName, attribute);
-                    }
-                }
-            }
+    public void removeSingleResources(Set<SingleResource> resources)
+            throws SimulatorException {
+        if (null == resources) {
+            return;
+        }
+        Iterator<SingleResource> itr = resources.iterator();
+        while (itr.hasNext()) {
+            removeResource(itr.next());
         }
-        return resourceAttributeMap;
     }
 
-    // This method gives all known possible values of the attribute in string
-    // format. It takes allowed values or range of values whichever is available
-    private List<String> getValueList(ResourceAttribute attributeN) {
-        Object[] allowedValues = attributeN.getAllowedValues();
-        List<String> valueList = new ArrayList<String>();
-        if (null != allowedValues && allowedValues.length > 0) {
-            for (Object value : allowedValues) {
-                if (null != value) {
-                    valueList.add(String.valueOf(value));
-                }
-            }
-        } else {
-            Type valueType = attributeN.getBaseType();
-            Range range = attributeN.getRange();
-            if (null != range) {
-                Object min = range.getMin();
-                Object max = range.getMax();
-                if (valueType == Type.INT) {
-                    int minI = (Integer) min;
-                    int maxI = (Integer) max;
-                    for (int value = minI; value <= maxI; value++) {
-                        valueList.add(String.valueOf(value));
-                    }
-                } else if (valueType == Type.DOUBLE) {
-                    double minD = (Double) min;
-                    double maxD = (Double) max;
-                    for (double value = minD; value <= maxD; value++) {
-                        valueList.add(String.valueOf(value));
-                    }
-                }
-            }
+    public void removeCollectionResources(Set<CollectionResource> resources)
+            throws SimulatorException {
+        if (null == resources) {
+            return;
         }
-        Object attValue = attributeN.getValue();
-        if (valueList.size() < 1 && null != attValue) {
-            valueList.add(String.valueOf(attValue));
+        Iterator<CollectionResource> itr = resources.iterator();
+        while (itr.hasNext()) {
+            removeResource(itr.next());
         }
-        return valueList;
     }
 
-    public void deleteResourceByURI(final String resourceURI) {
-        if (null != resourceURI) {
-            new Thread() {
-                @Override
-                public void run() {
-                    SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
-                    if (null != resource) {
-                        String resourceType = resource.getResourceType();
+    public void removeResource(Resource res) throws SimulatorException {
+        // 1. Unregister the resource from the platform.
+        SimulatorResource simRes = res.getSimulatorResource();
+        try {
+            simRes.stop();
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
 
-                        // Unregister the resource from the platform
-                        deleteResource(resource);
+        Set<CollectionResource> collectionMembership;
+        Set<Device> deviceMembership;
 
-                        // Delete from the local data structure
-                        deleteLocalResourceDetails(resourceType, resourceURI);
+        if (res instanceof SingleResource) {
+            collectionMembership = ((SingleResource) res)
+                    .getCollectionMembership();
+            deviceMembership = ((SingleResource) res).getDeviceMembership();
+        } else {
+            collectionMembership = ((CollectionResource) res).getMembership();
+            deviceMembership = ((CollectionResource) res).getDeviceMembership();
+        }
 
-                        // Notify the UI listener for removing this resource
-                        // from UI
-                        resourceDeletedUINotification();
+        // 2. Delete from the collections to which this resource is a member.
+        if (null != collectionMembership && !collectionMembership.isEmpty()) {
+            removeResourceFromCollection(collectionMembership, res);
+        }
 
-                        if (null != currentResourceInSelection
-                                && resource == currentResourceInSelection) {
-                            // Listeners might query the resource being deleted
-                            // if exists. So set the currently selection to
-                            // null.
-                            setCurrentResourceInSelection(null);
+        // 3. Delete from the devices to which this resource is a member.
+        if (null != deviceMembership && !deviceMembership.isEmpty()) {
+            removeResourceFromDevice(deviceMembership, res);
+        }
 
-                            // Notify all observers for resource selection
-                            // change event
-                            resourceSelectionChangedUINotification();
-                        }
-                    }
-                }
-            }.start();
-        }
-    }
-
-    private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
-        SimulatorResource resource = null;
-        if (null != resourceURI) {
-            synchronized (resourceMap) {
-                Set<String> typeSet = resourceMap.keySet();
-                Iterator<String> typeItr = typeSet.iterator();
-                String resourceType;
-                Map<String, SimulatorResource> resourceTypeMap;
-                while (typeItr.hasNext()) {
-                    resourceType = typeItr.next();
-                    resourceTypeMap = resourceMap.get(resourceType);
-                    if (null != resourceTypeMap) {
-                        resource = resourceTypeMap.get(resourceURI);
-                        if (null != resource) {
-                            break;
-                        }
-                    }
-                }
+        // 4. Delete this resource
+        data.deleteResource(res);
+    }
+
+    public void removeDevice(Device dev) {
+        Set<Resource> childs = dev.getChildResources();
+        if (null != childs && !childs.isEmpty()) {
+            // 1. Remove the reference from all the children.
+            Iterator<Resource> itr = childs.iterator();
+            Resource res;
+            while (itr.hasNext()) {
+                res = itr.next();
+                res.removeDeviceMembership(dev);
             }
         }
-        return resource;
+        // 2. Delete the device.
+        data.deleteDevice(dev);
     }
 
-    private void deleteResource(SimulatorResource resource) {
-        if (null != resource) {
-            SimulatorResourceServer resourceServerN = resource
-                    .getResourceServer();
-            if (null != resourceServerN) {
-                try {
-                    SimulatorManager.deleteResource(resourceServerN);
-                } catch (SimulatorException e) {
-                    Activator
-                            .getDefault()
-                            .getLogManager()
-                            .log(Level.ERROR.ordinal(),
-                                    new Date(),
-                                    "[" + e.getClass().getSimpleName() + "]"
-                                            + e.code().toString() + "-"
-                                            + e.message());
-                }
+    public boolean isUriUnique(List<MetaProperty> properties) {
+        if (null == properties) {
+            return false;
+        }
+        MetaProperty prop;
+        Iterator<MetaProperty> itr = properties.iterator();
+        while (itr.hasNext()) {
+            prop = itr.next();
+            if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
+                String uri = prop.getPropValue();
+                return !data.isResourceExist(uri);
             }
         }
+        return false;
     }
 
-    public void deleteResourceByType(final String resourceType) {
-        if (null != resourceType) {
-            new Thread() {
-                @Override
-                public void run() {
-                    // Unregister the resources from the platform
-                    deleteResource(resourceType);
+    public List<CollectionResource> getCollectionsForAddingToSingleResource(
+            SingleResource resource) {
+        List<CollectionResource> collectionResources = data
+                .getCollectionResources();
+        if (null == collectionResources || collectionResources.isEmpty()) {
+            return null;
+        }
+
+        Set<CollectionResource> collectionMembership;
+        collectionMembership = resource.getCollectionMembership();
+        if (null == collectionMembership || collectionMembership.isEmpty()) {
+            return collectionResources;
+        }
 
-                    // Delete from the local data structure
-                    deleteLocalResourceDetails(resourceType, null);
+        if (collectionMembership.size() == collectionResources.size()) {
+            return null;
+        }
 
-                    // Notify the UI listener for removing this resource from UI
-                    resourceDeletedUINotification();
+        collectionResources.removeAll(collectionMembership);
 
-                    if (null != currentResourceInSelection
-                            && resourceType.equals(currentResourceInSelection
-                                    .getResourceType())) {
-                        // Listeners might query the resource being deleted if
-                        // exists. So set the currently selection to null.
-                        setCurrentResourceInSelection(null);
+        // Sort the list
+        Collections.sort(collectionResources,
+                Utility.collectionResourceComparator);
 
-                        // Notify all observers for resource selection change
-                        // event
-                        resourceSelectionChangedUINotification();
-                    }
-                }
-            }.start();
-        }
+        return collectionResources;
     }
 
-    private void deleteResource(String resourceType) {
-        if (null != resourceType) {
-            try {
-                SimulatorManager.deleteResources(resourceType);
-            } catch (SimulatorException e) {
-                Activator
-                        .getDefault()
-                        .getLogManager()
-                        .log(Level.ERROR.ordinal(),
-                                new Date(),
-                                "[" + e.getClass().getSimpleName() + "]"
-                                        + e.code().toString() + "-"
-                                        + e.message());
-            }
+    public List<SingleResource> getSingleTypeResourcesForAddingToCollectionResource(
+            CollectionResource colRes) {
+        List<SingleResource> singleResources = data.getSingleResources();
+        if (null == singleResources || singleResources.isEmpty()) {
+            return null;
         }
-    }
 
-    public void deleteAllResources() {
-        new Thread() {
-            @Override
-            public void run() {
-                // Unregister the resources from the platform
-                deleteResource();
+        Set<SingleResource> childs;
+        childs = colRes.getSingleTypeChildResources();
+        if (null == childs || childs.isEmpty()) {
+            return singleResources;
+        }
 
-                // Delete from the local data structure
-                deleteLocalResourceDetails(null, null);
+        if (childs.size() == singleResources.size()) {
+            return null;
+        }
 
-                // Notify the UI listener for removing this resource from UI
-                resourceDeletedUINotification();
+        singleResources.removeAll(childs);
 
-                // Listeners might query the resource being deleted if exists.
-                // So set the currently selection to null.
-                setCurrentResourceInSelection(null);
+        // Sort the list
+        Collections.sort(singleResources, Utility.singleResourceComparator);
 
-                // Notify all observers for resource selection change event
-                resourceSelectionChangedUINotification();
-            }
-        }.start();
+        return singleResources;
     }
 
-    private void deleteResource() {
-        try {
-            SimulatorManager.deleteResources(null);
-        } catch (SimulatorException e) {
-            Activator
-                    .getDefault()
-                    .getLogManager()
-                    .log(Level.ERROR.ordinal(),
-                            new Date(),
-                            "[" + e.getClass().getSimpleName() + "]"
-                                    + e.code().toString() + "-" + e.message());
+    public List<SingleResource> getSingleTypeResourcesForAddingToDevice(
+            Device dev) {
+        List<SingleResource> singleResources = data.getSingleResources();
+        if (null == singleResources || singleResources.isEmpty()) {
+            return null;
         }
-    }
 
-    private void deleteLocalResourceDetails(String resourceType,
-            String resourceURI) {
-        if (null != resourceType && null != resourceURI) {
-            removeResourceFromMap(resourceType, resourceURI);
-            removeResourceUriFromOrderedMap(resourceType, resourceURI);
-        } else {
-            synchronized (resourceMap) {
-                if (null != resourceType) {
-                    removeResourceUriFromOrderedMap(resourceType, null);
-                    resourceMap.remove(resourceType);
-                } else {
-                    resourceMap.clear();
-                    removeResourceUriFromOrderedMap(null, null);
-                }
-            }
+        Set<SingleResource> childs;
+        childs = dev.getSingleTypeChildResources();
+        if (null == childs || childs.isEmpty()) {
+            return singleResources;
         }
-        // Remove the display name from the map
-        if (null != resourceURI) {
-            String dispName = Utility.uriToDisplayName(resourceURI);
-            if (null != dispName)
-                removeResourceDisplayName(dispName);
+
+        if (childs.size() == singleResources.size()) {
+            return null;
         }
+
+        singleResources.removeAll(childs);
+
+        // Sort the list
+        Collections.sort(singleResources, Utility.singleResourceComparator);
+
+        return singleResources;
     }
 
-    private void resourceCreatedUINotification() {
-        synchronized (resourceListChangedUIListeners) {
-            if (resourceListChangedUIListeners.size() > 0) {
-                IResourceListChangedUIListener listener;
-                Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
-                        .iterator();
-                while (listenerItr.hasNext()) {
-                    listener = listenerItr.next();
-                    if (null != listener) {
-                        listener.onResourceCreation();
-                    }
-                }
-            }
+    public List<CollectionResource> getCollectionTypeResourcesForAddingToCollectionResource(
+            CollectionResource colRes) {
+        List<CollectionResource> collectionResources = data
+                .getCollectionResources();
+        if (null == collectionResources || collectionResources.isEmpty()) {
+            return null;
         }
-    }
 
-    private void resourceDeletedUINotification() {
-        synchronized (resourceListChangedUIListeners) {
-            if (resourceListChangedUIListeners.size() > 0) {
-                IResourceListChangedUIListener listener;
-                Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
-                        .iterator();
-                while (listenerItr.hasNext()) {
-                    listener = listenerItr.next();
-                    if (null != listener) {
-                        listener.onResourceDeletion();
-                    }
-                }
-            }
+        // Remove the colRes from the list
+        collectionResources.remove(colRes);
+
+        Set<CollectionResource> childs;
+        childs = colRes.getCollectionTypeChildResources();
+        if (null == childs || childs.isEmpty()) {
+            return collectionResources;
         }
-    }
 
-    private void resourceSelectionChangedUINotification() {
-        synchronized (resourceSelectionChangedUIListeners) {
-            if (resourceSelectionChangedUIListeners.size() > 0) {
-                IResourceSelectionChangedUIListener listener;
-                Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
-                        .iterator();
-                while (listenerItr.hasNext()) {
-                    listener = listenerItr.next();
-                    if (null != listener) {
-                        listener.onResourceSelectionChange();
-                    }
-                }
-            }
+        if (childs.size() == collectionResources.size()) {
+            return null;
         }
+
+        collectionResources.removeAll(childs);
+
+        // Sort the list
+        Collections.sort(collectionResources,
+                Utility.collectionResourceComparator);
+
+        return collectionResources;
     }
 
-    private void resourceModelChangedUINotification(
-            ModelChangeNotificationType notificationType, String resourceURI,
-            Set<LocalResourceAttribute> valueChangeSet) {
-        synchronized (resourceModelChangedUIListeners) {
-            if (resourceModelChangedUIListeners.size() > 0
-                    && notificationType != ModelChangeNotificationType.NONE
-                    && null != resourceURI) {
-                IResourceModelChangedUIListener listener;
-                Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
-                        .iterator();
-                while (listenerItr.hasNext()) {
-                    listener = listenerItr.next();
-                    if (null != listener) {
-                        listener.onResourceModelChange(notificationType,
-                                resourceURI, valueChangeSet);
-                    }
-                }
-            }
+    public List<CollectionResource> getCollectionTypeResourcesForAddingToDevice(
+            Device dev) {
+        List<CollectionResource> collectionResources = data
+                .getCollectionResources();
+        if (null == collectionResources || collectionResources.isEmpty()) {
+            return null;
         }
-    }
 
-    private void resourceAutomationStartedUINotification(String resourceURI) {
-        synchronized (automationUIListeners) {
-            if (automationUIListeners.size() > 0 && null != resourceURI) {
-                IAutomationUIListener listener;
-                Iterator<IAutomationUIListener> listenerItr = automationUIListeners
-                        .iterator();
-                while (listenerItr.hasNext()) {
-                    listener = listenerItr.next();
-                    if (null != listener) {
-                        listener.onResourceAutomationStart(resourceURI);
-                    }
-                }
-            }
+        Set<CollectionResource> childs;
+        childs = dev.getCollectionTypeChildResources();
+        if (null == childs || childs.isEmpty()) {
+            return collectionResources;
         }
-    }
 
-    private void automationCompleteUINotification(String resourceURI,
-            String attName) {
-        synchronized (automationUIListeners) {
-            if (automationUIListeners.size() > 0 && null != resourceURI) {
-                IAutomationUIListener listener;
-                Iterator<IAutomationUIListener> listenerItr = automationUIListeners
-                        .iterator();
-                while (listenerItr.hasNext()) {
-                    listener = listenerItr.next();
-                    if (null != listener) {
-                        listener.onAutomationComplete(resourceURI, attName);
-                    }
-                }
-            }
+        if (childs.size() == collectionResources.size()) {
+            return null;
         }
+
+        collectionResources.removeAll(childs);
+
+        // Sort the list
+        Collections.sort(collectionResources,
+                Utility.collectionResourceComparator);
+
+        return collectionResources;
     }
 
-    private void observerListChangedUINotification(String resourceURI) {
-        synchronized (observerUIListeners) {
-            if (observerUIListeners.size() > 0 && null != resourceURI) {
-                IObserverListChangedUIListener listener;
-                Iterator<IObserverListChangedUIListener> listenerItr = observerUIListeners
-                        .iterator();
-                while (listenerItr.hasNext()) {
-                    listener = listenerItr.next();
-                    if (null != listener) {
-                        listener.onObserverListChanged(resourceURI);
-                    }
-                }
-            }
+    public List<Device> getDevicesForAddingToResource(Resource resource) {
+        List<Device> devices = data.getDevices();
+        if (null == devices || devices.isEmpty()) {
+            return null;
+        }
+
+        Set<Device> deviceMembership;
+        if (resource instanceof SingleResource) {
+            deviceMembership = ((SingleResource) resource)
+                    .getDeviceMembership();
+        } else {
+            deviceMembership = ((CollectionResource) resource)
+                    .getDeviceMembership();
+        }
+        if (null == deviceMembership || deviceMembership.isEmpty()) {
+            return devices;
         }
+
+        if (devices.size() == deviceMembership.size()) {
+            return null;
+        }
+
+        devices.removeAll(deviceMembership);
+
+        // Sort the list
+        Collections.sort(devices, Utility.deviceComparator);
+
+        return devices;
     }
 
-    public List<String> getResourceTypeList() {
-        List<String> typeList = null;
-        synchronized (resourceMap) {
-            if (resourceMap.size() > 0) {
-                typeList = new ArrayList<String>();
-                Set<String> typeSet = resourceMap.keySet();
-                Iterator<String> typeItr = typeSet.iterator();
-                while (typeItr.hasNext()) {
-                    typeList.add(typeItr.next());
-                }
-            }
+    public List<CollectionResource> getResourceReferences(
+            SingleResource resource) {
+        List<CollectionResource> resources = Utility
+                .getCollectionResourceListFromSet(resource
+                        .getCollectionMembership());
+        if (null == resources || resources.isEmpty()) {
+            return null;
         }
-        return typeList;
+
+        Collections.sort(resources, Utility.collectionResourceComparator);
+
+        return resources;
     }
 
-    public boolean isTypeExist(String resType) {
-        synchronized (resourceMap) {
-            if (resourceMap.containsKey(resType)) {
-                return true;
-            }
+    public List<Device> getDeviceReferences(Resource resource) {
+        Set<Device> deviceMembership;
+        if (resource instanceof SingleResource) {
+            deviceMembership = ((SingleResource) resource)
+                    .getDeviceMembership();
+        } else {
+            deviceMembership = ((CollectionResource) resource)
+                    .getDeviceMembership();
         }
-        return false;
+
+        List<Device> devices = Utility.getDeviceListFromSet(deviceMembership);
+        if (null == devices || devices.isEmpty()) {
+            return null;
+        }
+
+        Collections.sort(devices, Utility.deviceComparator);
+
+        return devices;
     }
 
-    public List<String> getURIList() {
-        List<String> list = null;
-        synchronized (orderedResourceUriMap) {
-            Set<String> typeSet = orderedResourceUriMap.keySet();
-            List<String> typeList = Utility.convertSetToList(typeSet);
-            if (null == typeList || typeList.size() < 1) {
-                return list;
-            }
-            list = new ArrayList<String>();
+    public List<SingleResource> getSingleTypeChilds(CollectionResource colRes) {
+        Set<SingleResource> childs = colRes.getSingleTypeChildResources();
+        return Utility.getSingleResourceListFromSet(childs);
+    }
+
+    public List<SingleResource> getSingleTypeChilds(Device dev) {
+        Set<SingleResource> childs = dev.getSingleTypeChildResources();
+        return Utility.getSingleResourceListFromSet(childs);
+    }
+
+    public List<CollectionResource> getCollectionTypeChilds(
+            CollectionResource colRes) {
+        Set<CollectionResource> childs = colRes
+                .getCollectionTypeChildResources();
+        return Utility.getCollectionResourceListFromSet(childs);
+    }
+
+    public List<CollectionResource> getCollectionTypeChilds(Device dev) {
+        Set<CollectionResource> childs = dev.getCollectionTypeChildResources();
+        return Utility.getCollectionResourceListFromSet(childs);
+    }
 
-            // Sort the types
-            Collections.sort(typeList);
+    public void resourceSelectionChanged(final Resource selectedResource) {
+        new Thread() {
+            @Override
+            public void run() {
 
-            // Add all URIs to the output list
-            Iterator<String> typeItr = typeList.iterator();
-            while (typeItr.hasNext()) {
-                List<String> l = orderedResourceUriMap.get(typeItr.next());
-                if (null != l) {
-                    list.addAll(l);
+                setCurrentDeviceInSelection(null);
+
+                if (null != selectedResource) {
+                    setCurrentResourceInSelection(selectedResource);
+                } else {
+                    setCurrentResourceInSelection(null);
                 }
+                // Notify all observers for resource selection change event
+                UiListenerHandler.getInstance()
+                        .resourceSelectionChangedUINotification(
+                                selectedResource);
             }
-        }
-        return list;
+        }.start();
     }
 
-    public void resourceSelectionChanged(final String selectedItem) {
+    public void deviceSelectionChanged(final Device selectedDevice) {
         new Thread() {
             @Override
             public void run() {
-                // Check whether the item selected is a resource or resource
-                // category
-                if (isTypeExist(selectedItem)) {
-                    // Given item is a resource Type
-                    setCurrentResourceInSelection(null);
+
+                setCurrentResourceInSelection(null);
+
+                if (null != selectedDevice) {
+                    setCurrentDeviceInSelection(selectedDevice);
                 } else {
-                    // Given item is a resource URI
-                    SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
-                    if (null != resource) {
-                        setCurrentResourceInSelection(resource);
-                    } else {
-                        setCurrentResourceInSelection(null);
-                    }
+                    setCurrentDeviceInSelection(null);
                 }
                 // Notify all observers for resource selection change event
-                resourceSelectionChangedUINotification();
+                UiListenerHandler.getInstance()
+                        .deviceSelectionChangedUINotification(selectedDevice);
             }
         }.start();
     }
 
-    public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
+    public List<MetaProperty> getMetaProperties(Resource resource) {
         if (null != resource) {
             String propName;
             String propValue;
@@ -1166,13 +1528,12 @@ public class ResourceManager {
 
             for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
                 propName = Constants.META_PROPERTIES[index];
-                if (propName.equals(Constants.RESOURCE_URI)) {
+                if (propName.equals(Constants.RESOURCE_NAME)) {
+                    propValue = resource.getResourceName();
+                } else if (propName.equals(Constants.RESOURCE_URI)) {
                     propValue = resource.getResourceURI();
                 } else if (propName.equals(Constants.RESOURCE_TYPE)) {
-                    propValue = resource.getResourceType();
-                } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
-                    // TODO: Temporarily ignoring till the implementation.
-                    propValue = null;
+                    propValue = resource.getResourceTypes().toString();
                 } else {
                     propValue = null;
                 }
@@ -1180,17 +1541,228 @@ public class ResourceManager {
                     metaPropertyList.add(new MetaProperty(propName, propValue));
                 }
             }
+            return metaPropertyList;
+        }
+        return null;
+    }
 
+    public List<MetaProperty> getMetaProperties(Device dev) {
+        if (null != dev) {
+            List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
+            metaPropertyList.add(new MetaProperty(Constants.DEVICE_NAME, dev
+                    .getDeviceName()));
             return metaPropertyList;
         }
         return null;
     }
 
-    public List<LocalResourceAttribute> getAttributes(SimulatorResource resource) {
+    public boolean startResource(Resource resource) throws SimulatorException {
+        if (null == resource) {
+            return false;
+        }
+        SimulatorResource server = resource.getSimulatorResource();
+        if (null == server) {
+            return false;
+        }
+        try {
+            server.start();
+            resource.setStarted(true);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "There is an error while starting the resource.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
+        return true;
+    }
+
+    public boolean stopResource(Resource resource) throws SimulatorException {
+        if (null == resource) {
+            return false;
+        }
+        SimulatorResource server = resource.getSimulatorResource();
+        if (null == server) {
+            return false;
+        }
+        try {
+            server.stop();
+            resource.setStarted(false);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "There is an error while stopping the resource.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
+        return true;
+    }
+
+    public boolean changeResourceName(Resource resource, String newName)
+            throws SimulatorException {
+        if (null == resource || null == newName) {
+            return false;
+        }
+
+        if (!stopResource(resource)) {
+            return false;
+        }
+
+        SimulatorResource server = resource.getSimulatorResource();
+        try {
+            server.setName(newName);
+            resource.setResourceName(newName);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "There is an error while changing the resource name.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
+
+        if (!startResource(resource)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    public boolean changeDeviceName(Device dev, String newName) {
+        if (null == dev || null == newName) {
+            return false;
+        }
+        data.changeDeviceName(dev, dev.getDeviceName(), newName);
+        return true;
+    }
+
+    public boolean changeResourceURI(Resource resource, String newURI)
+            throws SimulatorException {
+        if (null == resource || null == newURI) {
+            return false;
+        }
+
+        if (!stopResource(resource)) {
+            return false;
+        }
+
+        String curURI = resource.getResourceURI();
+        SimulatorResource server = resource.getSimulatorResource();
+        try {
+            server.setURI(newURI);
+            data.changeResourceURI(resource, curURI, newURI);
+        } catch (SimulatorException e) {
+            Activator
+                    .getDefault()
+                    .getLogManager()
+                    .log(Level.ERROR.ordinal(),
+                            new Date(),
+                            "There is an error while changing the resource URI.\n"
+                                    + Utility.getSimulatorErrorString(e, null));
+            throw e;
+        }
+
+        if (!startResource(resource)) {
+            return false;
+        }
+        return true;
+    }
+
+    public boolean updateResourceProperties(Resource resource,
+            List<MetaProperty> properties, boolean uriChanged,
+            boolean nameChanged) throws SimulatorException {
+        if (null == resource || null == properties) {
+            return false;
+        }
+
+        // Updating the properties
+        Iterator<MetaProperty> itr = properties.iterator();
+        MetaProperty property;
+        String propName;
+        String propValue;
+        String resName = null;
+        String resURI = null;
+        while (itr.hasNext()) {
+            property = itr.next();
+            if (null == property) {
+                continue;
+            }
+            propName = property.getPropName();
+            propValue = property.getPropValue();
+            if (propName.equals(Constants.RESOURCE_NAME)) {
+                resName = propValue;
+            } else if (propName.equals(Constants.RESOURCE_URI)) {
+                resURI = propValue;
+            }
+        }
+
+        if (nameChanged) {
+            if (!changeResourceName(resource, resName)) {
+                return false;
+            }
+
+            // Notify UI Listeners
+            UiListenerHandler.getInstance().propertiesChangedUINotification(
+                    Resource.class);
+        }
+
+        if (uriChanged) {
+            if (!changeResourceURI(resource, resURI)) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    public boolean updateDeviceProperties(Device dev,
+            List<MetaProperty> properties) {
+        if (null == dev || null == properties) {
+            return false;
+        }
+
+        // Updating the properties
+        Iterator<MetaProperty> itr = properties.iterator();
+        MetaProperty property;
+        String propName;
+        String propValue;
+        String devName = null;
+        while (itr.hasNext()) {
+            property = itr.next();
+            if (null == property) {
+                continue;
+            }
+            propName = property.getPropName();
+            propValue = property.getPropValue();
+            if (propName.equals(Constants.DEVICE_NAME)) {
+                devName = propValue;
+            }
+        }
+
+        if (!changeDeviceName(dev, devName)) {
+            return false;
+        }
+
+        // Notify UI Listeners
+        UiListenerHandler.getInstance().propertiesChangedUINotification(
+                Device.class);
+
+        return true;
+    }
+
+    public List<LocalResourceAttribute> getAttributes(Resource resource) {
         List<LocalResourceAttribute> attList = null;
         if (null != resource) {
             Map<String, LocalResourceAttribute> attMap = resource
-                    .getResourceAttributesMap();
+                    .getResourceAttributes();
             if (null != attMap && attMap.size() > 0) {
                 attList = new ArrayList<LocalResourceAttribute>();
                 Set<String> attNameSet = attMap.keySet();
@@ -1206,137 +1778,269 @@ public class ResourceManager {
                 }
             }
         }
-        return attList;
+        return attList;
+    }
+
+    public List<SimulatorResourceAttribute> getAttributes(
+            SimulatorResourceModel model) {
+        List<SimulatorResourceAttribute> attList = null;
+        if (null != model) {
+            Map<String, SimulatorResourceAttribute> attMap = model
+                    .getAttributes();
+            if (null != attMap && attMap.size() > 0) {
+                attList = new ArrayList<SimulatorResourceAttribute>();
+                Set<String> attNameSet = attMap.keySet();
+                String attName;
+                SimulatorResourceAttribute attribute;
+                Iterator<String> attNameItr = attNameSet.iterator();
+                while (attNameItr.hasNext()) {
+                    attName = attNameItr.next();
+                    attribute = attMap.get(attName);
+                    if (null != attribute) {
+                        attList.add(attribute);
+                    }
+                }
+            }
+        }
+        return attList;
+    }
+
+    public List<SRMItem> getIndexedAttributes(SimulatorResourceModel[] model) {
+        List<SRMItem> indexedAttList = null;
+        if (null != model && model.length > 0) {
+            indexedAttList = new ArrayList<SRMItem>();
+            int i = 0;
+            for (SimulatorResourceModel m : model) {
+                indexedAttList.add(new SRMItem(i++, m));
+            }
+        }
+        return indexedAttList;
     }
 
-    public void attributeValueUpdated(SimulatorResource resource,
-            String attributeName, String value) {
+    public void attributeValueUpdated(SingleResource resource,
+            String attributeName, AttributeValue value) {
         if (null != resource && null != attributeName && null != value) {
-            SimulatorResourceServer server = resource.getResourceServer();
-            if (null != server) {
-                LocalResourceAttribute att = resource
-                        .getAttribute(attributeName);
-                if (null == att) {
-                    return;
-                }
-                Type baseType = att.getAttValBaseType();
+            SimulatorSingleResource simRes = (SimulatorSingleResource) resource
+                    .getSimulatorResource();
+            if (null != simRes) {
                 try {
-                    if (baseType == Type.STRING) {
-                        server.updateAttributeString(attributeName, value);
-                    } else if (baseType == Type.INT) {
-                        int val;
-                        try {
-                            val = Integer.parseInt(value);
-                            server.updateAttributeInteger(attributeName, val);
-                        } catch (NumberFormatException nfe) {
-                            return;
-                        }
-                    } else if (baseType == Type.DOUBLE) {
-                        double val;
-                        try {
-                            val = Double.parseDouble(value);
-                            server.updateAttributeDouble(attributeName, val);
-                        } catch (NumberFormatException nfe) {
-                            return;
-                        }
-                    } else if (baseType == Type.BOOL) {
-                        boolean val;
-                        val = Boolean.parseBoolean(value);
-                        server.updateAttributeBoolean(attributeName, val);
-                    }
+                    simRes.updateAttribute(attributeName, value);
                 } catch (SimulatorException e) {
                     Activator
                             .getDefault()
                             .getLogManager()
-                            .log(Level.ERROR.ordinal(),
-                                    new Date(),
-                                    "[" + e.getClass().getSimpleName() + "]"
-                                            + e.code().toString() + "-"
-                                            + e.message());
+                            .log(Level.ERROR.ordinal(), new Date(),
+                                    Utility.getSimulatorErrorString(e, null));
                 }
             }
         }
     }
 
-    private ModelChangeNotificationType compareAndUpdateLocalAttributes(
-            Map<String, LocalResourceAttribute> resourceAttributeMapOld,
-            Map<String, LocalResourceAttribute> resourceAttributeMapNew,
-            Set<LocalResourceAttribute> valueChangeSet) {
-        ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
-        if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
-            Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
-            Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
-            String attName;
-            LocalResourceAttribute attributeOld;
-            LocalResourceAttribute attributeNew;
-            Object attValueOld;
-            Object attValueNew;
-            String oldValueStr;
-            String newValueStr;
-            while (attributeMapOldItr.hasNext()) {
-                attName = attributeMapOldItr.next();
-                if (resourceAttributeMapNew.containsKey(attName)) {
-                    attributeOld = resourceAttributeMapOld.get(attName);
-                    attributeNew = resourceAttributeMapNew.get(attName);
-                    // Copy the attribute value from new to old if the value
-                    // has been changed
-                    // Comparing only the attribute's value considering the
-                    // fact that only the value can be changed
-                    if (null != attributeOld && null != attributeNew) {
-                        attValueOld = attributeOld.getAttributeValue();
-                        attValueNew = attributeNew.getAttributeValue();
-
-                        oldValueStr = String.valueOf(attValueOld);
-                        newValueStr = String.valueOf(attValueNew);
-
-                        if (null != oldValueStr && null != newValueStr) {
-                            if (!oldValueStr.equals(newValueStr)) {
-                                attributeOld.setAttributeValue(attValueNew);
-                                notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
-                                valueChangeSet.add(attributeOld);
-                            }
-                        }
-                    }
-                    resourceAttributeMapNew.remove(attName);
-                } else {
-                    // Attribute doesn't exist in the new model. Hence
-                    // removing it from the model.
-                    resourceAttributeMapOld.remove(attName);
-                    notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
+    // TODO: This method should get the status from the native layer.
+    public boolean isResourceStarted(Resource resource) {
+        if (null == resource) {
+            return false;
+        }
+        return resource.isStarted();
+    }
+
+    public boolean isPropertyValueInvalid(Resource resource,
+            List<MetaProperty> properties, String propName) {
+        if (null == resource || null == properties || null == propName) {
+            return false;
+        }
+        boolean invalid = false;
+        MetaProperty prop;
+        Iterator<MetaProperty> itr = properties.iterator();
+        while (itr.hasNext()) {
+            prop = itr.next();
+            if (prop.getPropName().equals(propName)) {
+                String value = prop.getPropValue();
+                if (null == value || value.trim().isEmpty()) {
+                    invalid = true;
                 }
             }
-            // Check for new attributes in the new model
-            if (resourceAttributeMapNew.size() > 0) {
-                Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
-                Iterator<String> remainingAttItr = remainingAttSet.iterator();
-                LocalResourceAttribute attribute;
-                while (remainingAttItr.hasNext()) {
-                    attName = remainingAttItr.next();
-                    if (null != attName) {
-                        attribute = resourceAttributeMapNew.get(attName);
-                        if (null != attribute) {
-                            resourceAttributeMapOld.put(attName, attribute);
-                        }
-                    }
+        }
+        return invalid;
+    }
+
+    public boolean isPropertyValueInvalid(Device dev,
+            List<MetaProperty> properties, String propName) {
+        if (null == dev || null == properties || null == propName) {
+            return false;
+        }
+        boolean invalid = false;
+        MetaProperty prop;
+        Iterator<MetaProperty> itr = properties.iterator();
+        while (itr.hasNext()) {
+            prop = itr.next();
+            if (prop.getPropName().equals(propName)) {
+                String value = prop.getPropValue();
+                if (null == value || value.trim().isEmpty()) {
+                    invalid = true;
+                }
+            }
+        }
+        return invalid;
+    }
+
+    public boolean isPropValueChanged(Resource resource,
+            List<MetaProperty> properties, String propName) {
+        if (null == resource || null == properties || null == propName) {
+            return false;
+        }
+        boolean changed = false;
+        MetaProperty prop;
+        String oldValue;
+        Iterator<MetaProperty> itr = properties.iterator();
+        while (itr.hasNext()) {
+            prop = itr.next();
+            if (prop.getPropName().equals(propName)) {
+                oldValue = getPropertyValueFromResource(resource, propName);
+                if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
+                    changed = true;
+                }
+                break;
+            }
+        }
+        return changed;
+    }
+
+    public boolean isPropValueChanged(Device dev,
+            List<MetaProperty> properties, String propName) {
+        if (null == dev || null == properties || null == propName) {
+            return false;
+        }
+        boolean changed = false;
+        MetaProperty prop;
+        String oldValue;
+        Iterator<MetaProperty> itr = properties.iterator();
+        while (itr.hasNext()) {
+            prop = itr.next();
+            if (prop.getPropName().equals(propName)) {
+                oldValue = dev.getDeviceName();
+                if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
+                    changed = true;
+                }
+                break;
+            }
+        }
+        return changed;
+    }
+
+    private String getPropertyValueFromResource(Resource resource,
+            String propName) {
+        if (null == resource || null == propName) {
+            return null;
+        }
+        if (propName.equals(Constants.RESOURCE_URI)) {
+            return resource.getResourceURI();
+        } else if (propName.equals(Constants.RESOURCE_NAME)) {
+            return resource.getResourceName();
+        } else if (propName.equals(Constants.RESOURCE_TYPE)) {
+            return resource.getResourceTypes().toString();
+        } else {
+            return null;
+        }
+    }
+
+    public boolean isURIChanged(Resource resource, List<MetaProperty> properties) {
+        if (null == resource || null == properties) {
+            return false;
+        }
+        boolean changed = false;
+        MetaProperty prop;
+        Iterator<MetaProperty> itr = properties.iterator();
+        while (itr.hasNext()) {
+            prop = itr.next();
+            if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
+                if (!prop.getPropValue().equals(resource.getResourceURI())) {
+                    changed = true;
                 }
-                notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
+                break;
+            }
+        }
+        return changed;
+    }
+
+    public boolean startResource(SingleResource resource) {
+        if (null == resource || resource.isStarted()) {
+            return false;
+        }
+        boolean result;
+        SimulatorResource server = resource.getSimulatorResource();
+        if (null == server) {
+            result = false;
+        } else {
+            try {
+                server.start();
+                resource.setStarted(true);
+                result = true;
+            } catch (SimulatorException e) {
+                Activator
+                        .getDefault()
+                        .getLogManager()
+                        .log(Level.ERROR.ordinal(), new Date(),
+                                Utility.getSimulatorErrorString(e, null));
+                result = false;
+            }
+        }
+        return result;
+    }
+
+    public boolean stopResource(SingleResource resource) {
+        if (null == resource || !resource.isStarted()) {
+            return false;
+        }
+        boolean result;
+        SimulatorResource server = resource.getSimulatorResource();
+        if (null == server) {
+            result = false;
+        } else {
+            try {
+                server.stop();
+                resource.setStarted(false);
+                result = true;
+            } catch (SimulatorException e) {
+                Activator
+                        .getDefault()
+                        .getLogManager()
+                        .log(Level.ERROR.ordinal(), new Date(),
+                                Utility.getSimulatorErrorString(e, null));
+                result = false;
             }
         }
-        return notificationType;
+        return result;
+    }
+
+    public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
+        if (null == att) {
+            return false;
+        }
+        AttributeProperty prop = att.property();
+        if (null == prop) {
+            return false;
+        }
+        Type attProp = prop.type();
+        if (attProp == Type.UNKNOWN) {
+            return false;
+        }
+        return true;
     }
 
-    public int startAutomation(SimulatorResource resource,
-            LocalResourceAttribute attribute, AutomationType autoType,
+    public int startAutomation(SingleResource resource,
+            AttributeElement attribute, AutoUpdateType autoType,
             int autoUpdateInterval) {
         int autoId = -1;
         if (null != resource && null != attribute) {
-            SimulatorResourceServer resourceServerN = resource
-                    .getResourceServer();
-            if (null != resourceServerN) {
-                String attrName = attribute.getAttributeName();
+            SimulatorSingleResource server = (SimulatorSingleResource) resource
+                    .getSimulatorResource();
+            if (null != server) {
+                String attrName = attribute.getSimulatorResourceAttribute()
+                        .name();
                 try {
-                    autoId = resourceServerN.startAttributeAutomation(attrName,
-                            autoType, autoUpdateInterval, automationListener);
+                    autoId = server.startAttributeUpdation(attrName, autoType,
+                            autoUpdateInterval, automationListener);
                 } catch (SimulatorException e) {
                     Activator
                             .getDefault()
@@ -1349,27 +2053,25 @@ public class ResourceManager {
                     return -1;
                 }
                 if (-1 != autoId) {
-                    attribute.setAutomationId(autoId);
-                    attribute.setAutomationType(autoType);
-                    attribute.setAutomationUpdateInterval(autoUpdateInterval);
-                    attribute.setAutomationInProgress(true);
+                    attribute.setAutoUpdateId(autoId);
+                    attribute.setAutoUpdateType(autoType);
+                    attribute.setAutoUpdateInterval(autoUpdateInterval);
+                    attribute.setAutoUpdateState(true);
                     resource.setAttributeAutomationInProgress(true);
-                } else {
-                    attribute.setAutomationInProgress(false);
                 }
             }
         }
         return autoId;
     }
 
-    public void stopAutomation(SimulatorResource resource,
-            LocalResourceAttribute att, int autoId) {
+    public void stopAutomation(SingleResource resource, AttributeElement att,
+            int autoId) {
         if (null != resource) {
-            SimulatorResourceServer resourceServerN = resource
-                    .getResourceServer();
-            if (null != resourceServerN) {
+            SimulatorSingleResource server = (SimulatorSingleResource) resource
+                    .getSimulatorResource();
+            if (null != server) {
                 try {
-                    resourceServerN.stopAutomation(autoId);
+                    server.stopUpdation(autoId);
                 } catch (SimulatorException e) {
                     Activator
                             .getDefault()
@@ -1382,119 +2084,61 @@ public class ResourceManager {
                     return;
                 }
                 // Change the automation status
-                att.setAutomationInProgress(false);
+                att.setAutoUpdateState(false);
                 resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
             }
         }
     }
 
-    private boolean isAnyAttributeInAutomation(SimulatorResource resource) {
+    public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
+            int autoUpdateInterval, final SingleResource resource) {
         if (null == resource) {
             return false;
         }
-        Map<String, LocalResourceAttribute> attMap = resource
-                .getResourceAttributesMap();
-        if (null == attMap) {
-            return false;
-        }
         boolean status = false;
-        Set<String> keySet = attMap.keySet();
-        Iterator<String> attItr = keySet.iterator();
-        while (attItr.hasNext()) {
-            LocalResourceAttribute attribute = attMap.get(attItr.next());
-            if (attribute.isAutomationInProgress()) {
-                status = true;
-                break;
-            }
-        }
-        return status;
-    }
-
-    private LocalResourceAttribute getAttributeWithGivenAutomationId(
-            SimulatorResource resource, int automationId) {
-        LocalResourceAttribute targetAttribute = null;
-        if (null != resource) {
-            Map<String, LocalResourceAttribute> attributeMap = resource
-                    .getResourceAttributesMap();
-            if (null != attributeMap) {
-                Set<String> attNameSet = attributeMap.keySet();
-                Iterator<String> attNameItr = attNameSet.iterator();
-                String attName;
-                LocalResourceAttribute attribute;
-                while (attNameItr.hasNext()) {
-                    attName = attNameItr.next();
-                    if (null != attName) {
-                        attribute = attributeMap.get(attName);
-                        if (null != attribute) {
-                            if (attribute.isAutomationInProgress()
-                                    && (attribute.getAutomationId() == automationId)) {
-                                targetAttribute = attribute;
-                                break;
-                            }
-                        }
-                    }
-                }
+        changeResourceLevelAutomationStatus(resource, true);
+        // Invoke the native automation method
+        SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
+                .getSimulatorResource();
+        if (null != resourceServer) {
+            int autoId = -1;
+            try {
+                autoId = resourceServer.startResourceUpdation(autoType,
+                        autoUpdateInterval, automationListener);
+            } catch (SimulatorException e) {
+                Activator
+                        .getDefault()
+                        .getLogManager()
+                        .log(Level.ERROR.ordinal(), new Date(),
+                                Utility.getSimulatorErrorString(e, null));
+                autoId = -1;
             }
-        }
-        return targetAttribute;
-    }
+            if (-1 == autoId) {
+                // Automation request failed and hence status is being
+                // rolled back
+                changeResourceLevelAutomationStatus(resource, false);
+            } else {
+                // Automation request accepted.
+                resource.setAutomationId(autoId);
 
-    public boolean startResourceAutomationUIRequest(final String resourceURI) {
-        if (null == resourceURI) {
-            return false;
-        }
-        boolean status = false;
-        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
-        if (null != resource) {
-            changeResourceLevelAutomationStatus(resource, true);
-
-            // Invoke the native automation method
-            SimulatorResourceServer resourceServer = resource
-                    .getResourceServer();
-            if (null != resourceServer) {
-                // TODO: Temporarily handling the normal one-time automation for
-                // resources
-                int autoId = -1;
-                try {
-                    autoId = resourceServer.startResourceAutomation(
-                            AutomationType.NORMAL, 500, automationListener);
-                } catch (SimulatorException e) {
-                    Activator
-                            .getDefault()
-                            .getLogManager()
-                            .log(Level.ERROR.ordinal(),
-                                    new Date(),
-                                    "[" + e.getClass().getSimpleName() + "]"
-                                            + e.code().toString() + "-"
-                                            + e.message());
-                    autoId = -1;
-                }
-                if (-1 == autoId) {
-                    // Automation request failed and hence status is being
-                    // rolled back
-                    changeResourceLevelAutomationStatus(resource, false);
-                } else {
-                    // Automation request accepted.
-                    resource.setAutomationId(autoId);
-
-                    // Notify the UI listeners in a different thread.
-                    Thread notifyThread = new Thread() {
-                        public void run() {
-                            resourceAutomationStartedUINotification(resourceURI);
-                        };
+                // Notify the UI listeners in a different thread.
+                Thread notifyThread = new Thread() {
+                    public void run() {
+                        UiListenerHandler.getInstance()
+                                .resourceAutomationStartedUINotification(
+                                        resource);
                     };
-                    notifyThread.setPriority(Thread.MAX_PRIORITY);
-                    notifyThread.start();
+                };
+                notifyThread.setPriority(Thread.MAX_PRIORITY);
+                notifyThread.start();
 
-                    status = true;
-                }
+                status = true;
             }
         }
         return status;
     }
 
-    public boolean stopResourceAutomationUIRequest(final String resourceURI) {
-        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
+    public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
         if (null == resource) {
             return false;
         }
@@ -1502,178 +2146,263 @@ public class ResourceManager {
         if (-1 == autoId) {
             return false;
         }
-        SimulatorResourceServer resourceServer = resource.getResourceServer();
+        SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
+                .getSimulatorResource();
         if (null == resourceServer) {
             return false;
         }
         // Call native method
         try {
-            resourceServer.stopAutomation(autoId);
+            resourceServer.stopUpdation(autoId);
         } catch (SimulatorException e) {
             Activator
                     .getDefault()
                     .getLogManager()
-                    .log(Level.ERROR.ordinal(),
-                            new Date(),
-                            "[" + e.getClass().getSimpleName() + "]"
-                                    + e.code().toString() + "-" + e.message());
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
             return false;
         }
 
         // Notify the UI Listeners. Invoke the automation complete callback.
         Thread stopThread = new Thread() {
             public void run() {
-                automationListener.onAutomationComplete(resourceURI, autoId);
+                automationListener.onUpdateComplete(resource.getResourceURI(),
+                        autoId);
             }
         };
         stopThread.start();
         return true;
     }
 
+    private boolean isAnyAttributeInAutomation(SingleResource resource) {
+        if (null == resource || null == resource.getResourceRepresentation()) {
+            return false;
+        }
+
+        Map<String, AttributeElement> attributes = resource
+                .getResourceRepresentation().getAttributes();
+        if (null == attributes || 0 == attributes.size())
+            return false;
+
+        for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
+            if (entry.getValue().isAutoUpdateInProgress())
+                return true;
+        }
+
+        return false;
+    }
+
     // Changes the automation state of the resource and its attributes
-    private void changeResourceLevelAutomationStatus(
-            SimulatorResource resource, boolean status) {
-
-        Map<String, LocalResourceAttribute> attributeMap = resource
-                .getResourceAttributesMap();
-        if (null != attributeMap) {
-            Set<String> attrNameSet = attributeMap.keySet();
-            Iterator<String> attrNameItr = attrNameSet.iterator();
-            String attrName;
-            LocalResourceAttribute attribute;
-            while (attrNameItr.hasNext()) {
-                attrName = attrNameItr.next();
-                attribute = attributeMap.get(attrName);
-                if (null != attribute) {
-                    attribute.setAutomationInProgress(status);
-                }
-            }
+    private void changeResourceLevelAutomationStatus(SingleResource resource,
+            boolean status) {
+
+        if (null == resource || null == resource.getResourceRepresentation()) {
+            return;
+        }
+
+        Map<String, AttributeElement> attributes = resource
+                .getResourceRepresentation().getAttributes();
+        if (null == attributes || 0 == attributes.size())
+            return;
+
+        for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
+            entry.getValue().setAutoUpdateState(status);
         }
+
         resource.setResourceAutomationInProgress(status);
     }
 
-    public boolean isResourceAutomationStarted(String resourceURI) {
-        boolean status = false;
-        if (null == resourceURI) {
-            return status;
+    private AttributeElement getAttributeWithGivenAutomationId(
+            SingleResource resource, int automationId) {
+        if (null == resource || null == resource.getResourceRepresentation()) {
+            return null;
         }
 
-        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
-        if (null != resource) {
-            status = resource.isResourceAutomationInProgress();
+        Map<String, AttributeElement> attributes = resource
+                .getResourceRepresentation().getAttributes();
+        if (null == attributes || 0 == attributes.size())
+            return null;
+
+        for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
+            if (automationId == entry.getValue().getAutoUpdateId())
+                return entry.getValue();
         }
-        return status;
+
+        return null;
     }
 
-    public boolean isAttributeAutomationStarted(String resourceURI) {
+    public boolean isResourceAutomationStarted(SingleResource resource) {
         boolean status = false;
-        if (null == resourceURI) {
-            return status;
-        }
-        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
         if (null != resource) {
-            status = resource.isAttributeAutomationInProgress();
+            status = resource.isResourceAutomationInProgress();
         }
         return status;
     }
 
-    public LocalResourceAttribute getAttributeByResourceURI(String uri,
-            String attName) {
-        if (null == uri || null == attName) {
-            return null;
-        }
-        SimulatorResource resource = getSimulatorResourceByURI(uri);
+    public boolean isAttributeAutomationStarted(SingleResource resource) {
         if (null == resource) {
+            return false;
+        }
+        return resource.isAttributeAutomationInProgress();
+    }
+
+    public LocalResourceAttribute getAttributeByResourceURI(
+            SingleResource resource, String attName) {
+        if (null == resource || null == attName) {
             return null;
         }
         Map<String, LocalResourceAttribute> attMap = resource
-                .getResourceAttributesMap();
+                .getResourceAttributes();
         if (null == attMap) {
             return null;
         }
         return attMap.get(attName);
     }
 
-    public void notifyObserverRequest(SimulatorResource res, int observerId) {
-        if (null == res) {
+    public void notifyObserverRequest(Resource resource, int observerId) {
+        if (null == resource) {
             return;
         }
-        SimulatorResourceServer server = res.getResourceServer();
-        if (null == server) {
+        SimulatorResource simulatorResource = resource.getSimulatorResource();
+        if (null == simulatorResource) {
             return;
         }
         try {
-            server.notifyObserver(observerId);
+            simulatorResource.notifyObserver(observerId);
         } catch (SimulatorException e) {
             Activator
                     .getDefault()
                     .getLogManager()
-                    .log(Level.ERROR.ordinal(),
-                            new Date(),
-                            "[" + e.getClass().getSimpleName() + "]"
-                                    + e.code().toString() + "-" + e.message());
+                    .log(Level.ERROR.ordinal(), new Date(),
+                            Utility.getSimulatorErrorString(e, null));
         }
     }
 
-    public boolean isAttHasRangeOrAllowedValues(LocalResourceAttribute att) {
+    public void shutdown() {
+        threadHandle.interrupt();
+    }
+
+    public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
         if (null == att) {
-            return false;
-        }
-        Object[] obj = att.getAllowedValues();
-        if (null != obj && obj.length > 0) {
-            return true;
-        }
-        Object min = att.getMinValue();
-        Object max = att.getMaxValue();
-        if (null != min && null != max) {
-            return true;
+            return null;
         }
-        return false;
-    }
 
-    public boolean isResourceAutomationAllowed(String resourceURI) {
-        if (null == resourceURI) {
-            return false;
+        AttributeValue val = att.value();
+        if (null == val) {
+            return null;
         }
-        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
-        if (null == resource) {
-            return false;
+
+        TypeInfo type = val.typeInfo();
+
+        AttributeProperty prop = att.property();
+        if (null == prop) {
+            return null;
         }
-        Map<String, LocalResourceAttribute> attributes = resource
-                .getResourceAttributesMap();
-        if (null == attributes || attributes.size() < 1) {
-            return false;
+
+        List<String> values = new ArrayList<String>();
+
+        Type valuesType = prop.type();
+
+        if (valuesType == Type.UNKNOWN) {
+            // Adding the default value
+            values.add(Utility.getAttributeValueAsString(val));
+            return values;
         }
-        Set<String> keySet = attributes.keySet();
-        Iterator<String> itr = keySet.iterator();
-        String attName;
-        boolean allowed = false;
-        LocalResourceAttribute att;
-        while (itr.hasNext()) {
-            attName = itr.next();
-            if (null != attName) {
-                att = attributes.get(attName);
-                if (null != att) {
-                    allowed = allowed | isAttHasRangeOrAllowedValues(att);
+
+        if (type.mType != ValueType.RESOURCEMODEL) {
+            if (type.mType == ValueType.ARRAY) {
+                if (type.mDepth == 1) {
+                    AttributeProperty childProp = prop.getChildProperty();
+                    if (null != childProp) {
+                        valuesType = childProp.type();
+                        if (valuesType == Type.RANGE) {
+                            List<String> list = getRangeForPrimitiveNonArrayAttributes(
+                                    childProp, type.mBaseType);
+                            if (null != list) {
+                                values.addAll(list);
+                            }
+                        } else if (valuesType == Type.VALUESET) {
+                            List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
+                                    childProp.valueSet(), type.mBaseType);
+                            if (null != list) {
+                                values.addAll(list);
+                            }
+                        }
+                    }
+                }
+            } else {
+                if (valuesType == Type.RANGE) {
+                    List<String> list = getRangeForPrimitiveNonArrayAttributes(
+                            prop, type.mType);
+                    if (null != list) {
+                        values.addAll(list);
+                    }
+                } else if (valuesType == Type.VALUESET) {
+                    List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
+                            prop.valueSet(), type.mType);
+                    if (null != list) {
+                        values.addAll(list);
+                    }
                 }
             }
         }
-        return allowed;
+
+        return values;
     }
 
-    public Image getImage(String resourceURI) {
-        if (null == resourceURI) {
+    public List<String> getRangeForPrimitiveNonArrayAttributes(
+            AttributeProperty prop, ValueType type) {
+        if (null == prop) {
             return null;
         }
-        SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
-        if (null == resource) {
+
+        if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
             return null;
         }
-        return Activator.getDefault().getImageRegistry()
-                .get(resource.getResourceType());
+
+        List<String> values = new ArrayList<String>();
+        switch (type) {
+            case INTEGER:
+                int min = (int) prop.min();
+                int max = (int) prop.max();
+                for (int iVal = min; iVal <= max; iVal++) {
+                    values.add(String.valueOf(iVal));
+                }
+                break;
+            case DOUBLE:
+                double minD = (double) prop.min();
+                double maxD = (double) prop.max();
+                for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
+                    values.add(String.valueOf(iVal));
+                }
+                break;
+            default:
+        }
+        return values;
     }
 
-    public void shutdown() {
-        threadHandle.interrupt();
+    public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
+            AttributeValue[] attValues, ValueType type) {
+        if (null == attValues || attValues.length < 1) {
+            return null;
+        }
+
+        if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
+            return null;
+        }
+
+        Object obj;
+        List<String> values = new ArrayList<String>();
+        for (AttributeValue val : attValues) {
+            if (null == val) {
+                continue;
+            }
+            obj = val.get();
+            if (null == obj) {
+                continue;
+            }
+            values.add(String.valueOf(obj));
+        }
+        return values;
     }
 }
\ No newline at end of file