2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.generator;
26 import com.fasterxml.jackson.databind.JsonNode;
28 import java.util.HashSet;
29 import java.util.Iterator;
32 import java.util.regex.Pattern;
34 public class JsonHelper {
36 private enum PopulateType{
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);
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);
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;
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,"/");
60 Set<String> inputParametersOrSchemaRef;
61 Set<String> outputParametersOrSchemaRef;
62 if(populateType == PopulateType.SCHEMA_REF){
63 inputParametersOrSchemaRef = new HashSet();
64 inputParametersOrSchemaRef.add(bodyInputSchemaRefSuffix);
66 outputParametersOrSchemaRef = new HashSet();
67 outputParametersOrSchemaRef.add(bodyOutputSchemaRefSuffix);
69 inputParametersOrSchemaRef = extractParams(jsonNode,bodyInputSchemaRefSuffix);
70 outputParametersOrSchemaRef = extractParams(jsonNode,bodyOutputSchemaRefSuffix);
72 jsonBodyInputParams.put(rpcOperation,inputParametersOrSchemaRef);
73 jsonBodyOutputParams.put(rpcOperation,outputParametersOrSchemaRef);
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());
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;