Catalog alignment
[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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.asdctool.main;
24
25 import org.openecomp.sdc.asdctool.impl.GraphJsonValidator;
26 import org.openecomp.sdc.asdctool.impl.GraphMLConverter;
27 import org.openecomp.sdc.asdctool.impl.GraphMLDataAnalyzer;
28
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.stream.Collectors;
33
34 public class ExportImportMenu {
35
36         enum ExportImportEnum {
37                 DATA_REPORT("Usage: get-data-report-from-graph-ml <full path of .graphml file>", "get-data-report-from-graph-ml"){
38                         @Override
39                         void handle(String[] args) {
40                                 if (verifyParamsLength(args, 2)) {
41                                         usage();
42                                         System.exit(1);
43                                 }
44                                 String[] dataArgs = new String[] { args[1] };
45                                 if (new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs) == null) {
46                                         System.exit(2);
47                                 }
48                         }
49                 },
50                 EXPORT("Usage: export <janusgraph.properties> <output directory>", "export"){
51                         @Override
52                         void handle(String[] args) {
53                                 if (verifyParamsLength(args, 3)) {
54                                         usage();
55                                         System.exit(1);
56                                 }
57
58                                 if (!GRAPH_ML_CONVERTER.exportGraph(args)) {
59                                         System.exit(2);
60                                 }
61                         }
62                 },EXPORT_AS_GRAPH("Usage: export-as-graph-ml <janusgraph.properties> <output directory>", "export-as-graph-ml"){
63                         @Override
64                         void handle(String[] args) {
65                                 if (verifyParamsLength(args, 3)) {
66                                         usage();
67                                         System.exit(1);
68                                 }
69                                 if (GRAPH_ML_CONVERTER.exportGraphMl(args) == null) {
70                                         System.exit(2);
71                                 }
72                         }
73                 },EXPORT_USERS("Usage: exportusers <janusgraph.properties> <output directory>", "exportusers"){
74                         @Override
75                         void handle(String[] args) {
76                                 if (verifyParamsLength(args, 3)) {
77                                         usage();
78                                         System.exit(1);
79                                 }
80                                 if (!GRAPH_ML_CONVERTER.exportUsers(args)) {
81                                         System.exit(2);
82                                 }
83                         }
84                 },EXPORT_WITH_REPORT("Usage: export-as-graph-ml-with-data-report <janusgraph.properties> <output directory>", "export-as-graph-ml-with-data-report"){
85                         @Override
86                         void handle(String[] args) {
87                                 if (verifyParamsLength(args, 3)) {
88                                         usage();
89                                         System.exit(1);
90                                 }
91                                 if (GRAPH_ML_CONVERTER.exportGraphMl(args) == null) {
92                                         System.exit(2);
93                                 }
94                                 String[] dataArgs = new String[] {GRAPH_ML_CONVERTER.exportGraphMl(args)};
95                                 if (new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs) == null) {
96                                         System.exit(2);
97                                 }
98                         }
99                 },FIND_PROBLEM("Usage: findproblem <janusgraph.properties> <graph file location>", "findproblem"){
100                         @Override
101                         void handle(String[] args) {
102                                 if (verifyParamsLength(args, 3)) {
103                                         usage();
104                                         System.exit(1);
105                                 }
106                                 if (!GRAPH_ML_CONVERTER.findErrorInJsonGraph(args)) {
107                                         System.exit(2);
108                                 }
109                         }
110                 },IMPORT("Usage: import <janusgraph.properties> <graph file location>", "import"){
111                         @Override
112                         void handle(String[] args) {
113                                 if (verifyParamsLength(args, 3)) {
114                                         usage();
115                                         System.exit(1);
116                                 }
117                                 if (!GRAPH_ML_CONVERTER.importGraph(args)) {
118                                         System.exit(2);
119                                 }
120                         }
121                 },VALIDATE_JSON("Usage: validate-json <export graph path>", "validate-json"){
122                         @Override
123                         void handle(String[] args) throws IOException {
124                                 if (verifyParamsLength(args, 2)) {
125                                         usage();
126                                         System.exit(1);
127                                 }
128                                 String jsonFilePath = args[1];
129                                 GraphJsonValidator graphJsonValidator = new GraphJsonValidator();
130                                 if (graphJsonValidator.verifyJanusGraphJson(jsonFilePath)) {
131                                         System.exit(2);
132                                 }
133                         }
134                 },NONE{
135                         @Override
136                         void handle(String[] args) {
137                                 usage();
138                                 System.exit(1);
139                         }
140
141                         void usage(){
142                                 Arrays.stream(ExportImportEnum.values()).filter(type -> type != NONE).forEach(ExportImportEnum::usage);
143                         }
144                 };
145
146                 private static final GraphMLConverter GRAPH_ML_CONVERTER = new GraphMLConverter();
147                 private String usage;
148                 private String keyword;
149
150                 ExportImportEnum(String usage, String keyword) {
151                         this.usage = usage;
152                         this.keyword = keyword;
153                 }
154
155                 ExportImportEnum() {}
156
157                 void usage(){
158                         System.out.println(usage);
159                 }
160
161                 static ExportImportEnum getByKeyword(String keyword) {
162                         List<ExportImportEnum> collected = Arrays.stream(ExportImportEnum.values())
163                                 .filter(type -> type != NONE)
164                                 .filter(type -> type.keyword.equals(keyword))
165                                 .collect(Collectors.toList());
166                         return collected.isEmpty() ? NONE : collected.get(0);
167                 }
168
169                 abstract void handle(String[] args) throws IOException;
170
171                 private static boolean verifyParamsLength(String[] args, int i) {
172                         if (args == null) {
173                                 return i > 0;
174                         }
175                         return args.length < i;
176                 }
177         }
178
179         public static void main(String[] args) throws Exception {
180                 ExportImportEnum type;
181                 if (args == null || args.length < 1) {
182                         type = ExportImportEnum.NONE;
183                 }else{
184                         type = ExportImportEnum.getByKeyword(getOperation(args).toLowerCase());
185                 }
186                 type.handle(args);
187         }
188
189         private static String getOperation(String[] args) {
190                 String operation = null;
191                 if (args != null) {
192                         operation = args[0];
193                 }
194                 return operation;
195         }
196
197 }