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