Upgrade SDC from Titan to Janus Graph
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / main / ExportImportMenu.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.sdc.asdctool.main;
22
23 import org.openecomp.sdc.asdctool.impl.GraphJsonValidator;
24 import org.openecomp.sdc.asdctool.impl.GraphMLConverter;
25 import org.openecomp.sdc.asdctool.impl.GraphMLDataAnalyzer;
26
27 public class ExportImportMenu {
28
29         private static void usageAndExit() {
30                 exportUsage();
31                 importUsage();
32                 exportUsersUsage();
33                 validateJsonUsage();
34
35                 System.exit(1);
36         }
37
38         private static void importUsage() {
39                 System.out.println("Usage: import <janusgraph.properties> <graph file location>");
40         }
41
42         private static void validateJsonUsage() {
43                 System.out.println("Usage: validate-json <export graph path>");
44         }
45
46         private static void exportUsage() {
47                 System.out.println("Usage: export <janusgraph.properties> <output directory>");
48         }
49
50         private static void dataReportUsage() {
51                 System.out.println("Usage: get-data-report-from-graph-ml <full path of .graphml file>");
52         }
53
54         private static void exportUsersUsage() {
55                 System.out.println("Usage: exportusers <janusgraph.properties> <output directory>");
56         }
57
58         public static void main(String[] args) throws Exception {
59
60                 if (args == null || args.length < 1) {
61                         usageAndExit();
62                 }
63
64                 String operation = args[0];
65                 GraphMLConverter graphMLConverter = new GraphMLConverter();
66                 switch (operation.toLowerCase()) {
67
68                 case "export":
69                         boolean isValid = verifyParamsLength(args, 3);
70                         if (false == isValid) {
71                                 exportUsage();
72                                 System.exit(1);
73                         }
74
75                         boolean result = graphMLConverter.exportGraph(args);
76                         if (result == false) {
77                                 System.exit(2);
78                         }
79
80                         break;
81                 case "import":
82                         isValid = verifyParamsLength(args, 3);
83                         if (false == isValid) {
84                                 importUsage();
85                                 System.exit(1);
86                         }
87                         result = graphMLConverter.importGraph(args);
88                         if (result == false) {
89                                 System.exit(2);
90                         }
91                         break;
92
93                 case "exportusers":
94                         isValid = verifyParamsLength(args, 3);
95                         if (false == isValid) {
96                                 importUsage();
97                                 System.exit(1);
98                         }
99                         result = graphMLConverter.exportUsers(args);
100                         if (result == false) {
101                                 System.exit(2);
102                         }
103                         break;
104
105                 case "findproblem":
106                         isValid = verifyParamsLength(args, 3);
107                         if (false == isValid) {
108                                 importUsage();
109                                 System.exit(1);
110                         }
111                         result = graphMLConverter.findErrorInJsonGraph(args);
112                         if (result == false) {
113                                 System.exit(2);
114                         }
115                         break;
116                 case "validate-json":
117                         String jsonFilePath = validateAndGetJsonFilePath(args);
118                         GraphJsonValidator graphJsonValidator = new GraphJsonValidator();
119                         if (graphJsonValidator.verifyJanusGraphJson(jsonFilePath)) {
120                                 System.exit(2);
121                         }
122                         break;
123
124                 case "export-as-graph-ml":
125                         isValid = verifyParamsLength(args, 3);
126                         if (false == isValid) {
127                                 exportUsage();
128                                 System.exit(1);
129                         }
130                         String mlFile = graphMLConverter.exportGraphMl(args);
131                         if (mlFile == null) {
132                                 System.exit(2);
133                         }
134                         break;
135                 case "export-as-graph-ml-with-data-report":
136                         isValid = verifyParamsLength(args, 3);
137                         if (false == isValid) {
138                                 exportUsage();
139                                 System.exit(1);
140                         }
141                         mlFile = graphMLConverter.exportGraphMl(args);
142                         if (mlFile == null) {
143                                 System.exit(2);
144                         }
145                         String[] dataArgs = new String[] { mlFile };
146                         mlFile = new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs);
147                         if (mlFile == null) {
148                                 System.exit(2);
149                         }
150                         break;
151                 case "get-data-report-from-graph-ml":
152                         isValid = verifyParamsLength(args, 2);
153                         if (false == isValid) {
154                                 dataReportUsage();
155                                 System.exit(1);
156                         }
157                         dataArgs = new String[] { args[1] };
158                         mlFile = new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs);
159                         if (mlFile == null) {
160                                 System.exit(2);
161                         }
162                         break;
163                 default:
164                         usageAndExit();
165                 }
166
167         }
168
169         private static String validateAndGetJsonFilePath(String[] args) {
170                 boolean isValid;
171                 isValid = verifyParamsLength(args, 2);
172                 if (!isValid) {
173             validateJsonUsage();
174             System.exit(1);
175         }
176         return args[1];
177         }
178
179         private static boolean verifyParamsLength(String[] args, int i) {
180                 if (args == null) {
181                         if (i > 0) {
182                                 return false;
183                         }
184                         return true;
185                 }
186
187                 if (args.length >= i) {
188                         return true;
189                 }
190                 return false;
191         }
192
193 }