2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.asdctool.impl;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
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;
36 * simple util class to verify that the janusgraph export json graph is not corrupted
38 public class GraphJsonValidator {
40 private static Logger log = Logger.getLogger(GraphJsonValidator.class.getName());
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 -> {
49 verifyJsonLine(objectMapper, atomicInteger, line);
50 } catch (RuntimeException | IOException e) {
51 logInvalidJsonRow(atomicInteger, line, e);
52 invalidRows.add(atomicInteger.get());
56 return verificationResult(invalidRows);
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();
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);
71 private boolean verificationResult(List<Integer> invalidRows) {
72 if (!invalidRows.isEmpty()) {
73 log.info("the following lines are not valid: " + invalidRows);