1043e7a78d5a6bb043207da4686b6d145abac2ac
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.res.common.util.response;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.onap.vfc.nfvo.res.common.constant.HttpConstant;
24 import org.onap.vfc.nfvo.res.common.constant.ParamConstant;
25 import org.onap.vfc.nfvo.res.common.constant.ResponseConstant;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import net.sf.json.JSONObject;
30
31 /**
32  * <br/>
33  * <p>
34  * Utility for generate Roa get/add/update/delete method status
35  * </p>
36  *
37  * @author
38  * @version NFVO 0.5 2016-3-17
39  */
40 public final class RoaResponseUtil {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(RoaResponseUtil.class);
43
44     private RoaResponseUtil() {
45
46     }
47
48     /**
49      * Generate get method response<br/>
50      *
51      * @param list
52      *            The basic response for get method
53      * @return JSONObject The response for http request
54      * @since NFVO 0.5
55      */
56     public static <T> JSONObject get(List<T> list) {
57         Map<String, Object> map = new HashMap<String, Object>(10);
58         map.put(ParamConstant.PARAM_DATA, list);
59         LOGGER.info("function=get; msg=get map:{}", map.toString());
60         return ResponseUtil.genHttpResponse(HttpConstant.OK_CODE, ResponseConstant.QUERY_SUCESS_MSG, map);
61     }
62
63     /**
64      * Generate different response by different parameter for add method <br/>
65      *
66      * @param result
67      *            The result
68      * @return JSONObject
69      * @since NFVO 0.5
70      */
71     public static JSONObject add(int result) {
72         LOGGER.info("function=add; msg=add result{}", result);
73         if(result <= 0) {
74             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, ResponseConstant.ADD_FAIL_MSG);
75         } else {
76             return ResponseUtil.genHttpResponse(HttpConstant.OK_CODE, ResponseConstant.ADD_SUCESS_MSG);
77         }
78     }
79
80     /**
81      * Generate different response by different parameter for update method <br/>
82      *
83      * @param result
84      *            The result
85      * @return JSONObject The response for http request
86      * @since NFVO 0.5
87      */
88     public static JSONObject update(int result) {
89         LOGGER.info("function=update; msg=update result{}", result);
90         if(result <= 0) {
91             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, ResponseConstant.MOD_FAIL_MSG);
92         } else {
93             return ResponseUtil.genHttpResponse(HttpConstant.OK_CODE, ResponseConstant.MOD_SUCESS_MSG);
94         }
95     }
96
97     /**
98      * Generate different response by different parameter for delete method <br/>
99      *
100      * @param result
101      *            The result
102      * @return delete JSONObject of the response for http request
103      * @since NFVO 0.5
104      */
105     public static JSONObject delete(int result) {
106         LOGGER.info("function=delete; msg=delete result{}", result);
107         if(result <= 0) {
108             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, ResponseConstant.DEL_FAIL_MSG);
109         } else {
110             return ResponseUtil.genHttpResponse(HttpConstant.OK_CODE, ResponseConstant.DEL_SUCESS_MSG);
111         }
112     }
113 }