2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.generator;
27 import com.fasterxml.jackson.databind.JsonNode;
29 import java.util.HashSet;
30 import java.util.Iterator;
33 import java.util.regex.Pattern;
35 public class JsonHelper {
37 private enum PopulateType{
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);
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);
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;
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,"/");
61 Set<String> inputParametersOrSchemaRef;
62 Set<String> outputParametersOrSchemaRef;
63 if(populateType == PopulateType.SCHEMA_REF){
64 inputParametersOrSchemaRef = new HashSet();
65 inputParametersOrSchemaRef.add(bodyInputSchemaRefSuffix);
67 outputParametersOrSchemaRef = new HashSet();
68 outputParametersOrSchemaRef.add(bodyOutputSchemaRefSuffix);
70 inputParametersOrSchemaRef = extractParams(jsonNode,bodyInputSchemaRefSuffix);
71 outputParametersOrSchemaRef = extractParams(jsonNode,bodyOutputSchemaRefSuffix);
73 jsonBodyInputParams.put(rpcOperation,inputParametersOrSchemaRef);
74 jsonBodyOutputParams.put(rpcOperation,outputParametersOrSchemaRef);
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());
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;