b52e34bad550ce76d19958478e5e3fa6363b9f98
[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 import org.apache.commons.lang3.StringUtils;
25 import org.onap.aai.schemagen.GenerateXsd;
26 import org.onap.aai.setup.SchemaVersion;
27
28 public class PatchOperation {
29     private String useOpId;
30     private String xmlRootElementName;
31     private String tag;
32     private String path;
33     private String pathParams;
34     private String prefixForPatch;
35     private SchemaVersion version;
36     private String basePath;
37
38     public PatchOperation(String useOpId, String xmlRootElementName, String tag, String path,
39                           String pathParams, SchemaVersion v, String basePath) {
40         super();
41         this.useOpId = useOpId;
42         this.xmlRootElementName = xmlRootElementName;
43         this.tag = tag;
44         this.path = path;
45         this.pathParams = pathParams;
46         this.prefixForPatch = "";
47         this.version = v;
48         this.basePath = basePath;
49     }
50
51     public void setPrefixForPatchRef(String prefixForPatchRef) {
52         this.prefixForPatch = prefixForPatchRef;
53     }
54
55     public String toString() {
56         StringTokenizer st;
57         st = new StringTokenizer(path, "/");
58         //a valid tag is necessary
59         if (StringUtils.isEmpty(tag)) {
60             return "";
61         }
62         if (path.contains("/relationship/")) { // filter paths with relationship-list
63             return "";
64         }
65         if (path.endsWith("/relationship-list")) {
66             return "";
67         }
68         if (path.startsWith("/search")) {
69             return "";
70         }
71         //No Patch operation paths end with "relationship"
72
73         if (path.endsWith("/relationship")) {
74             return "";
75         }
76         if (!path.endsWith("}")) {
77             return "";
78         }
79
80         StringBuilder pathSb = new StringBuilder();
81         StringBuilder relationshipExamplesSb = new StringBuilder();
82         if (path.endsWith("/relationship")) {
83             pathSb.append("  ").append(path).append(":\n");
84         }
85         pathSb.append("    patch:\n");
86         pathSb.append("      tags:\n");
87         pathSb.append("        - ").append(tag).append("\n");
88
89         if (path.endsWith("/relationship")) {
90             pathSb.append("      summary: see node definition for valid relationships\n");
91         } else {
92             relationshipExamplesSb.append("[See Examples](apidocs").append(basePath)
93                 .append("/relations/").append(version.toString()).append("/").append(useOpId)
94                 .append(".json)");
95             pathSb.append("      summary: update an existing ").append(xmlRootElementName)
96                 .append("\n");
97             pathSb.append("      description: |\n");
98             pathSb.append("        Update an existing ").append(xmlRootElementName)
99                 .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.")
130             .append(relationshipExamplesSb.toString()).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 }