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 / NodeGetOperation.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.ArrayList;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.StringTokenizer;
27 import java.util.Vector;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.aai.schemagen.GenerateXsd;
31
32 public class NodeGetOperation {
33     static Map<String, Vector<String>> containers = new HashMap<String, Vector<String>>();
34     static ArrayList<String> checklist = createChecklist();
35
36     private static ArrayList<String> createChecklist() {
37         ArrayList<String> list = new ArrayList<String>();
38         return list;
39     }
40
41     public static void addContainerProps(String container, Vector<String> containerProps) {
42         containers.put(container, containerProps);
43     }
44
45     public static void resetContainers() {
46         containers = new HashMap<String, Vector<String>>();
47         checklist = createChecklist();
48     }
49
50     private String useOpId;
51     private String xmlRootElementName;
52     private String tag;
53     private String path;
54     private String CRUDpath;
55     private String pathParams;
56     private String queryParams;
57
58     public NodeGetOperation(String useOpId, String xmlRootElementName, String tag, String path,
59         String pathParams) {
60         super();
61         this.useOpId = useOpId;
62         this.xmlRootElementName = xmlRootElementName;
63         this.tag = tag;
64         this.CRUDpath = path;
65         this.path = nodePath();
66         this.pathParams = pathParams;
67         StringBuilder p = new StringBuilder();
68
69         if (containers.get(xmlRootElementName) == null) {
70             this.queryParams = "";
71         } else {
72             this.queryParams = String.join("", containers.get(xmlRootElementName));
73             for (String param : containers.get(xmlRootElementName)) {
74                 p.append(param);
75             }
76             this.queryParams = p.toString();
77         }
78     }
79
80     String nodePath() {
81         String path = null;
82         int loc = CRUDpath.indexOf(xmlRootElementName);
83         if (loc > 0) {
84             path = "/nodes/" + CRUDpath.substring(loc);
85         }
86         return path;
87     }
88
89     @Override
90     public String toString() {
91         StringTokenizer st;
92         st = new StringTokenizer(CRUDpath, "/");
93         // Path has to be longer than one element
94         /*
95          * if ( st.countTokens() <= 1) {
96          * return "";
97          * }
98          */
99         // a valid tag is necessary
100         if (StringUtils.isEmpty(tag)) {
101             return "";
102         }
103         if (CRUDpath.endsWith("/relationship")) {
104             return "";
105         }
106         if (CRUDpath.contains("/relationship/")) { // filter paths with relationship-list
107             return "";
108         }
109         if (CRUDpath.endsWith("/relationship-list")) {
110             return "";
111         }
112         if (CRUDpath.startsWith("/search")) {
113             return "";
114         }
115         if (CRUDpath.startsWith("/actions")) {
116             return "";
117         }
118         if (CRUDpath.startsWith("/nodes")) {
119             return "";
120         }
121         if (checklist.contains(xmlRootElementName)) {
122             return "";
123         }
124         StringBuilder pathSb = new StringBuilder();
125         if (path.indexOf('{') == -1) {
126             path += "?parameter=value[&parameter2=value2]";
127         }
128         pathSb.append("  ").append(path).append(":\n");
129         pathSb.append("    get:\n");
130         pathSb.append("      tags:\n");
131         pathSb.append("        - Operations" + "\n");
132         pathSb.append("      summary: returns ").append(xmlRootElementName).append("\n");
133
134         pathSb.append("      description: returns ").append(xmlRootElementName).append("\n");
135         pathSb.append("      operationId: get").append(useOpId).append("\n");
136         pathSb.append("      produces:\n");
137         pathSb.append("        - application/json\n");
138         pathSb.append("        - application/xml\n");
139
140         pathSb.append("      responses:\n");
141         pathSb.append("        \"200\":\n");
142         pathSb.append("          description: successful operation\n");
143         pathSb.append("          schema:\n");
144         pathSb.append("              $ref: \"#/definitions/").append(xmlRootElementName)
145             .append("\"\n");
146         pathSb.append("        \"default\":\n");
147         pathSb.append("          ").append(GenerateXsd.getResponsesUrl());
148         if (StringUtils.isNotEmpty(pathParams) || StringUtils.isNotEmpty(queryParams)) {
149             pathSb.append("\n      parameters:\n");
150         }
151         if (StringUtils.isNotEmpty(pathParams)) {
152             pathSb.append(pathParams);
153         }
154         if (StringUtils.isNotEmpty(pathParams) && StringUtils.isNotEmpty(queryParams)) {
155             pathSb.append("\n");
156         }
157         if (StringUtils.isNotEmpty(queryParams)) {
158             pathSb.append(queryParams);
159         }
160         checklist.add(xmlRootElementName);
161         return pathSb.toString();
162     }
163 }