968d9ef23b69c673f16bf8ee49f3c278be26abdb
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / db / schema / ScriptDriver.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.db.schema;
23
24 import java.io.IOException;
25 import java.util.UUID;
26
27 import org.apache.commons.configuration.ConfigurationException;
28 import org.codehaus.jackson.JsonGenerationException;
29 import org.codehaus.jackson.map.JsonMappingException;
30 import org.codehaus.jackson.map.ObjectMapper;
31 import org.onap.aai.dbmap.AAIGraphConfig;
32 import org.onap.aai.exceptions.AAIException;
33 import org.onap.aai.introspection.Version;
34 import org.onap.aai.logging.LoggingContext;
35 import org.onap.aai.logging.LoggingContext.StatusCode;
36 import org.onap.aai.util.AAIConfig;
37 import com.beust.jcommander.JCommander;
38 import com.beust.jcommander.Parameter;
39 import com.thinkaurelius.titan.core.TitanFactory;
40 import com.thinkaurelius.titan.core.TitanGraph;
41
42 public class ScriptDriver {
43
44
45         /**
46          * The main method.
47          *
48          * @param args the arguments
49          * @throws AAIException the AAI exception
50          * @throws JsonGenerationException the json generation exception
51          * @throws JsonMappingException the json mapping exception
52          * @throws IOException Signals that an I/O exception has occurred.
53          */
54         public static void main (String[] args) throws AAIException, IOException, ConfigurationException {
55                 CommandLineArgs cArgs = new CommandLineArgs();
56                 
57                 LoggingContext.init();
58                 LoggingContext.component("DBSchemaScriptDriver");
59                 LoggingContext.partnerName("NA");
60                 LoggingContext.targetEntity("AAI");
61                 LoggingContext.requestId(UUID.randomUUID().toString());
62                 LoggingContext.serviceName("AAI");
63                 LoggingContext.targetServiceName("main");
64                 LoggingContext.statusCode(StatusCode.COMPLETE);
65                 LoggingContext.responseCode(LoggingContext.SUCCESS);
66                 
67                 new JCommander(cArgs, args);
68                 
69                 if (cArgs.help) {
70                         System.out.println("-c [path to graph configuration] -type [what you want to audit - oxm or graph]");
71                 }
72                 String config = cArgs.config;
73                 AAIConfig.init();
74                 try (TitanGraph graph = TitanFactory.open(new AAIGraphConfig.Builder(config).forService(ScriptDriver.class.getSimpleName()).withGraphType("NA").buildConfiguration())) {
75                         if (!("oxm".equals(cArgs.type) || "graph".equals(cArgs.type))) {
76                                 System.out.println("type: " + cArgs.type + " not recognized.");
77                                 System.exit(1);
78                         }
79
80                         AuditDoc doc = null;
81                         if ("oxm".equals(cArgs.type)) {
82                                 doc = AuditorFactory.getOXMAuditor(Version.v8).getAuditDoc();
83                         } else if ("graph".equals(cArgs.type)) {
84                                 doc = AuditorFactory.getGraphAuditor(graph).getAuditDoc();
85                         }
86
87                         ObjectMapper mapper = new ObjectMapper();
88
89                         String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(doc);
90                         System.out.println(json);
91                 }
92         }
93         
94 }
95
96 class CommandLineArgs {
97         
98         @Parameter(names = "--help", description = "Help")
99         public boolean help = false;
100         
101         @Parameter(names = "-c", description = "Configuration", required=true)
102         public String config;
103         
104         @Parameter(names = "-type", description = "Type", required=true)
105         public String type = "graph";
106         
107
108 }