d1e7ca53b05fb641d39b580f213bc644930c5dc3
[aai/schema-service.git] / aai-schema-gen / src / main / java / org / onap / aai / schemagen / 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  * <p>
13  * http://www.apache.org/licenses/LICENSE-2.0
14  * <p>
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
23 package org.onap.aai.schemagen.genxsd;
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 PutOperation {
30     public static final String RELATIONSHIP = "relationship";
31     private String useOpId;
32     private String xmlRootElementName;
33     private String tag;
34     private String path;
35     private String pathParams;
36     private SchemaVersion version;
37     private String basePath;
38
39     public PutOperation(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.version = v;
48         this.basePath = basePath;
49     }
50
51     @Override
52     public String toString() {
53         //a valid tag is necessary
54         if (StringUtils.isEmpty(tag)) {
55             return "";
56         }
57         //All Put operation paths end with "relationship"
58         //or there is a parameter at the end of the path
59         //and there is a parameter in the path
60         if (path.contains("/" + RELATIONSHIP + "/")) { // filter paths with relationship-list
61             return "";
62         }
63         if (path.endsWith("/" + RELATIONSHIP + "-list")) {
64             return "";
65         }
66         if (!path.endsWith("/" + RELATIONSHIP) && !path.endsWith("}")) {
67             return "";
68         }
69         if (path.startsWith("/search")) {
70             return "";
71         }
72         StringBuilder pathSb = new StringBuilder();
73         StringBuilder relationshipExamplesSb = new StringBuilder();
74         if (path.endsWith("/" + RELATIONSHIP)) {
75             pathSb.append("  ").append(path).append(":\n");
76         }
77         pathSb.append("    put:\n");
78         pathSb.append("      tags:\n");
79         pathSb.append("        - ").append(tag).append("\n");
80
81         if (path.endsWith("/" + RELATIONSHIP)) {
82             pathSb.append("      summary: see node definition for valid relationships\n");
83         } else {
84             pathSb.append("      summary: create or update an existing ")
85                 .append(xmlRootElementName).append("\n");
86             pathSb.append("      description: |\n        Create or update an existing ")
87                 .append(xmlRootElementName).append(
88                 ".\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");
89         }
90         relationshipExamplesSb.append("[Valid relationship examples shown here](apidocs")
91             .append(basePath).append("/relations/").append(version.toString()).append("/")
92             .append(useOpId.replace("RelationshipListRelationship", "")).append(".json)");
93         pathSb.append("      operationId: createOrUpdate").append(useOpId).append("\n");
94         pathSb.append("      consumes:\n");
95         pathSb.append("        - application/json\n");
96         pathSb.append("        - application/xml\n");
97         pathSb.append("      produces:\n");
98         pathSb.append("        - application/json\n");
99         pathSb.append("        - application/xml\n");
100         pathSb.append("      responses:\n");
101         pathSb.append("        \"default\":\n");
102         pathSb.append("          ").append(GenerateXsd.getResponsesUrl());
103
104         pathSb.append("      parameters:\n");
105         pathSb.append(pathParams); // for nesting
106         pathSb.append("        - name: body\n");
107         pathSb.append("          in: body\n");
108         pathSb.append("          description: ").append(xmlRootElementName)
109             .append(" object that needs to be created or updated. ")
110             .append(relationshipExamplesSb.toString()).append("\n");
111         pathSb.append("          required: true\n");
112         pathSb.append("          schema:\n");
113         String useElement = xmlRootElementName;
114         if (xmlRootElementName.equals("relationship")) {
115             useElement += "-dict";
116         }
117         pathSb.append("            $ref: \"#/definitions/").append(useElement).append("\"\n");
118         this.tagRelationshipPathMapEntry();
119         return pathSb.toString();
120     }
121
122     public String tagRelationshipPathMapEntry() {
123         if (path.endsWith("/" + RELATIONSHIP)) {
124             PutRelationPathSet.add(useOpId, path);
125         }
126         return "";
127     }
128
129 }