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