aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java
blob: 9b224fc81c8e138a0cc9177aaa48ebd98a1fa163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */
package org.onap.appc.adapter.chef.impl;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.appc.adapter.chef.ChefAdapter;
import org.onap.appc.adapter.chef.chefapi.ApiMethod;
import org.onap.appc.adapter.chef.chefclient.ChefApiClient;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

/**
 * This class implements the {@link ChefAdapter} interface. This interface
 * defines the behaviors that our service provides.
 */
public class ChefAdapterImpl implements ChefAdapter {
    // chef server Initialize variable
    private String username = StringUtils.EMPTY;
    private String clientPrivatekey = StringUtils.EMPTY;
    private String chefserver = StringUtils.EMPTY;
    private String serverAddress = StringUtils.EMPTY;
    private String organizations = StringUtils.EMPTY;

    @SuppressWarnings("nls")
    public static final String MDC_ADAPTER = "adapter";

    @SuppressWarnings("nls")
    public static final String MDC_SERVICE = "service";

    @SuppressWarnings("nls")
    public static final String OUTCOME_FAILURE = "failure";

    @SuppressWarnings("nls")
    public static final String OUTCOME_SUCCESS = "success";

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER = "provider";

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER_IDENTITY = "identity";

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER_NAME = "name";

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER_TENANT = "tenant";

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";

    @SuppressWarnings("nls")
    public static final String PROPERTY_PROVIDER_TYPE = "type";


