Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / util / ConvertQueryPropertiesToJson.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.rest.util;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Properties;
30
31
32 import org.onap.aai.util.AAIConstants;
33
34 public class ConvertQueryPropertiesToJson {
35         
36         private final static int maxfilesize = 256000;
37         
38         private void addStart( StringBuilder sb ) {
39                 sb.append("{\n  \"stored-queries\":[{\n");
40         }
41         
42         private void addRequiredQueryProperties( StringBuilder sb, List<String> rqd ) {
43                 Iterator it = rqd.iterator();
44                 sb.append("      \"query\":{\n        \"required-properties\":[");
45                 while( it.hasNext()) {
46                         sb.append("\"" + it.next() + "\"");
47                         if ( it.hasNext()) {
48                                 sb.append(",");
49                         }
50                 }
51                 sb.append("]\n      },\n");
52         }
53         
54         private void addAnotherQuery( StringBuilder sb, String queryName, String query, List<String> rqd ) {
55                 sb.append("    \"" + queryName + "\":{\n");
56                 if ( !rqd.isEmpty()) {
57                       addRequiredQueryProperties( sb, rqd);
58                 }
59                 sb.append("      \"stored-query\":\"" + query + "\"\n    }\n  },{\n");
60         }
61         
62         private void addLastQuery( StringBuilder sb, String queryName, String query, List<String> rqd ) {
63                 sb.append("    \"" + queryName + "\":{\n");
64                 if ( !rqd.isEmpty() ) {
65                       addRequiredQueryProperties( sb, rqd);
66                 }
67                 sb.append("      \"stored-query\":\"" + query + "\"\n    }\n  }]\n}\n");
68         }
69         
70         private String get2ndParameter( String paramString) {
71                 String endParams = paramString.substring(0, paramString.indexOf(')'));
72                 String result = endParams.substring(endParams.indexOf(',') + 1 );
73                 String lastParam = result.trim();
74                 if ( lastParam.startsWith("\\") || lastParam.startsWith("'") || lastParam.startsWith("new ") ){
75                         return null;
76                 }
77                 
78                 return lastParam;
79         }
80         
81         private List<String> findRqdProperties( String query) {
82                 String[] parts = query.split("getVerticesByProperty");
83                 List<String> result = new ArrayList<String>();
84                 if ( parts.length == 1 )
85                         return result;
86                 int count = 0;
87                 String foundRqdProperty;
88                 while ( count++ < parts.length - 1 ) {
89                         foundRqdProperty = get2ndParameter(parts[count]);
90                         if ( foundRqdProperty != null  && !result.contains(foundRqdProperty)) {
91                                 result.add(foundRqdProperty);
92                         }
93                 }
94                 return result;
95         }
96
97         public  String convertProperties( Properties props ) {
98                 Enumeration<?> e = props.propertyNames();
99                 StringBuilder sb = new StringBuilder(maxfilesize);
100                 String queryName;
101                 String query;
102                 addStart( sb );
103                 List<String> rqd;
104                 while ( e.hasMoreElements()) {
105                         queryName = (String)e.nextElement();
106                         query = props.getProperty(queryName).trim().replace("\"", "\\\"");
107                         rqd = findRqdProperties(  query);
108                         if ( e.hasMoreElements()) {
109                                 addAnotherQuery( sb, queryName, query, rqd);
110                         } else {
111                                 addLastQuery( sb, queryName, query, rqd);
112                         }
113                 }
114                 
115         return sb.toString();
116         }
117         public static void main(String[] args) {
118                 File queryFile = new File(AAIConstants.AAI_HOME_ETC_QUERY);
119                 Properties properties = new Properties();
120         try (FileInputStream fis = new FileInputStream(queryFile)){
121             properties.load(fis);
122         } catch (IOException e) {
123                 e.printStackTrace();
124             System.out.println("Error occurred during the processing of query file: " + e);
125         }
126         ConvertQueryPropertiesToJson c = new ConvertQueryPropertiesToJson();
127         String json = c.convertProperties(properties);
128         System.out.println("returned json:\n" + json);
129         }
130
131 }