2 * Copyright 2016 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
20 import java.io.InputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.TimeoutException;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * For execute local command
32 * (support control overtime)
37 * @author quanzhong@huawei.com
38 * @version NFVO 0.5 Sep 19, 2016
40 public class LocalComandUtils {
41 private LocalComandUtils(){
45 private static final Logger log = LoggerFactory.getLogger(LocalComandUtils.class);
54 public static String formatCommand(List<String> command) {
55 StringBuilder builder = new StringBuilder();
57 for(String cmd : command) {
58 builder.append(cmd).append(" "); // NOSONAR
61 return builder.toString();
72 public static ExeRes execute(String dir, String... command) {
73 List<String> commands = new ArrayList<>(command.length);
74 for(String arg : command) {
77 return execute(dir, commands);
82 * execute local command
85 * @param dir the command path
87 * @param timeout millis
88 * @return response msg
91 public static ExeRes execute(String dir, List<String> command,long timeout) {
92 ExeRes er = new ExeRes();
93 StringBuilder sb = new StringBuilder();
95 if(SwitchController.isDebugModel()) {
96 command.set(0, "juju.bat");
98 ProcessBuilder pb = new ProcessBuilder(command);
99 if(StringUtils.isNotBlank(dir)) {
100 pb.directory(new File(dir));
102 pb.redirectErrorStream(true);
103 Process p = pb.start();
105 // wait the process result
106 buildProcessResult(er, p, timeout);
108 InputStream in = p.getInputStream();
109 byte[] buffer = new byte[1024];
111 while((length = in.read(buffer)) > 0) {
112 sb.append(new String(buffer, 0, length));
115 er.setBody(sb.toString());
116 } catch(Exception e) {
117 er.setCode(ExeRes.FAILURE);
118 er.setBody(e.getMessage());
119 log.error("execute the command failed:{}", command, e);
125 * execute local command
128 * @param dir the command path
130 * @return response msg
133 public static ExeRes execute(String dir, List<String> command) {
134 return execute(dir,command,Constant.PROCESS_WAIT_MILLIS);
142 * @param timeout millis
143 * @throws TimeoutException
144 * @throws InterruptedException
147 private static void buildProcessResult(ExeRes er, Process p,long timeout) throws TimeoutException, InterruptedException {
148 Worker worker = new Worker(p);
151 worker.join(timeout);
152 if(worker.exitValue != null) {
153 int exit = worker.exitValue;
155 er.setCode(ExeRes.FAILURE);
156 log.warn("the process exit non-normal");
158 er.setCode(ExeRes.SUCCESS);
161 er.setCode(ExeRes.FAILURE);
162 log.warn("the process execute timeout.");
163 throw new TimeoutException();
165 } catch(InterruptedException e) {
167 Thread.currentThread().interrupt();
178 * @author quanzhong@huawei.com
179 * @version NFVO 0.5 Sep 19, 2016
181 private static class Worker extends Thread {
183 private final Process process;
185 private Integer exitValue;
187 private Worker(Process process) {
188 this.process = process;
199 exitValue = process.waitFor();
200 } catch(InterruptedException e) {
214 public static class ExeRes {
216 public static final int SUCCESS = 0;
218 public static final int FAILURE = -1;
225 * @return Returns the code.
227 public int getCode() {
232 * @param code The code to set.
234 public void setCode(int code) {
239 * @return Returns the body.
241 public String getBody() {
246 * @param body The body to set.
248 public void setBody(String body) {
253 public String toString() {
255 return EntityUtils.toString(this, this.getClass());
256 } catch(Exception e) {
257 log.error("to string error:", e);
258 return "code:" + this.getCode() + ",body:" + this.getBody();