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 ctx.setAttribute(dgContext, contextData);
422 private String fetchContextData(String key, JSONObject jsonConfig) {
424 return jsonConfig.getString(key);
425 } catch (Exception e) {
426 logger.error("Failed getting string value corresponding to " + key + ". Trying to fetch nested json object", e);
428 return jsonConfig.getJSONObject(key).toString();
429 } catch (Exception ex) {
430 logger.error("Failed getting json object corresponding to " + key + ". Trying to fetch array", ex);
431 return jsonConfig.getJSONArray(key).toString();
436 @SuppressWarnings("nls")
438 public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
439 String string1 = params.get("String1");
440 String string2 = params.get("String2");
441 String dgContext = params.get("dgContext");
442 String contextData = string1 + string2;
443 ctx.setAttribute(dgContext, contextData);
447 * Send GET request to chef server
449 @SuppressWarnings("nls")
452 public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
453 logger.info("chef get method");
454 chefInfo(params, ctx);
455 String chefAction = params.get(CHEF_ACTION_STR);
456 RequestContext rc = new RequestContext(ctx);
461 if (privateKeyCheck()) {
462 ChefResponse chefResponse = getApiMethod(chefAction);
463 code = chefResponse.getStatusCode();
464 message = chefResponse.getBody();
467 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
469 chefServerResult(rc, code, message);
473 * Send PUT request to chef server
475 @SuppressWarnings("nls")
478 public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
479 chefInfo(params, ctx);
480 String chefAction = params.get(CHEF_ACTION_STR);
481 String chefNodeStr = params.get("chefRequestBody");
482 RequestContext rc = new RequestContext(ctx);
486 if (privateKeyCheck()) {
487 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
489 ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr);
490 code = chefResponse.getStatusCode();
491 message = chefResponse.getBody();
494 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
496 logger.info(code + " " + message);
497 chefServerResult(rc, code, message);
501 * send Post request to chef server
504 public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
505 chefInfo(params, ctx);
506 logger.info("chef Post method");
507 logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations);
508 String chefNodeStr = params.get("chefRequestBody");
509 String chefAction = params.get(CHEF_ACTION_STR);
511 RequestContext rc = new RequestContext(ctx);
515 // should load pem from somewhere else
516 if (privateKeyCheck()) {
517 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
519 // need pass path into it
521 ChefResponse chefResponse = chefApiClient.post(chefAction, chefNodeStr);
522 code = chefResponse.getStatusCode();
523 message = chefResponse.getBody();
526 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
528 logger.info(code + " " + message);
529 chefServerResult(rc, code, message);
533 * send delete request to chef server
536 public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
537 logger.info("chef delete method");
538 chefInfo(params, ctx);
539 String chefAction = params.get(CHEF_ACTION_STR);
540 RequestContext rc = new RequestContext(ctx);
544 if (privateKeyCheck()) {
545 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
546 ChefResponse chefResponse = chefApiClient.delete(chefAction);
547 code = chefResponse.getStatusCode();
548 message = chefResponse.getBody();
551 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
553 logger.info(code + " " + message);
554 chefServerResult(rc, code, message);
558 * Trigger target vm run chef
561 public void trigger(Map<String, String> params, SvcLogicContext ctx) {
562 logger.info("Run trigger method");
563 String tVmIp = params.get("ip");
564 RequestContext rc = new RequestContext(ctx);
567 try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
568 HttpGet httpGet = new HttpGet(tVmIp);
569 HttpResponse response = httpClient.execute(httpGet);
570 int responseCode = response.getStatusLine().getStatusCode();
571 HttpEntity entity = response.getEntity();
572 String responseOutput = EntityUtils.toString(entity);
573 chefClientResult(rc, responseCode, responseOutput);
575 } catch (Exception e) {
576 logger.error("An error occurred when executing trigger method", e);
577 doFailure(rc, 500, e.toString());
581 @SuppressWarnings("nls")
583 public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
586 chefInfo(params, ctx);
587 String jobID = params.get("jobid");
588 String retry = params.get("retryTimes");
589 String intrva = params.get("retryInterval");
590 if (StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) {
592 int retryTimes = Integer.parseInt(params.get("retryTimes"));
593 int retryInterval = Integer.parseInt(params.get("retryInterval"));
595 String chefAction = "/pushy/jobs/" + jobID;
597 RequestContext rc = new RequestContext(ctx);
599 SvcLogicContext svcLogic = rc.getSvcLogicContext();
600 String message = StringUtils.EMPTY;
601 String status = StringUtils.EMPTY;
602 for (int i = 0; i < retryTimes; i++) {
603 sleepFor(retryInterval);
604 ChefResponse chefResponse = getApiMethod(chefAction);
605 code = chefResponse.getStatusCode();
606 message = chefResponse.getBody();
607 JSONObject obj = new JSONObject(message);
608 status = obj.getString("status");
609 if (!"running".equals(status)) {
610 logger.info(i + " time " + code + " " + status);
614 resolveSvcLogicAttributes(svcLogic, message, status);
616 throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
618 } catch (Exception e) {
620 logger.error("An error occurred when executing checkPushJob method", e);
621 doFailure(ctx, code, e.getMessage());
625 private void resolveSvcLogicAttributes(SvcLogicContext svcLogic, String message, String status) {
626 if ("complete".equals(status)) {
627 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "200");
628 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
630 if ("running".equals(status)) {
631 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "202");
632 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, "chef client runtime out");
634 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "500");
635 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
640 private void sleepFor(int retryInterval) {
642 Thread.sleep(retryInterval); // 1000 milliseconds is one second.
643 } catch (InterruptedException ex) {
644 Thread.currentThread().interrupt();
648 @SuppressWarnings("nls")
650 public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
653 chefInfo(params, ctx);
654 String pushRequest = params.get("pushRequest");
655 String chefAction = "/pushy/jobs";
656 RequestContext rc = new RequestContext(ctx);
658 SvcLogicContext svcLogic = rc.getSvcLogicContext();
659 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
660 ChefResponse chefResponse = chefApiClient.post(chefAction, pushRequest);
662 code = chefResponse.getStatusCode();
663 String message = chefResponse.getBody();
665 int startIndex = message.indexOf("jobs") + 6;
666 int endIndex = message.length() - 2;
667 String jobID = message.substring(startIndex, endIndex);
668 svcLogic.setAttribute("jobID", jobID);
671 chefServerResult(rc, code, message);
672 } catch (Exception e) {
674 logger.error("An error occurred when executing pushJob method", e);
675 doFailure(ctx, code, e.getMessage());
679 @SuppressWarnings("static-method")
680 private void doFailure(RequestContext rc, int code, String message) {
681 SvcLogicContext svcLogic = rc.getSvcLogicContext();
682 String msg = (message == null) ? Integer.toString(code) : message;
683 if (msg.contains("\n")) {
684 msg = msg.substring(msg.indexOf('\n'));
689 status = Integer.toString(code);
690 } catch (Exception e) {
692 logger.error("Parsing status code failed. Setting it to \"500\"", e);
694 svcLogic.setAttribute("chefAgent.code", status);
695 svcLogic.setAttribute("chefAgent.message", msg);
699 * @param rc The request context that manages the state and recovery of the request for the life of its processing.
701 @SuppressWarnings("static-method")
702 private void doSuccess(RequestContext rc) {
703 SvcLogicContext svcLogic = rc.getSvcLogicContext();
704 svcLogic.setAttribute("chefAgent.code", "200");
707 @SuppressWarnings("static-method")
708 private void chefServerResult(RequestContext rc, int code, String message) {
709 initSvcLogic(rc, code, message, "server");
712 @SuppressWarnings("static-method")
713 private void chefClientResult(RequestContext rc, int code, String message) {
714 initSvcLogic(rc, code, message, "client");
717 private void initSvcLogic(RequestContext rc, int code, String message, String target) {
719 SvcLogicContext svcLogic = rc.getSvcLogicContext();
720 String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR;
721 String messageStr = "client".equals(target) ? CHEF_SERVER_RESULT_MSG_STR : CHEF_CLIENT_RESULT_MSG_STR;
723 svcLogic.setStatus(OUTCOME_SUCCESS);
724 svcLogic.setAttribute(codeStr, Integer.toString(code));
725 svcLogic.setAttribute(messageStr, message);
726 logger.info(codeStr + ": " + svcLogic.getAttribute(codeStr));
727 logger.info(messageStr + ": " + svcLogic.getAttribute(messageStr));
730 @SuppressWarnings("static-method")
731 private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
733 String cutMessage = message.contains("\n") ? message.substring(message.indexOf('\n')) : message;
735 svcLogic.setStatus(OUTCOME_FAILURE);
736 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, Integer.toString(code));
737 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, cutMessage);
739 throw new SvcLogicException("Chef Adapter error:" + cutMessage);