Add instructions to invoke the linter and code formatter plugins to the README 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  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
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
21 package org.onap.aai.schemagen.genxsd;
22
23 import java.util.StringTokenizer;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.aai.schemagen.GenerateXsd;
27 import org.onap.aai.setup.SchemaVersion;
28
29 public class PatchOperation {
30     private String useOpId;
31     private String xmlRootElementName;
32     private String tag;
33     private String path;
34     private String pathParams;
35     private String prefixForPatch;
36     private SchemaVersion version;
37     private String basePath;
38
39     public PatchOperation(String useOpId, String xmlRootElementName, String tag, String path,
40         String pathParams, SchemaVersion v, String basePath) {
41         super();
42         this.useOpId = useOpId;
43         this.xmlRootElementName = xmlRootElementName;
44         this.tag = tag;
45         this.path = path;
46         this.pathParams = pathParams;
47         this.prefixForPatch = "";
48         this.version = v;
49         this.basePath = basePath;
50     }
51
52     public void setPrefixForPatchRef(String prefixForPatchRef) {
53         this.prefixForPatch = prefixForPatchRef;
54     }
55
56     public String toString() {
57         StringTokenizer st;
58         st = new StringTokenizer(path, "/");
59         // a valid tag is necessary
60         if (StringUtils.isEmpty(tag)) {
61             return "";
62         }
63         if (path.contains("/relationship/")) { // filter paths with relationship-list
64             return "";
65         }
66         if (path.endsWith("/relationship-list")) {
67             return "";
68         }
69         if (path.startsWith("/search")) {
70             return "";
71         }
72         // No Patch operation paths end with "relationship"
73
74         if (path.endsWith("/relationship")) {
75             return "";
76         }
77         if (!path.endsWith("}")) {
78             return "";
79         }
80
81         StringBuilder pathSb = new StringBuilder();
82         StringBuilder relationshipExamplesSb = new StringBuilder();
83         if (path.endsWith("/relationship")) {
84             pathSb.append("  ").append(path).append(":\n");
85         }
86         pathSb.append("    patch:\n");
87         pathSb.append("      tags:\n");
88         pathSb.append("        - ").append(tag).append("\n");
89
90         if (path.endsWith("/relationship")) {
91             pathSb.append("      summary: see node definition for valid relationships\n");
92         } else {
93             relationshipExamplesSb.append("[See Examples](apidocs").append(basePath)
94                 .append("/relations/").append(version.toString()).append("/").append(useOpId)
95                 .append(".json)");
96             pathSb.append("      summary: update an existing ").append(xmlRootElementName)
97                 .append("\n");
98             pathSb.append("      description: |\n");
99             pathSb.append("        Update an existing ").append(xmlRootElementName).append("\n");
100             pathSb.append("        #\n");
101             pathSb.append(
102                 "        Note:  Endpoints that are not devoted to object relationships support both PUT and PATCH operations.\n");
103             pathSb.append("        The PUT operation will entirely replace an existing object.\n");
104             pathSb.append(
105                 "        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");
106             pathSb.append("        #\n");
107             pathSb.append("        Other differences between PUT and PATCH are:\n");
108             pathSb.append("        #\n");
109             pathSb.append(
110                 "        - For PATCH, you can send any of the values shown in sample REQUEST body.  There are no required values.\n");
111             pathSb.append(
112                 "        - For PATCH, resource-id which is a required REQUEST body element for PUT, must not be sent.\n");
113             pathSb.append(
114                 "        - PATCH cannot be used to update relationship elements; there are dedicated PUT operations for this.\n");
115         }
116         pathSb.append("      operationId: Update").append(useOpId).append("\n");
117         pathSb.append("      consumes:\n");
118         pathSb.append("        - application/json\n");
119         pathSb.append("      produces:\n");
120         pathSb.append("        - application/json\n");
121         pathSb.append("      responses:\n");
122         pathSb.append("        \"default\":\n");
123         pathSb.append("          ").append(GenerateXsd.getResponsesUrl());
124         pathSb.append("      parameters:\n");
125         pathSb.append(pathParams); // for nesting
126         pathSb.append("        - name: body\n");
127         pathSb.append("          in: body\n");
128         pathSb.append("          description: ").append(xmlRootElementName)
129             .append(" object that needs to be updated.").append(relationshipExamplesSb.toString())
130             .append("\n");
131         pathSb.append("          required: true\n");
132         pathSb.append("          schema:\n");
133         pathSb.append("            $ref: \"#/definitions/").append(prefixForPatch)
134             .append(xmlRootElementName).append("\"\n");
135         return pathSb.toString();
136     }
137 }