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