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