Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / main / ZusammenMainTool.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.core.tools.main;
22
23 import static org.openecomp.core.tools.util.Utils.printMessage;
24
25 import java.time.Duration;
26 import java.time.Instant;
27 import java.util.Optional;
28 import org.openecomp.core.tools.commands.Command;
29 import org.openecomp.core.tools.commands.CommandsHolder;
30 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33
34 public class ZusammenMainTool {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(ZusammenMainTool.class);
37
38     public static void main(String[] args) {
39         Command command = getCommandToRun(args);
40
41         Instant startTime = Instant.now();
42         SessionContextProviderFactory.getInstance().createInterface().create("GLOBAL_USER", "dox");
43         if (!command.execute(args)) {
44             command.printUsage();
45             System.exit(-1);
46         }
47         Instant stopTime = Instant.now();
48
49         printDuration(command, startTime, stopTime);
50         System.exit(0);
51     }
52
53     private static Command getCommandToRun(String[] args) {
54         Optional<Command> command = CommandsHolder.getCommand(args);
55         if (!command.isPresent()) {
56             LOGGER.error("Illegal execution.");
57             CommandsHolder.printUsages();
58             System.exit(-1);
59         }
60         return command.get();
61     }
62
63     private static void printDuration(Command command, Instant startTime, Instant stopTime) {
64         Duration duration = Duration.between(startTime, stopTime);
65         long minutesPart = duration.toMinutes();
66         long secondsPart = duration.minusMinutes(minutesPart).getSeconds();
67
68         printMessage(LOGGER, String.format("Zusammen tools command %s finished. Total run time was %s:%s minutes.",
69                 command.getCommandName(), minutesPart, secondsPart));
70     }
71 }