aboutsummaryrefslogtreecommitdiffstats
path: root/aai-traversal/src/main/java/org/onap/aai/rest/util/ConvertQueryPropertiesToJson.java
blob: e8d1547917a362054cbda90c417b7cc497a58acb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.rest.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;


import org.onap.aai.util.AAIConstants;

public class ConvertQueryPropertiesToJson {

    private static final int maxfilesize = 256000;

    private void addStart(StringBuilder sb) {
        sb.append("{\n  \"stored-queries\":[{\n");
    }

    private void addRequiredQueryProperties(StringBuilder sb, List<String> rqd) {
        Iterator it = rqd.iterator();
        sb.append("      \"query\":{\n        \"required-properties\":[");
        while (it.hasNext()) {
            sb.append("\"" + it.next() + "\"");
            if (it.hasNext()) {
                sb.append(",");
            }
        }
        sb.append("]\n      },\n");
    }

    private void addAnotherQuery(StringBuilder sb, String queryName, String query, List<String> rqd) {
        sb.append("    \"" + queryName + "\":{\n");
        if (!rqd.isEmpty()) {
            addRequiredQueryProperties(sb, rqd);
        }
        sb.append("      \"stored-query\":\"" + query + "\"\n    }\n  },{\n");
    }

    private void addLastQuery(StringBuilder sb, String queryName, String query, List<String> rqd) {
        sb.append("    \"" + queryName + "\":{\n");
        if (!rqd.isEmpty()) {
            addRequiredQueryProperties(sb, rqd);
        }
        sb.append("      \"stored-query\":\"" + query + "\"\n    }\n  }]\n}\n");
    }

    private String get2ndParameter(String paramString) {
        String endParams = paramString.substring(0, paramString.indexOf(')'));
        String result = endParams.substring(endParams.indexOf(',') + 1);
        String lastParam = result.trim();
        if (lastParam.startsWith("\\") || lastParam.startsWith("'") || lastParam.startsWith("new ")) {
            return null;
        }

        return lastParam;
    }

    private List<String> findRqdProperties(String query) {
        String[] parts = query.split("getVerticesByProperty");
        List<String> result = new ArrayList<String>();
        if (parts.length == 1) {
            return result;
        }
        int count = 0;
        String foundRqdProperty;
        while (count++ < parts.length - 1) {
            foundRqdProperty = get2ndParameter(parts[count]);
            if (foundRqdProperty != null && !result.contains(foundRqdProperty)) {
                result.add(foundRqdProperty);
            }
        }
        return result;
    }

    public String convertProperties(Properties props) {
        Enumeration<?> e = props.propertyNames();
        StringBuilder sb = new StringBuilder(maxfilesize);
        String queryName;
        String query;
        addStart(sb);
        List<String> rqd;
        while (e.hasMoreElements()) {
            queryName = (String) e.nextElement();
            query = props.getProperty(queryName).trim().replace("\"", "\\\"");
            rqd = findRqdProperties(query);
            if (e.hasMoreElements()) {
                addAnotherQuery(sb, queryName, query, rqd);
            } else {
                addLastQuery(sb, queryName, query, rqd);
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        File queryFile = new File(AAIConstants.AAI_HOME_ETC_QUERY);
        Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream(queryFile)) {
            properties.load(fis);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Error occurred during the processing of query file: " + e);
        }
        ConvertQueryPropertiesToJson c = new ConvertQueryPropertiesToJson();
        String json = c.convertProperties(properties);
        System.out.println("returned json:\n" + json);
    }

}