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.appc.adapter.chef.chefclient.ChefApiClientFactory;
44 import org.onap.appc.adapter.chef.chefclient.ChefResponse;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
47 import com.att.eelf.configuration.EELFLogger;
48 import com.att.eelf.configuration.EELFManager;
51 * This class implements the {@link ChefAdapter} interface. This interface defines the behaviors that our service
54 public class ChefAdapterImpl implements ChefAdapter {
56 // chef server Initialize variable
57 private String username = StringUtils.EMPTY;
58 private String clientPrivatekey = StringUtils.EMPTY;
59 private String chefserver = StringUtils.EMPTY;
60 private String serverAddress = StringUtils.EMPTY;
61 private String organizations = StringUtils.EMPTY;
63 @SuppressWarnings("nls")
64 public static final String MDC_ADAPTER = "adapter";
66 @SuppressWarnings("nls")
67 public static final String MDC_SERVICE = "service";
69 @SuppressWarnings("nls")
70 public static final String OUTCOME_FAILURE = "failure";
72 @SuppressWarnings("nls")
73 public static final String OUTCOME_SUCCESS = "success";
75 @SuppressWarnings("nls")
76 public static final String PROPERTY_PROVIDER = "provider";
78 @SuppressWarnings("nls")
79 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
81 @SuppressWarnings("nls")
82 public static final String PROPERTY_PROVIDER_NAME = "name";
84 @SuppressWarnings("nls")
85 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
87 @SuppressWarnings("nls")
88 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
90 @SuppressWarnings("nls")
91 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
93 @SuppressWarnings("nls")
94 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
96 @SuppressWarnings("nls")
97 public static final String PROPERTY_PROVIDER_TYPE = "type";
100 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdapterImpl.class);
102 private static final String CANNOT_FIND_PRIVATE_KEY_STR =
103 "Cannot find the private key in the APPC file system, please load the private key to ";
105 private static final String POSTING_REQUEST_JSON_ERROR_STR = "Error posting request due to invalid JSON block: ";
106 private static final String POSTING_REQUEST_ERROR_STR = "Error posting request: ";
107 private static final String CHEF_CLIENT_RESULT_CODE_STR = "chefClientResult.code";
108 private static final String CHEF_SERVER_RESULT_CODE_STR = "chefServerResult.code";
109 private static final String CHEF_CLIENT_RESULT_MSG_STR = "chefClientResult.message";
110 private static final String CHEF_SERVER_RESULT_MSG_STR = "chefServerResult.message";
111 private static final String CHEF_ACTION_STR = "chefAction";
112 private static final String NODE_LIST_STR = "NodeList";
113 private ChefApiClientFactory chefApiClientFactory = new ChefApiClientFactory();
116 * This default constructor is used as a work around because the activator wasnt getting called
118 public ChefAdapterImpl() {
122 public ChefAdapterImpl(Properties props) {
127 * This constructor is used primarily in the test cases to bypass initialization of the adapter for isolated,
128 * disconnected testing
130 * @param initialize True if the adapter is to be initialized, can false if not
133 public ChefAdapterImpl(boolean initialize) {
139 public ChefAdapterImpl(String key) {
143 @SuppressWarnings("nls")
145 public void vnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
148 logger.info("environment of VNF-C");
149 chefInfo(params, ctx);
150 RequestContext rc = new RequestContext(ctx);
151 logger.info("Context" + ctx);
153 String env = params.get("Environment");
154 logger.info("Environmnet" + env);
155 if (env.equals(StringUtils.EMPTY)) {
156 chefServerResult(rc, 200, "Skip Environment block ");
158 JSONObject envJ = new JSONObject(env);
159 String envName = envJ.getString("name");
161 if (privateKeyCheck()) {
162 // update the details of an environment on the Chef server.
163 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
164 ChefResponse chefResponse = chefApiClient.put("/environments/" + envName, env);
165 code = chefResponse.getStatusCode();
166 message = chefResponse.getBody();
168 // need create a new environment
169 chefResponse = chefApiClient.post("/environments", env);
170 code = chefResponse.getStatusCode();
171 message = chefResponse.getBody();
172 logger.info("requestbody {}", chefResponse.getBody());
177 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
178 doFailure(ctx, code, message);
180 chefServerResult(rc, code, message);
182 } catch (JSONException e) {
184 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
185 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
186 } catch (Exception e) {
188 logger.error(POSTING_REQUEST_ERROR_STR, e);
189 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
193 @SuppressWarnings("nls")
195 public void vnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
196 logger.info("update the nodeObjects of VNF-C");
199 chefInfo(params, ctx);
200 String nodeListS = params.get(NODE_LIST_STR);
201 String nodeS = params.get("Node");
202 if (StringUtils.isNotBlank(nodeListS) && StringUtils.isNotBlank(nodeS)) {
203 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
204 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
205 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
206 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
207 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
208 RequestContext rc = new RequestContext(ctx);
211 String message = null;
212 if (privateKeyCheck()) {
213 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
215 for (String nodeName: nodes) {
216 JSONObject nodeJ = new JSONObject(nodeS);
217 nodeJ.remove("name");
218 nodeJ.put("name", nodeName);
219 String nodeObject = nodeJ.toString();
220 logger.info(nodeObject);
221 ChefResponse chefResponse = cac.put("/nodes/" + nodeName, nodeObject);
222 code = chefResponse.getStatusCode();
223 message = chefResponse.getBody();
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 = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
274 ChefResponse chefResponse = cac.post(chefAction, pushRequest);
275 code = chefResponse.getStatusCode();
276 logger.info("pushRequest:" + pushRequest);
277 logger.info("requestbody: {}", chefResponse.getBody());
278 String message = chefResponse.getBody();
280 int startIndex = message.indexOf("jobs") + 5;
281 int endIndex = message.length() - 2;
282 String jobID = message.substring(startIndex, endIndex);
283 svcLogic.setAttribute("jobID", jobID);
286 chefServerResult(rc, code, message);
288 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
290 } catch (JSONException e) {
292 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
293 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
294 } catch (Exception e) {
296 logger.error(POSTING_REQUEST_ERROR_STR, e);
297 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
301 @SuppressWarnings("nls")
303 public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
306 chefInfo(params, ctx);
307 String nodeListS = params.get(NODE_LIST_STR);
308 if (StringUtils.isNotBlank(nodeListS)) {
309 nodeListS = nodeListS.replace("[", StringUtils.EMPTY);
310 nodeListS = nodeListS.replace("]", StringUtils.EMPTY);
311 nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
312 nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
313 List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
314 JSONObject result = new JSONObject();
315 String returnMessage = StringUtils.EMPTY;
317 for (String node : nodes) {
318 String chefAction = "/nodes/" + node;
320 if (privateKeyCheck()) {
321 ChefResponse chefResponse = getApiMethod(chefAction);
322 code = chefResponse.getStatusCode();
323 message = chefResponse.getBody();
326 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
327 doFailure(ctx, code, message);
330 JSONObject nodeResult = new JSONObject();
331 JSONObject allNodeData = new JSONObject(message);
332 allNodeData = allNodeData.getJSONObject("normal");
333 String attribute = "PushJobOutput";
335 String resultData = allNodeData.optString(attribute);
336 if (resultData == null) {
337 resultData = allNodeData.optJSONObject(attribute).toString();
339 if (resultData == null) {
340 resultData = allNodeData.optJSONArray(attribute).toString();
342 if (resultData == null) {
344 returnMessage = "Cannot find " + attribute;
349 nodeResult.put(attribute, resultData);
350 result.put(node, nodeResult);
351 returnMessage = result.toString();
354 returnMessage = message + " Cannot access: " + node;
355 doFailure(ctx, code, message);
360 RequestContext rc = new RequestContext(ctx);
362 chefServerResult(rc, code, returnMessage);
364 throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
366 } catch (JSONException e) {
368 logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
369 doFailure(ctx, code, POSTING_REQUEST_JSON_ERROR_STR + e.getMessage());
370 } catch (Exception e) {
372 logger.error(POSTING_REQUEST_ERROR_STR , e);
373 doFailure(ctx, code, POSTING_REQUEST_ERROR_STR + e.getMessage());
377 private ChefResponse getApiMethod(String chefAction) {
378 ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
379 return cac.get(chefAction);
385 @SuppressWarnings("nls")
387 public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) {
388 logger.info("nodeObejctBuilder");
389 String name = params.get("nodeobject.name");
390 String normal = params.get("nodeobject.normal");
391 String overrides = params.get("nodeobject.overrides");
392 String defaults = params.get("nodeobject.defaults");
393 String runList = params.get("nodeobject.run_list");
394 String chefEnvironment = params.get("nodeobject.chef_environment");
395 String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults
396 + "},\"chef_type\":\"node\",\"run_list\":[" + runList + "],\"override\":{" + overrides
397 + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\""
398 + chefEnvironment + "\"}";
399 logger.info(nodeObject);
400 RequestContext rc = new RequestContext(ctx);
402 SvcLogicContext svcLogic = rc.getSvcLogicContext();
403 svcLogic.setAttribute("chef.nodeObject", nodeObject);
407 * send get request to chef server
409 private void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
411 username = params.get("username");
412 serverAddress = params.get("serverAddress");
413 organizations = params.get("organizations");
414 if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(serverAddress)
415 && StringUtils.isNotBlank(organizations)) {
416 chefserver = "https://" + serverAddress + "/organizations/" + organizations;
417 clientPrivatekey = "/opt/onap/appc/chef/" + serverAddress + "/" + organizations + "/" + username + ".pem";
418 logger.info(" clientPrivatekey " + clientPrivatekey);
420 doFailure(ctx, 401, "Missing mandatory param(s) such as username, serverAddress, organizations");
424 private Boolean privateKeyCheck() {
425 File f = new File(clientPrivatekey);
427 logger.info("Key exists");
430 logger.info("Key doesn't exists");
435 @SuppressWarnings("nls")
437 public void retrieveData(Map<String, String> params, SvcLogicContext ctx) {
438 String allConfigData = params.get("allConfig");
439 String key = params.get("key");
440 String dgContext = params.get("dgContext");
441 JSONObject jsonConfig = new JSONObject(allConfigData);
442 String contextData = fetchContextData(key, jsonConfig);
444 RequestContext rc = new RequestContext(ctx);
446 SvcLogicContext svcLogic = rc.getSvcLogicContext();
447 svcLogic.setAttribute(dgContext, contextData);
450 private String fetchContextData(String key, JSONObject jsonConfig) {
452 return jsonConfig.getString(key);
453 } catch (Exception e) {
454 logger.error("Failed getting string value corresponding to " + key + ". Trying to fetch nested json object", e);
456 return jsonConfig.getJSONObject(key).toString();
457 } catch (Exception ex) {
458 logger.error("Failed getting json object corresponding to " + key + ". Trying to fetch array", ex);
459 return jsonConfig.getJSONArray(key).toString();
464 @SuppressWarnings("nls")
466 public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
467 String string1 = params.get("String1");
468 String string2 = params.get("String2");
469 String dgContext = params.get("dgContext");
470 String contextData = string1 + string2;
471 RequestContext rc = new RequestContext(ctx);
473 SvcLogicContext svcLogic = rc.getSvcLogicContext();
474 svcLogic.setAttribute(dgContext, contextData);
478 * Send GET request to chef server
480 @SuppressWarnings("nls")
483 public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
484 logger.info("chef get method");
485 chefInfo(params, ctx);
486 String chefAction = params.get(CHEF_ACTION_STR);
487 RequestContext rc = new RequestContext(ctx);
492 if (privateKeyCheck()) {
493 ChefResponse chefResponse = getApiMethod(chefAction);
494 code = chefResponse.getStatusCode();
495 message = chefResponse.getBody();
498 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
500 chefServerResult(rc, code, message);
504 * Send PUT request to chef server
506 @SuppressWarnings("nls")
509 public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
510 chefInfo(params, ctx);
511 String chefAction = params.get(CHEF_ACTION_STR);
512 String chefNodeStr = params.get("chefRequestBody");
513 RequestContext rc = new RequestContext(ctx);
517 if (privateKeyCheck()) {
518 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
520 ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr);
521 code = chefResponse.getStatusCode();
522 message = chefResponse.getBody();
525 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
527 logger.info(code + " " + message);
528 chefServerResult(rc, code, message);
532 * send Post request to chef server
535 public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
536 chefInfo(params, ctx);
537 logger.info("chef Post method");
538 logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations);
539 String chefNodeStr = params.get("chefRequestBody");
540 String chefAction = params.get(CHEF_ACTION_STR);
542 RequestContext rc = new RequestContext(ctx);
546 // should load pem from somewhere else
547 if (privateKeyCheck()) {
548 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
550 // need pass path into it
552 ChefResponse chefResponse = chefApiClient.post(chefAction, chefNodeStr);
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 * send delete request to chef server
567 public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
568 logger.info("chef delete method");
569 chefInfo(params, ctx);
570 String chefAction = params.get(CHEF_ACTION_STR);
571 RequestContext rc = new RequestContext(ctx);
575 if (privateKeyCheck()) {
576 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
577 ChefResponse chefResponse = chefApiClient.delete(chefAction);
578 code = chefResponse.getStatusCode();
579 message = chefResponse.getBody();
582 message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
584 logger.info(code + " " + message);
585 chefServerResult(rc, code, message);
589 * Trigger target vm run chef
592 public void trigger(Map<String, String> params, SvcLogicContext ctx) {
593 logger.info("Run trigger method");
594 String tVmIp = params.get("ip");
595 RequestContext rc = new RequestContext(ctx);
598 try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
599 HttpGet httpGet = new HttpGet(tVmIp);
600 HttpResponse response = httpClient.execute(httpGet);
601 int responseCode = response.getStatusLine().getStatusCode();
602 HttpEntity entity = response.getEntity();
603 String responseOutput = EntityUtils.toString(entity);
604 chefClientResult(rc, responseCode, responseOutput);
606 } catch (Exception e) {
607 logger.error("An error occurred when executing trigger method", e);
608 doFailure(rc, 500, e.toString());
612 @SuppressWarnings("nls")
614 public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
617 chefInfo(params, ctx);
618 String jobID = params.get("jobid");
619 String retry = params.get("retryTimes");
620 String intrva = params.get("retryInterval");
621 if (StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) {
623 int retryTimes = Integer.parseInt(params.get("retryTimes"));
624 int retryInterval = Integer.parseInt(params.get("retryInterval"));
626 String chefAction = "/pushy/jobs/" + jobID;
628 RequestContext rc = new RequestContext(ctx);
630 SvcLogicContext svcLogic = rc.getSvcLogicContext();
631 String message = StringUtils.EMPTY;
632 String status = StringUtils.EMPTY;
633 for (int i = 0; i < retryTimes; i++) {
634 sleepFor(retryInterval);
635 ChefResponse chefResponse = getApiMethod(chefAction);
636 code = chefResponse.getStatusCode();
637 message = chefResponse.getBody();
638 JSONObject obj = new JSONObject(message);
639 status = obj.getString("status");
640 if (!"running".equals(status)) {
641 logger.info(i + " time " + code + " " + status);
645 resolveSvcLogicAttributes(svcLogic, message, status);
647 throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
649 } catch (Exception e) {
651 logger.error("An error occurred when executing checkPushJob method", e);
652 doFailure(ctx, code, e.getMessage());
656 private void resolveSvcLogicAttributes(SvcLogicContext svcLogic, String message, String status) {
657 if ("complete".equals(status)) {
658 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "200");
659 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
661 if ("running".equals(status)) {
662 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "202");
663 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, "chef client runtime out");
665 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, "500");
666 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, message);
671 private void sleepFor(int retryInterval) {
673 Thread.sleep(retryInterval); // 1000 milliseconds is one second.
674 } catch (InterruptedException ex) {
675 Thread.currentThread().interrupt();
679 @SuppressWarnings("nls")
681 public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
684 chefInfo(params, ctx);
685 String pushRequest = params.get("pushRequest");
686 String chefAction = "/pushy/jobs";
687 RequestContext rc = new RequestContext(ctx);
689 SvcLogicContext svcLogic = rc.getSvcLogicContext();
690 ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
691 ChefResponse chefResponse = chefApiClient.post(chefAction, pushRequest);
693 code = chefResponse.getStatusCode();
694 String message = chefResponse.getBody();
696 int startIndex = message.indexOf("jobs") + 6;
697 int endIndex = message.length() - 2;
698 String jobID = message.substring(startIndex, endIndex);
699 svcLogic.setAttribute("jobID", jobID);
702 chefServerResult(rc, code, message);
703 } catch (Exception e) {
705 logger.error("An error occurred when executing pushJob method", e);
706 doFailure(ctx, code, e.getMessage());
710 @SuppressWarnings("static-method")
711 private void doFailure(RequestContext rc, int code, String message) {
712 SvcLogicContext svcLogic = rc.getSvcLogicContext();
713 String msg = (message == null) ? Integer.toString(code) : message;
714 if (msg.contains("\n")) {
715 msg = msg.substring(msg.indexOf('\n'));
720 status = Integer.toString(code);
721 } catch (Exception e) {
723 logger.error("Parsing status code failed. Setting it to \"500\"", e);
725 svcLogic.setAttribute("chefAgent.code", status);
726 svcLogic.setAttribute("chefAgent.message", msg);
730 * @param rc The request context that manages the state and recovery of the request for the life of its processing.
732 @SuppressWarnings("static-method")
733 private void doSuccess(RequestContext rc) {
734 SvcLogicContext svcLogic = rc.getSvcLogicContext();
735 svcLogic.setAttribute("chefAgent.code", "200");
738 @SuppressWarnings("static-method")
739 private void chefServerResult(RequestContext rc, int code, String message) {
740 initSvcLogic(rc, code, message, "server");
743 @SuppressWarnings("static-method")
744 private void chefClientResult(RequestContext rc, int code, String message) {
745 initSvcLogic(rc, code, message, "client");
748 private void initSvcLogic(RequestContext rc, int code, String message, String target) {
750 SvcLogicContext svcLogic = rc.getSvcLogicContext();
751 String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR;
752 String messageStr = "client".equals(target) ? CHEF_SERVER_RESULT_MSG_STR : CHEF_CLIENT_RESULT_MSG_STR;
754 svcLogic.setStatus(OUTCOME_SUCCESS);
755 svcLogic.setAttribute(codeStr, Integer.toString(code));
756 svcLogic.setAttribute(messageStr, message);
757 logger.info(codeStr + ": " + svcLogic.getAttribute(codeStr));
758 logger.info(messageStr + ": " + svcLogic.getAttribute(messageStr));
763 * initialize the provider adapter by building the context cache
765 private void initialize() {
767 logger.info("Initialize Chef Adapter");
770 @SuppressWarnings("static-method")
771 private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {
773 String cutMessage = message.contains("\n") ? message.substring(message.indexOf('\n')) : message;
775 svcLogic.setStatus(OUTCOME_FAILURE);
776 svcLogic.setAttribute(CHEF_SERVER_RESULT_CODE_STR, Integer.toString(code));
777 svcLogic.setAttribute(CHEF_SERVER_RESULT_MSG_STR, cutMessage);
779 throw new SvcLogicException("Chef Adapter error:" + cutMessage);