re base code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / GraphJsonValidator.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.impl;
22
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
26
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.concurrent.atomic.AtomicInteger;
33
34 /**
35  * simple util class to verify that the titan export json graph is not corrupted
36  */
37 public class GraphJsonValidator {
38
39     private static Logger log = Logger.getLogger(GraphJsonValidator.class.getName());
40
41     public boolean verifyTitanJson(String filePath) throws IOException {
42         ObjectMapper objectMapper = new ObjectMapper();
43         List<Integer> invalidRows = new ArrayList<>();
44         AtomicInteger atomicInteger = new AtomicInteger(1);
45         Files.lines(Paths.get(filePath)).forEach(line -> {
46             try {
47                 verifyJsonLine(objectMapper, atomicInteger, line);
48             } catch (RuntimeException  | IOException e) {
49                 logInvalidJsonRow(atomicInteger, line, e);
50                 invalidRows.add(atomicInteger.get());
51             }
52         });
53         return verificationResult(invalidRows);
54     }
55
56     private void verifyJsonLine(ObjectMapper objectMapper, AtomicInteger atomicInteger, String line) throws IOException {
57         log.info("verifying line: " +  atomicInteger.get());
58         objectMapper.readTree(line);
59         atomicInteger.incrementAndGet();
60     }
61
62     private void logInvalidJsonRow(AtomicInteger atomicInteger, String line, Exception e) {
63         log.error("Invalid Json!!!!!!!!!!!!!!!!!!!!", e);
64         log.info("line number: " +  atomicInteger.get());
65         log.info("line value: " + line);
66     }
67
68     private boolean verificationResult(List<Integer> invalidRows) {
69         if (!invalidRows.isEmpty()) {
70             log.info("the following lines are not valid: " + invalidRows);
71             return false;
72         }
73         return true;
74     }
75
76 }