2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 IBM
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.chef.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.Arrays;
29 import java.util.List;
31 import java.util.Optional;
32 import org.apache.commons.lang.StringUtils;
33 import org.json.JSONException;
34 import org.json.JSONObject;
35 import org.onap.appc.adapter.chef.ChefAdapter;
36 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
37 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
38 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43 * This class implements the {@link ChefAdapter} interface. This interface
44 * defines the behaviors that our service provides.
46 public class ChefAdapterImpl implements ChefAdapter {
48 // chef server Initialize variable
49 private String username = StringUtils.EMPTY;
50 private String clientPrivatekey = StringUtils.EMPTY;
51 private String chefserver = StringUtils.EMPTY;
52 private String serverAddress = StringUtils.EMPTY;
53 private String organizations = StringUtils.EMPTY;
55 @SuppressWarnings("nls")
56 public static final String MDC_ADAPTER = "adapter";
58 @SuppressWarnings("nls")
59 public static final String MDC_SERVICE = "service";
61 @SuppressWarnings("nls")
62 public static final String OUTCOME_FAILURE = "failure";
64 @SuppressWarnings("nls")
65 public static final String OUTCOME_SUCCESS = "success";
67 @SuppressWarnings("nls")
68 public static final String PROPERTY_PROVIDER = "provider";
70 @SuppressWarnings("nls")
71 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
73 @SuppressWarnings("nls")
74 public static final String PROPERTY_PROVIDER_NAME = "name";
76 @SuppressWarnings("nls")
77 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
79 @SuppressWarnings("nls")
80 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
82 @SuppressWarnings("nls")
83 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
85 @SuppressWarnings("nls")
86 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
88 @SuppressWarnings("nls")
89 public static final String PROPERTY_PROVIDER_TYPE = "type";
91 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdapterImpl.class);
93 private static final String CANNOT_FIND_PRIVATE_KEY_STR = "Cannot find the private key in the APPC file system, please load the private key to ";
95 private static final String POSTING_REQUEST_JSON_ERROR_STR = "Error posting request due to invalid JSON block: ";
96 private static final String POSTING_REQUEST_ERROR_STR = "Error posting request: ";
97 private static final String CHEF_CLIENT_RESULT_CODE_STR = "chefClientResult.code";
98 private static final String CHEF_SERVER_RESULT_CODE_STR = "chefServerResult.code";
99 private static final String CHEF_CLIENT_RESULT_MSG_STR = "chefClientResult.message";
100 private static final String CHEF_SERVER_RESULT_MSG_STR = "chefServerResult.message";
101 private static final String CHEF_ACTION_STR = "chefAction";
102 private static final String NODE_LIST_STR = "NodeList";
103 private static final Integer STATUS_OK = 200;
104 private static final Integer STATUS_PUSHJOBCHECK = 201;
105 private static final Integer PUSHJOBSTATUS= 202;
106 private static final Integer KEY_NOTFOUND = 500;
107 private static final Integer NO_ENVIRONMENT = 404;
108 private static final Integer APPC_ERRORCODE = 401;
110 private final ChefApiClientFactory chefApiClientFactory;
111 private final PrivateKeyChecker privateKeyChecker;
113 ChefAdapterImpl(ChefApiClientFactory chefApiClientFactory, PrivateKeyChecker privateKeyChecker) {
114 this.chefApiClientFactory = chefApiClientFactory;
115 this.privateKeyChecker = privateKeyChecker;
116 logger.info("Initialize Chef Adapter");
119 @SuppressWarnings("nls")
121 public void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
123 logger.info("environment of VNF-C");
124 chefInfo(params, ctx);
125 String env = params.get("Environment");
126 logger.info("Environmnet" + env);
127 if (env.equals(StringUtils.EMPTY)) {
128 chefServerResult(ctx, STATUS_OK, "Skip Environment block ");
131 if (privateKeyChecker.doesExist(clientPrivatekey)) {
133 JSONObject envJ = new JSONObject(env);
134 String envName = envJ.getString("name");
135 // update the details of an environment on the Chef server.
136 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
138 ChefResponse chefResponse = chefApiClient.put("/environments/" + envName, env);
139 code = chefResponse.getStatusCode();
140 message = chefResponse.getBody();
141 if (code == NO_ENVIRONMENT) {
142 // need create a new environment
143 chefResponse = chefApiClient.post("/environments", env);
144 code = chefResponse.getStatusCode();
145 message = chefResponse.getBody();
146 logger.info("requestbody {}", chefResponse.getBody());
148 chefServerResult(ctx, code, message);
149 } catch (JSONException e) {
150 code = APPC_ERRORCODE;
151 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
152 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
153 } catch (Exception e) {
154 code = APPC_ERRORCODE;
155 logger.error(POSTING_REQUEST_ERROR_STR + "vnfcEnvironment", e);
156 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + "vnfcEnvironment" + e.getMessage());
160 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
161 doFailure(ctx, code, message);
166 @SuppressWarnings("nls")
168 public void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
169 logger.info("update the nodeObjects of VNF-C");
171 final String LOG_ERR_METHOD_STR = "vnfcNodeobjects";
173 chefInfo(params, ctx);
174 String nodeListS = params.get(NODE_LIST_STR);
175 String nodeS = params.get("Node");
176 if (StringUtils.isNotBlank(nodeListS) && StringUtils.isNotBlank(nodeS)) {
177 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
178 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
179 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
180 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
181 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
183 String message = null;
184 if (privateKeyChecker.doesExist(clientPrivatekey)) {
185 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username,
188 for (String nodeName : nodes) {
189 JSONObject nodeJ = new JSONObject(nodeS);
190 nodeJ.remove("name");
191 nodeJ.put("name", nodeName);
192 String nodeObject = nodeJ.toString();
193 logger.info(nodeObject);
194 ChefResponse chefResponse = cac.put("/nodes/" + nodeName, nodeObject);
195 code = chefResponse.getStatusCode();
196 message = chefResponse.getBody();
197 if (code != STATUS_OK) {
203 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
204 doFailure(ctx, code, message);
206 chefServerResult(ctx, code, message);
208 throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList ");
210 } catch (JSONException e) {
211 code = APPC_ERRORCODE;
212 logger.error(POSTING_REQUEST_JSON_ERROR_STR + LOG_ERR_METHOD_STR, e);
213 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + LOG_ERR_METHOD_STR + e.getMessage());
214 } catch (Exception e) {
215 code = APPC_ERRORCODE;
216 logger.error(POSTING_REQUEST_ERROR_STR + LOG_ERR_METHOD_STR, e);
217 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + LOG_ERR_METHOD_STR + e.getMessage());
222 public void vnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
225 chefInfo(params, ctx);
226 String nodeList = params.get(NODE_LIST_STR);
227 if (StringUtils.isNotBlank(nodeList)) {
228 String isCallback = params.get("CallbackCapable");
229 String chefAction = "/pushy/jobs";
232 if ("true".equals(isCallback)) {
233 String requestId = params.get("RequestId");
234 String callbackUrl = params.get("CallbackUrl");
235 pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
236 + nodeList + "," + "\"env\": {\"RequestId\": \"" + requestId + "\", \"CallbackUrl\": \""
237 + callbackUrl + "\"}," + "\"capture_output\": true" + "}";
239 pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
240 + nodeList + "," + "\"env\": {}," + "\"capture_output\": true" + "}";
242 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
243 ChefResponse chefResponse = cac.post(chefAction, pushRequest);
244 code = chefResponse.getStatusCode();
245 logger.info("pushRequest:" + pushRequest);
246 logger.info("requestbody: {}", chefResponse.getBody());
247 String message = chefResponse.getBody();
248 if (code == STATUS_PUSHJOBCHECK) {
249 int startIndex = message.indexOf("jobs") + 5;
250 int endIndex = message.length() - 2;
251 String jobID = message.substring(startIndex, endIndex);
252 ctx.setAttribute("jobID", jobID);
255 chefServerResult(ctx, code, message);
257 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
259 } catch (Exception e) {
260 code = APPC_ERRORCODE;
261 logger.error(POSTING_REQUEST_ERROR_STR + "vnfcPushJob", e);
262 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + "vnfcPushJob" + e.getMessage());
266 @SuppressWarnings("nls")
268 public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
269 int code = STATUS_OK;
270 final String LOG_STR = "fetchResults";
272 chefInfo(params, ctx);
273 String nodeListS = params.get(NODE_LIST_STR);
274 if (StringUtils.isNotBlank(nodeListS)) {
275 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
276 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
277 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
278 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
279 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
280 JSONObject result = new JSONObject();
281 String returnMessage = StringUtils.EMPTY;
283 for (String node : nodes) {
284 String chefAction = "/nodes/" + node;
286 if (privateKeyChecker.doesExist(clientPrivatekey)) {
287 ChefResponse chefResponse = getApiMethod(chefAction);
288 code = chefResponse.getStatusCode();
289 message = chefResponse.getBody();
292 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
293 doFailure(ctx, code, message);
295 if (code == STATUS_OK) {
296 JSONObject nodeResult = new JSONObject();
297 JSONObject allNodeData = new JSONObject(message);
298 allNodeData = allNodeData.getJSONObject("normal");
299 String attribute = "PushJobOutput";
301 String resultData = allNodeData.optString(attribute, null);
302 if (resultData == null) {
303 resultData = Optional.ofNullable(allNodeData.optJSONObject(attribute))
304 .map(p -> p.toString()).orElse(null);
305 if (resultData == null) {
306 resultData = Optional.ofNullable(allNodeData.optJSONArray(attribute))
307 .map(p -> p.toString()).orElse(null);
309 if (resultData == null) {
311 returnMessage = "Cannot find " + attribute;
316 nodeResult.put(attribute, resultData);
317 result.put(node, nodeResult);
318 returnMessage = result.toString();
321 returnMessage = message + " Cannot access: " + node;
322 doFailure(ctx, code, message);
327 chefServerResult(ctx, code, returnMessage);
329 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
331 } catch (JSONException e) {
332 code = APPC_ERRORCODE;
333 logger.error(POSTING_REQUEST_JSON_ERROR_STR + LOG_STR, e);
334 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + LOG_STR + e.getMessage());
335 } catch (Exception e) {
336 code = APPC_ERRORCODE;
337 logger.error(POSTING_REQUEST_ERROR_STR + LOG_STR, e);
338 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + LOG_STR + e.getMessage());
342 private ChefResponse getApiMethod(String chefAction) {
343 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
344 return cac.get(chefAction);
350 @SuppressWarnings("nls")
352 public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) {
353 logger.info("nodeObejctBuilder");
354 String name = params.get("nodeobject.name");
355 String normal = params.get("nodeobject.normal");
356 String overrides = params.get("nodeobject.overrides");
357 String defaults = params.get("nodeobject.defaults");
358 String runList = params.get("nodeobject.run_list");
359 String chefEnvironment = params.get("nodeobject.chef_environment");
360 String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults
361 + "},\"chef_type\":\"node\",\"run_list\":[" + runList + "],\"override\":{" + overrides
362 + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\""
363 + chefEnvironment + "\",}";
364 logger.info(nodeObject);
365 ctx.setAttribute("chef.nodeObject", nodeObject);
369 * send get request to chef server
371 private void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
373 username = params.get("username");
374 serverAddress = params.get("serverAddress");
375 organizations = params.get("organizations");
376 if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(serverAddress)
377 && StringUtils.isNotBlank(organizations)) {
378 chefserver = "https://" + serverAddress + "/organizations/" + organizations;
379 clientPrivatekey = "/opt/onap/appc/chef/" + serverAddress + "/" + organizations + "/" + username + ".pem";
380 logger.info(" clientPrivatekey " + clientPrivatekey);
382 doFailure(ctx, APPC_ERRORCODE, "Missing mandatory param(s) such as username, serverAddress, organizations");
386 @SuppressWarnings("nls")
388 public void retrieveData(Map<String, String> params, SvcLogicContext ctx) {
389 String allConfigData = params.get("allConfig");
390 String key = params.get("key");
391 String dgContext = params.get("dgContext");
392 JSONObject jsonConfig = new JSONObject(allConfigData);
393 String contextData = fetchContextData(key, jsonConfig);
394 ctx.setAttribute(dgContext, contextData);
397 private String fetchContextData(String key, JSONObject jsonConfig) {
399 return jsonConfig.getString(key);
400 } catch (Exception e) {
401 logger.error("Failed getting string value corresponding to " + key + ". Trying to fetch nested json object",
404 return jsonConfig.getJSONObject(key).toString();
405 } catch (Exception ex) {
406 logger.error("Failed getting json object corresponding to " + key + ". Trying to fetch array", ex);
407 return jsonConfig.getJSONArray(key).toString();
412 @SuppressWarnings("nls")
414 public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
415 String string1 = params.get("String1");
416 String string2 = params.get("String2");
417 String dgContext = params.get("dgContext");
418 String contextData = string1 + string2;
419 ctx.setAttribute(dgContext, contextData);
423 * Send GET request to chef server
425 @SuppressWarnings("nls")
428 public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
429 logger.info("chef get method");
430 chefInfo(params, ctx);
431 String chefAction = params.get(CHEF_ACTION_STR);
434 if (privateKeyChecker.doesExist(clientPrivatekey)) {
435 ChefResponse chefResponse = getApiMethod(chefAction);
436 code = chefResponse.getStatusCode();
437 message = chefResponse.getBody();
440 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
442 chefServerResult(ctx, code, message);
446 * Send PUT request to chef server
448 @SuppressWarnings("nls")
451 public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
452 chefInfo(params, ctx);
453 String chefAction = params.get(CHEF_ACTION_STR);
454 String chefNodeStr = params.get("chefRequestBody");
457 if (privateKeyChecker.doesExist(clientPrivatekey)) {
458 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
460 ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr);
461 code = chefResponse.getStatusCode();
462 message = chefResponse.getBody();
465 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
467 logger.info(code + " " + message);
468 chefServerResult(ctx, code, message);
472 * send Post request to chef server
475 public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
476 chefInfo(params, ctx);
477 logger.info("chef Post method");
478 logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations);
479 String chefNodeStr = params.get("chefRequestBody");
480 String chefAction = params.get(CHEF_ACTION_STR);
483 // should load pem from somewhere else
484 if (privateKeyChecker.doesExist(clientPrivatekey)) {
485 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
487 // need pass path into it
489 ChefResponse chefResponse = chefApiClient.post(chefAction, chefNodeStr);
490 code = chefResponse.getStatusCode();
491 message = chefResponse.getBody();
494 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
496 logger.info(code + " " + message);
497 chefServerResult(ctx, code, message);
501 * send delete request to chef server
504 public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
505 logger.info("chef delete method");
506 chefInfo(params, ctx);
507 String chefAction = params.get(CHEF_ACTION_STR);
510 if (privateKeyChecker.doesExist(clientPrivatekey)) {
511 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
513 ChefResponse chefResponse = chefApiClient.delete(chefAction);
514 code = chefResponse.getStatusCode();
515 message = chefResponse.getBody();
518 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
520 logger.info(code + " " + message);
521 chefServerResult(ctx, code, message);
525 * Trigger target vm run chef
528 public void trigger(Map<String, String> params, SvcLogicContext svcLogicContext) {
529 logger.info("Run trigger method");
530 String tVmIp = params.get("ip");
532 ChefResponse chefResponse = chefApiClientFactory.create(tVmIp, organizations).get("");
533 chefClientResult(svcLogicContext, chefResponse.getStatusCode(), chefResponse.getBody());
534 svcLogicContext.setAttribute("chefAgent.code", STATUS_OK.toString());
535 } catch (Exception e) {
536 logger.error("An error occurred when executing trigger method", e);
537 svcLogicContext.setAttribute("chefAgent.code", KEY_NOTFOUND.toString());
538 svcLogicContext.setAttribute("chefAgent.message", e.toString());
542 @SuppressWarnings("nls")
544 public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
547 chefInfo(params, ctx);
548 String jobID = params.get("jobid");
549 String retry = params.get("retryTimes");
550 String intrva = params.get("retryInterval");
551 if (StringUtils.isNotBlank(jobID) && StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) {
553 int retryTimes = Integer.parseInt(params.get("retryTimes"));
554 int retryInterval = Integer.parseInt(params.get("retryInterval"));
555 String chefAction = "/pushy/jobs/" + jobID;
556 String message = StringUtils.EMPTY;
557 String status = StringUtils.EMPTY;
558 for (int i = 0; i < retryTimes; i++) {
559 sleepFor(retryInterval);
560 ChefResponse chefResponse = getApiMethod(chefAction);
561 code = chefResponse.getStatusCode();
562 message = chefResponse.getBody();
563 JSONObject obj = new JSONObject(message);
564 status = obj.getString("status");
565 if (!"running".equals(status)) {
566 logger.info(i + " time " + code + " " + status);
570 resolveSvcLogicAttributes(ctx, message, status);
572 throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
574 } catch (Exception e) {
575 code = APPC_ERRORCODE;
576 logger.error("An error occurred when executing checkPushJob method", e);
577 doFailure(ctx, code, e.getMessage());
581 private void resolveSvcLogicAttributes(SvcLogicContext svcLogic, String message, String status) {
582 if ("complete".equals(status)) {
583 if (hasFailedNode(message)) {
584 String finalMessage = "PushJob Status Complete but check failed nodes in the message :" + message;
585 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, APPC_ERRORCODE.toString());
586 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, finalMessage);
588 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, STATUS_OK.toString());
589 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
591 } else if ("running".equals(status)) {
592 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, PUSHJOBSTATUS.toString());
593 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, "chef client runtime out");
595 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, KEY_NOTFOUND.toString());
596 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
600 private Boolean hasFailedNode(String message) throws JSONException {
602 JSONObject messageJson = new JSONObject(message);
603 JSONObject node = messageJson.getJSONObject("nodes");
604 final String failed = "failed";
606 logger.debug("Status Complete but node details in the message is null : " + message);
609 if (node.has(failed) && !(node.isNull(failed)) && (node.getJSONArray(failed).length() != 0)) {
610 logger.debug("Status Complete but one or more Failed nodes ....FAILURE " + message);
613 logger.debug("Status Complete and no failed nodes ....SUCCESS " + message);
614 return Boolean.FALSE;
615 } catch (JSONException e) {
616 logger.error("Exception occured in hasFailedNode", e);
617 throw new JSONException("Exception occured in hasFailedNode" + e.getMessage());
622 private void sleepFor(int retryInterval) {
624 Thread.sleep(retryInterval); // 1000 milliseconds is one second.
625 } catch (InterruptedException ex) {
626 Thread.currentThread().interrupt();
630 @SuppressWarnings("nls")
632 public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
635 chefInfo(params, ctx);
636 String pushRequest = params.get("pushRequest");
637 String chefAction = "/pushy/jobs";
638 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
640 ChefResponse chefResponse = chefApiClient.post(chefAction, pushRequest);
641 code = chefResponse.getStatusCode();
642 String message = chefResponse.getBody();
643 if (code == STATUS_PUSHJOBCHECK) {
644 int startIndex = message.indexOf("jobs") + 6;
645 int endIndex = message.length() - 2;
646 String jobID = message.substring(startIndex, endIndex);
647 ctx.setAttribute("jobID", jobID);
650 chefServerResult(ctx, code, message);
651 } catch (Exception e) {
652 code = APPC_ERRORCODE;
653 logger.error("An error occurred when executing pushJob method", e);
654 doFailure(ctx, code, e.getMessage());
658 @SuppressWarnings("static-method")
659 private void chefServerResult(SvcLogicContext svcLogicContext, int code, String message) {
660 initSvcLogic(svcLogicContext, code, message, "server");
663 @SuppressWarnings("static-method")
664 private void chefClientResult(SvcLogicContext svcLogicContext, int code, String message) {
665 initSvcLogic(svcLogicContext, code, message, "client");
668 private void initSvcLogic(SvcLogicContext svcLogicContext, int code, String message, String target) {
670 String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR;
671 String messageStr = "client".equals(target) ? CHEF_CLIENT_RESULT_MSG_STR : CHEF_SERVER_RESULT_MSG_STR;
672 svcLogicContext.setStatus(OUTCOME_SUCCESS);
673 svcLogicContext.setAttribute(codeStr, Integer.toString(code));
674 svcLogicContext.setAttribute(messageStr, message);
675 logger.info(codeStr + ": " + svcLogicContext.getAttribute(codeStr));
676 logger.info(messageStr + ": " + svcLogicContext.getAttribute(messageStr));
679 @SuppressWarnings("static-method")
680 private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
682 String cutMessage = message.contains("\n") ? message.substring(message.indexOf('\n')) : message;
683 svcLogic.setStatus(OUTCOME_FAILURE);
684 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, Integer.toString(code));
685 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, cutMessage);
686 throw new SvcLogicException("Chef Adapter error:" + cutMessage);