6d9bf628fa26a3e752d215feaf1c6120077e8286
[appc.git] / appc-client / client-kit / src / test / java / org / openecomp / appc / generator / JsonHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.generator;
26
27 import com.fasterxml.jackson.databind.JsonNode;
28
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.regex.Pattern;
34
35 public class JsonHelper {
36
37     private enum PopulateType{
38         PARAMETER,
39         SCHEMA_REF,
40         ;
41     }
42
43     public static void populateRpcInputOutputParamsFromJson(final JsonNode jsonNode, Map<String, Set<String>> jsonBodyInputParams, Map<String, Set<String>> jsonBodyOutputParams) {
44         populateRpcInputOutputParamsFromJson(jsonNode,jsonBodyInputParams, jsonBodyOutputParams,PopulateType.PARAMETER,false);
45     }
46     public static void populateRpcInputOutputSchemaRefFromJson(final JsonNode jsonNode, Map<String, Set<String>> jsonBodyInputParams, Map<String, Set<String>> jsonBodyOutputParams) {
47         populateRpcInputOutputParamsFromJson(jsonNode,jsonBodyInputParams, jsonBodyOutputParams,PopulateType.SCHEMA_REF,true);
48     }
49     public static void populateRpcInputOutputParamsFromJson(final JsonNode jsonNode, Map<String, Set<String>> jsonBodyInputParams, Map<String, Set<String>> jsonBodyOutputParams,PopulateType populateType,boolean normalizeRpcOperation) {
50         //get all yang2json Input and output Params and populate jsonBodyInputParams & jsonBodyOutputParams
51         for(Iterator<JsonNode> pathsElements = jsonNode.get("paths").elements(); pathsElements.hasNext(); ){
52             JsonNode pathNode = pathsElements.next();
53             String rpcOperation = pathNode.get("post").get("operationId").textValue();
54             String bodyInputSchemaRef = pathNode.get("post").get("parameters").get(0).get("schema").get("properties").get("input").get("$ref").textValue();
55             String bodyInputSchemaRefSuffix = JsonHelper.getStringSuffix(bodyInputSchemaRef,"/");
56             rpcOperation = normalizeRpcOperation ? rpcOperation.replaceAll(Pattern.quote("-"),"").toLowerCase() : rpcOperation;
57
58             String bodyOutputSchemaRef = pathNode.get("post").get("responses").get("200").get("schema").get("properties").get("output").get("$ref").textValue();
59             String bodyOutputSchemaRefSuffix = JsonHelper.getStringSuffix(bodyOutputSchemaRef,"/");
60
61             Set<String> inputParametersOrSchemaRef;
62             Set<String> outputParametersOrSchemaRef;
63             if(populateType == PopulateType.SCHEMA_REF){
64                 inputParametersOrSchemaRef = new HashSet();
65                 inputParametersOrSchemaRef.add(bodyInputSchemaRefSuffix);
66
67                 outputParametersOrSchemaRef = new HashSet();
68                 outputParametersOrSchemaRef.add(bodyOutputSchemaRefSuffix);
69             }else{
70                 inputParametersOrSchemaRef = extractParams(jsonNode,bodyInputSchemaRefSuffix);
71                 outputParametersOrSchemaRef = extractParams(jsonNode,bodyOutputSchemaRefSuffix);
72             }
73             jsonBodyInputParams.put(rpcOperation,inputParametersOrSchemaRef);
74             jsonBodyOutputParams.put(rpcOperation,outputParametersOrSchemaRef);
75         }
76     }
77
78     private static Set<String> extractParams(final JsonNode jsonNode,String schemaRefName) {
79         Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.get("definitions").get(schemaRefName).get("properties").fields();
80         Set<String> params = new HashSet();
81         for( ;fields.hasNext(); ){
82             Map.Entry<String, JsonNode> fieldEntry = fields.next();
83             params.add(fieldEntry.getKey());
84         }
85         return params;
86     }
87
88     public static String getStringSuffix(String input, String afterString){
89         int indexOf = input.lastIndexOf(afterString);
90         String stringSuffix = (indexOf > -1) ? input.substring(indexOf+1) : input;
91         return stringSuffix;
92     }
93 }