2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2017 Amdocs
 
   8  * =============================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  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 org.apache.commons.lang.StringUtils;
 
  32 import org.json.JSONException;
 
  33 import org.json.JSONObject;
 
  34 import org.onap.appc.adapter.chef.ChefAdapter;
 
  35 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
 
  36 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
 
  37 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
 
  38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  42  * This class implements the {@link ChefAdapter} interface. This interface defines the behaviors that our service
 
  45 public class ChefAdapterImpl implements ChefAdapter {
 
  47     // chef server Initialize variable
 
  48     private String username = StringUtils.EMPTY;
 
  49     private String clientPrivatekey = StringUtils.EMPTY;
 
  50     private String chefserver = StringUtils.EMPTY;
 
  51     private String serverAddress = StringUtils.EMPTY;
 
  52     private String organizations = StringUtils.EMPTY;
 
  54     @SuppressWarnings("nls")
 
  55     public static final String MDC_ADAPTER = "adapter";
 
  57     @SuppressWarnings("nls")
 
  58     public static final String MDC_SERVICE = "service";
 
  60     @SuppressWarnings("nls")
 
  61     public static final String OUTCOME_FAILURE = "failure";
 
  63     @SuppressWarnings("nls")
 
  64     public static final String OUTCOME_SUCCESS = "success";
 
  66     @SuppressWarnings("nls")
 
  67     public static final String PROPERTY_PROVIDER = "provider";
 
  69     @SuppressWarnings("nls")
 
  70     public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
 
  72     @SuppressWarnings("nls")
 
  73     public static final String PROPERTY_PROVIDER_NAME = "name";
 
  75     @SuppressWarnings("nls")
 
  76     public static final String PROPERTY_PROVIDER_TENANT = "tenant";
 
  78     @SuppressWarnings("nls")
 
  79     public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
 
  81     @SuppressWarnings("nls")
 
  82     public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
 
  84     @SuppressWarnings("nls")
 
  85     public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
 
  87     @SuppressWarnings("nls")
 
  88     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 =
 
  94         "Cannot find the private key in the APPC file system, please load the private key to ";
 
  96     private static final String POSTING_REQUEST_JSON_ERROR_STR = "Error posting request due to invalid JSON block: ";
 
  97     private static final String POSTING_REQUEST_ERROR_STR = "Error posting request: ";
 
  98     private static final String CHEF_CLIENT_RESULT_CODE_STR = "chefClientResult.code";
 
  99     private static final String CHEF_SERVER_RESULT_CODE_STR = "chefServerResult.code";
 
 100     private static final String CHEF_CLIENT_RESULT_MSG_STR = "chefClientResult.message";
 
 101     private static final String CHEF_SERVER_RESULT_MSG_STR = "chefServerResult.message";
 
 102     private static final String CHEF_ACTION_STR = "chefAction";
 
 103     private static final String NODE_LIST_STR = "NodeList";
 
 104     private final ChefApiClientFactory chefApiClientFactory;
 
 105     private final PrivateKeyChecker privateKeyChecker;
 
 107     ChefAdapterImpl(ChefApiClientFactory chefApiClientFactory, PrivateKeyChecker privateKeyChecker) {
 
 108         this.chefApiClientFactory = chefApiClientFactory;
 
 109         this.privateKeyChecker = privateKeyChecker;
 
 110         logger.info("Initialize Chef Adapter");
 
 113     @SuppressWarnings("nls")
 
 115     public void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 118             logger.info("environment of VNF-C");
 
 119             chefInfo(params, ctx);
 
 120             RequestContext rc = new RequestContext(ctx);
 
 121             logger.info("Context" + ctx);
 
 123             String env = params.get("Environment");
 
 124             logger.info("Environmnet" + env);
 
 125             if (env.equals(StringUtils.EMPTY)) {
 
 126                 chefServerResult(rc, 200, "Skip Environment block ");
 
 128                 JSONObject envJ = new JSONObject(env);
 
 129                 String envName = envJ.getString("name");
 
 131                 if (privateKeyChecker.doesExist(clientPrivatekey)) {
 
 132                     // update the details of an environment on the Chef server.
 
 133                     ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 134                     ChefResponse chefResponse = chefApiClient.put("/environments/" + envName, env);
 
 135                     code = chefResponse.getStatusCode();
 
 136                     message = chefResponse.getBody();
 
 138                         // need create a new environment
 
 139                         chefResponse = chefApiClient.post("/environments", env);
 
 140                         code = chefResponse.getStatusCode();
 
 141                         message = chefResponse.getBody();
 
 142                         logger.info("requestbody {}", chefResponse.getBody());
 
 147                     message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
 
 148                     doFailure(ctx, code, message);
 
 150                 chefServerResult(rc, code, message);
 
 152         } catch (JSONException e) {
 
 154             logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
 
 155             doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
 
 156         } catch (Exception e) {
 
 158             logger.error(POSTING_REQUEST_ERROR_STR, e);
 
 159             doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
 
 163     @SuppressWarnings("nls")
 
 165     public void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 166         logger.info("update the nodeObjects of VNF-C");
 
 169             chefInfo(params, ctx);
 
 170             String nodeListS = params.get(NODE_LIST_STR);
 
 171             String nodeS = params.get("Node");
 
 172             if (StringUtils.isNotBlank(nodeListS) && StringUtils.isNotBlank(nodeS)) {
 
 173                 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
 
 174                 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
 
 175                 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
 
 176                 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
 
 177                 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
 
 178                 RequestContext rc = new RequestContext(ctx);
 
 181                 String message = null;
 
 182                 if (privateKeyChecker.doesExist(clientPrivatekey)) {
 
 183                     ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 185                     for (String nodeName: nodes) {
 
 186                         JSONObject nodeJ = new JSONObject(nodeS);
 
 187                         nodeJ.remove("name");
 
 188                         nodeJ.put("name", nodeName);
 
 189                         String nodeObject = nodeJ.toString();
 
 190                         logger.info(nodeObject);
 
 191                         ChefResponse chefResponse = cac.put("/nodes/" + nodeName, nodeObject);
 
 192                         code = chefResponse.getStatusCode();
 
 193                         message = chefResponse.getBody();
 
 200                     message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
 
 201                     doFailure(ctx, code, message);
 
 203                 chefServerResult(rc, code, message);
 
 205                 throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList ");
 
 207         } catch (JSONException e) {
 
 209             logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
 
 210             doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
 
 211         } catch (Exception e) {
 
 213             logger.error(POSTING_REQUEST_ERROR_STR, e);
 
 214             doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
 
 219     public void vnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 222             chefInfo(params, ctx);
 
 223             String nodeList = params.get(NODE_LIST_STR);
 
 224             if (StringUtils.isNotBlank(nodeList)) {
 
 225                 String isCallback = params.get("CallbackCapable");
 
 226                 String chefAction = "/pushy/jobs";
 
 229                 if ("true".equals(isCallback)) {
 
 230                     String requestId = params.get("RequestId");
 
 231                     String callbackUrl = params.get("CallbackUrl");
 
 232                     pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
 
 233                         + nodeList + "," + "\"env\": {\"RequestId\": \"" + requestId + "\", \"CallbackUrl\": \""
 
 234                         + callbackUrl + "\"}," + "\"capture_output\": true" + "}";
 
 236                     pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
 
 237                         + nodeList + "," + "\"env\": {}," + "\"capture_output\": true" + "}";
 
 239                 RequestContext rc = new RequestContext(ctx);
 
 242                 SvcLogicContext svcLogic = rc.getSvcLogicContext();
 
 243                 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 244                 ChefResponse chefResponse = cac.post(chefAction, pushRequest);
 
 245                 code = chefResponse.getStatusCode();
 
 246                 logger.info("pushRequest:" + pushRequest);
 
 247                 logger.info("requestbody: {}", chefResponse.getBody());
 
 248                 String message = chefResponse.getBody();
 
 250                     int startIndex = message.indexOf("jobs") + 5;
 
 251                     int endIndex = message.length() - 2;
 
 252                     String jobID = message.substring(startIndex, endIndex);
 
 253                     svcLogic.setAttribute("jobID", jobID);
 
 256                 chefServerResult(rc, code, message);
 
 258                 throw new SvcLogicException("Missing Mandatory param(s)  NodeList ");
 
 260         } catch (JSONException e) {
 
 262             logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
 
 263             doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
 
 264         } catch (Exception e) {
 
 266             logger.error(POSTING_REQUEST_ERROR_STR, e);
 
 267             doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
 
 271     @SuppressWarnings("nls")
 
 273     public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 276             chefInfo(params, ctx);
 
 277             String nodeListS = params.get(NODE_LIST_STR);
 
 278             if (StringUtils.isNotBlank(nodeListS)) {
 
 279                 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
 
 280                 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
 
 281                 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
 
 282                 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
 
 283                 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
 
 284                 JSONObject result = new JSONObject();
 
 285                 String returnMessage = StringUtils.EMPTY;
 
 287                 for (String node : nodes) {
 
 288                     String chefAction = "/nodes/" + node;
 
 290                     if (privateKeyChecker.doesExist(clientPrivatekey)) {
 
 291                         ChefResponse chefResponse = getApiMethod(chefAction);
 
 292                         code = chefResponse.getStatusCode();
 
 293                         message = chefResponse.getBody();
 
 296                         message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
 
 297                         doFailure(ctx, code, message);
 
 300                         JSONObject nodeResult = new JSONObject();
 
 301                         JSONObject allNodeData = new JSONObject(message);
 
 302                         allNodeData = allNodeData.getJSONObject("normal");
 
 303                         String attribute = "PushJobOutput";
 
 305                         String resultData = allNodeData.optString(attribute);
 
 306                         if (resultData == null) {
 
 307                             resultData = allNodeData.optJSONObject(attribute).toString();
 
 309                             if (resultData == null) {
 
 310                                 resultData = allNodeData.optJSONArray(attribute).toString();
 
 312                                 if (resultData == null) {
 
 314                                     returnMessage = "Cannot find " + attribute;
 
 319                         nodeResult.put(attribute, resultData);
 
 320                         result.put(node, nodeResult);
 
 321                         returnMessage = result.toString();
 
 324                         returnMessage = message + " Cannot access: " + node;
 
 325                         doFailure(ctx, code, message);
 
 330                 RequestContext rc = new RequestContext(ctx);
 
 332                 chefServerResult(rc, code, returnMessage);
 
 334                 throw new SvcLogicException("Missing Mandatory param(s)  NodeList ");
 
 336         } catch (JSONException e) {
 
 338             logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
 
 339             doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
 
 340         } catch (Exception e) {
 
 342             logger.error(POSTING_REQUEST_ERROR_STR , e);
 
 343             doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
 
 347     private ChefResponse getApiMethod(String chefAction) {
 
 348         ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 349         return cac.get(chefAction);
 
 355     @SuppressWarnings("nls")
 
 357     public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) {
 
 358         logger.info("nodeObejctBuilder");
 
 359         String name = params.get("nodeobject.name");
 
 360         String normal = params.get("nodeobject.normal");
 
 361         String overrides = params.get("nodeobject.overrides");
 
 362         String defaults = params.get("nodeobject.defaults");
 
 363         String runList = params.get("nodeobject.run_list");
 
 364         String chefEnvironment = params.get("nodeobject.chef_environment");
 
 365         String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults
 
 366             + "},\"chef_type\":\"node\",\"run_list\":[" + runList + "],\"override\":{" + overrides
 
 367             + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\""
 
 368             + chefEnvironment + "\",}";
 
 369         logger.info(nodeObject);
 
 370         ctx.setAttribute("chef.nodeObject", nodeObject);
 
 374      * send get request to chef server
 
 376     private void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 378         username = params.get("username");
 
 379         serverAddress = params.get("serverAddress");
 
 380         organizations = params.get("organizations");
 
 381         if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(serverAddress)
 
 382             && StringUtils.isNotBlank(organizations)) {
 
 383             chefserver = "https://" + serverAddress + "/organizations/" + organizations;
 
 384             clientPrivatekey = "/opt/onap/appc/chef/" + serverAddress + "/" + organizations + "/" + username + ".pem";
 
 385             logger.info(" clientPrivatekey  " + clientPrivatekey);
 
 387             doFailure(ctx, 401, "Missing mandatory param(s) such as username, serverAddress, organizations");
 
 391     @SuppressWarnings("nls")
 
 393     public void retrieveData(Map<String, String> params, SvcLogicContext ctx) {
 
 394         String allConfigData = params.get("allConfig");
 
 395         String key = params.get("key");
 
 396         String dgContext = params.get("dgContext");
 
 397         JSONObject jsonConfig = new JSONObject(allConfigData);
 
 398         String contextData = fetchContextData(key, jsonConfig);
 
 400         ctx.setAttribute(dgContext, contextData);
 
 403     private String fetchContextData(String key, JSONObject jsonConfig) {
 
 405             return jsonConfig.getString(key);
 
 406         } catch (Exception e) {
 
 407             logger.error("Failed getting string value corresponding to " + key + ". Trying to fetch nested json object", e);
 
 409                 return jsonConfig.getJSONObject(key).toString();
 
 410             } catch (Exception ex) {
 
 411                 logger.error("Failed getting json object corresponding to " + key + ". Trying to fetch array", ex);
 
 412                 return jsonConfig.getJSONArray(key).toString();
 
 417     @SuppressWarnings("nls")
 
 419     public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
 
 420         String string1 = params.get("String1");
 
 421         String string2 = params.get("String2");
 
 422         String dgContext = params.get("dgContext");
 
 423         String contextData = string1 + string2;
 
 424         ctx.setAttribute(dgContext, contextData);
 
 428      * Send GET request to chef server
 
 430     @SuppressWarnings("nls")
 
 433     public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 434         logger.info("chef get method");
 
 435         chefInfo(params, ctx);
 
 436         String chefAction = params.get(CHEF_ACTION_STR);
 
 437         RequestContext rc = new RequestContext(ctx);
 
 442         if (privateKeyChecker.doesExist(clientPrivatekey)) {
 
 443             ChefResponse chefResponse = getApiMethod(chefAction);
 
 444             code = chefResponse.getStatusCode();
 
 445             message = chefResponse.getBody();
 
 448             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
 
 450         chefServerResult(rc, code, message);
 
 454      * Send PUT request to chef server
 
 456     @SuppressWarnings("nls")
 
 459     public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 460         chefInfo(params, ctx);
 
 461         String chefAction = params.get(CHEF_ACTION_STR);
 
 462         String chefNodeStr = params.get("chefRequestBody");
 
 463         RequestContext rc = new RequestContext(ctx);
 
 467         if (privateKeyChecker.doesExist(clientPrivatekey)) {
 
 468             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 470             ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr);
 
 471             code = chefResponse.getStatusCode();
 
 472             message = chefResponse.getBody();
 
 475             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
 
 477         logger.info(code + "   " + message);
 
 478         chefServerResult(rc, code, message);
 
 482      * send Post request to chef server
 
 485     public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 486         chefInfo(params, ctx);
 
 487         logger.info("chef Post method");
 
 488         logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations);
 
 489         String chefNodeStr = params.get("chefRequestBody");
 
 490         String chefAction = params.get(CHEF_ACTION_STR);
 
 492         RequestContext rc = new RequestContext(ctx);
 
 496         // should load pem from somewhere else
 
 497         if (privateKeyChecker.doesExist(clientPrivatekey)) {
 
 498             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 500             // need pass path into it
 
 502             ChefResponse chefResponse = chefApiClient.post(chefAction, chefNodeStr);
 
 503             code = chefResponse.getStatusCode();
 
 504             message = chefResponse.getBody();
 
 507             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
 
 509         logger.info(code + "   " + message);
 
 510         chefServerResult(rc, code, message);
 
 514      * send delete request to chef server
 
 517     public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 518         logger.info("chef delete method");
 
 519         chefInfo(params, ctx);
 
 520         String chefAction = params.get(CHEF_ACTION_STR);
 
 521         RequestContext rc = new RequestContext(ctx);
 
 525         if (privateKeyChecker.doesExist(clientPrivatekey)) {
 
 526             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 527             ChefResponse chefResponse = chefApiClient.delete(chefAction);
 
 528             code = chefResponse.getStatusCode();
 
 529             message = chefResponse.getBody();
 
 532             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
 
 534         logger.info(code + "   " + message);
 
 535         chefServerResult(rc, code, message);
 
 539      * Trigger target vm run chef
 
 542     public void trigger(Map<String, String> params, SvcLogicContext svcLogicContext) {
 
 543         logger.info("Run trigger method");
 
 544         String tVmIp = params.get("ip");
 
 545         RequestContext rc = new RequestContext(svcLogicContext);
 
 549             ChefResponse chefResponse = chefApiClientFactory.create(tVmIp).get("");
 
 550             chefClientResult(rc, chefResponse.getStatusCode(), chefResponse.getBody());
 
 551             svcLogicContext.setAttribute("chefAgent.code", "200");
 
 552         } catch (Exception e) {
 
 553             logger.error("An error occurred when executing trigger method", e);
 
 554             svcLogicContext.setAttribute("chefAgent.code", "500");
 
 555             svcLogicContext.setAttribute("chefAgent.message", e.toString());
 
 559     @SuppressWarnings("nls")
 
 561     public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 564             chefInfo(params, ctx);
 
 565             String jobID = params.get("jobid");
 
 566             String retry = params.get("retryTimes");
 
 567             String intrva = params.get("retryInterval");
 
 568             if (StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) {
 
 570                 int retryTimes = Integer.parseInt(params.get("retryTimes"));
 
 571                 int retryInterval = Integer.parseInt(params.get("retryInterval"));
 
 573                 String chefAction = "/pushy/jobs/" + jobID;
 
 575                 RequestContext rc = new RequestContext(ctx);
 
 577                 SvcLogicContext svcLogic = rc.getSvcLogicContext();
 
 578                 String message = StringUtils.EMPTY;
 
 579                 String status = StringUtils.EMPTY;
 
 580                 for (int i = 0; i < retryTimes; i++) {
 
 581                     sleepFor(retryInterval);
 
 582                     ChefResponse chefResponse = getApiMethod(chefAction);
 
 583                     code = chefResponse.getStatusCode();
 
 584                     message = chefResponse.getBody();
 
 585                     JSONObject obj = new JSONObject(message);
 
 586                     status = obj.getString("status");
 
 587                     if (!"running".equals(status)) {
 
 588                         logger.info(i + " time " + code + "   " + status);
 
 592                 resolveSvcLogicAttributes(svcLogic, message, status);
 
 594                 throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
 
 596         } catch (Exception e) {
 
 598             logger.error("An error occurred when executing checkPushJob method", e);
 
 599             doFailure(ctx, code, e.getMessage());
 
 603     private void resolveSvcLogicAttributes(SvcLogicContext svcLogic, String message, String status) {
 
 604         if ("complete".equals(status)) {
 
 605             svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "200");
 
 606             svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
 
 608             if ("running".equals(status)) {
 
 609                 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "202");
 
 610                 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, "chef client runtime out");
 
 612                 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "500");
 
 613                 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
 
 618     private void sleepFor(int retryInterval) {
 
 620             Thread.sleep(retryInterval); // 1000 milliseconds is one second.
 
 621         } catch (InterruptedException ex) {
 
 622             Thread.currentThread().interrupt();
 
 626     @SuppressWarnings("nls")
 
 628     public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
 
 631             chefInfo(params, ctx);
 
 632             String pushRequest = params.get("pushRequest");
 
 633             String chefAction = "/pushy/jobs";
 
 634             RequestContext rc = new RequestContext(ctx);
 
 636             SvcLogicContext svcLogic = rc.getSvcLogicContext();
 
 637             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
 
 638             ChefResponse chefResponse = chefApiClient.post(chefAction, pushRequest);
 
 640             code = chefResponse.getStatusCode();
 
 641             String message = chefResponse.getBody();
 
 643                 int startIndex = message.indexOf("jobs") + 6;
 
 644                 int endIndex = message.length() - 2;
 
 645                 String jobID = message.substring(startIndex, endIndex);
 
 646                 svcLogic.setAttribute("jobID", jobID);
 
 649             chefServerResult(rc, code, message);
 
 650         } catch (Exception e) {
 
 652             logger.error("An error occurred when executing pushJob method", e);
 
 653             doFailure(ctx, code, e.getMessage());
 
 657     @SuppressWarnings("static-method")
 
 658     private void chefServerResult(RequestContext rc, int code, String message) {
 
 659         initSvcLogic(rc, code, message, "server");
 
 662     @SuppressWarnings("static-method")
 
 663     private void chefClientResult(RequestContext rc, int code, String message) {
 
 664         initSvcLogic(rc, code, message, "client");
 
 667     private void initSvcLogic(RequestContext rc, int code, String message, String target) {
 
 669         SvcLogicContext svcLogic = rc.getSvcLogicContext();
 
 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;
 
 673         svcLogic.setStatus(OUTCOME_SUCCESS);
 
 674         svcLogic.setAttribute(codeStr, Integer.toString(code));
 
 675         svcLogic.setAttribute(messageStr, message);
 
 676         logger.info(codeStr + ": " + svcLogic.getAttribute(codeStr));
 
 677         logger.info(messageStr + ": " + svcLogic.getAttribute(messageStr));
 
 680     @SuppressWarnings("static-method")
 
 681     private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
 
 683         String cutMessage = message.contains("\n") ? message.substring(message.indexOf('\n')) : message;
 
 685         svcLogic.setStatus(OUTCOME_FAILURE);
 
 686         svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, Integer.toString(code));
 
 687         svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, cutMessage);
 
 689         throw new SvcLogicException("Chef Adapter error:" + cutMessage);