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.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.List;
29 import java.util.Vector;
31 import org.oic.simulator.AttributeValue;
32 import org.oic.simulator.AttributeValue.TypeInfo;
33 import org.oic.simulator.AttributeValue.ValueType;
34 import org.oic.simulator.ILogger.Level;
35 import org.oic.simulator.InvalidArgsException;
36 import org.oic.simulator.SimulatorException;
37 import org.oic.simulator.SimulatorResourceModel;
39 import oic.simulator.serviceprovider.Activator;
40 import oic.simulator.serviceprovider.model.AttributeElement;
41 import oic.simulator.serviceprovider.model.Resource;
42 import oic.simulator.serviceprovider.model.SingleResource;
45 * This class has common utility methods.
47 public class Utility {
49 public static String getAutomationStatus(boolean status) {
51 return Constants.ENABLED;
53 return Constants.DISABLED;
57 public static String getAutomationString(boolean status) {
59 return Constants.ENABLE;
61 return Constants.DISABLE;
65 public static boolean getAutomationBoolean(String status) {
67 if (status.equals(Constants.ENABLE)) {
74 public static int getUpdateIntervalFromString(String value) {
75 int result = Constants.DEFAULT_AUTOMATION_INTERVAL;
78 result = Integer.parseInt(value);
79 } catch (NumberFormatException e) {
83 .log(Level.ERROR.ordinal(),
85 getSimulatorErrorString(
87 "Update interval convertion failed."
88 + "Taking the default value("
89 + Constants.DEFAULT_AUTOMATION_INTERVAL
96 public static List<String> convertSetToList(Set<String> typeSet) {
97 if (null == typeSet) {
100 List<String> list = new ArrayList<String>();
101 Iterator<String> typeItr = typeSet.iterator();
102 while (typeItr.hasNext()) {
103 list.add(typeItr.next());
108 public static List<SingleResource> getSingleResourceListFromSet(
109 Set<SingleResource> resources) {
110 if (null == resources) {
113 List<SingleResource> list = new ArrayList<SingleResource>();
114 Iterator<SingleResource> typeItr = resources.iterator();
115 while (typeItr.hasNext()) {
116 list.add(typeItr.next());
121 public static Set<String> convertVectorToSet(Vector<String> vector) {
122 if (null == vector || vector.isEmpty()) {
125 Set<String> resultSet = new HashSet<String>();
126 Enumeration<String> e = vector.elements();
127 while (e.hasMoreElements()) {
128 resultSet.add(e.nextElement());
133 public static String getSimulatorErrorString(Exception e, String info) {
138 if (e instanceof SimulatorException) {
139 SimulatorException simEx = (SimulatorException) e;
140 detail = simEx.message() + "\n";
141 detail += "Exception Type: " + simEx.getClass().getSimpleName()
143 detail += "Error code: " + simEx.code().toString();
145 if (null != info && !info.isEmpty())
146 detail = info + "\n";
148 detail = "Description not available\n";
149 detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
150 String msg = e.getMessage();
151 if (null != msg && !msg.isEmpty()) {
152 detail += "Message: " + e.getMessage();
158 public static Set<String> getAttributeTypes() {
159 Set<String> attTypes = new HashSet<String>();
160 ValueType[] types = ValueType.values();
162 attTypes.add(Constants.INT);
163 attTypes.add(Constants.DOUBLE);
164 attTypes.add(Constants.BOOL);
165 attTypes.add(Constants.STRING);
170 public static ValueType getAttributeTypeEnum(String type) {
171 if (null != type && type.trim().length() > 0) {
172 if (type.equalsIgnoreCase(Constants.INT)) {
173 return ValueType.INTEGER;
175 if (type.equalsIgnoreCase(Constants.DOUBLE)) {
176 return ValueType.DOUBLE;
178 if (type.equalsIgnoreCase(Constants.BOOL)) {
179 return ValueType.BOOLEAN;
181 if (type.equalsIgnoreCase(Constants.STRING)) {
182 return ValueType.STRING;
185 return ValueType.UNKNOWN;
188 public static int[] convertSetToArrayInt(Set<Object> allowedValues) {
189 if (null == allowedValues || allowedValues.size() < 1) {
192 int[] arr = new int[allowedValues.size()];
193 Iterator<Object> itr = allowedValues.iterator();
196 while (itr.hasNext()) {
197 arr[i++] = (int) itr.next();
199 } catch (Exception e) {
205 public static double[] convertSetToArrayDouble(Set<Object> allowedValues) {
206 if (null == allowedValues || allowedValues.size() < 1) {
209 double[] arr = new double[allowedValues.size()];
210 Iterator<Object> itr = allowedValues.iterator();
213 while (itr.hasNext()) {
214 arr[i++] = (double) itr.next();
216 } catch (Exception e) {
222 public static boolean[] convertSetToArrayBoolean(Set<Object> allowedValues) {
223 if (null == allowedValues || allowedValues.size() < 1) {
226 boolean[] arr = new boolean[allowedValues.size()];
227 Iterator<Object> itr = allowedValues.iterator();
230 while (itr.hasNext()) {
231 arr[i++] = (boolean) itr.next();
233 } catch (Exception e) {
239 public static String[] convertSetToArrayString(Set<Object> allowedValues) {
240 if (null == allowedValues || allowedValues.size() < 1) {
243 String[] arr = new String[allowedValues.size()];
244 Iterator<Object> itr = allowedValues.iterator();
247 while (itr.hasNext()) {
248 arr[i++] = (String) itr.next();
250 } catch (Exception e) {
256 public static Vector<Integer> convertSetToVectorInt(
257 Set<Object> allowedValues) {
258 if (null == allowedValues || allowedValues.size() < 1) {
261 Vector<Integer> resultVec = new Vector<Integer>();
262 Iterator<Object> itr = allowedValues.iterator();
264 while (itr.hasNext()) {
265 resultVec.add((Integer) itr.next());
267 } catch (Exception e) {
273 public static Vector<Double> convertSetToVectorDouble(
274 Set<Object> allowedValues) {
275 if (null == allowedValues || allowedValues.size() < 1) {
278 Vector<Double> resultVec = new Vector<Double>();
279 Iterator<Object> itr = allowedValues.iterator();
281 while (itr.hasNext()) {
282 resultVec.add((Double) itr.next());
284 } catch (Exception e) {
290 public static Vector<String> convertSetToVectorString(
291 Set<String> allowedValues) {
292 if (null == allowedValues || allowedValues.size() < 1) {
295 Vector<String> resultVec = new Vector<String>();
296 Iterator<String> itr = allowedValues.iterator();
298 while (itr.hasNext()) {
299 resultVec.add((String) itr.next());
301 } catch (Exception e) {
307 public static Set<Object> convertSetStringToSetObject(Set<String> values,
309 if (null == values || values.isEmpty()) {
312 Set<Object> resultSet = new HashSet<Object>();
313 if (AttributeValue.ValueType.INTEGER == type) {
315 Iterator<String> itr = values.iterator();
316 while (itr.hasNext()) {
318 val = Integer.parseInt(itr.next());
320 } catch (NumberFormatException nfe) {
324 .log(Level.ERROR.ordinal(),
326 "There is an error while doing internal convertion(string set to object set).\n"
327 + Utility.getSimulatorErrorString(
331 } else if (AttributeValue.ValueType.DOUBLE == type) {
333 Iterator<String> itr = values.iterator();
334 while (itr.hasNext()) {
336 val = Double.parseDouble(itr.next());
338 } catch (NumberFormatException nfe) {
342 .log(Level.ERROR.ordinal(),
344 "There is an error while doing internal convertion(string set to object set).\n"
345 + Utility.getSimulatorErrorString(
349 } else if (AttributeValue.ValueType.BOOLEAN == type) {
351 resultSet.add(false);
352 } else if (AttributeValue.ValueType.STRING == type) {
353 Iterator<String> itr = values.iterator();
354 while (itr.hasNext()) {
355 resultSet.add(itr.next());
361 public static List<Resource> convertSingleTypeResourceListToBaseType(
362 List<SingleResource> resources) {
363 if (null == resources || resources.isEmpty()) {
366 List<Resource> resultSet = new ArrayList<Resource>();
367 Iterator<SingleResource> itr = resources.iterator();
368 while (itr.hasNext()) {
369 resultSet.add(itr.next());
374 public static String[] convertListToStringArray(List<String> valueList) {
376 if (null != valueList && valueList.size() > 0) {
377 strArr = valueList.toArray(new String[1]);
379 strArr = new String[1];
384 public static Comparator<Resource> resourceComparator = new Comparator<Resource>() {
403 .equalsIgnoreCase(s2Part)) {
404 return extractInt(s1)
430 public static Comparator<SingleResource> singleResourceComparator = new Comparator<SingleResource>() {
433 SingleResource res2) {
449 .equalsIgnoreCase(s2Part)) {
450 return extractInt(s1)
476 public static Comparator<AttributeElement> attributeComparator = new Comparator<AttributeElement>() {
478 AttributeElement att1,
479 AttributeElement att2) {
481 .getSimulatorResourceAttribute()
484 .getSimulatorResourceAttribute()
497 .equalsIgnoreCase(s2Part)) {
498 return extractInt(s1)
525 // This method only works for attributes whose values are of type int,
526 // double, bool, string and 1-D array of primitive types
527 public static String getAttributeValueAsString(AttributeValue val) {
532 Object value = val.get();
537 TypeInfo type = val.typeInfo();
538 if (type.mBaseType == ValueType.RESOURCEMODEL
539 || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
543 AttributeValueStringConverter converter = new AttributeValueStringConverter(
545 return converter.toString();
548 public static boolean isUriValid(String resURI) {
549 if (null == resURI || resURI.length() < 2 || resURI.length() > 63
550 || !resURI.startsWith("/") || resURI.endsWith("/")
551 || resURI.contains("/..") || resURI.contains("//")
552 || resURI.contains("/./") || resURI.contains("?")
553 || resURI.contains("#") || resURI.contains("%")) {
559 public static boolean isResourceTypeValid(String resType) {
560 if (null == resType || resType.isEmpty()) {
564 char[] ch = resType.toCharArray();
566 if (c != '.' && c != '-' && (c < 'a' || c > 'z')
567 && (c < '0' || c > '9')) {
575 public static Map<String, String> getResourceInterfaces(
576 Class<? extends Resource> resourceClass) {
577 Map<String, String> ifTypes = null;
578 if (resourceClass == SingleResource.class) {
579 ifTypes = new HashMap<String, String>();
580 ifTypes.put(Constants.BASELINE_INTERFACE, "Baseline");
581 ifTypes.put(Constants.READ_ONLY_INTERFACE, "Read-Only");
582 ifTypes.put(Constants.READ_WRITE_INTERFACE, "Read-Write");
583 ifTypes.put(Constants.ACTUATOR_INTERFACE, "Actuator");
584 ifTypes.put(Constants.SENSOR_INTERFACE, "Sensor");
589 public static String removeWhiteSpacesInArrayValues(String value) {
590 if (null == value || value.isEmpty())
593 value = value.trim();
595 String token[] = value.split(",");
596 StringBuilder result = new StringBuilder();
597 for (int i = 0; i < token.length; i++) {
598 result.append(token[i].trim());
599 if (i + 1 < token.length) {
604 return result.toString();
607 public static AttributeValue cloneAttributeValue(AttributeValue value)
608 throws InvalidArgsException, NullPointerException {
609 AttributeValue clone = null;
611 AttributeValue.TypeInfo typeInfo = value.typeInfo();
613 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
614 SimulatorResourceModel resModel = (SimulatorResourceModel) value
616 SimulatorResourceModel modelCopy = new SimulatorResourceModel();
618 for (Map.Entry<String, AttributeValue> entry : resModel.get()
620 String attName = entry.getKey();
621 AttributeValue attValue = entry.getValue();
622 modelCopy.set(attName, cloneAttributeValue(attValue));
624 clone = new AttributeValue(modelCopy);
625 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
626 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
627 if (typeInfo.mDepth == 1) {
628 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) value
630 SimulatorResourceModel[] modelArrayCopy = new SimulatorResourceModel[resModelArray.length];
631 for (int i = 0; i < resModelArray.length; i++) {
632 AttributeValue attValue = cloneAttributeValue(new AttributeValue(
634 if (null != attValue) {
635 modelArrayCopy[i] = (SimulatorResourceModel) attValue
639 clone = new AttributeValue(modelArrayCopy);
640 } else if (typeInfo.mDepth == 2) {
641 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) value
643 SimulatorResourceModel[][] modelArrayCopy = new SimulatorResourceModel[resModelArray.length][];
644 for (int i = 0; i < resModelArray.length; i++) {
645 AttributeValue attValue = cloneAttributeValue(new AttributeValue(
647 if (null != attValue) {
648 modelArrayCopy[i] = (SimulatorResourceModel[]) attValue
652 clone = new AttributeValue(modelArrayCopy);
653 } else if (typeInfo.mDepth == 3) {
654 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) value
656 SimulatorResourceModel[][][] modelArrayCopy = new SimulatorResourceModel[resModelArray.length][][];
657 for (int i = 0; i < resModelArray.length; i++) {
658 AttributeValue attValue = cloneAttributeValue(new AttributeValue(
660 if (null != attValue) {
661 modelArrayCopy[i] = (SimulatorResourceModel[][]) attValue
665 clone = new AttributeValue(modelArrayCopy);
668 String attValueInString = new AttributeValueStringConverter(value)
670 clone = AttributeValueBuilder.build(attValueInString,