58a2e852f0312c30ed2388f87db9fee9d7819edf
[so.git] / adapters / mso-catalog-db-adapter / src / main / java / org / onap / so / adapters / catalogdb / catalogrest / QueryResourceRecipe.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.adapters.catalogdb.catalogrest;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.so.db.catalog.beans.Recipe;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.SerializationFeature;
32
33 /**
34  * serivce csar query support 
35  * <br>
36  * <p>
37  * </p>
38  * 
39  * @author
40  * @version     ONAP Beijing Release  2018-02-28
41  */
42 public class QueryResourceRecipe extends CatalogQuery{
43     protected static Logger logger = LoggerFactory.getLogger(QueryResourceRecipe.class);
44     
45     private Recipe resourceRecipe;
46     
47     public QueryResourceRecipe(Recipe resourceRecipe){
48         this.resourceRecipe =resourceRecipe;
49     }
50
51     @Override
52     public String toString() {
53
54         return resourceRecipe.toString();
55     }
56
57     @Override
58     public String JSON2(boolean isArray, boolean isEmbed) {
59        Map<String, String> valueMap = new HashMap<>();
60         valueMap.put("id",  null == resourceRecipe || null == resourceRecipe.getId()
61                 ? StringUtils.EMPTY :String.valueOf(resourceRecipe.getId()));
62         valueMap.put("action",  null == resourceRecipe || null == resourceRecipe.getAction()
63                 ? StringUtils.EMPTY :resourceRecipe.getAction());
64         valueMap.put("orchestrationUri", null == resourceRecipe || null == resourceRecipe.getOrchestrationUri()
65                 ? StringUtils.EMPTY : resourceRecipe.getOrchestrationUri());
66         valueMap.put("recipeTimeout", null == resourceRecipe || null == resourceRecipe.getRecipeTimeout()
67                 ? StringUtils.EMPTY : String.valueOf(resourceRecipe.getRecipeTimeout()));
68         valueMap.put("paramXSD", null == resourceRecipe || null == resourceRecipe.getParamXsd()
69                 ? StringUtils.EMPTY : resourceRecipe.getParamXsd());
70         valueMap.put("description", null == resourceRecipe || null == resourceRecipe.getDescription()
71                 ? StringUtils.EMPTY : resourceRecipe.getDescription());
72         ObjectMapper mapper = new ObjectMapper();
73         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
74         String jsonStr = "";
75         try {
76             jsonStr = mapper.writeValueAsString(valueMap);
77         } catch(JsonProcessingException e) {
78             logger.error("Error creating JSON", e);
79         }
80         return jsonStr;
81     }
82
83 }