2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.serviceprovider.utils;
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.Date;
22 import java.util.Enumeration;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
27 import java.util.Vector;
29 import oic.simulator.serviceprovider.Activator;
30 import oic.simulator.serviceprovider.model.CollectionResource;
31 import oic.simulator.serviceprovider.model.Device;
32 import oic.simulator.serviceprovider.model.LocalResourceAttribute;
33 import oic.simulator.serviceprovider.model.Resource;
34 import oic.simulator.serviceprovider.model.SingleResource;
36 import org.oic.simulator.AttributeValue;
37 import org.oic.simulator.AttributeValue.TypeInfo;
38 import org.oic.simulator.AttributeValue.ValueType;
39 import org.oic.simulator.ILogger.Level;
40 import org.oic.simulator.InvalidArgsException;
41 import org.oic.simulator.SimulatorException;
42 import org.oic.simulator.SimulatorResourceAttribute;
43 import org.oic.simulator.SimulatorResourceModel;
46 * This class has common utility methods.
48 public class Utility {
50 public static String fileNameToDisplay(String fileName) {
51 if (null == fileName || fileName.length() < 1) {
54 // Remove the RAML file standard prefix
55 int len = Constants.RAML_FILE_PREFIX.length();
57 if (fileName.startsWith(Constants.RAML_FILE_PREFIX)) {
58 fileName = fileName.substring(len);
62 // Removing the file extension
63 int index = fileName.lastIndexOf('.');
64 fileName = fileName.substring(0, index);
68 public static String displayToFileName(String displayName) {
69 if (null == displayName || displayName.length() < 1) {
74 fileName = Constants.RAML_FILE_PREFIX + displayName;
76 // Adding the file extension
77 fileName = fileName + Constants.RAML_FILE_EXTENSION;
82 public static String getAutomationStatus(boolean status) {
84 return Constants.ENABLED;
86 return Constants.DISABLED;
90 public static String getAutomationString(boolean status) {
92 return Constants.ENABLE;
94 return Constants.DISABLE;
98 public static boolean getAutomationBoolean(String status) {
100 if (status.equals(Constants.ENABLE)) {
107 public static int getUpdateIntervalFromString(String value) {
108 int result = Constants.DEFAULT_AUTOMATION_INTERVAL;
111 result = Integer.parseInt(value);
112 } catch (NumberFormatException e) {
116 .log(Level.ERROR.ordinal(),
118 getSimulatorErrorString(
120 "Update interval convertion failed."
121 + "Taking the default value("
122 + Constants.DEFAULT_AUTOMATION_INTERVAL
129 public static List<String> convertSetToList(Set<String> typeSet) {
130 if (null == typeSet) {
133 List<String> list = new ArrayList<String>();
134 Iterator<String> typeItr = typeSet.iterator();
135 while (typeItr.hasNext()) {
136 list.add(typeItr.next());
141 public static List<SingleResource> getSingleResourceListFromSet(
142 Set<SingleResource> resources) {
143 if (null == resources) {
146 List<SingleResource> list = new ArrayList<SingleResource>();
147 Iterator<SingleResource> typeItr = resources.iterator();
148 while (typeItr.hasNext()) {
149 list.add(typeItr.next());
154 public static List<CollectionResource> getCollectionResourceListFromSet(
155 Set<CollectionResource> resources) {
156 if (null == resources) {
159 List<CollectionResource> list = new ArrayList<CollectionResource>();
160 Iterator<CollectionResource> typeItr = resources.iterator();
161 while (typeItr.hasNext()) {
162 list.add(typeItr.next());
167 public static List<Device> getDeviceListFromSet(Set<Device> devices) {
168 if (null == devices) {
171 List<Device> list = new ArrayList<Device>();
172 Iterator<Device> typeItr = devices.iterator();
173 while (typeItr.hasNext()) {
174 list.add(typeItr.next());
179 public static Set<String> convertVectorToSet(Vector<String> vector) {
180 if (null == vector || vector.isEmpty()) {
183 Set<String> resultSet = new HashSet<String>();
184 Enumeration<String> e = vector.elements();
185 while (e.hasMoreElements()) {
186 resultSet.add(e.nextElement().toString());
191 public static String getSimulatorErrorString(Exception e, String info) {
196 if (e instanceof SimulatorException) {
197 SimulatorException simEx = (SimulatorException) e;
198 detail = simEx.message() + "\n";
199 detail += "Exception Type: " + simEx.getClass().getSimpleName()
201 detail += "Error code: " + simEx.code().toString();
203 detail = info + "\n";
204 detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
205 detail += "Message: " + e.getMessage();
210 public static Set<String> getAttributeTypes() {
211 Set<String> attTypes = new HashSet<String>();
212 ValueType[] types = ValueType.values();
214 attTypes.add(Constants.INT);
215 attTypes.add(Constants.DOUBLE);
216 attTypes.add(Constants.BOOL);
217 attTypes.add(Constants.STRING);
222 public static ValueType getAttributeTypeEnum(String type) {
223 if (null != type && type.trim().length() > 0) {
224 if (type.equalsIgnoreCase(Constants.INT)) {
225 return ValueType.INTEGER;
227 if (type.equalsIgnoreCase(Constants.DOUBLE)) {
228 return ValueType.DOUBLE;
230 if (type.equalsIgnoreCase(Constants.BOOL)) {
231 return ValueType.BOOLEAN;
233 if (type.equalsIgnoreCase(Constants.STRING)) {
234 return ValueType.STRING;
237 return ValueType.UNKNOWN;
240 public static int[] convertSetToArrayInt(Set<Object> allowedValues) {
241 if (null == allowedValues || allowedValues.size() < 1) {
244 int[] arr = new int[allowedValues.size()];
245 Iterator<Object> itr = allowedValues.iterator();
248 while (itr.hasNext()) {
249 arr[i++] = (int) itr.next();
251 } catch (Exception e) {
257 public static double[] convertSetToArrayDouble(Set<Object> allowedValues) {
258 if (null == allowedValues || allowedValues.size() < 1) {
261 double[] arr = new double[allowedValues.size()];
262 Iterator<Object> itr = allowedValues.iterator();
265 while (itr.hasNext()) {
266 arr[i++] = (double) itr.next();
268 } catch (Exception e) {
274 public static boolean[] convertSetToArrayBoolean(Set<Object> allowedValues) {
275 if (null == allowedValues || allowedValues.size() < 1) {
278 boolean[] arr = new boolean[allowedValues.size()];
279 Iterator<Object> itr = allowedValues.iterator();
282 while (itr.hasNext()) {
283 arr[i++] = (boolean) itr.next();
285 } catch (Exception e) {
291 public static String[] convertSetToArrayString(Set<Object> allowedValues) {
292 if (null == allowedValues || allowedValues.size() < 1) {
295 String[] arr = new String[allowedValues.size()];
296 Iterator<Object> itr = allowedValues.iterator();
299 while (itr.hasNext()) {
300 arr[i++] = (String) itr.next();
302 } catch (Exception e) {
308 public static Vector<Integer> convertSetToVectorInt(
309 Set<Object> allowedValues) {
310 if (null == allowedValues || allowedValues.size() < 1) {
313 Vector<Integer> resultVec = new Vector<Integer>();
314 Iterator<Object> itr = allowedValues.iterator();
316 while (itr.hasNext()) {
317 resultVec.add((Integer) itr.next());
319 } catch (Exception e) {
325 public static Vector<Double> convertSetToVectorDouble(
326 Set<Object> allowedValues) {
327 if (null == allowedValues || allowedValues.size() < 1) {
330 Vector<Double> resultVec = new Vector<Double>();
331 Iterator<Object> itr = allowedValues.iterator();
333 while (itr.hasNext()) {
334 resultVec.add((Double) itr.next());
336 } catch (Exception e) {
342 public static Vector<String> convertSetToVectorString(
343 Set<Object> allowedValues) {
344 if (null == allowedValues || allowedValues.size() < 1) {
347 Vector<String> resultVec = new Vector<String>();
348 Iterator<Object> itr = allowedValues.iterator();
350 while (itr.hasNext()) {
351 resultVec.add((String) itr.next());
353 } catch (Exception e) {
359 public static Set<Object> convertSetStringToSetObject(Set<String> values,
361 if (null == values || values.isEmpty()) {
364 Set<Object> resultSet = new HashSet<Object>();
365 if (type.equals(Constants.INT)) {
367 Iterator<String> itr = values.iterator();
368 while (itr.hasNext()) {
370 val = Integer.parseInt(itr.next());
372 } catch (NumberFormatException nfe) {
373 // Added for safety. Nothing to do.
376 } else if (type.equals(Constants.DOUBLE)) {
378 Iterator<String> itr = values.iterator();
379 while (itr.hasNext()) {
381 val = Double.parseDouble(itr.next());
383 } catch (NumberFormatException nfe) {
384 // Added for safety. Nothing to do.
387 } else if (type.equals(Constants.BOOL)) {
389 resultSet.add(false);
391 Iterator<String> itr = values.iterator();
392 while (itr.hasNext()) {
393 resultSet.add(itr.next());
399 public static List<Resource> convertSingleTypeResourceListToBaseType(
400 List<SingleResource> resources) {
401 if (null == resources || resources.isEmpty()) {
404 List<Resource> resultSet = new ArrayList<Resource>();
405 Iterator<SingleResource> itr = resources.iterator();
406 while (itr.hasNext()) {
407 resultSet.add(itr.next());
412 public static List<Resource> convertCollectionTypeResourceListToBaseType(
413 List<CollectionResource> resources) {
414 if (null == resources || resources.isEmpty()) {
417 List<Resource> resultSet = new ArrayList<Resource>();
418 Iterator<CollectionResource> itr = resources.iterator();
419 while (itr.hasNext()) {
420 resultSet.add(itr.next());
425 public static Comparator<Resource> resourceComparator = new Comparator<Resource>() {
444 .equalsIgnoreCase(s2Part)) {
445 return extractInt(s1)
471 public static Comparator<SingleResource> singleResourceComparator = new Comparator<SingleResource>() {
474 SingleResource res2) {
490 .equalsIgnoreCase(s2Part)) {
491 return extractInt(s1)
517 public static Comparator<CollectionResource> collectionResourceComparator = new Comparator<CollectionResource>() {
519 CollectionResource res1,
520 CollectionResource res2) {
536 .equalsIgnoreCase(s2Part)) {
537 return extractInt(s1)
563 public static Comparator<Device> deviceComparator = new Comparator<Device>() {
582 .equalsIgnoreCase(s2Part)) {
583 return extractInt(s1)
609 // This method only works for attributes whose values are of type int,
610 // double, bool, string and 1-D array of primitive types
611 public static String getAttributeValueAsString(AttributeValue val) {
615 Object value = val.get();
619 TypeInfo type = val.typeInfo();
620 if (type.mType == ValueType.RESOURCEMODEL
621 || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL)
622 || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
625 if (type.mType == ValueType.ARRAY) {
626 if (type.mBaseType == ValueType.INTEGER) {
627 Integer[] values = (Integer[]) value;
628 if (null == values || values.length < 1) {
631 List<Integer> list = new ArrayList<Integer>();
632 for (Integer i : values) {
635 return list.toString();
636 } else if (type.mBaseType == ValueType.DOUBLE) {
637 Double[] values = (Double[]) value;
638 if (null == values || values.length < 1) {
641 List<Double> list = new ArrayList<Double>();
642 for (Double i : values) {
645 return list.toString();
646 } else if (type.mBaseType == ValueType.BOOLEAN) {
647 Boolean[] values = (Boolean[]) value;
648 if (null == values || values.length < 1) {
651 List<Boolean> list = new ArrayList<Boolean>();
652 for (Boolean i : values) {
655 return list.toString();
656 } else if (type.mBaseType == ValueType.STRING) {
657 String[] values = (String[]) value;
658 if (null == values || values.length < 1) {
661 List<String> list = new ArrayList<String>();
662 for (String i : values) {
665 return list.toString();
670 return String.valueOf(value);
674 public static List<LocalResourceAttribute> getDummyAttributes() {
675 List<LocalResourceAttribute> attributes = null;
676 attributes = new ArrayList<LocalResourceAttribute>();
679 SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
680 "integer", new AttributeValue(2), null);
681 attributes.add(addAttribute(attribute));
684 attribute = new SimulatorResourceAttribute("boolean",
685 new AttributeValue(false), null);
686 attributes.add(addAttribute(attribute));
689 attribute = new SimulatorResourceAttribute("string",
690 new AttributeValue("india"), null);
691 attributes.add(addAttribute(attribute));
693 // Integer array attribute
694 int iarr[] = { 1, 2, 3 };
695 attribute = new SimulatorResourceAttribute("integerArr",
696 new AttributeValue(iarr), null);
697 attributes.add(addAttribute(attribute));
699 // Double array attribute
700 double darr[] = { 1.5, 2.51, 3.15 };
701 attribute = new SimulatorResourceAttribute("doubleArr",
702 new AttributeValue(darr), null);
703 attributes.add(addAttribute(attribute));
705 // Boolean array attribute
706 boolean barr[] = { false, true, false };
707 attribute = new SimulatorResourceAttribute("boolArr",
708 new AttributeValue(barr), null);
709 attributes.add(addAttribute(attribute));
711 // String array attribute
712 String sarr[] = { "senthil", "muruga", "sriram" };
713 attribute = new SimulatorResourceAttribute("stringArr",
714 new AttributeValue(sarr), null);
715 attributes.add(addAttribute(attribute));
717 // Model type complex attribute
718 attribute = new SimulatorResourceAttribute("subAtt1",
719 new AttributeValue("chennai"), null);
720 SimulatorResourceModel model = new SimulatorResourceModel();
722 model.addAttribute(attribute);
723 } catch (InvalidArgsException e) {
724 // TODO Auto-generated catch block
727 attribute = new SimulatorResourceAttribute("subAtt2",
728 new AttributeValue("madurai"), null);
730 model.addAttribute(attribute);
731 } catch (InvalidArgsException e) {
732 // TODO Auto-generated catch block
736 SimulatorResourceModel subModel = new SimulatorResourceModel();
738 subModel.addAttribute(attribute);
739 } catch (InvalidArgsException e) {
740 // TODO Auto-generated catch block
743 attribute = new SimulatorResourceAttribute("modelsubAtt3",
744 new AttributeValue(subModel), null);
746 model.addAttribute(attribute);
747 } catch (InvalidArgsException e) {
748 // TODO Auto-generated catch block
752 SimulatorResourceAttribute modelAtt = new SimulatorResourceAttribute(
753 "modelAtt1", new AttributeValue(model));
754 attributes.add(addAttribute(modelAtt));
756 // 1-D array of model
757 attribute = new SimulatorResourceAttribute("subAtt1",
758 new AttributeValue("chennai"), null);
759 SimulatorResourceModel[] modelArr = new SimulatorResourceModel[2];
760 model = new SimulatorResourceModel();
762 model.addAttribute(attribute);
764 } catch (InvalidArgsException e) {
765 // TODO Auto-generated catch block
768 attribute = new SimulatorResourceAttribute("subAtt2",
769 new AttributeValue("madurai"), null);
770 model = new SimulatorResourceModel();
772 model.addAttribute(attribute);
774 } catch (InvalidArgsException e) {
775 // TODO Auto-generated catch block
778 modelAtt = new SimulatorResourceAttribute("modelAtt2",
779 new AttributeValue(modelArr));
780 attributes.add(addAttribute(modelAtt));
785 private static LocalResourceAttribute addAttribute(
786 SimulatorResourceAttribute att) {
787 LocalResourceAttribute localAtt = new LocalResourceAttribute();
789 localAtt = new LocalResourceAttribute();
791 localAtt.setResourceAttributeRef(att);
793 // Initially disabling the automation
794 localAtt.setAutomationInProgress(false);
796 // Assigning the default automation interval
797 localAtt.setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
799 // Setting the default automation type
800 localAtt.setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);