6a8664acb96b5ebfde7ec96ac374dd16f1bd2a9a
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / impl / ChefAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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=========================================================
23  */
24 package org.onap.appc.adapter.chef.impl;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.Map;
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;
41
42 /**
43  * This class implements the {@link ChefAdapter} interface. This interface
44  * defines the behaviors that our service provides.
45  */
46 public class ChefAdapterImpl implements ChefAdapter {
47
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;
54
55     @SuppressWarnings("nls")
56     public static final String MDC_ADAPTER = "adapter";
57
58     @SuppressWarnings("nls")
59     public static final String MDC_SERVICE = "service";
60
61     @SuppressWarnings("nls")
62     public static final String OUTCOME_FAILURE = "failure";
63
64     @SuppressWarnings("nls")
65     public static final String OUTCOME_SUCCESS = "success";
66
67     @SuppressWarnings("nls")
68     public static final String PROPERTY_PROVIDER = "provider";
69
70     @SuppressWarnings("nls")
71     public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
72
73     @SuppressWarnings("nls")
74     public static final String PROPERTY_PROVIDER_NAME = "name";
75
76     @SuppressWarnings("nls")
77     public static final String PROPERTY_PROVIDER_TENANT = "tenant";
78
79     @SuppressWarnings("nls")
80     public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
81
82     @SuppressWarnings("nls")
83     public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
84
85     @SuppressWarnings("nls")
86     public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
87
88     @SuppressWarnings("nls")
89     public static final String PROPERTY_PROVIDER_TYPE = "type";
90
91     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdapterImpl.class);
92
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 ";
94
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;
109
110     private final ChefApiClientFactory chefApiClientFactory;
111     private final PrivateKeyChecker privateKeyChecker;
112
113     ChefAdapterImpl(ChefApiClientFactory chefApiClientFactory, PrivateKeyChecker privateKeyChecker) {
114         this.chefApiClientFactory = chefApiClientFactory;
115         this.privateKeyChecker = privateKeyChecker;
116         logger.info("Initialize Chef Adapter");
117     }
118
119     @SuppressWarnings("nls")
120     @Override
121     public void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
122         int code;
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 ");
129         } else {
130             String message;
131             if (privateKeyChecker.doesExist(clientPrivatekey)) {
132                 try {
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,
137                             clientPrivatekey);
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());
147                     }
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());
157                 }
158             } else {
159                 code = KEY_NOTFOUND;
160                 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
161                 doFailure(ctx, code, message);
162             }
163         }
164     }
165
166     @SuppressWarnings("nls")
167     @Override
168     public void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
169         logger.info("update the nodeObjects of VNF-C");
170         int code;
171         final String LOG_ERR_METHOD_STR = "vnfcNodeobjects";
172         try {
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*"));
182                 code = STATUS_OK;
183                 String message = null;
184                 if (privateKeyChecker.doesExist(clientPrivatekey)) {
185                     ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username,
186                             clientPrivatekey);
187
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) {
198                             break;
199                         }
200                     }
201                 } else {
202                     code = KEY_NOTFOUND;
203                     message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
204                     doFailure(ctx, code, message);
205                 }
206                 chefServerResult(ctx, code, message);
207             } else {
208                 throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList ");
209             }
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());
218         }
219     }
220
221     @Override
222     public void vnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
223         int code;
224         try {
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";
230                 // need work on this
231                 String pushRequest;
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" + "}";
238                 } else {
239                     pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
240                             + nodeList + "," + "\"env\": {}," + "\"capture_output\": true" + "}";
241                 }
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);
253                     logger.info(jobID);
254                 }
255                 chefServerResult(ctx, code, message);
256             } else {
257                 throw new SvcLogicException("Missing Mandatory param(s)  NodeList ");
258             }
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());
263         }
264     }
265
266     @SuppressWarnings("nls")
267     @Override
268     public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
269         int code = STATUS_OK;
270         final String LOG_STR = "fetchResults";
271         try {
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;
282
283                 for (String node : nodes) {
284                     String chefAction = "/nodes/" + node;
285                     String message;
286                     if (privateKeyChecker.doesExist(clientPrivatekey)) {
287                         ChefResponse chefResponse = getApiMethod(chefAction);
288                         code = chefResponse.getStatusCode();
289                         message = chefResponse.getBody();
290                     } else {
291                         code = KEY_NOTFOUND;
292                         message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
293                         doFailure(ctx, code, message);
294                     }
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";
300
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);
308
309                                 if (resultData == null) {
310                                     code = KEY_NOTFOUND;
311                                     returnMessage = "Cannot find " + attribute;
312                                     break;
313                                 }
314                             }
315                         }
316                         nodeResult.put(attribute, resultData);
317                         result.put(node, nodeResult);
318                         returnMessage = result.toString();
319                     } else {
320                         code = KEY_NOTFOUND;
321                         returnMessage = message + " Cannot access: " + node;
322                         doFailure(ctx, code, message);
323                         break;
324                     }
325                 }
326
327                 chefServerResult(ctx, code, returnMessage);
328             } else {
329                 throw new SvcLogicException("Missing Mandatory param(s)  NodeList ");
330             }
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());
339         }
340     }
341
342     private ChefResponse getApiMethod(String chefAction) {
343         ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
344         return cac.get(chefAction);
345     }
346
347     /**
348      * build node object
349      */
350     @SuppressWarnings("nls")
351     @Override
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);
366     }
367
368     /**
369      * send get request to chef server
370      */
371     private void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
372
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);
381         } else {
382             doFailure(ctx, APPC_ERRORCODE, "Missing mandatory param(s) such as username, serverAddress, organizations");
383         }
384     }
385
386     @SuppressWarnings("nls")
387     @Override
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);
395     }
396
397     private String fetchContextData(String key, JSONObject jsonConfig) {
398         try {
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",
402                     e);
403             try {
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();
408             }
409         }
410     }
411
412     @SuppressWarnings("nls")
413     @Override
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);
420     }
421
422     /**
423      * Send GET request to chef server
424      */
425     @SuppressWarnings("nls")
426
427     @Override
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);
432         int code;
433         String message;
434         if (privateKeyChecker.doesExist(clientPrivatekey)) {
435             ChefResponse chefResponse = getApiMethod(chefAction);
436             code = chefResponse.getStatusCode();
437             message = chefResponse.getBody();
438         } else {
439             code = KEY_NOTFOUND;
440             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
441         }
442         chefServerResult(ctx, code, message);
443     }
444
445     /**
446      * Send PUT request to chef server
447      */
448     @SuppressWarnings("nls")
449
450     @Override
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");
455         int code;
456         String message;
457         if (privateKeyChecker.doesExist(clientPrivatekey)) {
458             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
459                     clientPrivatekey);
460             ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr);
461             code = chefResponse.getStatusCode();
462             message = chefResponse.getBody();
463         } else {
464             code = KEY_NOTFOUND;
465             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
466         }
467         logger.info(code + "   " + message);
468         chefServerResult(ctx, code, message);
469     }
470
471     /**
472      * send Post request to chef server
473      */
474     @Override
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);
481         int code;
482         String message;
483         // should load pem from somewhere else
484         if (privateKeyChecker.doesExist(clientPrivatekey)) {
485             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
486                     clientPrivatekey);
487             // need pass path into it
488             // "/nodes/testnode"
489             ChefResponse chefResponse = chefApiClient.post(chefAction, chefNodeStr);
490             code = chefResponse.getStatusCode();
491             message = chefResponse.getBody();
492         } else {
493             code = KEY_NOTFOUND;
494             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
495         }
496         logger.info(code + "   " + message);
497         chefServerResult(ctx, code, message);
498     }
499
500     /**
501      * send delete request to chef server
502      */
503     @Override
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);
508         int code;
509         String message;
510         if (privateKeyChecker.doesExist(clientPrivatekey)) {
511             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
512                     clientPrivatekey);
513             ChefResponse chefResponse = chefApiClient.delete(chefAction);
514             code = chefResponse.getStatusCode();
515             message = chefResponse.getBody();
516         } else {
517             code = KEY_NOTFOUND;
518             message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
519         }
520         logger.info(code + "   " + message);
521         chefServerResult(ctx, code, message);
522     }
523
524     /**
525      * Trigger target vm run chef
526      */
527     @Override
528     public void trigger(Map<String, String> params, SvcLogicContext svcLogicContext) {
529         logger.info("Run trigger method");
530         String tVmIp = params.get("ip");
531         try {
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());
539         }
540     }
541
542     @SuppressWarnings("nls")
543     @Override
544     public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
545         int code;
546         try {
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)) {
552
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);
567                         break;
568                     }
569                 }
570                 resolveSvcLogicAttributes(ctx, message, status);
571             } else {
572                 throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
573             }
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());
578         }
579     }
580
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);
587             } else {
588                 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, STATUS_OK.toString());
589                 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
590             }
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");
594         } else {
595             svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, KEY_NOTFOUND.toString());
596             svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
597         }
598     }
599
600     private Boolean hasFailedNode(String message) throws JSONException {
601         try {   
602             JSONObject messageJson = new JSONObject(message);
603             JSONObject node = messageJson.getJSONObject("nodes");
604             final String failed = "failed";
605             if (node == null) {
606                 logger.debug("Status Complete but node details in the message is null : " + message);
607                 return Boolean.TRUE;
608             }
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);
611                 return Boolean.TRUE;
612             }
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());
618         }
619
620     }
621
622     private void sleepFor(int retryInterval) {
623         try {
624             Thread.sleep(retryInterval); // 1000 milliseconds is one second.
625         } catch (InterruptedException ex) {
626             Thread.currentThread().interrupt();
627         }
628     }
629
630     @SuppressWarnings("nls")
631     @Override
632     public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
633         int code;
634         try {
635             chefInfo(params, ctx);
636             String pushRequest = params.get("pushRequest");
637             String chefAction = "/pushy/jobs";
638             ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username,
639                     clientPrivatekey);
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);
648                 logger.info(jobID);
649             }
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());
655         }
656     }
657
658     @SuppressWarnings("static-method")
659     private void chefServerResult(SvcLogicContext svcLogicContext, int code, String message) {
660         initSvcLogic(svcLogicContext, code, message, "server");
661     }
662
663     @SuppressWarnings("static-method")
664     private void chefClientResult(SvcLogicContext svcLogicContext, int code, String message) {
665         initSvcLogic(svcLogicContext, code, message, "client");
666     }
667
668     private void initSvcLogic(SvcLogicContext svcLogicContext, int code, String message, String target) {
669
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));
677     }
678
679     @SuppressWarnings("static-method")
680     private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
681
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);
687     }
688 }