    private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdapterImpl.class);

    private static final String CANNOT_FIND_PRIVATE_KEY_STR =
            "Cannot find the private key in the APPC file system, please load the private key to ";

    /**
     * This default constructor is used as a work around because the activator wasnt
     * getting called
     */
    public ChefAdapterImpl() {
        initialize();
    }

    public ChefAdapterImpl(Properties props) {
        initialize();
    }

    /**
     * This constructor is used primarily in the test cases to bypass initialization
     * of the adapter for isolated, disconnected testing
     *
     * @param initialize
     *        True if the adapter is to be initialized, can false if not
     */

    public ChefAdapterImpl(boolean initialize) {
        if (initialize) {
            initialize();
        }
    }

    public ChefAdapterImpl(String key) {
        initialize();
    }

    /**
     * Returns the symbolic name of the adapter
     *
     * @return The adapter name
     * @see org.onap.appc.adapter.chef.ChefAdapter#getAdapterName()
     */
    @Override
    public String getAdapterName() {
        return "chef adapter";
    }

    @SuppressWarnings("nls")
    @Override
    public void VnfcEnvironment(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        int code;
        try {
            logger.info("environment of VNF-C");
            chefInfo(params, ctx);
            RequestContext rc = new RequestContext(ctx);
            logger.info("Context" + ctx);
            rc.isAlive();
            String env = params.get("Environment");
            logger.info("Environmnet" + env);
            if (env.equals(StringUtils.EMPTY)) {
                chefServerResult(rc, "200", "Skip Environment block ");
            } else {
                JSONObject env_J = new JSONObject(env);
                String envName = env_J.getString("name");
                String message = null;
                if (privateKeyCheck()) {
                    // update the details of an environment on the Chef server.
                    ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
                    ApiMethod am = cac.put("/environments/" + envName).body(env);
                    am.execute();
                    code = am.getReturnCode();
                    message = am.getResponseBodyAsString();
                    if (code == 404) {
                        // need create a new environment
                        am = cac.post("/environments").body(env);
                        am.execute();
                        code = am.getReturnCode();
                        message = am.getResponseBodyAsString();
                        logger.info("requestbody" + am.getReqBody());
                    }

                } else {
                    code = 500;
                    message = "Cannot find the private key in the APPC file system, please load the private key to "
                            + clientPrivatekey;
                    doFailure(ctx, code, message);
                }
                chefServerResult(rc, Integer.toString(code), message);
            }
        }

        catch (JSONException e) {
            code = 401;
            doFailure(ctx, code, "Error posting request  due to invalid JSON block. Reason = " + e.getMessage());
        } catch (Exception e) {
            code = 401;
            doFailure(ctx, code, "Error posting request :Reason = " + e.getMessage());
        }
    }

    @SuppressWarnings("nls")
    @Override
    public void VnfcNodeobjects(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        logger.info("update the nodeObjects of VNF-C");
        int code;
        try {
            chefInfo(params, ctx);
            String nodeList_S = params.get("NodeList");
            String node_S = params.get("Node");
            if (StringUtils.isNotBlank(nodeList_S) && StringUtils.isNotBlank(node_S)) {
                nodeList_S = nodeList_S.replace("[", StringUtils.EMPTY);
                nodeList_S = nodeList_S.replace("]", StringUtils.EMPTY);
                nodeList_S = nodeList_S.replace("\"", StringUtils.EMPTY);
                nodeList_S = nodeList_S.replace(" ", StringUtils.EMPTY);
                List<String> nodes = Arrays.asList(nodeList_S.split("\\s*,\\s*"));
                RequestContext rc = new RequestContext(ctx);
                rc.isAlive();
                code = 200;
                String message = null;
                if (privateKeyCheck()) {
                    ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);

                    for (int i = 0; i < nodes.size(); i++) {
                        String nodeName = nodes.get(i);
                        JSONObject node_J = new JSONObject(node_S);
                        node_J.remove("name");
                        node_J.put("name", nodeName);
                        String nodeObject = node_J.toString();
                        logger.info(nodeObject);
                        ApiMethod am = cac.put("/nodes/" + nodeName).body(nodeObject);
                        am.execute();
                        code = am.getReturnCode();
                        message = am.getResponseBodyAsString();
                        if (code != 200) {
                            break;
                        }
                    }
                } else {
                    code = 500;
                    message = "Cannot find the private key in the APPC file system, please load the private key to "
                            + clientPrivatekey;
                    doFailure(ctx, code, message);
                }
                chefServerResult(rc, Integer.toString(code), message);
            }
            else {
                throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList ");
            }
        } catch (JSONException e) {
            code = 401;
            doFailure(ctx, code, "Error posting request  due to invalid JSON block. Reason = " + e.getMessage());
        } catch (Exception ex) {
            code = 401;
            doFailure(ctx, code, "Error posting request :Reason = " + ex.getMessage());
        }
    }

    @Override
    public void VnfcPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        int code;
        try {
            chefInfo(params, ctx);
            String nodeList = params.get("NodeList");
            if (StringUtils.isNotBlank(nodeList)) {
                String isCallback = params.get("CallbackCapable");
                String chefAction = "/pushy/jobs";
                // need work on this
                String pushRequest = StringUtils.EMPTY;
                if (isCallback.equals("true")) {
                    String requestId = params.get("RequestId");
                    String callbackUrl = params.get("CallbackUrl");
                    pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
                            + nodeList + "," + "\"env\": {\"RequestId\": \"" + requestId + "\", \"CallbackUrl\": \""
                            + callbackUrl + "\"}," + "\"capture_output\": true" + "}";
                } else {
                    pushRequest = "{" + "\"command\": \"chef-client\"," + "\"run_timeout\": 300," + "\"nodes\":"
                            + nodeList + "," + "\"env\": {}," + "\"capture_output\": true" + "}";
                }
                RequestContext rc = new RequestContext(ctx);

                rc.isAlive();
                SvcLogicContext svcLogic = rc.getSvcLogicContext();
                ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
                ApiMethod am = cac.post(chefAction).body(pushRequest);
                am.execute();
                code = am.getReturnCode();
                logger.info("pushRequest:" + pushRequest);
                logger.info("requestbody:" + am.getReqBody());
                String message = am.getResponseBodyAsString();
                if (code == 201) {
                    int startIndex = message.indexOf("jobs") + 5;
                    int endIndex = message.length() - 2;
                    String jobID = message.substring(startIndex, endIndex);
                    svcLogic.setAttribute("jobID", jobID);
                    logger.info(jobID);
                }
                chefServerResult(rc, Integer.toString(code), message);
            }
            else {
                throw new SvcLogicException("Missing Mandatory param(s)  NodeList ");
            }
        } catch (JSONException e) {
            code = 401;
            doFailure(ctx, code, "Error posting request  due to invalid JSON block. Reason = " + e.getMessage());
        } catch (Exception e) {
            code = 401;
            doFailure(ctx, code, e.getMessage());
        }
    }

    @SuppressWarnings("nls")
    @Override
    public void fetchResults(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        int code;
        try {
            chefInfo(params, ctx);
            String nodeList_S = params.get("NodeList");
            if (StringUtils.isNotBlank(nodeList_S)) {
                nodeList_S = nodeList_S.replace("[", StringUtils.EMPTY);
                nodeList_S = nodeList_S.replace("]", StringUtils.EMPTY);
                nodeList_S = nodeList_S.replace("\"", StringUtils.EMPTY);
                nodeList_S = nodeList_S.replace(" ", StringUtils.EMPTY);
                List<String> nodes = Arrays.asList(nodeList_S.split("\\s*,\\s*"));
                JSONObject Result = new JSONObject();
                String returnCode = "200";
                String returnMessage = StringUtils.EMPTY;

                for (int i = 0; i < nodes.size(); i++) {
                    String node = nodes.get(i);
                    String chefAction = "/nodes/" + node;
                    String message = null;
                    if (privateKeyCheck()) {
                        ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
                        ApiMethod am = cac.get(chefAction);
                        am.execute();
                        code = am.getReturnCode();
                        message = am.getResponseBodyAsString();
                    } else {
                        code = 500;
                        message = "Cannot find the private key in the APPC file system, please load the private key to "
                                + clientPrivatekey;
                        doFailure(ctx, code, message);
                    }
                    if (code == 200) {
                        JSONObject nodeResult = new JSONObject();
                        JSONObject allNodeData = new JSONObject(message);
                        allNodeData = allNodeData.getJSONObject("normal");
                        String attribute = "PushJobOutput";
                        String resultData;
                        try {
                            resultData = allNodeData.getString(attribute);
                        } catch (Exception exc1) {
                            try {
                                resultData = allNodeData.getJSONObject(attribute).toString();
                            } catch (Exception exc2) {
                                try {
                                    resultData = allNodeData.getJSONArray(attribute).toString();
                                } catch (Exception exc3) {
                                    returnCode = "500";
                                    returnMessage = "cannot find " + attribute;
                                    break;
                                }
                            }
                        }
                        nodeResult.put(attribute, resultData);
                        Result.put(node, nodeResult);
                    } else {
                        returnCode = "500";
                        returnMessage = message + " Cannot access: " + node;
                        doFailure(ctx, code, message);
                        break;
                    }
                }
                RequestContext rc = new RequestContext(ctx);
                rc.isAlive();
                if (!returnCode.equals("500")) {
                    returnMessage = Result.toString();
                    returnCode = "200";
                }
                chefServerResult(rc, returnCode, returnMessage);
            }
            else {
                throw new SvcLogicException("Missing Mandatory param(s)  NodeList ");
            }
        } catch (JSONException e) {
            code = 401;
            doFailure(ctx, code, "Error posting request  due to invalid JSON block. Reason = " + e.getMessage());
        } catch (Exception ex) {
            code = 401;
            doFailure(ctx, code, "Error posting request :Reason = " + ex.getMessage());
        }
    }

    /**
     * build node object
     */
    @SuppressWarnings("nls")
    @Override
    public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) {
        logger.info("nodeObejctBuilder");
        String name = params.get("nodeobject.name");
        String normal = params.get("nodeobject.normal");
        String overrides = params.get("nodeobject.overrides");
        String defaults = params.get("nodeobject.defaults");
        String runList = params.get("nodeobject.run_list");
        String chefEnvironment = params.get("nodeobject.chef_environment");
        String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults
                + "},\"chef_type\":\"node\",\"run_list\":[" + runList + "],\"override\":{" + overrides
                + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\""
                + chefEnvironment + "\"}";
        logger.info(nodeObject);
        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();
        SvcLogicContext svcLogic = rc.getSvcLogicContext();
        svcLogic.setAttribute("chef.nodeObject", nodeObject);
    }

    /**
     * send get request to chef server
     * 
     * @throws SvcLogicException
     */
    public void chefInfo(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {

        username = params.get("username");
        serverAddress = params.get("serverAddress");
        organizations = params.get("organizations");
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(serverAddress)
                && StringUtils.isNotBlank(organizations)) {
            chefserver = "https://" + serverAddress + "/organizations/" + organizations;
            clientPrivatekey = "/opt/app/bvc/chef/" + serverAddress + "/" + organizations + "/" + username + ".pem";
            logger.info(" clientPrivatekey  " + clientPrivatekey);
        } else {
            String message = "Missing mandatory param(s) such as username,serverAddres,organizations";
            doFailure(ctx, 401, message);
        }
    }

    public Boolean privateKeyCheck() {
        File f = new File(clientPrivatekey);
        if (f.exists()) {
            logger.info("Key exists");
            return true;
        } else {
            logger.info("Key doesnot exists");
            return false;
        }
    }

    @SuppressWarnings("nls")
    @Override
    public void retrieveData(Map<String, String> params, SvcLogicContext ctx) {
        String contextData = "someValue";
        String allConfigData = params.get("allConfig");
        String key = params.get("key");
        String dgContext = params.get("dgContext");
        JSONObject josnConfig = new JSONObject(allConfigData);

        try {
            contextData = josnConfig.getString(key);
        } catch (Exception ex) {
            try {
                contextData = josnConfig.getJSONObject(key).toString();
            } catch (Exception exc) {
                contextData = josnConfig.getJSONArray(key).toString();
            }
        }

        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();
        SvcLogicContext svcLogic = rc.getSvcLogicContext();
        svcLogic.setAttribute(dgContext, contextData);
    }

    @SuppressWarnings("nls")
    @Override
    public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
        String string1 = params.get("String1");
        String string2 = params.get("String2");
        String dgContext = params.get("dgContext");
        String contextData = string1 + string2;
        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();
        SvcLogicContext svcLogic = rc.getSvcLogicContext();
        svcLogic.setAttribute(dgContext, contextData);
    }

    /**
     * Send GET request to chef server
     * 
     * @throws SvcLogicException
     */
    @SuppressWarnings("nls")

    @Override
    public void chefGet(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        logger.info("chef get method");
        chefInfo(params, ctx);
        String chefAction = params.get("chefAction");
        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();
        int code;
        String message = null;

        if (privateKeyCheck()) {
            ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
            ApiMethod am = cac.get(chefAction);
            am.execute();
            code = am.getReturnCode();
            message = am.getResponseBodyAsString();
        } else {
            code = 500;
            message = "Cannot find the private key in the APPC file system, please load the private key to "
                    + clientPrivatekey;
        }
        chefServerResult(rc, Integer.toString(code), message);
    }

    /**
     * Send PUT request to chef server
     * 
     * @throws SvcLogicException
     */
    @SuppressWarnings("nls")

    @Override
    public void chefPut(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        chefInfo(params, ctx);
        String chefAction = params.get("chefAction");
        String chefNodeStr = params.get("chefRequestBody");
        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();
        int code;
        String message;
        if (privateKeyCheck()) {
            ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);

            ApiMethod am = cac.put(chefAction).body(chefNodeStr);
            am.execute();
            code = am.getReturnCode();
            message = am.getResponseBodyAsString();
        } else {
            code = 500;
            message = "Cannot find the private key in the APPC file system, please load the private key to "
                    + clientPrivatekey;
        }
        logger.info(code + "   " + message);
        chefServerResult(rc, Integer.toString(code), message);
    }

    /**
     * send Post request to chef server
     * 
     * @throws SvcLogicException
     */
    @Override
    public void chefPost(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        chefInfo(params, ctx);
        logger.info("chef Post method");
        logger.info(username + " " + clientPrivatekey + " " + chefserver + " " + organizations);
        String chefNodeStr = params.get("chefRequestBody");
        String chefAction = params.get("chefAction");

        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();
        int code;
        String message;
        // should load pem from somewhere else
        if (privateKeyCheck()) {
            ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);

            // need pass path into it
            // "/nodes/testnode"
            ApiMethod am = cac.post(chefAction).body(chefNodeStr);
            am.execute();
            code = am.getReturnCode();
            message = am.getResponseBodyAsString();
        } else {
            code = 500;
            message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
        }
        logger.info(code + "   " + message);
        chefServerResult(rc, Integer.toString(code), message);
    }

    /**
     * send delete request to chef server
     * 
     * @throws SvcLogicException
     */
    @Override
    public void chefDelete(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        logger.info("chef delete method");
        chefInfo(params, ctx);
        String chefAction = params.get("chefAction");
        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();
        int code;
        String message = null;
        if (privateKeyCheck()) {
            ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
            ApiMethod am = cac.delete(chefAction);
            am.execute();
            code = am.getReturnCode();
            message = am.getResponseBodyAsString();
        } else {
            code = 500;
            message = "Cannot find the private key in the APPC file system, please load the private key to "
                    + clientPrivatekey;
        }
        logger.info(code + "   " + message);
        chefServerResult(rc, Integer.toString(code), message);
    }

    /**
     * Trigger target vm run chef
     */
    @Override
    public void trigger(Map<String, String> params, SvcLogicContext ctx) {
        logger.info("Run trigger method");
        String tVmIp = params.get("ip");
        RequestContext rc = new RequestContext(ctx);
        rc.isAlive();

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(tVmIp);
            HttpResponse response = null;
            response = httpClient.execute(httpGet);
            int responseCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            String responseOutput = EntityUtils.toString(entity);
            chefClientResult(rc, Integer.toString(responseCode), responseOutput);
            doSuccess(rc);
        } catch (Exception ex) {
            doFailure(rc, 500, ex.toString());
        }
    }

    @SuppressWarnings("nls")
    @Override
    public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        int code;
        try {
            chefInfo(params, ctx);
            String jobID = params.get("jobid");
            String retry = params.get("retryTimes");
            String intrva = params.get("retryInterval");
            if (StringUtils.isNotBlank(retry) && StringUtils.isNotBlank(intrva)) {

                int retryTimes = Integer.parseInt(params.get("retryTimes"));
                int retryInterval = Integer.parseInt(params.get("retryInterval"));

                String chefAction = "/pushy/jobs/" + jobID;

                RequestContext rc = new RequestContext(ctx);
                rc.isAlive();
                SvcLogicContext svcLogic = rc.getSvcLogicContext();
                String message = StringUtils.EMPTY;
                String status = StringUtils.EMPTY;
                for (int i = 0; i < retryTimes; i++) {
                    try {
                        Thread.sleep(retryInterval); // 1000 milliseconds is one second.
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                    ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
                    ApiMethod am = cac.get(chefAction);
                    am.execute();
                    code = am.getReturnCode();
                    message = am.getResponseBodyAsString();
                    JSONObject obj = new JSONObject(message);
                    status = obj.getString("status");
                    if (!status.equals("running")) {
                        logger.info(i + " time " + code + "   " + status);
                        break;
                    }
                }
                if (status.equals("complete")) {
                    svcLogic.setAttribute("chefServerResult.code", "200");
                    svcLogic.setAttribute("chefServerResult.message", message);
                } else {
                    if (status.equals("running")) {
                        svcLogic.setAttribute("chefServerResult.code", "202");
                        svcLogic.setAttribute("chefServerResult.message", "chef client runtime out");
                    } else {
                        svcLogic.setAttribute("chefServerResult.code", "500");
                        svcLogic.setAttribute("chefServerResult.message", message);
                    }
                }
            }
            else {
                throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
            }
        } catch (Exception e) {
            code = 401;
            doFailure(ctx, code, e.getMessage());
        }
    }

    @SuppressWarnings("nls")
    @Override
    public void pushJob(Map<String, String> params, SvcLogicContext ctx) throws SvcLogicException {
        int code;
        try {
            chefInfo(params, ctx);
            String pushRequest = params.get("pushRequest");
            String chefAction = "/pushy/jobs";
            RequestContext rc = new RequestContext(ctx);
            rc.isAlive();
            SvcLogicContext svcLogic = rc.getSvcLogicContext();
            ChefApiClient cac = new ChefApiClient(username, clientPrivatekey, chefserver, organizations);
            ApiMethod am = cac.post(chefAction).body(pushRequest);

            am.execute();
            code = am.getReturnCode();
            String message = am.getResponseBodyAsString();
            if (code == 201) {
                int startIndex = message.indexOf("jobs") + 6;
                int endIndex = message.length() - 2;
                String jobID = message.substring(startIndex, endIndex);
                svcLogic.setAttribute("jobID", jobID);
                logger.info(jobID);
            }
            chefServerResult(rc, Integer.toString(code), message);
        } catch (Exception e) {
            code = 401;
            doFailure(ctx, code, e.getMessage());
        }
    }

    @SuppressWarnings("static-method")
    private void doFailure(RequestContext rc, int code, String message) {
        SvcLogicContext svcLogic = rc.getSvcLogicContext();
        String msg = (message == null) ? Integer.toString(code) : message;
        if (msg.contains("\n")) {
            msg = msg.substring(msg.indexOf("\n"));
        }

        String status;
        try {
            status = Integer.toString(code);
        } catch (Exception e) {

            status = "500";
        }
        svcLogic.setAttribute("chefAgent.code", status);
        svcLogic.setAttribute("chefAgent.message", msg);
    }

    /**
     * @param rc
     *        The request context that manages the state and recovery of the
     *        request for the life of its processing.
     */
    @SuppressWarnings("static-method")
    private void doSuccess(RequestContext rc) {
        SvcLogicContext svcLogic = rc.getSvcLogicContext();
        svcLogic.setAttribute("chefAgent.code", "200");
    }

    @SuppressWarnings("static-method")
    private void chefServerResult(RequestContext rc, String code, String message) {
        SvcLogicContext svcLogic = rc.getSvcLogicContext();
        svcLogic.setStatus(OUTCOME_SUCCESS);
        svcLogic.setAttribute("chefServerResult.code", code);
        svcLogic.setAttribute("chefServerResult.message", message);
        logger.info("chefServerResult.code" + svcLogic.getAttribute("chefServerResult.code"));
        logger.info("chefServerResult.message" + svcLogic.getAttribute("chefServerResult.message"));
    }

    @SuppressWarnings("static-method")
    private void chefClientResult(RequestContext rc, String code, String message) {
        SvcLogicContext svcLogic = rc.getSvcLogicContext();
        svcLogic.setStatus(OUTCOME_SUCCESS);
        svcLogic.setAttribute("chefClientResult.code", code);
        svcLogic.setAttribute("chefClientResult.message", message);
        logger.info("chefClientResult.code" + svcLogic.getAttribute("chefClientResult.code"));
        logger.info("chefClientResult.message" + svcLogic.getAttribute("chefClientResult.message"));
    }

    /**
     * initialize the provider adapter by building the context cache
     */
    private void initialize() {

        logger.info("Initialize Chef Adapter");
    }

    @SuppressWarnings("static-method")
    private void doFailure(SvcLogicContext svcLogic, int code, String message) throws SvcLogicException {

        if (message.contains("\n")) {
            message = message.substring(message.indexOf("\n"));
        }
        svcLogic.setStatus(OUTCOME_FAILURE);
        svcLogic.setAttribute("chefServerResult.code", Integer.toString(code));
        svcLogic.setAttribute("chefServerResult.message", message);

        throw new SvcLogicException("Chef Adapater Error = " + message);
    }
}