More license header changes to appc-client files
[appc.git] / appc-client / client-kit / src / test / java / org / onap / appc / generator / JsonHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.generator;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.regex.Pattern;
33
34 public class JsonHelper {
35
36     private enum PopulateType{
37         PARAMETER,
38         SCHEMA_REF,
39         ;
40     }
41
42     public static void populateRpcInputOutputParamsFromJson(final JsonNode jsonNode, Map<String, Set<String>> jsonBodyInputParams, Map<String, Set<String>> jsonBodyOutputParams) {
43         populateRpcInputOutputParamsFromJson(jsonNode,jsonBodyInputParams, jsonBodyOutputParams,PopulateType.PARAMETER,false);
44     }
45     public static void populateRpcInputOutputSchemaRefFromJson(final JsonNode jsonNode, Map<String, Set<String>> jsonBodyInputParams, Map<String, Set<String>> jsonBodyOutputParams) {
46         populateRpcInputOutputParamsFromJson(jsonNode,jsonBodyInputParams, jsonBodyOutputParams,PopulateType.SCHEMA_REF,true);
47     }
48     public static void populateRpcInputOutputParamsFromJson(final JsonNode jsonNode, Map<String, Set<String>> jsonBodyInputParams, Map<String, Set<String>> jsonBodyOutputParams,PopulateType populateType,boolean normalizeRpcOperation) {
49         //get all yang2json Input and output Params and populate jsonBodyInputParams & jsonBodyOutputParams
50         for(Iterator<JsonNode> pathsElements = jsonNode.get("paths").elements(); pathsElements.hasNext(); ){
51             JsonNode pathNode = pathsElements.next();
52             String rpcOperation = pathNode.get("post").get("operationId").textValue();
53             String bodyInputSchemaRef = pathNode.get("post").get("parameters").get(0).get("schema").get("properties").get("input").get("$ref").textValue();
54             String bodyInputSchemaRefSuffix = JsonHelper.getStringSuffix(bodyInputSchemaRef,"/");
55             rpcOperation = normalizeRpcOperation ? rpcOperation.replaceAll(Pattern.quote("-"),"").toLowerCase() : rpcOperation;
56
57             String bodyOutputSchemaRef = pathNode.get("post").get("responses").get("200").get("schema").get("properties").get("output").get("$ref").textValue();
58             String bodyOutputSchemaRefSuffix = JsonHelper.getStringSuffix(bodyOutputSchemaRef,"/");
59
60             Set<String> inputParametersOrSchemaRef;
61             Set<String> outputParametersOrSchemaRef;
62             if(populateType == PopulateType.SCHEMA_REF){
63                 inputParametersOrSchemaRef = new HashSet();
64                 inputParametersOrSchemaRef.add(bodyInputSchemaRefSuffix);
65
66                 outputParametersOrSchemaRef = new HashSet();
67                 outputParametersOrSchemaRef.add(bodyOutputSchemaRefSuffix);
68             }else{
69                 inputParametersOrSchemaRef = extractParams(jsonNode,bodyInputSchemaRefSuffix);
70                 outputParametersOrSchemaRef = extractParams(jsonNode,bodyOutputSchemaRefSuffix);
71             }
72             jsonBodyInputParams.put(rpcOperation,inputParametersOrSchemaRef);
73             jsonBodyOutputParams.put(rpcOperation,outputParametersOrSchemaRef);
74         }
75     }
76
77     private static Set<String> extractParams(final JsonNode jsonNode,String schemaRefName) {
78         Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.get("definitions").get(schemaRefName).get("properties").fields();
79         Set<String> params = new HashSet();
80         for( ;fields.hasNext(); ){
81             Map.Entry<String, JsonNode> fieldEntry = fields.next();
82             params.add(fieldEntry.getKey());
83         }
84         return params;
85     }
86
87     public static String getStringSuffix(String input, String afterString){
88         int indexOf = input.lastIndexOf(afterString);
89         String stringSuffix = (indexOf > -1) ? input.substring(indexOf+1) : input;
90         return stringSuffix;
91     }
92 }