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=========================================================
25 package org.openecomp.appc.adapter.chef.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.http.HttpEntity;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.impl.client.HttpClients;
34 import org.apache.http.util.EntityUtils;
35 import org.json.JSONObject;
36 import org.openecomp.appc.Constants;
37 import org.openecomp.appc.adapter.chef.ChefAdapter;
38 import org.openecomp.appc.adapter.chef.chefapi.ApiMethod;
39 import org.openecomp.appc.adapter.chef.chefclient.ChefApiClient;
40 import org.openecomp.appc.configuration.Configuration;
41 import org.openecomp.appc.configuration.ConfigurationFactory;
42 import org.openecomp.sdnc.sli.SvcLogicContext;
48 * This class implements the {@link ChefAdapter} interface. This interface
49 * defines the behaviors that our service provides.
51 public class ChefAdapterImpl implements ChefAdapter {
52 //chef server Initialize variable
53 private String clientName="";
54 private String clientPrivatekey="";
55 private String chefserver="";
56 private String serverAddress="";
57 private String organizations="";
60 * The constant for the status code for a successful outcome
62 private static final String OUTCOME_SUCCESS = "success";
65 * The logger to be used
67 private final EELFLogger logger = EELFManager.getInstance().getLogger(ChefAdapterImpl.class);
70 * A reference to the adapter configuration object.
72 private Configuration configuration;
75 * This default constructor is used as a work around because the activator
76 * wasnt getting called
78 public ChefAdapterImpl() {
84 * This constructor is used primarily in the test cases to bypass
85 * initialization of the adapter for isolated, disconnected testing
88 * True if the adapter is to be initialized, can false if not
90 public ChefAdapterImpl(boolean initialize) {
91 configuration = ConfigurationFactory.getConfiguration();
98 public ChefAdapterImpl(String key) {
104 * Returns the symbolic name of the adapter
106 * @return The adapter name
107 * @see org.openecomp.appc.adapter.chef.ChefAdapter#getAdapterName()
110 public String getAdapterName() {
111 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
114 private void X__________________________________X() {
118 * @see org.openecomp.appc.adapter.chef.ChefAdapter#evacuateServer(java.util.Map,
119 * org.openecomp.sdnc.sli.SvcLogicContext)
122 private void X___________________________________X() {
126 * @see org.openecomp.appc.adapter.chef.ProviderAdapter#rebuildServer(java.util.Map,
127 * org.openecomp.sdnc.sli.SvcLogicContext)
135 @SuppressWarnings("nls")
137 public void nodeObejctBuilder(Map<String, String> params, SvcLogicContext ctx) {
138 logger.info("nodeObejctBuilder");
139 String name = params.get("org.openecomp.appc.instance.nodeobject.name");
140 String normal = params.get("org.openecomp.appc.instance.nodeobject.normal");
141 String overrides = params.get("org.openecomp.appc.instance.nodeobject.overrides");
142 String defaults = params.get("org.openecomp.appc.instance.nodeobject.defaults");
143 String run_list = params.get("org.openecomp.appc.instance.nodeobject.run_list");
144 String chef_environment = params.get("org.openecomp.appc.instance.nodeobject.chef_environment");
145 String nodeObject = "{\"json_class\":\"Chef::Node\",\"default\":{" + defaults
146 + "},\"chef_type\":\"node\",\"run_list\":[" + run_list + "],\"override\":{" + overrides
147 + "},\"normal\": {" + normal + "},\"automatic\":{},\"name\":\"" + name + "\",\"chef_environment\":\""
148 + chef_environment + "\"}";
149 logger.info(nodeObject);
151 RequestContext rc = new RequestContext(ctx);
153 SvcLogicContext svcLogic = rc.getSvcLogicContext();
154 svcLogic.setAttribute("org.openecomp.appc.chef.nodeObject", nodeObject);
159 * Nicolas send get request to chef server
161 public void chefInfo(Map<String, String> params) {
162 clientName = params.get("org.openecomp.appc.instance.username");
163 serverAddress = params.get("org.openecomp.appc.instance.serverAddress");
164 organizations = params.get("org.openecomp.appc.instance.organizations");
165 chefserver = "https://" + serverAddress + "/organizations/" + organizations;
166 clientPrivatekey = "/opt/app/bvc/chef/" + serverAddress + "/" + organizations + "/" + clientName + ".pem";
169 public Boolean privateKeyCheck() {
170 File f = new File(clientPrivatekey);
174 @SuppressWarnings("nls")
176 public void retrieveData(Map<String, String> params, SvcLogicContext ctx) {
177 String allConfigData = params.get("org.openecomp.appc.instance.allConfig");
178 String key = params.get("org.openecomp.appc.instance.key");
179 String dgContext = params.get("org.openecomp.appc.instance.dgContext");
180 JSONObject josnConfig = new JSONObject(allConfigData);
184 contextData = josnConfig.getString(key);
185 } catch (Exception ex) {
187 contextData = josnConfig.getJSONObject(key).toString();
188 } catch (Exception exc) {
189 contextData = josnConfig.getJSONArray(key).toString();
193 RequestContext rc = new RequestContext(ctx);
195 SvcLogicContext svcLogic = rc.getSvcLogicContext();
196 svcLogic.setAttribute(dgContext, contextData);
199 @SuppressWarnings("nls")
201 public void combineStrings(Map<String, String> params, SvcLogicContext ctx) {
203 String String1 = params.get("org.openecomp.appc.instance.String1");
204 String String2 = params.get("org.openecomp.appc.instance.String2");
205 String dgContext = params.get("org.openecomp.appc.instance.dgContext");
206 String contextData = String1 + String2;
207 RequestContext rc = new RequestContext(ctx);
209 SvcLogicContext svcLogic = rc.getSvcLogicContext();
210 svcLogic.setAttribute(dgContext, contextData);
215 * Send GET request to chef server
218 @SuppressWarnings("nls")
220 public void chefGet(Map<String, String> params, SvcLogicContext ctx) {
221 logger.info("chef get method");
223 String chefAction = params.get("org.openecomp.appc.instance.chefAction");
224 RequestContext rc = new RequestContext(ctx);
228 if (privateKeyCheck()) {
229 ChefApiClient cac = new ChefApiClient(clientName, clientPrivatekey, chefserver, organizations);
230 ApiMethod am = cac.get(chefAction);
232 code = am.getReturnCode();
233 message = am.getResponseBodyAsString();
236 message = "Cannot find the private key in the APPC file system, please load the private key to "
239 chefServerResult(rc, Integer.toString(code), message);
244 * Send PUT request to chef server
246 @SuppressWarnings("nls")
248 public void chefPut(Map<String, String> params, SvcLogicContext ctx) {
250 String chefAction = params.get("org.openecomp.appc.instance.chefAction");
251 String CHEF_NODE_STR = params.get("org.openecomp.appc.instance.chefRequestBody");
252 RequestContext rc = new RequestContext(ctx);
256 if (privateKeyCheck()) {
257 ChefApiClient cac = new ChefApiClient(clientName, clientPrivatekey, chefserver, organizations);
259 ApiMethod am = cac.put(chefAction).body(CHEF_NODE_STR);
261 code = am.getReturnCode();
262 message = am.getResponseBodyAsString();
265 message = "Cannot find the private key in the APPC file system, please load the private key to "
268 logger.info(code + " " + message);
269 chefServerResult(rc, Integer.toString(code), message);
274 * Nicolas send Post request to chef server
277 @SuppressWarnings("nls")
279 public void chefPost(Map<String, String> params, SvcLogicContext ctx) {
281 logger.info("chef Post method");
282 logger.info(clientName + " " + clientPrivatekey + " " + chefserver + " " + organizations);
283 String CHEF_NODE_STR = params.get("org.openecomp.appc.instance.chefRequestBody");
284 String chefAction = params.get("org.openecomp.appc.instance.chefAction");
286 RequestContext rc = new RequestContext(ctx);
290 // should load pem from somewhere else
291 if (privateKeyCheck()) {
292 ChefApiClient cac = new ChefApiClient(clientName, clientPrivatekey, chefserver, organizations);
294 // need pass path into it
296 ApiMethod am = cac.post(chefAction).body(CHEF_NODE_STR);
298 code = am.getReturnCode();
299 message = am.getResponseBodyAsString();
302 message = "Cannot find the private key in the APPC file system, please load the private key to "
305 logger.info(code + " " + message);
306 chefServerResult(rc, Integer.toString(code), message);
310 * Nicolas send delete request to chef server
313 @SuppressWarnings("nls")
315 public void chefDelete(Map<String, String> params, SvcLogicContext ctx) {
316 logger.info("chef delete method");
318 String chefAction = params.get("org.openecomp.appc.instance.chefAction");
319 RequestContext rc = new RequestContext(ctx);
323 if (privateKeyCheck()) {
324 ChefApiClient cac = new ChefApiClient(clientName, clientPrivatekey, chefserver, organizations);
325 ApiMethod am = cac.delete(chefAction);
327 code = am.getReturnCode();
328 message = am.getResponseBodyAsString();
331 message = "Cannot find the private key in the APPC file system, please load the private key to "
334 logger.info(code + " " + message);
335 chefServerResult(rc, Integer.toString(code), message);
340 * Trigger target vm run chef
343 @SuppressWarnings("nls")
345 public void trigger(Map<String, String> params, SvcLogicContext ctx) {
346 logger.info("Run trigger method");
347 String tVmIp = params.get("org.openecomp.appc.instance.ip");
348 //String tUrl = "http://" + tVmIp;
349 RequestContext rc = new RequestContext(ctx);
353 HttpGet httpGet = new HttpGet(tVmIp);
354 HttpClient httpClient = HttpClients.createDefault();
355 HttpResponse response;
356 response = httpClient.execute(httpGet);
357 int responseCode=response.getStatusLine().getStatusCode();
358 HttpEntity entity = response.getEntity();
359 String responseOutput=EntityUtils.toString(entity);
360 chefClientResult(rc,Integer.toString(responseCode),responseOutput);
362 } catch (Exception ex) {
363 doFailure(rc, 500 , ex.toString());
368 @SuppressWarnings("nls")
370 public void checkPushJob(Map<String, String> params, SvcLogicContext ctx) {
372 String jobID = params.get("org.openecomp.appc.instance.jobid");
373 int retryTimes = Integer.parseInt(params.get("org.openecomp.appc.instance.retryTimes"));
374 int retryInterval = Integer.parseInt(params.get("org.openecomp.appc.instance.retryInterval"));
375 String chefAction = "/pushy/jobs/" + jobID;
377 RequestContext rc = new RequestContext(ctx);
379 SvcLogicContext svcLogic = rc.getSvcLogicContext();
382 for (int i = 0; i < retryTimes; i++) {
384 Thread.sleep(retryInterval); // 1000 milliseconds is one second.
385 } catch (InterruptedException ex) {
386 Thread.currentThread().interrupt();
388 ChefApiClient cac = new ChefApiClient(clientName, clientPrivatekey, chefserver, organizations);
389 ApiMethod am = cac.get(chefAction);
391 int code = am.getReturnCode();
392 message = am.getResponseBodyAsString();
393 JSONObject obj = new JSONObject(message);
394 status = obj.getString("status");
395 if (!status.equals("running")) {
396 logger.info(i + " time " + code + " " + status);
401 if (status.equals("complete")) {
402 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.code", "200");
403 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.message", message);
405 if (status.equals("running")) {
406 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.code", "202");
407 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.message", "chef client runtime out");
409 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.code", "500");
410 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.message", message);
416 @SuppressWarnings("nls")
418 public void pushJob(Map<String, String> params, SvcLogicContext ctx) {
420 String pushRequest = params.get("org.openecomp.appc.instance.pushRequest");
421 String chefAction = "/pushy/jobs";
422 RequestContext rc = new RequestContext(ctx);
424 SvcLogicContext svcLogic = rc.getSvcLogicContext();
425 ChefApiClient cac = new ChefApiClient(clientName, clientPrivatekey, chefserver, organizations);
426 ApiMethod am = cac.post(chefAction).body(pushRequest);
429 int code = am.getReturnCode();
430 String message = am.getResponseBodyAsString();
432 int startIndex = message.indexOf("jobs") + 6;
433 int endIndex = message.length() - 2;
434 String jobID = message.substring(startIndex, endIndex);
435 svcLogic.setAttribute("org.openecomp.appc.jobID", jobID);
438 chefServerResult(rc, Integer.toString(code), message);
442 @SuppressWarnings("static-method")
443 private void doFailure(RequestContext rc, int code, String message) {
444 SvcLogicContext svcLogic = rc.getSvcLogicContext();
445 String msg = (message == null) ? Integer.toString(code) : message;
446 if (msg.contains("\n")) {
447 msg = msg.substring(msg.indexOf("\n"));
452 status = Integer.toString(code);
453 } catch (Exception e) {
456 svcLogic.setAttribute("org.openecomp.appc.chefAgent.code", status);
457 svcLogic.setAttribute("org.openecomp.appc.chefAgent.message", msg);
462 * The request context that manages the state and recovery of the
463 * request for the life of its processing.
466 @SuppressWarnings("static-method")
467 private void doSuccess(RequestContext rc) {
468 SvcLogicContext svcLogic = rc.getSvcLogicContext();
469 svcLogic.setAttribute("org.openecomp.appc.chefAgent.code", "200");
473 @SuppressWarnings("static-method")
474 private void chefServerResult(RequestContext rc, String code ,String message) {
475 SvcLogicContext svcLogic = rc.getSvcLogicContext();
476 svcLogic.setStatus(OUTCOME_SUCCESS);
477 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.code", code);
478 svcLogic.setAttribute("org.openecomp.appc.chefServerResult.message", message);
481 @SuppressWarnings("static-method")
482 private void chefClientResult(RequestContext rc, String code ,String message) {
483 SvcLogicContext svcLogic = rc.getSvcLogicContext();
484 svcLogic.setStatus(OUTCOME_SUCCESS);
485 svcLogic.setAttribute("org.openecomp.appc.chefClientResult.code", code);
486 svcLogic.setAttribute("org.openecomp.appc.chefClientResult.message", message);
490 * initialize the provider adapter by building the context cache
492 private void initialize() {
493 configuration = ConfigurationFactory.getConfiguration();
494 //need to fetch data from appc configurator or form some file in the appc vms
495 clientName="testnode";
496 clientPrivatekey="/etc/chef/client.pem";
497 serverAddress="http://example.com";
498 organizations="test";
499 chefserver=serverAddress+"/organizations/"+organizations;
500 logger.info("Initialize Chef Adapter");
503 private void initialize(String key) {
504 configuration = ConfigurationFactory.getConfiguration();
505 //need to fetch data from appc configurator or form some file in the appc vms
506 clientName="testnode";
507 clientPrivatekey=key;
508 serverAddress="http://example.com";
509 organizations="test";
510 chefserver=serverAddress+"/organizations/"+organizations;
511 logger.info("Initialize Chef Adapter");