summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/build/scripts/parse-json.py
blob: 83c70aac015723ddd1b41a522b00d9b1c6b8218f (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
#!/usr/bin/python

##############################################################################
###
### parse-json.py
###
### A utility to parse a cassnadra-commands file and return the commands per type
### An Example for a json file:
###     {
###         "create":{
###                 "choice_or_other":"CREATE TYPE IF NOT EXISTS choice_or_other (results text)",
###                 "vendor_license_model": "CREATE TABLE IF NOT EXISTS vendor_license_model (vlm_id text PRIMARY KEY, name text, description text, icon text)",
###                 "license_agreement": "CREATE TABLE IF NOT EXISTS license_agreement (vlm_id text, la_id text, name text, description text, type text, contract text, req_const text, fg_ids set<text>, PRIMARY KEY (vlm_id, la_id))",
###                 "feature_group": "CREATE TABLE IF NOT EXISTS feature_group (vlm_id text, fg_id text, name text, description text, ep_ids set<text>, lkg_ids set<text>, refd_by_las set<text>, PRIMARY KEY (vlm_id, fg_id))",
###                 "license_key_group": "CREATE TABLE IF NOT EXISTS license_key_group (vlm_id text,lkg_id text,name text,description text, type text, operational_scope text, ref_fgs set<text>,PRIMARY KEY (vlm_id, lkg_id))",
###         }
###     }
###
### The return for "create" will be:
###                 CREATE TYPE IF NOT EXISTS choice_or_other (results text)
###                 CREATE TABLE IF NOT EXISTS vendor_license_model (vlm_id text PRIMARY KEY, name text, description text, icon text)
###                 CREATE TABLE IF NOT EXISTS license_agreement (vlm_id text, la_id text, name text, description text, type text, contract text, req_const text, fg_ids set<text>, PRIMARY KEY (vlm_id, la_id))
###                 CREATE TABLE IF NOT EXISTS feature_group (vlm_id text, fg_id text, name text, description text, ep_ids set<text>, lkg_ids set<text>, refd_by_las set<text>, PRIMARY KEY (vlm_id, fg_id))
###                 CREATE TABLE IF NOT EXISTS license_key_group (vlm_id text,lkg_id text,name text,description text, type text, operational_scope text, ref_fgs set<text>,PRIMARY KEY (vlm_id, lkg_id))
### Usage:
###
###    parse-json.py -t create -f cassandra-commands.json
###
### For example:
###
###
### Author: Avi Ziv
### Version 1.0
### Date: 3 May 2016
###
##############################################################################

import sys, getopt
import json as json
from collections import OrderedDict


def readJsonFile(file, type):
    with open(file, 'r') as f:
        data = json.load(f, object_pairs_hook=OrderedDict)
        return data[type]


def printJsonTypeEntries(jsonData):
    for i in jsonData.keys():
        print(jsonData[i] + ';')


def usage():
    print('parseJsonFile.py [-f <json-file> & -t <cql-type: drop|create|insert|update|select]')


def main(argv):
    action = ''

    try:
        opts, args = getopt.getopt(argv, "h:f:t:")
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            usage()
            sys.exit()
        elif opt == '-f':
            jsonFile = arg
            action = 'file'
        elif opt == '-t':
            type = arg

    if action == 'file':
        sJson = readJsonFile(jsonFile, type)
        printJsonTypeEntries(sJson)
        sys.exit()
    else:
        usage()


if __name__ == "__main__":
    main(sys.argv[1:])