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;
27 import java.util.Arrays;
28 import java.util.List;
30 import java.util.Properties;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.http.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.apache.http.impl.client.HttpClients;
37 import org.apache.http.util.EntityUtils;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40 import org.onap.appc.adapter.chef.ChefAdapter;
41 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
42 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
43 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
50 * This class implements the {@link ChefAdapter} interface. This interface defines the behaviors that our service
53 public class ChefAdapterImpl implements ChefAdapter {
55 // chef server Initialize variable
56 private String username = StringUtils.EMPTY;
57 private String clientPrivatekey = StringUtils.EMPTY;
58 private String chefserver = StringUtils.EMPTY;
59 private String serverAddress = StringUtils.EMPTY;
60 private String organizations = StringUtils.EMPTY;
62 @SuppressWarnings("nls")
63 public static final String MDC_ADAPTER = "adapter";
65 @SuppressWarnings("nls")
66 public static final String MDC_SERVICE = "service";
68 @SuppressWarnings("nls")
69 public static final String OUTCOME_FAILURE = "failure";
71 @SuppressWarnings("nls")
72 public static final String OUTCOME_SUCCESS = "success";
74 @SuppressWarnings("nls")
75 public static final String PROPERTY_PROVIDER = "provider";
77 @SuppressWarnings("nls")
78 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
80 @SuppressWarnings("nls")
81 public static final String PROPERTY_PROVIDER_NAME = "name";
83 @SuppressWarnings("nls")
84 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
86 @SuppressWarnings("nls")
87 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
89 @SuppressWarnings("nls")
90 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
92 @SuppressWarnings("nls")
93 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
95 @SuppressWarnings("nls")
96 public static final String PROPERTY_PROVIDER_TYPE = "type";
99 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdapterImpl.class);
101 private static final String CANNOT_FIND_PRIVATE_KEY_STR =
102 "Cannot find the private key in the APPC file system, please load the private key to ";
104 private static final String POSTING_REQUEST_JSON_ERROR_STR = "Error posting request due to invalid JSON block: ";
105 private static final String POSTING_REQUEST_ERROR_STR = "Error posting request: ";
106 private static final String CHEF_CLIENT_RESULT_CODE_STR = "chefClientResult.code";
107 private static final String CHEF_SERVER_RESULT_CODE_STR = "chefServerResult.code";
108 private static final String CHEF_CLIENT_RESULT_MSG_STR = "chefClientResult.message";
109 private static final String CHEF_SERVER_RESULT_MSG_STR = "chefServerResult.message";
110 private static final String CHEF_ACTION_STR = "chefAction";
111 private static final String NODE_LIST_STR = "NodeList";
112 private ChefApiClientFactory chefApiClientFactory = new ChefApiClientFactory();
115 * This default constructor is used as a work around because the activator wasnt getting called
117 public ChefAdapterImpl() {
118 logger.info("Initialize Chef Adapter");
121 @SuppressWarnings("nls")
123 public void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
126 logger.info("environment of VNF-C");
127 chefInfo(params, ctx);
128 RequestContext rc = new RequestContext(ctx);
129 logger.info("Context" + ctx);
131 String env = params.get("Environment");
132 logger.info("Environmnet" + env);
133 if (env.equals(StringUtils.EMPTY)) {
134 chefServerResult(rc, 200, "Skip Environment block ");
136 JSONObject envJ = new JSONObject(env);
137 String envName = envJ.getString("name");
139 if (privateKeyCheck()) {
140 // update the details of an environment on the Chef server.
141 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
142 ChefResponse chefResponse = chefApiClient.put("/environments/" + envName, env);
143 code = chefResponse.getStatusCode();
144 message = chefResponse.getBody();
146 // need create a new environment
147 chefResponse = chefApiClient.post("/environments", env);
148 code = chefResponse.getStatusCode();
149 message = chefResponse.getBody();
150 logger.info("requestbody {}", chefResponse.getBody());
155 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
156 doFailure(ctx, code, message);
158 chefServerResult(rc, code, message);
160 } catch (JSONException e) {
162 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
163 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
164 } catch (Exception e) {
166 logger.error(POSTING_REQUEST_ERROR_STR, e);
167 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
171 @SuppressWarnings("nls")
173 public void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
174 logger.info("update the nodeObjects of VNF-C");
177 chefInfo(params, ctx);
178 String nodeListS = params.get(NODE_LIST_STR);
179 String nodeS = params.get("Node");
180 if (StringUtils.isNotBlank(nodeListS) && StringUtils.isNotBlank(nodeS)) {
181 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
182 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
183 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
184 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
185 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
186 RequestContext rc = new RequestContext(ctx);
189 String message = null;
190 if (privateKeyCheck()) {
191 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
193 for (String nodeName: nodes) {
194 JSONObject nodeJ = new JSONObject(nodeS);
195 nodeJ.remove("name");
196 nodeJ.put("name", nodeName);
197 String nodeObject = nodeJ.toString();
198 logger.info(nodeObject);
199 ChefResponse chefResponse = cac.put("/nodes/" + nodeName, nodeObject);
200 code = chefResponse.getStatusCode();
201 message = chefResponse.getBody();
208 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
209 doFailure(ctx, code, message);
211 chefServerResult(rc, code, message);
213 throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList ");
215 } catch (JSONException e) {
217 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
218 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
219 } catch (Exception e) {
221 logger.error(POSTING_REQUEST_ERROR_STR, e);
222 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
227 public void vnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
230 chefInfo(params, ctx);
231 String nodeList = params.get(NODE_LIST_STR);
232 if (StringUtils.isNotBlank(nodeList)) {
233 String isCallback = params.get("CallbackCapable");
234 String chefAction = "/pushy/jobs";
237 if ("true".equals(isCallback)) {
238 String requestId = params.get("RequestId");
239 String callbackUrl = params.get("CallbackUrl");
240 pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
241 + nodeList + "," + "\"env\": {\"RequestId\": \"" + requestId + "\", \"CallbackUrl\": \""
242 + callbackUrl + "\"}," + "\"capture_output\": true" + "}";
244 pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
245 + nodeList + "," + "\"env\": {}," + "\"capture_output\": true" + "}";
247 RequestContext rc = new RequestContext(ctx);
250 SvcLogicContext svcLogic = rc.getSvcLogicContext();
251 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
252 ChefResponse chefResponse = cac.post(chefAction, pushRequest);
253 code = chefResponse.getStatusCode();
254 logger.info("pushRequest:" + pushRequest);
255 logger.info("requestbody: {}", chefResponse.getBody());
256 String message = chefResponse.getBody();
258 int startIndex = message.indexOf("jobs") + 5;
259 int endIndex = message.length() - 2;
260 String jobID = message.substring(startIndex, endIndex);
261 svcLogic.setAttribute("jobID", jobID);
264 chefServerResult(rc, code, message);
266 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
268 } catch (JSONException e) {
270 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
271 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
272 } catch (Exception e) {
274 logger.error(POSTING_REQUEST_ERROR_STR, e);
275 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
279 @SuppressWarnings("nls")
281 public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
284 chefInfo(params, ctx);
285 String nodeListS = params.get(NODE_LIST_STR);
286 if (StringUtils.isNotBlank(nodeListS)) {
287 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
288 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
289 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
290 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
291 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
292 JSONObject result = new JSONObject();
293 String returnMessage = StringUtils.EMPTY;
295 for (String node : nodes) {
296 String chefAction = "/nodes/" + node;
298 if (privateKeyCheck()) {
299 ChefResponse chefResponse = getApiMethod(chefAction);
300 code = chefResponse.getStatusCode();
301 message = chefResponse.getBody();
304 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
305 doFailure(ctx, code, message);
308 JSONObject nodeResult = new JSONObject();
309 JSONObject allNodeData = new JSONObject(message);
310 allNodeData = allNodeData.getJSONObject("normal");
311 String attribute = "PushJobOutput";
313 String resultData = allNodeData.optString(attribute);
314 if (resultData == null) {
315 resultData = allNodeData.optJSONObject(attribute).toString();
317 if (resultData == null) {
318 resultData = allNodeData.optJSONArray(attribute).toString();
320 if (resultData == null) {
322 returnMessage = "Cannot find " + attribute;
327 nodeResult.put(attribute, resultData);
328 result.put(node, nodeResult);
329 returnMessage = result.toString();
332 returnMessage = message + " Cannot access: " + node;
333 doFailure(ctx, code, message);
338 RequestContext rc = new RequestContext(ctx);
340 chefServerResult(rc, code, returnMessage);
342 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
344 } catch (JSONException e) {
346 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
347 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
348 } catch (Exception e) {
350 logger.error(POSTING_REQUEST_ERROR_STR , e);
351 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
355 private ChefResponse getApiMethod(String chefAction) {
356 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
357 return cac.get(chefAction);
363 @SuppressWarnings("nls")
365 public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) {
366 logger.info("nodeObejctBuilder");
367 String name = params.get("nodeobject.name");
368 String normal = params.get("nodeobject.normal");
369 String overrides = params.get("nodeobject.overrides");
370 String defaults = params.get("nodeobject.defaults");
371 String runList = params.get("nodeobject.run_list");
372 String chefEnvironment = params.get("nodeobject.chef_environment");
373 String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults
374 + "},\"chef_type\":\"node\",\"run_list\":[" + runList + "],\"override\":{" + overrides
375 + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\""
376 + chefEnvironment + "\",}";
377 logger.info(nodeObject);
378 ctx.setAttribute("chef.nodeObject", nodeObject);
382 * send get request to chef server
384 private void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
386 username = params.get("username");
387 serverAddress = params.get("serverAddress");
388 organizations = params.get("organizations");
389 if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(serverAddress)
390 && StringUtils.isNotBlank(organizations)) {
391 chefserver = "https://" + serverAddress + "/organizations/" + organizations;
392 clientPrivatekey = "/opt/onap/appc/chef/" + serverAddress + "/" + organizations + "/" + username + ".pem";
393 logger.info(" clientPrivatekey " + clientPrivatekey);
395 doFailure(ctx, 401, "Missing mandatory param(s) such as username, serverAddress, organizations");
399 private Boolean privateKeyCheck() {
400 File f = new File(clientPrivatekey);
402 logger.info("Key exists");
405 logger.info("Key doesn't exists");
410 @SuppressWarnings("nls")
412 public void retrieveData(Map<String, String> params, SvcLogicContext ctx) {
413 String allConfigData = params.get("allConfig");
414 String key = params.get("key");
415 String dgContext = params.get("dgContext");
416 JSONObject jsonConfig = new JSONObject(allConfigData);
417 String contextData = fetchContextData(key, jsonConfig);
419 RequestContext rc = new RequestContext(ctx);
421 SvcLogicContext svcLogic = rc.getSvcLogicContext();
422 svcLogic.setAttribute(dgContext, contextData);
425 private String fetchContextData(String key, JSONObject jsonConfig) {
427 return jsonConfig.getString(key);
428 } catch (Exception e) {
429 logger.error("Failed getting string value corresponding to " + key + ". Trying to fetch nested json object", e);
431 return jsonConfig.getJSONObject(key).toString();
432 } catch (Exception ex) {
433 logger.error("Failed getting json object corresponding to " + key + ". Trying to fetch array", ex);
434 return jsonConfig.getJSONArray(key).toString();
439 @SuppressWarnings("nls")
441 public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
442 String string1 = params.get("String1");
443 String string2 = params.get("String2");
444 String dgContext = params.get("dgContext");
445 String contextData = string1 + string2;
446 RequestContext rc = new RequestContext(ctx);
448 SvcLogicContext svcLogic = rc.getSvcLogicContext();
449 svcLogic.setAttribute(dgContext, contextData);
453 * Send GET request to chef server
455 @SuppressWarnings("nls")
458 public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
459 logger.info("chef get method");
460 chefInfo(params, ctx);
461 String chefAction = params.get(CHEF_ACTION_STR);
462 RequestContext rc = new RequestContext(ctx);
467 if (privateKeyCheck()) {
468 ChefResponse chefResponse = getApiMethod(chefAction);
469 code = chefResponse.getStatusCode();
470 message = chefResponse.getBody();
473 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
475 chefServerResult(rc, code, message);
479 * Send PUT request to chef server
481 @SuppressWarnings("nls")
484 public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
485 chefInfo(params, ctx);
486 String chefAction = params.get(CHEF_ACTION_STR);
487 String chefNodeStr = params.get("chefRequestBody");
488 RequestContext rc = new RequestContext(ctx);
492 if (privateKeyCheck()) {
493 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
495 ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr);
496 code = chefResponse.getStatusCode();
497 message = chefResponse.getBody();
500 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
502 logger.info(code + " " + message);
503 chefServerResult(rc, code, message);
507 * send Post request to chef server
510 public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
511 chefInfo(params, ctx);
512 logger.info("chef Post method");
513 logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations);
514 String chefNodeStr = params.get("chefRequestBody");
515 String chefAction = params.get(CHEF_ACTION_STR);
517 RequestContext rc = new RequestContext(ctx);
521 // should load pem from somewhere else
522 if (privateKeyCheck()) {
523 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
525 // need pass path into it
527 ChefResponse chefResponse = chefApiClient.post(chefAction, chefNodeStr);
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 * send delete request to chef server
542 public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
543 logger.info("chef delete method");
544 chefInfo(params, ctx);
545 String chefAction = params.get(CHEF_ACTION_STR);
546 RequestContext rc = new RequestContext(ctx);
550 if (privateKeyCheck()) {
551 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
552 ChefResponse chefResponse = chefApiClient.delete(chefAction);
553 code = chefResponse.getStatusCode();
554 message = chefResponse.getBody();
557 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
559 logger.info(code + " " + message);
560 chefServerResult(rc, code, message);
564 * Trigger target vm run chef
567 public void trigger(Map<String, String> params, SvcLogicContext ctx) {
568 logger.info("Run trigger method");
569 String tVmIp = params.get("ip");
570 RequestContext rc = new RequestContext(ctx);
573 try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
574 HttpGet httpGet = new HttpGet(tVmIp);
575 HttpResponse response = httpClient.execute(httpGet);
576 int responseCode = response.getStatusLine().getStatusCode();
577 HttpEntity entity = response.getEntity();
578 String responseOutput = EntityUtils.toString(entity);
579 chefClientResult(rc, responseCode, responseOutput);
581 } catch (Exception e) {
582 logger.error("An error occurred when executing trigger method", e);
583 doFailure(rc, 500, e.toString());
587 @SuppressWarnings("nls")
589 public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
592 chefInfo(params, ctx);
593 String jobID = params.get("jobid");
594 String retry = params.get("retryTimes");
595 String intrva = params.get("retryInterval");
596 if (StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) {
598 int retryTimes = Integer.parseInt(params.get("retryTimes"));
599 int retryInterval = Integer.parseInt(params.get("retryInterval"));
601 String chefAction = "/pushy/jobs/" + jobID;
603 RequestContext rc = new RequestContext(ctx);
605 SvcLogicContext svcLogic = rc.getSvcLogicContext();
606 String message = StringUtils.EMPTY;
607 String status = StringUtils.EMPTY;
608 for (int i = 0; i < retryTimes; i++) {
609 sleepFor(retryInterval);
610 ChefResponse chefResponse = getApiMethod(chefAction);
611 code = chefResponse.getStatusCode();
612 message = chefResponse.getBody();
613 JSONObject obj = new JSONObject(message);
614 status = obj.getString("status");
615 if (!"running".equals(status)) {
616 logger.info(i + " time " + code + " " + status);
620 resolveSvcLogicAttributes(svcLogic, message, status);
622 throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
624 } catch (Exception e) {
626 logger.error("An error occurred when executing checkPushJob method", e);
627 doFailure(ctx, code, e.getMessage());
631 private void resolveSvcLogicAttributes(SvcLogicContext svcLogic, String message, String status) {
632 if ("complete".equals(status)) {
633 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "200");
634 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
636 if ("running".equals(status)) {
637 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "202");
638 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, "chef client runtime out");
640 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "500");
641 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
646 private void sleepFor(int retryInterval) {
648 Thread.sleep(retryInterval); // 1000 milliseconds is one second.
649 } catch (InterruptedException ex) {
650 Thread.currentThread().interrupt();
654 @SuppressWarnings("nls")
656 public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
659 chefInfo(params, ctx);
660 String pushRequest = params.get("pushRequest");
661 String chefAction = "/pushy/jobs";
662 RequestContext rc = new RequestContext(ctx);
664 SvcLogicContext svcLogic = rc.getSvcLogicContext();
665 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
666 ChefResponse chefResponse = chefApiClient.post(chefAction, pushRequest);
668 code = chefResponse.getStatusCode();
669 String message = chefResponse.getBody();
671 int startIndex = message.indexOf("jobs") + 6;
672 int endIndex = message.length() - 2;
673 String jobID = message.substring(startIndex, endIndex);
674 svcLogic.setAttribute("jobID", jobID);
677 chefServerResult(rc, code, message);
678 } catch (Exception e) {
680 logger.error("An error occurred when executing pushJob method", e);
681 doFailure(ctx, code, e.getMessage());
685 @SuppressWarnings("static-method")
686 private void doFailure(RequestContext rc, int code, String message) {
687 SvcLogicContext svcLogic = rc.getSvcLogicContext();
688 String msg = (message == null) ? Integer.toString(code) : message;
689 if (msg.contains("\n")) {
690 msg = msg.substring(msg.indexOf('\n'));
695 status = Integer.toString(code);
696 } catch (Exception e) {
698 logger.error("Parsing status code failed. Setting it to \"500\"", e);
700 svcLogic.setAttribute("chefAgent.code", status);
701 svcLogic.setAttribute("chefAgent.message", msg);
705 * @param rc The request context that manages the state and recovery of the request for the life of its processing.
707 @SuppressWarnings("static-method")
708 private void doSuccess(RequestContext rc) {
709 SvcLogicContext svcLogic = rc.getSvcLogicContext();
710 svcLogic.setAttribute("chefAgent.code", "200");
713 @SuppressWarnings("static-method")
714 private void chefServerResult(RequestContext rc, int code, String message) {
715 initSvcLogic(rc, code, message, "server");
718 @SuppressWarnings("static-method")
719 private void chefClientResult(RequestContext rc, int code, String message) {
720 initSvcLogic(rc, code, message, "client");
723 private void initSvcLogic(RequestContext rc, int code, String message, String target) {
725 SvcLogicContext svcLogic = rc.getSvcLogicContext();
726 String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR;
727 String messageStr = "client".equals(target) ? CHEF_SERVER_RESULT_MSG_STR : CHEF_CLIENT_RESULT_MSG_STR;
729 svcLogic.setStatus(OUTCOME_SUCCESS);
730 svcLogic.setAttribute(codeStr, Integer.toString(code));
731 svcLogic.setAttribute(messageStr, message);
732 logger.info(codeStr + ": " + svcLogic.getAttribute(codeStr));
733 logger.info(messageStr + ": " + svcLogic.getAttribute(messageStr));
736 @SuppressWarnings("static-method")
737 private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
739 String cutMessage = message.contains("\n") ? message.substring(message.indexOf('\n')) : message;
741 svcLogic.setStatus(OUTCOME_FAILURE);
742 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, Integer.toString(code));
743 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, cutMessage);
745 throw new SvcLogicException("Chef Adapter error:" + cutMessage);