Add unit tests for dupeTool, datasnapshot
[aai/graphadmin.git] / src / main / java / org / onap / aai / db / schema / ScriptDriver.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *    http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.onap.aai.db.schema;\r
21 \r
22 import java.io.IOException;\r
23 import java.util.UUID;\r
24 \r
25 import com.fasterxml.jackson.databind.ObjectMapper;\r
26 import org.apache.commons.configuration.ConfigurationException;\r
27 import org.apache.commons.configuration.PropertiesConfiguration;\r
28 import org.codehaus.jackson.JsonGenerationException;\r
29 import org.onap.aai.dbmap.AAIGraphConfig;\r
30 import org.onap.aai.edges.EdgeIngestor;\r
31 import org.onap.aai.exceptions.AAIException;\r
32 import org.onap.aai.setup.SchemaVersions;\r
33 import org.onap.aai.logging.LoggingContext;\r
34 import org.onap.aai.logging.LoggingContext.StatusCode;\r
35 import org.onap.aai.util.AAIConfig;\r
36 import com.beust.jcommander.JCommander;\r
37 import com.beust.jcommander.Parameter;\r
38 import org.janusgraph.core.JanusGraphFactory;\r
39 import org.janusgraph.core.JanusGraph;\r
40 import org.springframework.context.annotation.AnnotationConfigApplicationContext;\r
41 \r
42 public class ScriptDriver {\r
43 \r
44         /**\r
45          * The main method.\r
46          *\r
47          * @param args the arguments\r
48          * @throws AAIException the AAI exception\r
49          * @throws JsonGenerationException the json generation exception\r
50          * @throws IOException Signals that an I/O exception has occurred.\r
51          */\r
52         public static void main (String[] args) throws AAIException, IOException, ConfigurationException {\r
53                 CommandLineArgs cArgs = new CommandLineArgs();\r
54                 \r
55                 LoggingContext.init();\r
56                 LoggingContext.component("DBSchemaScriptDriver");\r
57                 LoggingContext.partnerName("NA");\r
58                 LoggingContext.targetEntity("AAI");\r
59                 LoggingContext.requestId(UUID.randomUUID().toString());\r
60                 LoggingContext.serviceName("AAI");\r
61                 LoggingContext.targetServiceName("main");\r
62                 LoggingContext.statusCode(StatusCode.COMPLETE);\r
63                 LoggingContext.responseCode(LoggingContext.SUCCESS);\r
64                 \r
65                 new JCommander(cArgs, args);\r
66                 \r
67                 if (cArgs.help) {\r
68                         System.out.println("-c [path to graph configuration] -type [what you want to audit - oxm or graph]");\r
69                 }\r
70 \r
71                 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(\r
72                                 "org.onap.aai.config",\r
73                                 "org.onap.aai.setup"\r
74                 );\r
75 \r
76                 AuditorFactory auditorFactory = ctx.getBean(AuditorFactory.class);\r
77                 SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class);\r
78                 EdgeIngestor edgeIngestor     = ctx.getBean(EdgeIngestor.class);\r
79 \r
80                 String config = cArgs.config;\r
81                 AAIConfig.init();\r
82 \r
83                 PropertiesConfiguration graphConfiguration = new AAIGraphConfig\r
84                         .Builder(config)\r
85                         .forService(ScriptDriver.class.getSimpleName())\r
86                         .withGraphType("NA")\r
87                         .buildConfiguration();\r
88 \r
89                 try (JanusGraph graph = JanusGraphFactory.open(graphConfiguration)) {\r
90                         if (!("oxm".equals(cArgs.type) || "graph".equals(cArgs.type))) {\r
91                                 System.out.println("type: " + cArgs.type + " not recognized.");\r
92                                 System.exit(1);\r
93                         }\r
94 \r
95                         AuditDoc doc = null;\r
96                         if ("oxm".equals(cArgs.type)) {\r
97                                 doc = auditorFactory.getOXMAuditor(schemaVersions.getDefaultVersion(), edgeIngestor).getAuditDoc();\r
98                         } else if ("graph".equals(cArgs.type)) {\r
99                                 doc = auditorFactory.getGraphAuditor(graph).getAuditDoc();\r
100                         }\r
101 \r
102                         ObjectMapper mapper = new ObjectMapper();\r
103 \r
104                         String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(doc);\r
105                         System.out.println(json);\r
106                 }\r
107         }\r
108         \r
109 }\r
110 \r
111 class CommandLineArgs {\r
112         \r
113         @Parameter(names = "--help", description = "Help")\r
114         public boolean help = false;\r
115         \r
116         @Parameter(names = "-c", description = "Configuration", required=true)\r
117         public String config;\r
118         \r
119         @Parameter(names = "-type", description = "Type", required=true)\r
120         public String type = "graph";\r
121         \r
122 \r
123 }