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