[ANDROID] Remove GroupSynchronization in things manager.
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / android / Sample / src / com / tm / sample / ConfigurationApiActivity.java
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 package com.tm.sample;
21
22 import java.util.ArrayList;
23 import java.util.EnumSet;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Vector;
28
29 import org.iotivity.base.OcException;
30 import org.iotivity.base.OcHeaderOption;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcRepresentation;
33 import org.iotivity.base.OcResource;
34 import org.iotivity.base.OcResourceHandle;
35 import org.iotivity.base.ResourceProperty;
36 import org.iotivity.service.tm.OCStackResult;
37 import org.iotivity.service.tm.GroupManager;
38 import org.iotivity.service.tm.ThingsMaintenance;
39 import org.iotivity.service.tm.GroupManager.*;
40 import org.iotivity.service.tm.ThingsConfiguration;
41 import org.iotivity.service.tm.ThingsConfiguration.*;
42 import org.iotivity.service.tm.ThingsMaintenance.*;
43
44 import android.app.Activity;
45 import android.app.Dialog;
46 import android.content.Context;
47 import android.os.Bundle;
48 import android.os.Handler;
49 import android.os.Message;
50 import android.util.Log;
51 import android.view.View;
52 import android.view.View.OnClickListener;
53 import android.widget.AdapterView;
54 import android.widget.AdapterView.OnItemClickListener;
55 import android.widget.ArrayAdapter;
56 import android.widget.Button;
57 import android.widget.EditText;
58 import android.widget.ListView;
59 import android.widget.Toast;
60
61 /*
62  * Activity for Handling all Configuration Apis as per user's selection on the UI.
63  * and updating of UI
64  */
65 public class ConfigurationApiActivity extends Activity {
66
67     private class ResourceInformation {
68         OcResource       resource       = null;
69         OcResourceHandle resourceHandle = null;
70     }
71
72     private final String                     LOG_TAG                                = "[TMSample] "
73                                                                                             + this.getClass()
74                                                                                                     .getSimpleName();
75
76     private final String                     CONFIGURATION_COLLECTION_RESOURCE_URI  = "/core/configuration/resourceset";
77     private final String                     CONFIGURATION_COLLECTION_RESOURCE_TYPE = "core.configuration.resourceset";
78     private final String                     MAINTENANCE_COLLECTION_RESOURCE_URI    = "/core/maintenance/resourceset";
79     private final String                     MAINTENANCE_COLLECTION_RESOURCE_TYPE   = "core.maintenance.resourceset";
80     private final String                     FACTORYSET_COLLECTION_RESOURCE_URI     = "/core/factoryset/resourceset";
81     private final String                     FACTORYSET_COLLECTION_RESOURCE_TYPE    = "core.factoryset.resourceset";
82
83     private final String                     CONFIGURATION_RESOURCE_URI             = "/oic/con";
84     private final String                     MAINTENANCE_RESOURCE_URI               = "/oic/mnt";
85     private final String                     FACTORYSET_RESOURCE_URI                = "/factorySet";
86
87     private ListView                         list;
88     private ArrayAdapter<String>             configurationApis;
89     private ArrayList<String>                configurationApisList;
90
91     private static int                       messageCount                           = 0;
92     private static EditText                  logs;
93     private static String                    logMessage                             = "";
94     private static Handler                   mHandler;
95     private static Message                   msg;
96
97     private GroupManager                     groupManager                           = null;
98     private ThingsConfiguration              thingsConfiguration                    = null;
99     private ThingsMaintenance                thingsMaintenance                      = null;
100     private Map<String, ResourceInformation> resourceList                           = null;
101     private Map<String, ResourceInformation> collectionList                         = null;
102
103     public boolean                           configurationResourceFlag              = false;
104     public boolean                           factorySetResourceFlag                 = false;
105     public boolean                           maintenanceResourceFlag                = false;
106
107     public static Context                    mcontext;
108     public String                            region                                 = "";
109     public boolean                           findGroupPressedFlag                   = false;
110
111     @Override
112     protected void onCreate(Bundle savedInstanceState) {
113         super.onCreate(savedInstanceState);
114         setContentView(R.layout.configapis);
115
116         mcontext = this;
117         groupManager = new GroupManager();
118         thingsConfiguration = ThingsConfiguration.getInstance();
119         thingsMaintenance = ThingsMaintenance.getInstance();
120
121         // set the listeners
122         setResourceListener();
123         setConfigurationListener();
124         setDiagnosticsListener();
125
126         // Create API menu list
127         configurationApisList = new ArrayList<String>();
128
129         logs = (EditText) findViewById(R.id.EditText);
130
131         list = (ListView) findViewById(R.id.configApisList);
132         configurationApisList.add("Find All Groups");
133         configurationApisList.add("Find All Resources");
134         configurationApisList.add("Get a Configuration Resource");
135         configurationApisList.add("Update Attribute (Region)");
136         configurationApisList.add("Factory Reset");
137         configurationApisList.add("Reboot");
138         configurationApisList.add("Get Supported Configuration Units");
139         configurationApis = new ArrayAdapter<String>(this,
140                 android.R.layout.simple_list_item_1, configurationApisList);
141         list.setAdapter(configurationApis);
142
143         // setting the Listener for calling the APIs as per User selection
144         list.setOnItemClickListener(new OnItemClickListener() {
145
146             @Override
147             public void onItemClick(AdapterView<?> parent, View view,
148                     int position, long id) {
149
150                 // Find All Groups
151                 if (position == 0) {
152                     Vector<String> resourceTypes = new Vector<String>();
153                     resourceTypes.add("core.configuration.resourceset");
154                     findCandidateResources(resourceTypes);
155
156                     logMessage = "";
157                     messageCount = 0;
158                     messageCount++;
159
160                     resourceTypes.clear();
161                     resourceTypes.add("core.diagnostics.resourceset");
162                     findCandidateResources(resourceTypes);
163
164                     messageCount++;
165
166                     resourceTypes.clear();
167                     resourceTypes.add("core.factoryset.resourceset");
168                     findCandidateResources(resourceTypes);
169
170                     messageCount++;
171
172                 } else if (position == 1) { // Find All Resources
173                     if (false == findGroupPressedFlag) {
174                         displayToastMessage("Configuration collection resource does not exist!");
175                     } else {
176                         Vector<String> resourceTypes = new Vector<String>();
177                         resourceTypes.add("oic.wk.con");
178                         findCandidateResources(resourceTypes);
179
180                         logMessage = "";
181                         messageCount = 0;
182                         messageCount++;
183
184                         resourceTypes.clear();
185                         resourceTypes.add("oic.wk.mnt");
186                         findCandidateResources(resourceTypes);
187
188                         messageCount++;
189
190                         resourceTypes.clear();
191                         resourceTypes.add("factorySet");
192                         findCandidateResources(resourceTypes);
193
194                         messageCount++;
195                     }
196                 } else if (position == 2) { // Get Configuration
197                     getConfiguration();
198                 } else if (position == 3) { // Update Attribute (Region)
199                     userInputDialog();
200                 } else if (position == 4) { // Factory Reset
201                     factoryReset();
202                 } else if (position == 5) { // Reboot
203                     reboot();
204                 } else if (position == 6) { // Get Supported Configuration Units
205                     String configurationUnits;
206                     configurationUnits = getListOfSupportedConfigurationUnits();
207                     Log.i(LOG_TAG, "Configuration Units:" + configurationUnits);
208                     logMessage = configurationUnits;
209                     logs.setText("");
210                     logs.setText(logMessage);
211
212                 }
213             }
214         });
215
216         resourceList = new HashMap<String, ResourceInformation>();
217         collectionList = new HashMap<String, ResourceInformation>();
218
219         try {
220             createResourceCollection(CONFIGURATION_COLLECTION_RESOURCE_URI,
221                     CONFIGURATION_COLLECTION_RESOURCE_TYPE);
222             createResourceCollection(MAINTENANCE_COLLECTION_RESOURCE_URI,
223                     MAINTENANCE_COLLECTION_RESOURCE_TYPE);
224             createResourceCollection(FACTORYSET_COLLECTION_RESOURCE_URI,
225                     FACTORYSET_COLLECTION_RESOURCE_TYPE);
226         } catch (OcException e) {
227             Log.e(LOG_TAG, "OcException occured! " + e.toString());
228         }
229
230         // handler for updating the UI i.e. LogMessage TextBox
231         mHandler = new Handler() {
232             @Override
233             public void handleMessage(Message msg) {
234                 switch (msg.what) {
235                     case 0:
236                         logs.setText("");
237                         logs.setText(logMessage);
238                         Log.i(LOG_TAG, logMessage);
239                 }
240             }
241         };
242
243     }
244
245     private void userInputDialog() {
246
247         ResourceInformation configurationCollection = collectionList
248                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
249         if (null == configurationCollection
250                 || null == configurationCollection.resource) {
251             displayToastMessage("Configuration collection resource does not exist!");
252         }
253         if (false == configurationResourceFlag) {
254             displayToastMessage("Configuration resource does not exist!");
255         } else {
256             final Dialog dialog = new Dialog(mcontext);
257             dialog.setContentView(R.layout.userinputforregionvalue);
258             dialog.setTitle("Enter the Region Value");
259
260             dialog.setCancelable(false);
261             dialog.show();
262             Button ok = (Button) dialog.findViewById(R.id.ok);
263             Button cancel = (Button) dialog.findViewById(R.id.cancel);
264
265             ok.setOnClickListener(new OnClickListener() {
266                 @Override
267                 public void onClick(View v) {
268
269                     EditText regionValue = (EditText) dialog
270                             .findViewById(R.id.region);
271                     region = regionValue.getText().toString();
272                     if (region.equalsIgnoreCase("")) {
273                         String toastmessage = "Please enter the Region Value";
274                         displayToastMessage(toastmessage);
275                     } else {
276                         dialog.dismiss();
277                         updateConfiguration(region);
278                     }
279                 }
280             });
281             cancel.setOnClickListener(new OnClickListener() {
282                 @Override
283                 public void onClick(View v) {
284                     dialog.dismiss();
285                 }
286             });
287         }
288     }
289
290     @Override
291     public void onBackPressed() {
292         super.onBackPressed();
293
294         // unregistering resource from the platform
295         deleteResources();
296         resourceList.clear();
297         resourceList = null;
298         collectionList.clear();
299         collectionList = null;
300     }
301
302     private void setResourceListener() {
303         groupManager
304                 .setFindCandidateResourceListener(new IFindCandidateResourceListener() {
305
306                     @Override
307                     public void onResourceFoundCallback(
308                             Vector<OcResource> resources) {
309                         // TODO Auto-generated method stub
310                         Log.i(LOG_TAG, "onResourceCallback: enter");
311                         for (int i = 0; i < resources.size(); i++) {
312                             OcResource resource = resources.get(i);
313                             String uri = resource.getUri();
314                             String host = resource.getHost();
315
316                             // Display resource information
317                             Log.i(LOG_TAG, "Resource URI:" + uri);
318                             Log.i(LOG_TAG, "Resource HOST: " + host);
319                             Log.i(LOG_TAG, "Resource types: ");
320                             logMessage += "Resource URI : " + uri + "\n";
321                             logMessage += "Resource Host : " + host + "\n";
322
323                             List<String> resourcetypes = resource
324                                     .getResourceTypes();
325                             for (int j = 0; j < resourcetypes.size(); j++) {
326                                 Log.i(LOG_TAG, resourcetypes.get(j));
327                                 logMessage += "ResourceType " + (j + 1) + " : "
328                                         + resourcetypes.get(j) + "\n";
329                             }
330
331                             Log.i(LOG_TAG, "Interface types: ");
332                             List<String> interfacetypes = resource
333                                     .getResourceInterfaces();
334                             for (int j = 0; j < interfacetypes.size(); j++) {
335                                 Log.i(LOG_TAG, interfacetypes.get(j));
336                                 logMessage += "interfacetype " + (j + 1)
337                                         + " : " + interfacetypes.get(j) + "\n";
338                             }
339
340                             try {
341                                 if (true == uri.contains("/resourceset")) {
342                                     collectionFound(resource);
343                                 } else {
344                                     resourceFound(resource);
345                                 }
346                             } catch (OcException e) {
347                                 Log.e(LOG_TAG,
348                                         "OcExcepion occured! " + e.toString());
349                             }
350                         }
351                         if (messageCount == 3) {
352                             msg = Message.obtain();
353                             msg.what = 0;
354                             mHandler.sendMessage(msg);
355                         }
356                     }
357                 });
358     }
359
360     private void setConfigurationListener() {
361         thingsConfiguration
362                 .setConfigurationListener(new IConfigurationListener() {
363                     @Override
364                     public void onBootStrapCallback(
365                             Vector<OcHeaderOption> headerOptions,
366                             OcRepresentation rep, int errorValue) {
367                         Log.i(LOG_TAG, "onBootStrapCallback: enter");
368                     }
369
370                     @Override
371                     public void onUpdateConfigurationsCallback(
372                             Vector<OcHeaderOption> headerOptions,
373                             OcRepresentation rep, int errorValue) {
374                         Log.i(LOG_TAG, "onUpdateConfigurationsCallback: enter");
375                         Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
376                         if (rep.hasAttribute("n")) {
377                             logMessage += "Device Name : "
378                                     + rep.getValueString("n") + "\n";
379                         }
380                         if (rep.hasAttribute("loc")) {
381                             logMessage += "Location : "
382                                     + rep.getValueString("loc") + "\n";
383                         }
384                         if (rep.hasAttribute("locn")) {
385                             logMessage += "Location Name : "
386                                     + rep.getValueString("locn") + "\n";
387                         }
388                         if (rep.hasAttribute("r")) {
389                             logMessage += "Region : " + rep.getValueString("r")
390                                     + "\n";
391                         }
392                         if (rep.hasAttribute("c")) {
393                             logMessage += "Currency : "
394                                     + rep.getValueString("c") + "\n";
395                         }
396                     }
397
398                     @Override
399                     public void onGetConfigurationsCallback(
400                             Vector<OcHeaderOption> headerOptions,
401                             OcRepresentation rep, int errorValue) {
402                         Log.i(LOG_TAG, "onGetConfigurationsCallback: enter");
403                         Log.i(LOG_TAG, "Resource URI: " + rep.getUri());
404                         logMessage += "Resource URI : " + rep.getUri() + "\n";
405
406                         if (rep.hasAttribute("n")) {
407                             logMessage += "Device Name : "
408                                     + rep.getValueString("n") + "\n";
409                         }
410                         if (rep.hasAttribute("loc")) {
411                             logMessage += "Location : "
412                                     + rep.getValueString("loc") + "\n";
413                         }
414                         if (rep.hasAttribute("locn")) {
415                             logMessage += "Location Name : "
416                                     + rep.getValueString("locn") + "\n";
417                         }
418                         if (rep.hasAttribute("r")) {
419                             logMessage += "Region : " + rep.getValueString("r")
420                                     + "\n";
421                         }
422                         if (rep.hasAttribute("c")) {
423                             logMessage += "Currency : "
424                                     + rep.getValueString("c") + "\n";
425                         }
426
427                         msg = Message.obtain();
428                         msg.what = 0;
429                         mHandler.sendMessage(msg);
430                     }
431                 });
432     }
433
434     private void setDiagnosticsListener() {
435         thingsMaintenance
436                 .setThingsMaintenanceListener(new IThingsMaintenanceListener() {
437
438                     @Override
439                     public void onRebootCallback(
440                             Vector<OcHeaderOption> headerOptions,
441                             OcRepresentation rep, int errorValue) {
442                         // TODO Auto-generated method stu
443                     }
444
445                     @Override
446                     public void onFactoryResetCallback(
447                             Vector<OcHeaderOption> headerOptions,
448                             OcRepresentation rep, int errorValue) {
449                         // TODO Auto-generated method stub
450
451                     }
452                 });
453     }
454
455     /**
456      * This method find the resources available in network.
457      */
458     private void findCandidateResources(Vector<String> resourceTypes) {
459         OCStackResult result = groupManager.findCandidateResources(
460                 resourceTypes, 5);
461         if (OCStackResult.OC_STACK_OK != result) {
462             Log.e(LOG_TAG, "Error while calling findCandidateResources");
463             String toastmessage = "findCandidateResources API returned error! ["
464                     + result + "]";
465             displayToastMessage(toastmessage);
466         }
467         if (messageCount == 1)
468             logMessage += "API RESULT : " + result.toString() + "\n";
469     }
470
471     /**
472      * This method gets the configuration data from con-server.
473      */
474     private void getConfiguration() {
475         Log.i(LOG_TAG, "There are " + resourceList.size()
476                 + " servers present in network");
477         ResourceInformation configurationCollection = collectionList
478                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
479         if (null == configurationCollection
480                 || null == configurationCollection.resource) {
481             displayToastMessage("Configuration collection resource does not exist!");
482             return;
483         }
484         if (false == configurationResourceFlag) {
485
486             displayToastMessage("Configuration resource does not exist!");
487             return;
488         }
489
490         String name = "all";
491         Vector<String> configs = new Vector<String>();
492         configs.add(name);
493
494         OCStackResult result = OCStackResult.OC_STACK_ERROR;
495         try {
496             result = thingsConfiguration.getConfigurations(
497                     configurationCollection.resource, configs);
498         } catch (OcException e) {
499             e.printStackTrace();
500         }
501
502         if (OCStackResult.OC_STACK_OK != result) {
503             Log.e(LOG_TAG, "getConfigurations failed!");
504             String toastmessage = "getConfigurations API returned error! ["
505                     + result + "]";
506             displayToastMessage(toastmessage);
507         }
508         logMessage = "API RESULT : " + result.toString() + "\n";
509         msg = Message.obtain();
510         msg.what = 0;
511         mHandler.sendMessage(msg);
512     }
513
514     /**
515      * This method update the configuration resource region attribute to given
516      * value.
517      *
518      * @param region
519      */
520     private void updateConfiguration(String region) {
521         ResourceInformation configurationCollection = collectionList
522                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
523         String name = "r";
524         Map<String, String> configurations = new HashMap<String, String>();
525
526         try {
527             configurations.put(name, region);
528         } catch (Exception e) {
529             Log.e(LOG_TAG, "Exception occured! " + e.toString());
530         }
531
532         OCStackResult result = OCStackResult.OC_STACK_ERROR;
533         try {
534             result = thingsConfiguration.updateConfigurations(
535                     configurationCollection.resource, configurations);
536         } catch (OcException e) {
537             e.printStackTrace();
538         }
539         if (OCStackResult.OC_STACK_OK != result) {
540             Log.e(LOG_TAG, "updateConfigurations failed!");
541             String toastmessage = "updateConfigurations API returned error! ["
542                     + result + "]";
543             displayToastMessage(toastmessage);
544         }
545         logMessage = "API RESULT : " + result.toString() + "\n";
546         logMessage += "Updating region to " + region;
547         msg = Message.obtain();
548         msg.what = 0;
549         mHandler.sendMessage(msg);
550     }
551
552     /**
553      * This method send request to reset all the configuration attribute values
554      * to default.
555      */
556     private void factoryReset() {
557         ResourceInformation diagnosticsCollection = collectionList
558                 .get(MAINTENANCE_COLLECTION_RESOURCE_URI);
559         if (null == diagnosticsCollection
560                 || null == diagnosticsCollection.resource) {
561             displayToastMessage("Diagnostic collection does not exist!");
562             return;
563         }
564
565         if (false == maintenanceResourceFlag) {
566             displayToastMessage("Diagnostic resource does not exist!");
567             return;
568         }
569
570         OCStackResult result = OCStackResult.values()[30];
571
572         try {
573             result = thingsMaintenance
574                     .factoryReset(diagnosticsCollection.resource);
575         } catch (OcException e) {
576             e.printStackTrace();
577         }
578
579         if (OCStackResult.OC_STACK_OK != result) {
580             Log.e(LOG_TAG, "factoryReset failed!");
581             String toastmessage = "factoryReset API returned error! [" + result
582                     + "]";
583             displayToastMessage(toastmessage);
584
585         }
586         logMessage = "API RESULT : " + result.toString() + "\n";
587         msg = Message.obtain();
588         msg.what = 0;
589         mHandler.sendMessage(msg);
590     }
591
592     /**
593      * This method send request to reboot server.
594      */
595     private void reboot() {
596         ResourceInformation diagnosticsCollection = collectionList
597                 .get(MAINTENANCE_COLLECTION_RESOURCE_URI);
598         if (null == diagnosticsCollection
599                 || null == diagnosticsCollection.resource) {
600             displayToastMessage("Diagnostic collection does not exist!");
601             return;
602         }
603         if (false == maintenanceResourceFlag) {
604             displayToastMessage("Diagnostic resource does not exist!");
605             return;
606         }
607
608         OCStackResult result = OCStackResult.OC_STACK_ERROR;
609         try {
610             result = thingsMaintenance.reboot(diagnosticsCollection.resource);
611         } catch (OcException e) {
612             e.printStackTrace();
613         }
614
615         if (OCStackResult.OC_STACK_OK != result) {
616             Log.e(LOG_TAG, "reboot failed!");
617             String toastmessage = "reboot API returned error! [" + result + "]";
618             displayToastMessage(toastmessage);
619         }
620         logMessage = "API RESULT : " + result.toString() + "\n";
621         msg = Message.obtain();
622         msg.what = 0;
623         mHandler.sendMessage(msg);
624     }
625
626     /**
627      * This method is for getting list of all supported configuration values.
628      * Response will be in JSON format (key-value pair).
629      */
630     private String getListOfSupportedConfigurationUnits() {
631         return thingsConfiguration.getListOfSupportedConfigurationUnits();
632     }
633
634     private void displayToastMessage(String message) {
635         Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
636         toast.show();
637         Log.i(LOG_TAG, message);
638     }
639
640     private void collectionFound(OcResource resource) {
641         String uri = resource.getUri();
642         ResourceInformation resourceInfo = collectionList.get(uri);
643         if (null == resourceInfo) {
644             Log.e(LOG_TAG, "Collection is not created!");
645             return;
646         }
647
648         if (null != resourceInfo.resource) {
649             Log.e(LOG_TAG, "Collection resource already updated!");
650             return;
651         }
652
653         resourceInfo.resource = resource;
654         if (3 == messageCount) {
655             findGroupPressedFlag = true;
656         }
657     }
658
659     /**
660      * This callback will be invoked when the interested resource discovered in
661      * network.
662      */
663     private void resourceFound(OcResource resource) throws OcException {
664         String uri = resource.getUri();
665         String host = resource.getHost();
666
667         // Check if the resource already exist in the map table
668         ResourceInformation resourceInfo = resourceList.get(uri + host);
669         if (null != resourceInfo) {
670             Log.e(LOG_TAG, "Resource already exists!");
671             return;
672         }
673
674         // Create resource
675         resourceInfo = new ResourceInformation();
676         resourceInfo.resource = resource;
677         if (uri.equalsIgnoreCase("/oic/con")) {
678             ResourceInformation collectionResource = collectionList
679                     .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
680             if (null == collectionResource
681                     || null == collectionResource.resourceHandle) {
682                 Log.e(LOG_TAG, "Invalid Configuration collection!");
683                 return;
684             }
685
686             ResourceInformation resourceInfoCollection;
687             resourceInfoCollection = collectionList
688                     .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
689             OcResourceHandle handle;
690             handle = resourceInfoCollection.resourceHandle;
691             resourceInfo.resourceHandle = handle;
692             resourceInfo.resourceHandle = groupManager.bindResourceToGroup(
693                     resource, handle);
694
695             resourceList.put(uri + host, resourceInfo);
696             configurationResourceFlag = true;
697         } else if (uri.equalsIgnoreCase("/oic/mnt")) {
698             ResourceInformation diagnosticResource = collectionList
699                     .get(MAINTENANCE_COLLECTION_RESOURCE_URI);
700             if (null == diagnosticResource
701                     || null == diagnosticResource.resourceHandle) {
702                 Log.e(LOG_TAG, "Invalid Configuration collection!");
703                 return;
704             }
705
706             ResourceInformation resourceInfoCollection;
707             resourceInfoCollection = collectionList
708                     .get(MAINTENANCE_COLLECTION_RESOURCE_URI);
709             OcResourceHandle handle;
710             handle = resourceInfoCollection.resourceHandle;
711             resourceInfo.resourceHandle = handle;
712             resourceInfo.resourceHandle = groupManager.bindResourceToGroup(
713                     resource, handle);
714
715             resourceList.put(uri + host, resourceInfo);
716             maintenanceResourceFlag = true;
717         } else if (uri.equalsIgnoreCase("/factorySet")) {
718             ResourceInformation factorysetResource = collectionList
719                     .get(FACTORYSET_COLLECTION_RESOURCE_URI);
720             if (null == factorysetResource
721                     || null == factorysetResource.resourceHandle) {
722                 Log.e(LOG_TAG, "Invalid Configuration collection!");
723                 return;
724             }
725
726             ResourceInformation resourceInfoCollection;
727             resourceInfoCollection = collectionList
728                     .get(FACTORYSET_COLLECTION_RESOURCE_URI);
729             OcResourceHandle handle;
730             handle = resourceInfoCollection.resourceHandle;
731             resourceInfo.resourceHandle = handle;
732             resourceInfo.resourceHandle = groupManager.bindResourceToGroup(
733                     resource, handle);
734
735             resourceList.put(uri + host, resourceInfo);
736             factorySetResourceFlag = true;
737         } else {
738             Log.e(LOG_TAG, "Resource is of different type: " + uri);
739             return;
740         }
741     }
742
743     private void createResourceCollection(String uri, String typename)
744             throws OcException {
745
746         Map<String, OcResourceHandle> groupList = new HashMap<String, OcResourceHandle>();
747
748         Log.i(LOG_TAG, "createGroup: " + typename);
749         // Check if the resource collection is already created
750         if (null != collectionList.get(uri)) {
751             Log.e(LOG_TAG, "Collection is already exist!");
752             return;
753         }
754
755         OcResourceHandle resourceHandle = null;
756
757         try {
758             resourceHandle = OcPlatform.registerResource(
759                     uri,
760                     typename,
761                     OcPlatform.BATCH_INTERFACE, null, EnumSet.of(
762                             ResourceProperty.DISCOVERABLE));
763         } catch (OcException e) {
764             Log.e(LOG_TAG, "go exception");
765             Log.e(LOG_TAG, "RegisterResource error. " + e.getMessage());
766         }
767         try {
768             OcPlatform.bindInterfaceToResource(resourceHandle, OcPlatform.GROUP_INTERFACE);
769         } catch (OcException e) {
770             // TODO Auto-generated catch block
771             e.printStackTrace();
772         }
773         try {
774             OcPlatform.bindInterfaceToResource(resourceHandle, OcPlatform.DEFAULT_INTERFACE);
775         } catch (OcException e) {
776             // TODO Auto-generated catch block
777             e.printStackTrace();
778         }
779
780         if (null == resourceHandle) {
781             Log.e(LOG_TAG, " createResourceCollection : resourceHandle is NULL");
782
783         }
784         // Add the collection resource to map table
785         ResourceInformation resourceInfo = new ResourceInformation();
786         resourceInfo.resourceHandle = resourceHandle;
787         collectionList.put(uri, resourceInfo);
788         Log.i(LOG_TAG, "size of collectionList : " + collectionList.size());
789     }
790
791     private void deleteResources() {
792         Log.i(LOG_TAG, "deleteResources: enter");
793         try {
794             // unregister all resources
795             for (ResourceInformation resource : resourceList.values()) {
796                 if (null != resource.resourceHandle) {
797                     if (resource.resource.getUri().equalsIgnoreCase(
798                             CONFIGURATION_RESOURCE_URI)) {
799                         ResourceInformation collectionResource = collectionList
800                                 .get(CONFIGURATION_COLLECTION_RESOURCE_URI);
801                         if (null != collectionResource
802                                 && null != collectionResource.resourceHandle) {
803                             OcPlatform
804                                     .unregisterResource(resource.resourceHandle);
805                             Log.i(LOG_TAG, "unregistered resource"
806                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
807
808                         }
809                     } else if (resource.resource.getUri().equalsIgnoreCase(
810                             MAINTENANCE_RESOURCE_URI)) {
811                         ResourceInformation diagnosticResource = collectionList
812                                 .get(MAINTENANCE_COLLECTION_RESOURCE_URI);
813                         if (null != diagnosticResource
814                                 && null != diagnosticResource.resourceHandle) {
815                             OcPlatform
816                                     .unregisterResource(resource.resourceHandle);
817                             Log.i(LOG_TAG, "unregistered resource"
818                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
819                         }
820                     } else if (resource.resource.getUri().equalsIgnoreCase(
821                             FACTORYSET_RESOURCE_URI)) {
822                         ResourceInformation factorysetResource = collectionList
823                                 .get(FACTORYSET_COLLECTION_RESOURCE_URI);
824                         if (null != factorysetResource
825                                 && null != factorysetResource.resourceHandle) {
826                             OcPlatform
827                                     .unregisterResource(resource.resourceHandle);
828                             Log.i(LOG_TAG, "unregistered resource"
829                                     + CONFIGURATION_COLLECTION_RESOURCE_URI);
830                         }
831                     }
832                 }
833             }
834
835         } catch (OcException e) {
836             Log.e(LOG_TAG, "OcException occured! " + e.toString());
837         }
838     }
839 }