Move the aai-schema, annotations and
[aai/schema-service.git] / aai-schema-gen / src / main / java / org / onap / aai / schemagen / genxsd / PatchOperation.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.schemagen.genxsd;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.onap.aai.schemagen.GenerateXsd;
24
25 import java.util.StringTokenizer;
26
27 public class PatchOperation {
28         private String useOpId;
29         private String xmlRootElementName;
30         private String tag;
31         private String path;
32         private String pathParams;
33
34                 public PatchOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams) {
35                         super();
36                         this.useOpId = useOpId;
37                         this.xmlRootElementName = xmlRootElementName;
38                         this.tag = tag;
39                         this.path = path;
40                         this.pathParams = pathParams;
41                 }
42
43                 public String toString() {
44                         StringTokenizer st;
45                         st = new StringTokenizer(path, "/");
46                         //a valid tag is necessary
47                         if ( StringUtils.isEmpty(tag) ) {
48                                 return "";
49                         }
50                         if ( path.contains("/relationship/") ) { // filter paths with relationship-list
51                                 return "";
52                         }
53                         if ( path.endsWith("/relationship-list")) {
54                                 return "";
55                         }
56                         if ( path.startsWith("/search")) {
57                                 return "";
58                         }
59                         //No Patch operation paths end with "relationship"
60
61                         if (path.endsWith("/relationship") ) {
62                                 return "";
63                         }
64                         if (!path.endsWith("}")) {
65                                 return "";
66                         }
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("    patch:\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                                 relationshipExamplesSb.append("[See Examples](apidocs/relations/"+GenerateXsd.getAPIVersion()+"/"+useOpId+".json)");
80                         } else {
81                                 pathSb.append("      summary: update an existing " + xmlRootElementName + "\n");
82                                 pathSb.append("      description: |\n");
83                                 pathSb.append("        Update an existing " + xmlRootElementName + "\n");
84                                 pathSb.append("        #\n");
85                                 pathSb.append("        Note:  Endpoints that are not devoted to object relationships support both PUT and PATCH operations.\n");
86                                 pathSb.append("        The PUT operation will entirely replace an existing object.\n");
87                                 pathSb.append("        The PATCH operation sends a \"description of changes\" for an existing object.  The entire set of changes must be applied.  An error result means no change occurs.\n");
88                                 pathSb.append("        #\n");
89                                 pathSb.append("        Other differences between PUT and PATCH are:\n");
90                                 pathSb.append("        #\n");
91                                 pathSb.append("        - For PATCH, you can send any of the values shown in sample REQUEST body.  There are no required values.\n");
92                                 pathSb.append("        - For PATCH, resource-id which is a required REQUEST body element for PUT, must not be sent.\n");
93                                 pathSb.append("        - PATCH cannot be used to update relationship elements; there are dedicated PUT operations for this.\n");
94                         }
95                         pathSb.append("      operationId: Update" + useOpId + "\n");
96                         pathSb.append("      consumes:\n");
97                         pathSb.append("        - application/json\n");
98                         pathSb.append("      produces:\n");
99                         pathSb.append("        - application/json\n");
100                         pathSb.append("      responses:\n");
101                         pathSb.append("        \"default\":\n");
102                         pathSb.append("          " + GenerateXsd.getResponsesUrl());
103                         pathSb.append("      parameters:\n");
104                         pathSb.append(pathParams); // for nesting
105                         pathSb.append("        - name: body\n");
106                         pathSb.append("          in: body\n");
107                         pathSb.append("          description: " + xmlRootElementName + " object that needs to be updated."+relationshipExamplesSb.toString()+"\n");
108                         pathSb.append("          required: true\n");
109                         pathSb.append("          schema:\n");
110                         pathSb.append("            $ref: \"#/patchDefinitions/" + xmlRootElementName + "\"\n");
111                         return pathSb.toString();
112                 }
113         }