update to swagger generation for WAPIml loading
[aai/schema-service.git] / aai-schema-gen / src / main / java / org / onap / aai / schemagen / genxsd / GetOperation.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.schemagen.genxsd;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.onap.aai.schemagen.GenerateXsd;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.StringTokenizer;
28 import java.util.Vector;
29
30 public class GetOperation {
31         static Map<String, Vector<String>> containers = new HashMap<String, Vector<String>>();
32         public static void addContainerProps(String container, Vector<String> containerProps) {
33                 containers.put(container, containerProps);;
34         }
35         private String useOpId;
36         private String xmlRootElementName;
37         private String tag;
38         private String path;
39         private String pathParams;
40         private String queryParams;
41
42                 public GetOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams) {
43                         super();
44                         this.useOpId = useOpId;
45                         this.xmlRootElementName = xmlRootElementName;
46                         this.tag = tag;
47                         this.path = path;
48                         this.pathParams = pathParams;
49 //                      StringBuilder p = new StringBuilder();
50
51                         if(containers.get(xmlRootElementName) == null) {
52                                 this.queryParams = "";
53                         } else {
54                                 this.queryParams= String.join("", containers.get(xmlRootElementName));
55 //                              for(String param : containers.get(xmlRootElementName)) {
56 //                                      p.append(param);
57 //                              }
58 //                              this.queryParams = p.toString();
59                         }
60                 }
61                 @Override
62                 public String toString() {
63                         StringTokenizer st;
64                         st = new StringTokenizer(path, "/");
65                         //Path has to be longer than one element
66                         /*
67                         if ( st.countTokens() <= 1) {
68                                 return "";
69                         }
70                         */
71                         //a valid tag is necessary
72                         if ( StringUtils.isEmpty(tag) ) {
73                                 return "";
74                         }
75                         if ( path.endsWith("/relationship") ) {
76                                 return "";
77                         }
78                         if ( path.contains("/relationship/") ) { // filter paths with relationship-list
79                                 return "";
80                         }
81                         if ( path.endsWith("/relationship-list")) {
82                                 return "";
83                         }
84                         if ( path.startsWith("/search")) {
85                                 return "";
86                         }
87                         StringBuffer pathSb = new StringBuffer();
88                         pathSb.append("  " + path + ":\n" );
89                         pathSb.append("    get:\n");
90                         pathSb.append("      tags:\n");
91                         pathSb.append("        - " + tag + "\n");
92                         pathSb.append("      summary: returns " + xmlRootElementName + "\n");
93
94                         pathSb.append("      description: returns " + xmlRootElementName + "\n");
95                         pathSb.append("      operationId: get" + useOpId + "\n");
96                         pathSb.append("      produces:\n");
97                         pathSb.append("        - application/json\n");
98                         pathSb.append("        - application/xml\n");
99
100                         pathSb.append("      responses:\n");
101                         pathSb.append("        \"200\":\n");
102                         pathSb.append("          description: successful operation\n");
103                         pathSb.append("          schema:\n");
104                         pathSb.append("              $ref: \"#/definitions/" + xmlRootElementName + "\"\n");
105                         pathSb.append("        \"default\":\n");
106                         pathSb.append("          " + GenerateXsd.getResponsesUrl());
107                         if ( StringUtils.isNotEmpty(pathParams) || StringUtils.isNotEmpty(queryParams)) {
108                                 pathSb.append("      parameters:\n");
109                         }
110                         if ( StringUtils.isNotEmpty(pathParams)) {
111                                 pathSb.append(pathParams);
112                         }
113 //                      if ( StringUtils.isNotEmpty(pathParams) && StringUtils.isNotEmpty(queryParams)) {
114 //                              pathSb.append("\n");
115 //                      }
116                         if ( StringUtils.isNotEmpty(queryParams)) {
117                                 pathSb.append(queryParams);
118                         }
119                         return pathSb.toString();
120                 }
121         }