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