Catalog alignment
[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 import java.util.stream.Stream;
34
35 /**
36  * simple util class to verify that the janusgraph export json graph is not corrupted
37  */
38 public class GraphJsonValidator {
39
40     private static Logger log = Logger.getLogger(GraphJsonValidator.class.getName());
41
42     public boolean verifyJanusGraphJson(String filePath) throws IOException {
43         ObjectMapper objectMapper = new ObjectMapper();
44         List<Integer> invalidRows = new ArrayList<>();
45         AtomicInteger atomicInteger = new AtomicInteger(1);
46         try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
47             stream.forEach(line -> {
48                 try {
49                     verifyJsonLine(objectMapper, atomicInteger, line);
50                 } catch (RuntimeException  | IOException e) {
51                     logInvalidJsonRow(atomicInteger, line, e);
52                     invalidRows.add(atomicInteger.get());
53                 }
54             });
55         }
56         return verificationResult(invalidRows);
57     }
58
59     private void verifyJsonLine(ObjectMapper objectMapper, AtomicInteger atomicInteger, String line) throws IOException {
60         log.info("verifying line: " +  atomicInteger.get());
61         objectMapper.readTree(line);
62         atomicInteger.incrementAndGet();
63     }
64
65     private void logInvalidJsonRow(AtomicInteger atomicInteger, String line, Exception e) {
66         log.error("Invalid Json!!!!!!!!!!!!!!!!!!!!", e);
67         log.info("line number: " +  atomicInteger.get());
68         log.info("line value: " + line);
69     }
70
71     private boolean verificationResult(List<Integer> invalidRows) {
72         if (!invalidRows.isEmpty()) {
73             log.info("the following lines are not valid: " + invalidRows);
74             return false;
75         }
76         return true;
77     }
78
79 }