summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvictor.gao <victor.gao@huawei.com>2018-03-28 14:30:26 +0800
committervictor.gao <victor.gao@huawei.com>2018-03-28 14:30:26 +0800
commita33d15a8f6a2b0ca5d1220c21170bead600da4a2 (patch)
tree8e705d716390703af572317be6334a50e2ffbb35
parentd0fdea0fe26cce07924606dbcefa21b9116a7f0c (diff)
Add multivimproxy util code
Change-Id: I34f94565e8dedffe63e9c5392682d68fc49cd921 Issue-ID: VFC-644 Signed-off-by: victor.gao <victor.gao@huawei.com>
-rw-r--r--service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtil.java290
-rw-r--r--service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtil.java390
-rw-r--r--service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtil.java209
3 files changed, 889 insertions, 0 deletions
diff --git a/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtil.java b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtil.java
new file mode 100644
index 0000000..14df83a
--- /dev/null
+++ b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/JsonUtil.java
@@ -0,0 +1,290 @@
+/*
+ * Copyright 2016 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.vfc.nfvo.multivimproxy.common.util;
+
+import org.onap.vfc.nfvo.multivimproxy.common.constant.ParamConstant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.sf.json.JSONArray;
+import net.sf.json.JSONException;
+import net.sf.json.JSONObject;
+
+/**
+ *
+ * Json Utility Class.<br/>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version VFC 1.0 Sep 10, 2016
+ */
+public final class JsonUtil {
+
+ private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);
+
+ private static final int TYPE_STRING = 0;
+
+ private static final int TYPE_INT = 1;
+
+ private static final int TYPE_JSONA = 2;
+
+ private static final int TYPE_JSONO = 3;
+
+ private static final int TYPE_LONG = 4;
+
+ private JsonUtil() {
+ }
+
+ /**
+ *
+ * Get Json Field String.<br>
+ *
+ * @param jsonObj
+ * @param fieldName
+ * @return
+ * @since VFC 1.0
+ */
+ public static String getJsonFieldStr(JSONObject jsonObj, String fieldName) {
+ return (String)getJsonFieldObject(jsonObj, fieldName, TYPE_STRING);
+ }
+
+ /**
+ *
+ * Get Json Field Integer.<br>
+ *
+ * @param jsonObj
+ * @param fieldName
+ * @return
+ * @since VFC 1.0
+ */
+ public static Integer getJsonFieldInt(JSONObject jsonObj, String fieldName) {
+ return (Integer)getJsonFieldObject(jsonObj, fieldName, TYPE_INT);
+ }
+
+ /**
+ *
+ * Get Json Field array.<br>
+ *
+ * @param jsonObj
+ * @param fieldName
+ * @return
+ * @since VFC 1.0
+ */
+ public static JSONArray getJsonFieldArr(JSONObject jsonObj, String fieldName) {
+ return (JSONArray)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONA);
+ }
+
+ /**
+ *
+ * Get Json Field Json.<br>
+ *
+ * @param jsonObj
+ * @param fieldName
+ * @return
+ * @since VFC 1.0
+ */
+ public static JSONObject getJsonFieldJson(JSONObject jsonObj, String fieldName) {
+ return (JSONObject)getJsonFieldObject(jsonObj, fieldName, TYPE_JSONO);
+ }
+
+ /**
+ *
+ * Get Json Field Long.<br>
+ *
+ * @param jsonObj
+ * @param fieldName
+ * @return
+ * @since VFC 1.0
+ */
+ public static Long getJsonFieldLong(JSONObject jsonObj, String fieldName) {
+ return (Long)getJsonFieldObject(jsonObj, fieldName, TYPE_LONG);
+ }
+
+ /**
+ *
+ * Get Json Field Object.<br>
+ *
+ * @param jsonObj
+ * @param fieldName
+ * @param classType
+ * @return
+ * @since VFC 1.0
+ */
+ private static Object getJsonFieldObject(JSONObject jsonObj, String fieldName, int classType) {
+ try {
+ if(null != jsonObj && jsonObj.has(fieldName)) {
+ Object result;
+ switch(classType) {
+ case TYPE_STRING:
+ result = "null".equals(jsonObj.getString(fieldName)) ? "" : jsonObj.getString(fieldName);
+ break;
+ case TYPE_INT:
+ result = jsonObj.getInt(fieldName);
+ break;
+ case TYPE_JSONA:
+ result = jsonObj.getJSONArray(fieldName);
+ break;
+ case TYPE_JSONO:
+ result = jsonObj.getJSONObject(fieldName);
+ break;
+ case TYPE_LONG:
+ result = jsonObj.getLong(fieldName);
+ break;
+ default:
+ result = null;
+ break;
+ }
+ return result;
+ }
+ } catch(JSONException e) {
+ LOG.error("function=getJsonFieldLong, exception: {} ", e);
+ return null;
+ }
+ return null;
+ }
+
+ /**
+ *
+ * Check whether the Json Object is empty.<br>
+ *
+ * @param jsonObject
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean isNullJson(JSONObject jsonObject) {
+ if(null == jsonObject || jsonObject.isEmpty()) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ *
+ * Get String value by Json.<br>
+ *
+ * @param json
+ * @param key
+ * @return
+ * @since VFC 1.0
+ */
+ public static String getStrValueByjson(JSONObject json, String key) {
+ JSONArray names = json.names();
+ String result = null;
+ for(int i = 0; i < names.size(); i++) {
+ String nodeName = names.getString(i);
+ if(json.optJSONObject(nodeName) != null) {
+ result = getStrValueByjson(json.getJSONObject(nodeName), key);
+ }
+ if(json.optJSONArray(nodeName) != null) {
+ result = getStrValueByJArray(json.getJSONArray(nodeName), key);
+ }
+ if(nodeName.equals(key)) {
+ result = json.getString(nodeName);
+ break;
+ }
+ }
+ return result;
+ }
+
+ private static String getStrValueByJArray(JSONArray json, String key) {
+ String result = null;
+ for(int i = 0; i < json.size(); i++) {
+ if(json.optJSONObject(i) != null) {
+ result = getStrValueByjson(json.getJSONObject(i), key);
+ }
+ if(json.optJSONArray(i) != null) {
+ result = getStrValueByJArray(json.getJSONArray(i), key);
+ }
+ }
+ return result;
+ }
+
+ /**
+ *
+ * Get Json Value by Json object.<br>
+ *
+ * @param json
+ * @param key
+ * @return
+ * @since VFC 1.0
+ */
+ public static JSONObject getJsonValueByjson(JSONObject json, String key) {
+ JSONObject resultJson = new JSONObject();
+ String result = getStrValueByjson(json, key);
+ if(null == result) {
+ return null;
+ }
+ resultJson.element(key, result);
+ return resultJson;
+
+ }
+
+ /**
+ *
+ * Get String Value by Json object.<br>
+ *
+ * @param json
+ * @param parentKey
+ * @param key
+ * @return
+ * @since VFC 1.0
+ */
+ public static String getStrValueByParentJson(JSONObject json, String parentKey, String key) {
+ if(parentKey.isEmpty()) {
+ return getStrValueByjson(json, key);
+ }
+ JSONObject parentJson = getJsonValueByjson(json, parentKey);
+ if(isNullJson(parentJson)) {
+ return null;
+ }
+ return getStrValueByjson(parentJson, key);
+
+ }
+
+ /**
+ *
+ * Get response Data.<br>
+ *
+ * @param obj
+ * @return
+ * @since VFC 1.0
+ */
+ public static JSONObject getResponseData(JSONObject obj) {
+ JSONObject result = new JSONObject();
+
+ Integer retCode = getJsonFieldInt(obj, ParamConstant.PARAM_RETCODE);
+ if(null == retCode || retCode == -1) {
+ return null;
+ }
+
+ if(obj.optJSONObject(ParamConstant.PARAM_DATA) != null) {
+ result = obj.getJSONObject(ParamConstant.PARAM_DATA);
+ } else if(obj.optJSONArray(ParamConstant.PARAM_DATA) != null) {
+ result = obj.getJSONArray(ParamConstant.PARAM_DATA).getJSONObject(0);
+ }
+ if(result.isEmpty()) {
+ return null;
+ }
+
+ if(result.containsKey(ParamConstant.PARAM_RETCODE)) {
+ result = getResponseData(result);
+ }
+ return result;
+ }
+
+}
diff --git a/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtil.java b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtil.java
new file mode 100644
index 0000000..c1c1189
--- /dev/null
+++ b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/RestfulUtil.java
@@ -0,0 +1,390 @@
+/*
+ * Copyright 2016-2017 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.vfc.nfvo.multivimproxy.common.util;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang.StringUtils;
+import org.onap.vfc.nfvo.multivimproxy.common.util.ResourceUtil;
+import org.onap.vfc.nfvo.multivimproxy.common.constant.Constant;
+import org.onap.vfc.nfvo.multivimproxy.common.constant.ParamConstant;
+import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.Restful;
+import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulAsyncCallback;
+import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulFactory;
+import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulOptions;
+import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulParametes;
+import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulResponse;
+import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.ServiceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.sf.json.JSONArray;
+import net.sf.json.JSONException;
+import net.sf.json.JSONObject;
+
+/**
+ * Restful Utility Class.<br>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version VFC 1.0 Sep 10, 2016
+ */
+public class RestfulUtil {
+
+ public static final String TYPE_GET = "get";
+
+ public static final String TYPE_PUT = "put";
+
+ public static final String TYPE_POST = "post";
+
+ public static final String TYPE_DEL = "delete";
+
+ public static final String CONTENT_TYPE = "Content-type";
+
+ public static final String APPLICATION = "application/json";
+
+ public static final String NO_RESULT_EXCEPTION =
+ "org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(RestfulUtil.class);
+
+ private static final Restful REST_CLIENT_HTTP = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
+
+ private static final Restful REST_CLIENT_HTTPS = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTPS);
+
+ private RestfulUtil() {
+ }
+
+ /**
+ * Get response object.<br>
+ *
+ * @param url
+ * @param type
+ * @return
+ * @since VFC 1.0
+ */
+ public static JSONObject getResponseObj(String url, String type) {
+ return getResponseObj(url, new RestfulParametes(), type);
+ }
+
+ /**
+ * Get response object.<br>
+ *
+ * @param url
+ * @param parametes
+ * @param type
+ * @return
+ * @since VFC 1.0
+ */
+ public static JSONObject getResponseObj(String url, RestfulParametes parametes, String type) {
+ try {
+ String content = RestfulUtil.getResponseContent(url, parametes, null, type);
+ LOGGER.error("function=getResponseObj, content : {}", content);
+ if(StringUtils.isEmpty(content)) {
+ return null;
+ }
+ return JSONObject.fromObject(content);
+ } catch(JSONException e) {
+ LOGGER.error("function=getResponseObj, exception : {}", e);
+ return null;
+ }
+ }
+
+ /**
+ * Get response content.<br>
+ *
+ * @param url
+ * @param restParametes
+ * @param type
+ * @return
+ * @since VFC 1.0
+ */
+ public static String getResponseContent(String url, RestfulParametes restParametes, String type) {
+ return getResponseContent(url, restParametes, null, type);
+ }
+
+ /**
+ * Get response map.<br>
+ *
+ * @param url
+ * @param restParametes
+ * @param opt
+ * @param type
+ * @return
+ * @since VFC 1.0
+ */
+ public static Map<String, Object> getResponseMap(String url, RestfulParametes restParametes, RestfulOptions opt,
+ String type) {
+ RestfulResponse response = restfulResponse(url, restParametes, opt, type);
+ return getResponseMap(response);
+ }
+
+ /**
+ * Get response content map.<br>
+ *
+ * @param url
+ * @param type
+ * @return
+ * @since VFC 1.0
+ */
+ public static Map<String, Object> getResponseContentMap(String url, String type) {
+ RestfulResponse response = restfulResponse(url, new RestfulParametes(), null, type);
+ return getResponseMap(response);
+ }
+
+ private static Map<String, Object> getResponseMap(RestfulResponse response) {
+ Map<String, Object> resMap = new HashMap<>(10);
+ if(null != response) {
+ resMap.put(Constant.RESPONSE_CONTENT, response.getResponseContent());
+ resMap.put(Constant.STATUS_CODE, response.getStatus());
+ }
+ return resMap;
+ }
+
+ /**
+ * Get response content.<br>
+ *
+ * @param url
+ * @param restParametes
+ * @param opt
+ * @param type
+ * @return
+ * @since VFC 1.0
+ */
+ public static String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
+ String type) {
+ String responseContent = null;
+ RestfulResponse rsp = restfulResponse(url, restParametes, opt, type);
+ if(rsp != null) {
+ int httpStatus = rsp.getStatus();
+ LOGGER.warn("function=getResponseContent, get response httpStatusCode : {} ", httpStatus);
+ if(httpStatus < HttpServletResponse.SC_BAD_REQUEST && httpStatus > 0) {
+ responseContent = rsp.getResponseContent();
+ LOGGER.warn("function=getResponseContent, get response data success!responseContent={}",
+ responseContent);
+ }
+ }
+ return responseContent;
+ }
+
+ public static RestfulResponse getResponse(String url, RestfulParametes restParametes, RestfulOptions opt,
+ String type) {
+ String responseContent = null;
+ RestfulResponse rsp = restfulResponse(url, restParametes, opt, type);
+ if(rsp != null) {
+ int httpStatus = rsp.getStatus();
+ LOGGER.warn("function=getResponseContent, get response httpStatusCode : {} ", httpStatus);
+ if(httpStatus < HttpServletResponse.SC_BAD_REQUEST && httpStatus > 0) {
+ responseContent = rsp.getResponseContent();
+ LOGGER.warn("function=getResponseContent, get response data success!responseContent={}",
+ responseContent);
+ }
+ }
+ return rsp;
+ }
+
+ /**
+ * Get restful response.<br>
+ *
+ * @param url
+ * @param restParametes
+ * @param type
+ * @return
+ * @since VFC 1.0
+ */
+ public static RestfulResponse getRestfulResponse(String url, RestfulParametes restParametes, String type) {
+ return restfulResponse(url, restParametes, null, type);
+ }
+
+ private static RestfulResponse restfulResponse(String url, RestfulParametes restParametes, RestfulOptions opt,
+ String type) {
+ RestfulResponse rsp = new RestfulResponse();
+ try {
+ Restful restClient = url.startsWith("https") ? REST_CLIENT_HTTPS : REST_CLIENT_HTTP;
+
+ if(restClient != null) {
+ if(TYPE_GET.equals(type)) {
+ rsp = restClient.get(url, restParametes, opt);
+ } else if(TYPE_POST.equals(type)) {
+ rsp = restClient.post(url, restParametes, opt);
+ } else if(TYPE_PUT.equals(type)) {
+ rsp = restClient.put(url, restParametes, opt);
+ } else if(TYPE_DEL.equals(type)) {
+ rsp = restClient.delete(url, restParametes, opt);
+ }
+ }
+ } catch(ServiceException e) {
+ LOGGER.error("function=restfulResponse, get restful response catch exception {} ", e);
+ }
+ LOGGER.warn("function=restfulResponse, response status is {}, context is {} ", rsp.getStatus(),
+ rsp.getResponseContent());
+ return rsp;
+ }
+
+ /**
+ * encapsulate the java reflect exception.<br>
+ *
+ * @param methodName, Restful's method.
+ * @param objects, method param array.
+ * @return
+ * @since VFC 1.0
+ */
+ public static RestfulResponse getRestRes(String methodName, Object... objects) {
+ try {
+ if(objects == null || REST_CLIENT_HTTP == null) {
+ return null;
+ }
+
+ Class<?>[] classes = new Class[objects.length];
+ for(int i = 0; i < objects.length; i++) {
+ classes[i] = objects[i].getClass();
+ }
+ if(methodName.startsWith("async")) {
+ classes[classes.length - 1] = RestfulAsyncCallback.class;
+ }
+
+ Class<?> rtType = methodName.startsWith("async") ? void.class : RestfulResponse.class;
+ MethodType mt = MethodType.methodType(rtType, classes);
+ Object result = MethodHandles.lookup().findVirtual(REST_CLIENT_HTTP.getClass(), methodName, mt)
+ .bindTo(REST_CLIENT_HTTP).invokeWithArguments(objects);
+ if(result != null) {
+ return (RestfulResponse)result;
+ }
+ LOGGER.warn("function=getRestRes, msg: invoke Restful async {} method which return type is Void.",
+ methodName);
+ return null;
+ } catch(ReflectiveOperationException e) {
+ LOGGER.error("function=getRestRes, msg=error occurs, e={}.", e);
+ } catch(Throwable e) {// NOSONAR
+ LOGGER.error("function=getRestRes, msg=Throwable, e={}.", e);
+ try {
+ throw (ServiceException)new ServiceException().initCause(e.getCause());
+ } catch(ServiceException se) {
+ LOGGER.error("function=getRestRes, msg=ServiceException occurs, e={}.", se);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Get response.<br>
+ *
+ * @param restParametes
+ * @param url
+ * @return
+ * @throws ServiceException
+ * @since VFC 1.0
+ */
+ public static JSONArray getResponseRes(RestfulParametes restParametes, String url) throws ServiceException {
+ String result = getResponseContent(url, restParametes, RestfulUtil.TYPE_GET);
+ if(null == result || result.isEmpty()) {
+ LOGGER.error("result from url:" + url + " result:" + result);
+ throw new ServiceException(ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
+ }
+
+ JSONArray rsArray = null;
+ try {
+ JSONObject rsJson = JSONObject.fromObject(result);
+ rsArray = rsJson.getJSONArray(ParamConstant.PARAM_DATA);
+ } catch(JSONException e) {
+ LOGGER.error("getResources error:" + e);
+ throw new ServiceException(ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
+ }
+ return rsArray;
+ }
+
+ /**
+ * Get response.<br>
+ *
+ * @param restParametes
+ * @param url
+ * @param iResName
+ * @return
+ * @throws ServiceException
+ * @since VFC 1.0
+ */
+ public static JSONArray getResponseRes(RestfulParametes restParametes, String url, String iResName)
+ throws ServiceException {
+ String result = getResponseContent(url, restParametes, RestfulUtil.TYPE_GET);
+ if(null == result || result.isEmpty()) {
+ LOGGER.error("result from url:" + url + " result:" + result);
+ throw new ServiceException(ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
+ }
+
+ JSONArray rsArray = null;
+ try {
+ JSONObject rsJson = JSONObject.fromObject(result);
+ rsArray = rsJson.getJSONArray(iResName);
+ String vimId = rsJson.getString(ParamConstant.PARAM_VIMID);
+ String vimName = rsJson.getString(ParamConstant.PARAM_VIMNAME);
+ for(int i = 0; i < rsArray.size(); i++) {
+ JSONObject jsonObj = rsArray.getJSONObject(i);
+ jsonObj.put(ParamConstant.PARAM_VIMID, vimId);
+ jsonObj.put(ParamConstant.PARAM_VIMNAME, vimName);
+ }
+ } catch(JSONException e) {
+ LOGGER.error("getResources error:" + e);
+ throw new ServiceException(ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
+ }
+ return rsArray;
+ }
+
+ /**
+ * <br>
+ *
+ * @param paramsMap
+ * @param params
+ * @return
+ * @since VFC 1.0
+ */
+ public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
+ String url = paramsMap.get("url");
+ String methodType = paramsMap.get("methodType");
+
+ RestfulResponse rsp = null;
+ Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
+ try {
+
+ RestfulParametes restfulParametes = new RestfulParametes();
+ Map<String, String> headerMap = new HashMap<>(3);
+ headerMap.put(CONTENT_TYPE, APPLICATION);
+ restfulParametes.setHeaderMap(headerMap);
+ restfulParametes.setRawData(params);
+
+ if(rest != null) {
+ if(TYPE_GET.equalsIgnoreCase(methodType)) {
+ rsp = rest.get(url, restfulParametes);
+ } else if(TYPE_POST.equalsIgnoreCase(methodType)) {
+ rsp = rest.post(url, restfulParametes);
+ } else if(TYPE_PUT.equalsIgnoreCase(methodType)) {
+ rsp = rest.put(url, restfulParametes);
+ } else if(TYPE_DEL.equalsIgnoreCase(methodType)) {
+ rsp = rest.delete(url, restfulParametes);
+ }
+ }
+ } catch(ServiceException e) {
+ LOGGER.error("function=getRemoteResponse, get restful response catch exception {}", e);
+ }
+ return rsp;
+ }
+}
diff --git a/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtil.java b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtil.java
new file mode 100644
index 0000000..3692d46
--- /dev/null
+++ b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/common/util/StringUtil.java
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2016 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.vfc.nfvo.multivimproxy.common.util;
+
+import java.math.BigDecimal;
+import java.text.DecimalFormat;
+
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * String Utility Class.<br>
+ * <p>
+ * </p>
+ *
+ * @author
+ * @version VFC 1.0 Sep 10, 2016
+ */
+public final class StringUtil {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(StringUtil.class);
+
+ private StringUtil() {
+ }
+
+ /**
+ *
+ * Check whether thestring is valid.<br>
+ *
+ * @param str
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean isValidString(String str) {
+ if(null == str || str.isEmpty()) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ *
+ * Check whether the value is larger than zero.<br>
+ *
+ * @param strs
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean isAnyLargeThanZero(String... strs) {
+ for(String str : strs) {
+ if(!StringUtils.isEmpty(str) && Float.parseFloat(str) > 0) {
+ LOGGER.info("isAnyLargeThanZero : {} is > 0", str);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ *
+ * Check whether the value is Integer.<br>
+ *
+ * @param strs
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean isInteger(String... strs) {
+ try {
+ for(String str : strs) {
+ if(!StringUtils.isEmpty(str) && Integer.parseInt(str) < 0) {
+ return false;
+ }
+ }
+ } catch(NumberFormatException e) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ *
+ * Check whether the input is Numeric.<br>
+ *
+ * @param strs
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean isNumeric(String... strs) {
+ try {
+ for(String str : strs) {
+ if(!StringUtils.isEmpty(str) && Float.parseFloat(str) < 0) {
+ return false;
+ }
+ }
+ } catch(NumberFormatException e) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ *
+ * Compare zero by float.<br>
+ *
+ * @param tatol
+ * @param used
+ * @param drTotal
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean compareZeroByFloat(String tatol, String used, String drTotal) {
+ Float ftotal = (float)0;
+ Float fused = (float)0;
+ Float fdrTotal = (float)0;
+ if(!StringUtils.isEmpty(tatol)) {
+ ftotal = new Float(tatol);
+ }
+ if(!StringUtils.isEmpty(used)) {
+ fused = new Float(used);
+ }
+ if(!StringUtils.isEmpty(drTotal)) {
+ fdrTotal = new Float(drTotal);
+ }
+ if(ftotal < fused + fdrTotal) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ *
+ * Compare zero by integer.<br>
+ *
+ * @param tatol
+ * @param used
+ * @param drTotal
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean compareZeroByInteger(String tatol, String used, String drTotal) {
+ Integer ftotal = 0;
+ Integer fused = 0;
+ Integer fdrTotal = 0;
+ if(!StringUtils.isEmpty(tatol)) {
+ ftotal = Integer.valueOf(tatol);
+ }
+ if(!StringUtils.isEmpty(used)) {
+ fused = Integer.valueOf(used);
+ }
+ if(!StringUtils.isEmpty(drTotal)) {
+ fdrTotal = Integer.valueOf(drTotal);
+ }
+ if(ftotal < fused + fdrTotal) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ *
+ * Number format.<br>
+ *
+ * @param data
+ * @return
+ * @since VFC 1.0
+ */
+ public static String numFormat(String data) {
+ if(null != data && !("".equals(data))) {
+ BigDecimal var = new BigDecimal(data);
+ DecimalFormat formatWithoutFraction = new DecimalFormat("############");
+ DecimalFormat formatWithFraction = new DecimalFormat("############.############");
+ if(new BigDecimal(var.intValue()).compareTo(var) == 0) {
+ return formatWithoutFraction.format(var);
+ }
+ return formatWithFraction.format(var);
+ }
+ return null;
+ }
+
+ /**
+ *
+ * <br>
+ *
+ * @param inputStr
+ * @return
+ * @since VFC 1.0
+ */
+ public static boolean checkXss(String inputStr) {
+ return inputStr.matches("[A-Za-z0-9_.']+");
+ }
+
+}