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.chefapi.ApiMethod;
42 import org.onap.appc.adapter.chef.chefclient.ChefApiClient;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
49 * This class implements the {@link ChefAdapter} interface. This interface defines the behaviors that our service
52 public class ChefAdapterImpl implements ChefAdapter {
54 // chef server Initialize variable
55 private String username = StringUtils.EMPTY;
56 private String clientPrivatekey = StringUtils.EMPTY;
57 private String chefserver = StringUtils.EMPTY;
58 private String serverAddress = StringUtils.EMPTY;
59 private String organizations = StringUtils.EMPTY;
61 @SuppressWarnings("nls")
62 public static final String MDC_ADAPTER = "adapter";
64 @SuppressWarnings("nls")
65 public static final String MDC_SERVICE = "service";
67 @SuppressWarnings("nls")
68 public static final String OUTCOME_FAILURE = "failure";
70 @SuppressWarnings("nls")
71 public static final String OUTCOME_SUCCESS = "success";
73 @SuppressWarnings("nls")
74 public static final String PROPERTY_PROVIDER = "provider";
76 @SuppressWarnings("nls")
77 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
79 @SuppressWarnings("nls")
80 public static final String PROPERTY_PROVIDER_NAME = "name";
82 @SuppressWarnings("nls")
83 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
85 @SuppressWarnings("nls")
86 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
88 @SuppressWarnings("nls")
89 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
91 @SuppressWarnings("nls")
92 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
94 @SuppressWarnings("nls")
95 public static final String PROPERTY_PROVIDER_TYPE = "type";
98 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdapterImpl.class);
100 private static final String CANNOT_FIND_PRIVATE_KEY_STR =
101 "Cannot find the private key in the APPC file system, please load the private key to ";
103 private static final String POSTING_REQUEST_JSON_ERROR_STR = "Error posting request due to invalid JSON block: ";
104 private static final String POSTING_REQUEST_ERROR_STR = "Error posting request: ";
105 private static final String CHEF_CLIENT_RESULT_CODE_STR = "chefClientResult.code";
106 private static final String CHEF_SERVER_RESULT_CODE_STR = "chefServerResult.code";
107 private static final String CHEF_CLIENT_RESULT_MSG_STR = "chefClientResult.message";
108 private static final String CHEF_SERVER_RESULT_MSG_STR = "chefServerResult.message";
109 private static final String CHEF_ACTION_STR = "chefAction";
110 private static final String NODE_LIST_STR = "NodeList";
113 * This default constructor is used as a work around because the activator wasnt getting called
115 public ChefAdapterImpl() {
119 public ChefAdapterImpl(Properties props) {
124 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
125 * disconnected testing
127 * @param initialize True if the adapter is to be initialized, can false if not
130 public ChefAdapterImpl(boolean initialize) {
136 public ChefAdapterImpl(String key) {
140 @SuppressWarnings("nls")
142 public void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
145 logger.info("environment of VNF-C");
146 chefInfo(params, ctx);
147 RequestContext rc = new RequestContext(ctx);
148 logger.info("Context" + ctx);
150 String env = params.get("Environment");
151 logger.info("Environmnet" + env);
152 if (env.equals(StringUtils.EMPTY)) {
153 chefServerResult(rc, 200, "Skip Environment block ");
155 JSONObject envJ = new JSONObject(env);
156 String envName = envJ.getString("name");
158 if (privateKeyCheck()) {
159 // update the details of an environment on the Chef server.
160 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
161 ApiMethod am = cac.put("/environments/" + envName).body(env);
163 code = am.getReturnCode();
164 message = am.getResponseBodyAsString();
166 // need create a new environment
167 am = cac.post("/environments").body(env);
169 code = am.getReturnCode();
170 message = am.getResponseBodyAsString();
171 logger.info("requestbody" + am.getReqBody());
176 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
177 doFailure(ctx, code, message);
179 chefServerResult(rc, code, message);
181 } catch (JSONException e) {
183 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
184 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
185 } catch (Exception e) {
187 logger.error(POSTING_REQUEST_ERROR_STR, e);
188 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
192 @SuppressWarnings("nls")
194 public void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
195 logger.info("update the nodeObjects of VNF-C");
198 chefInfo(params, ctx);
199 String nodeListS = params.get(NODE_LIST_STR);
200 String nodeS = params.get("Node");
201 if (StringUtils.isNotBlank(nodeListS) && StringUtils.isNotBlank(nodeS)) {
202 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
203 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
204 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
205 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
206 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
207 RequestContext rc = new RequestContext(ctx);
210 String message = null;
211 if (privateKeyCheck()) {
212 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
214 for (String nodeName: nodes) {
215 JSONObject nodeJ = new JSONObject(nodeS);
216 nodeJ.remove("name");
217 nodeJ.put("name", nodeName);
218 String nodeObject = nodeJ.toString();
219 logger.info(nodeObject);
220 ApiMethod am = cac.put("/nodes/" + nodeName).body(nodeObject);
222 code = am.getReturnCode();
223 message = am.getResponseBodyAsString();
230 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
231 doFailure(ctx, code, message);
233 chefServerResult(rc, code, message);
235 throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList ");
237 } catch (JSONException e) {
239 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
240 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
241 } catch (Exception e) {
243 logger.error(POSTING_REQUEST_ERROR_STR, e);
244 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
249 public void vnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
252 chefInfo(params, ctx);
253 String nodeList = params.get(NODE_LIST_STR);
254 if (StringUtils.isNotBlank(nodeList)) {
255 String isCallback = params.get("CallbackCapable");
256 String chefAction = "/pushy/jobs";
259 if ("true".equals(isCallback)) {
260 String requestId = params.get("RequestId");
261 String callbackUrl = params.get("CallbackUrl");
262 pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
263 + nodeList + "," + "\"env\": {\"RequestId\": \"" + requestId + "\", \"CallbackUrl\": \""
264 + callbackUrl + "\"}," + "\"capture_output\": true" + "}";
266 pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
267 + nodeList + "," + "\"env\": {}," + "\"capture_output\": true" + "}";
269 RequestContext rc = new RequestContext(ctx);
272 SvcLogicContext svcLogic = rc.getSvcLogicContext();
273 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
274 ApiMethod am = cac.post(chefAction).body(pushRequest);
276 code = am.getReturnCode();
277 logger.info("pushRequest:" + pushRequest);
278 logger.info("requestbody:" + am.getReqBody());
279 String message = am.getResponseBodyAsString();
281 int startIndex = message.indexOf("jobs") + 5;
282 int endIndex = message.length() - 2;
283 String jobID = message.substring(startIndex, endIndex);
284 svcLogic.setAttribute("jobID", jobID);
287 chefServerResult(rc, code, message);
289 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
291 } catch (JSONException e) {
293 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
294 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
295 } catch (Exception e) {
297 logger.error(POSTING_REQUEST_ERROR_STR, e);
298 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
302 @SuppressWarnings("nls")
304 public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
307 chefInfo(params, ctx);
308 String nodeListS = params.get(NODE_LIST_STR);
309 if (StringUtils.isNotBlank(nodeListS)) {
310 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
311 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
312 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
313 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
314 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
315 JSONObject result = new JSONObject();
316 String returnMessage = StringUtils.EMPTY;
318 for (String node : nodes) {
319 String chefAction = "/nodes/" + node;
321 if (privateKeyCheck()) {
322 ApiMethod am = getApiMethod(chefAction);
323 code = am.getReturnCode();
324 message = am.getResponseBodyAsString();
327 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
328 doFailure(ctx, code, message);
331 JSONObject nodeResult = new JSONObject();
332 JSONObject allNodeData = new JSONObject(message);
333 allNodeData = allNodeData.getJSONObject("normal");
334 String attribute = "PushJobOutput";
336 String resultData = allNodeData.optString(attribute);
337 if (resultData == null) {
338 resultData = allNodeData.optJSONObject(attribute).toString();
340 if (resultData == null) {
341 resultData = allNodeData.optJSONArray(attribute).toString();
343 if (resultData == null) {
345 returnMessage = "Cannot find " + attribute;
350 nodeResult.put(attribute, resultData);
351 result.put(node, nodeResult);
352 returnMessage = result.toString();
355 returnMessage = message + " Cannot access: " + node;
356 doFailure(ctx, code, message);
361 RequestContext rc = new RequestContext(ctx);
363 chefServerResult(rc, code, returnMessage);
365 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
367 } catch (JSONException e) {
369 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
370 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
371 } catch (Exception e) {
373 logger.error(POSTING_REQUEST_ERROR_STR , e);
374 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
378 private ApiMethod getApiMethod(String chefAction) {
379 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
381 return cac.get(chefAction).execute();
387 @SuppressWarnings("nls")
389 public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) {
390 logger.info("nodeObejctBuilder");
391 String name = params.get("nodeobject.name");
392 String normal = params.get("nodeobject.normal");
393 String overrides = params.get("nodeobject.overrides");
394 String defaults = params.get("nodeobject.defaults");
395 String runList = params.get("nodeobject.run_list");
396 String chefEnvironment = params.get("nodeobject.chef_environment");
397 String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults
398 + "},\"chef_type\":\"node\",\"run_list\":[" + runList + "],\"override\":{" + overrides
399 + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\""
400 + chefEnvironment + "\"}";
401 logger.info(nodeObject);
402 RequestContext rc = new RequestContext(ctx);
404 SvcLogicContext svcLogic = rc.getSvcLogicContext();
405 svcLogic.setAttribute("chef.nodeObject", nodeObject);
409 * send get request to chef server
411 private void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
413 username = params.get("username");
414 serverAddress = params.get("serverAddress");
415 organizations = params.get("organizations");
416 if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(serverAddress)
417 && StringUtils.isNotBlank(organizations)) {
418 chefserver = "https://" + serverAddress + "/organizations/" + organizations;
419 clientPrivatekey = "/opt/onap/appc/chef/" + serverAddress + "/" + organizations + "/" + username + ".pem";
420 logger.info(" clientPrivatekey " + clientPrivatekey);
422 doFailure(ctx, 401, "Missing mandatory param(s) such as username, serverAddress, organizations");
426 private Boolean privateKeyCheck() {
427 File f = new File(clientPrivatekey);
429 logger.info("Key exists");
432 logger.info("Key doesn't exists");
437 @SuppressWarnings("nls")
439 public void retrieveData(Map<String, String> params, SvcLogicContext ctx) {
440 String allConfigData = params.get("allConfig");
441 String key = params.get("key");
442 String dgContext = params.get("dgContext");
443 JSONObject jsonConfig = new JSONObject(allConfigData);
444 String contextData = fetchContextData(key, jsonConfig);
446 RequestContext rc = new RequestContext(ctx);
448 SvcLogicContext svcLogic = rc.getSvcLogicContext();
449 svcLogic.setAttribute(dgContext, contextData);
452 private String fetchContextData(String key, JSONObject jsonConfig) {
454 return jsonConfig.getString(key);
455 } catch (Exception e) {
456 logger.error("Failed getting string value corresponding to " + key + ". Trying to fetch nested json object", e);
458 return jsonConfig.getJSONObject(key).toString();
459 } catch (Exception ex) {
460 logger.error("Failed getting json object corresponding to " + key + ". Trying to fetch array", ex);
461 return jsonConfig.getJSONArray(key).toString();
466 @SuppressWarnings("nls")
468 public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
469 String string1 = params.get("String1");
470 String string2 = params.get("String2");
471 String dgContext = params.get("dgContext");
472 String contextData = string1 + string2;
473 RequestContext rc = new RequestContext(ctx);
475 SvcLogicContext svcLogic = rc.getSvcLogicContext();
476 svcLogic.setAttribute(dgContext, contextData);
480 * Send GET request to chef server
482 @SuppressWarnings("nls")
485 public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
486 logger.info("chef get method");
487 chefInfo(params, ctx);
488 String chefAction = params.get(CHEF_ACTION_STR);
489 RequestContext rc = new RequestContext(ctx);
494 if (privateKeyCheck()) {
495 ApiMethod am = getApiMethod(chefAction);
496 code = am.getReturnCode();
497 message = am.getResponseBodyAsString();
500 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
502 chefServerResult(rc, code, message);
506 * Send PUT request to chef server
508 @SuppressWarnings("nls")
511 public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
512 chefInfo(params, ctx);
513 String chefAction = params.get(CHEF_ACTION_STR);
514 String chefNodeStr = params.get("chefRequestBody");
515 RequestContext rc = new RequestContext(ctx);
519 if (privateKeyCheck()) {
520 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
522 ApiMethod am = cac.put(chefAction).body(chefNodeStr);
524 code = am.getReturnCode();
525 message = am.getResponseBodyAsString();
528 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
530 logger.info(code + " " + message);
531 chefServerResult(rc, code, message);
535 * send Post request to chef server
538 public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
539 chefInfo(params, ctx);
540 logger.info("chef Post method");
541 logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations);
542 String chefNodeStr = params.get("chefRequestBody");
543 String chefAction = params.get(CHEF_ACTION_STR);
545 RequestContext rc = new RequestContext(ctx);
549 // should load pem from somewhere else
550 if (privateKeyCheck()) {
551 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
553 // need pass path into it
555 ApiMethod am = cac.post(chefAction).body(chefNodeStr);
557 code = am.getReturnCode();
558 message = am.getResponseBodyAsString();
561 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
563 logger.info(code + " " + message);
564 chefServerResult(rc, code, message);
568 * send delete request to chef server
571 public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
572 logger.info("chef delete method");
573 chefInfo(params, ctx);
574 String chefAction = params.get(CHEF_ACTION_STR);
575 RequestContext rc = new RequestContext(ctx);
579 if (privateKeyCheck()) {
580 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
581 ApiMethod am = cac.delete(chefAction);
583 code = am.getReturnCode();
584 message = am.getResponseBodyAsString();
587 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
589 logger.info(code + " " + message);
590 chefServerResult(rc, code, message);
594 * Trigger target vm run chef
597 public void trigger(Map<String, String> params, SvcLogicContext ctx) {
598 logger.info("Run trigger method");
599 String tVmIp = params.get("ip");
600 RequestContext rc = new RequestContext(ctx);
603 try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
604 HttpGet httpGet = new HttpGet(tVmIp);
605 HttpResponse response = httpClient.execute(httpGet);
606 int responseCode = response.getStatusLine().getStatusCode();
607 HttpEntity entity = response.getEntity();
608 String responseOutput = EntityUtils.toString(entity);
609 chefClientResult(rc, responseCode, responseOutput);
611 } catch (Exception e) {
612 logger.error("An error occurred when executing trigger method", e);
613 doFailure(rc, 500, e.toString());
617 @SuppressWarnings("nls")
619 public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
622 chefInfo(params, ctx);
623 String jobID = params.get("jobid");
624 String retry = params.get("retryTimes");
625 String intrva = params.get("retryInterval");
626 if (StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) {
628 int retryTimes = Integer.parseInt(params.get("retryTimes"));
629 int retryInterval = Integer.parseInt(params.get("retryInterval"));
631 String chefAction = "/pushy/jobs/" + jobID;
633 RequestContext rc = new RequestContext(ctx);
635 SvcLogicContext svcLogic = rc.getSvcLogicContext();
636 String message = StringUtils.EMPTY;
637 String status = StringUtils.EMPTY;
638 for (int i = 0; i < retryTimes; i++) {
639 sleepFor(retryInterval);
640 ApiMethod am = getApiMethod(chefAction);
641 code = am.getReturnCode();
642 message = am.getResponseBodyAsString();
643 JSONObject obj = new JSONObject(message);
644 status = obj.getString("status");
645 if (!"running".equals(status)) {
646 logger.info(i + " time " + code + " " + status);
650 resolveSvcLogicAttributes(svcLogic, message, status);
652 throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
654 } catch (Exception e) {
656 logger.error("An error occurred when executing checkPushJob method", e);
657 doFailure(ctx, code, e.getMessage());
661 private void resolveSvcLogicAttributes(SvcLogicContext svcLogic, String message, String status) {
662 if ("complete".equals(status)) {
663 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "200");
664 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
666 if ("running".equals(status)) {
667 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "202");
668 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, "chef client runtime out");
670 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "500");
671 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
676 private void sleepFor(int retryInterval) {
678 Thread.sleep(retryInterval); // 1000 milliseconds is one second.
679 } catch (InterruptedException ex) {
680 Thread.currentThread().interrupt();
684 @SuppressWarnings("nls")
686 public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
689 chefInfo(params, ctx);
690 String pushRequest = params.get("pushRequest");
691 String chefAction = "/pushy/jobs";
692 RequestContext rc = new RequestContext(ctx);
694 SvcLogicContext svcLogic = rc.getSvcLogicContext();
695 ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
696 ApiMethod am = cac.post(chefAction).body(pushRequest);
699 code = am.getReturnCode();
700 String message = am.getResponseBodyAsString();
702 int startIndex = message.indexOf("jobs") + 6;
703 int endIndex = message.length() - 2;
704 String jobID = message.substring(startIndex, endIndex);
705 svcLogic.setAttribute("jobID", jobID);
708 chefServerResult(rc, code, message);
709 } catch (Exception e) {
711 logger.error("An error occurred when executing pushJob method", e);
712 doFailure(ctx, code, e.getMessage());
716 @SuppressWarnings("static-method")
717 private void doFailure(RequestContext rc, int code, String message) {
718 SvcLogicContext svcLogic = rc.getSvcLogicContext();
719 String msg = (message == null) ? Integer.toString(code) : message;
720 if (msg.contains("\n")) {
721 msg = msg.substring(msg.indexOf('\n'));
726 status = Integer.toString(code);
727 } catch (Exception e) {
729 logger.error("Parsing status code failed. Setting it to \"500\"", e);
731 svcLogic.setAttribute("chefAgent.code", status);
732 svcLogic.setAttribute("chefAgent.message", msg);
736 * @param rc The request context that manages the state and recovery of the request for the life of its processing.
738 @SuppressWarnings("static-method")
739 private void doSuccess(RequestContext rc) {
740 SvcLogicContext svcLogic = rc.getSvcLogicContext();
741 svcLogic.setAttribute("chefAgent.code", "200");
744 @SuppressWarnings("static-method")
745 private void chefServerResult(RequestContext rc, int code, String message) {
746 initSvcLogic(rc, code, message, "server");
749 @SuppressWarnings("static-method")
750 private void chefClientResult(RequestContext rc, int code, String message) {
751 initSvcLogic(rc, code, message, "client");
754 private void initSvcLogic(RequestContext rc, int code, String message, String target) {
756 SvcLogicContext svcLogic = rc.getSvcLogicContext();
757 String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR;
758 String messageStr = "client".equals(target) ? CHEF_SERVER_RESULT_MSG_STR : CHEF_CLIENT_RESULT_MSG_STR;
760 svcLogic.setStatus(OUTCOME_SUCCESS);
761 svcLogic.setAttribute(codeStr, Integer.toString(code));
762 svcLogic.setAttribute(messageStr, message);
763 logger.info(codeStr + ": " + svcLogic.getAttribute(codeStr));
764 logger.info(messageStr + ": " + svcLogic.getAttribute(messageStr));
769 * initialize the provider adapter by building the context cache
771 private void initialize() {
773 logger.info("Initialize Chef Adapter");
776 @SuppressWarnings("static-method")
777 private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
779 String cutMessage = message.contains("\n") ? message.substring(message.indexOf('\n')) : message;
781 svcLogic.setStatus(OUTCOME_FAILURE);
782 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, Integer.toString(code));
783 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, cutMessage);
785 throw new SvcLogicException("Chef Adapter error:" + cutMessage);