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