Sonar issue fixes for PutOperation.java
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / genxsd / PutOperation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright © 2018 IBM.
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  * ============LICENSE_END=========================================================
21  */
22 package org.onap.aai.util.genxsd;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.aai.setup.SchemaVersion;
26 import org.onap.aai.util.GenerateXsd;
27
28 public class PutOperation {
29     public static final String RELATIONSHIP = "relationship";
30     private String useOpId;
31     private String xmlRootElementName;
32     private String tag;
33     private String path;
34     private String pathParams;
35     private SchemaVersion version;
36
37         public PutOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams, SchemaVersion v) {
38             super();
39             this.useOpId = useOpId;
40             this.xmlRootElementName = xmlRootElementName;
41             this.tag = tag;
42             this.path = path;
43             this.pathParams = pathParams;
44             this.version = v;
45         }
46
47         @Override
48         public String toString() {
49             //a valid tag is necessary
50             if ( StringUtils.isEmpty(tag) ) {
51                 return "";
52             }
53             //All Put operation paths end with "relationship"
54             //or there is a parameter at the end of the path
55             //and there is a parameter in the path
56             if ( path.contains("/"+RELATIONSHIP+"/") ) { // filter paths with relationship-list
57                 return "";
58             }
59             if ( path.endsWith("/"+RELATIONSHIP+"-list")) {
60                 return "";
61             }
62             if ( !path.endsWith("/"+RELATIONSHIP)  &&  !path.endsWith("}") ) {
63                 return "";
64             }
65             if ( path.startsWith("/search")) {
66                 return "";
67             }
68             StringBuffer pathSb = new StringBuffer();
69             StringBuffer relationshipExamplesSb = new StringBuffer();
70             if ( path.endsWith("/"+RELATIONSHIP) ) {
71                 pathSb.append("  " + path + ":\n" );
72             }
73             pathSb.append("    put:\n");
74             pathSb.append("      tags:\n");
75             pathSb.append("        - " + tag + "\n");
76
77             if ( path.endsWith("/"+RELATIONSHIP) ) {
78                 pathSb.append("      summary: see node definition for valid relationships\n");
79             } else {
80                 pathSb.append("      summary: create or update an existing " + xmlRootElementName + "\n");
81                 pathSb.append("      description: |\n        Create or update an existing " + xmlRootElementName + ".\n        #\n        Note! This PUT method has a corresponding PATCH method that can be used to update just a few of the fields of an existing object, rather than a full object replacement.  An example can be found in the [PATCH section] below\n");
82             }
83             relationshipExamplesSb.append("[Valid relationship examples shown here](apidocs/relations/"+version.toString()+"/"+useOpId.replace("RelationshipListRelationship", "")+".json)");
84             pathSb.append("      operationId: createOrUpdate" + useOpId + "\n");
85             pathSb.append("      consumes:\n");
86             pathSb.append("        - application/json\n");
87             pathSb.append("        - application/xml\n");                    
88             pathSb.append("      produces:\n");
89             pathSb.append("        - application/json\n");
90             pathSb.append("        - application/xml\n");
91             pathSb.append("      responses:\n");
92             pathSb.append("        \"default\":\n");
93             pathSb.append("          " + GenerateXsd.getResponsesUrl());
94         
95             pathSb.append("      parameters:\n");
96             pathSb.append(pathParams); // for nesting
97             pathSb.append("        - name: body\n");
98             pathSb.append("          in: body\n");
99             pathSb.append("          description: " + xmlRootElementName + " object that needs to be created or updated. "+relationshipExamplesSb.toString()+"\n");
100             pathSb.append("          required: true\n");
101             pathSb.append("          schema:\n");
102             pathSb.append("            $ref: \"#/definitions/" + xmlRootElementName + "\"\n");
103             this.tagRelationshipPathMapEntry();
104             return pathSb.toString();
105         }
106
107         public String tagRelationshipPathMapEntry() {
108             if ( path.endsWith("/"+RELATIONSHIP) ) {
109                 PutRelationPathSet.add(useOpId, path);
110             }
111             return "";
112         }
113         
114     }