summaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-chef-adapter
diff options
context:
space:
mode:
authorMichal Kabaj <michal.kabaj@nokia.com>2018-03-19 09:25:59 +0100
committerTakamune Cho <tc012c@att.com>2018-03-23 15:34:24 +0000
commit0b760e28aac46e6a1122706b68e77e6cbb151f60 (patch)
treea3120fa3edc1cc394e5ba167caa892bdd3dca5b5 /appc-adapters/appc-chef-adapter
parent704b11680b5688f1697029ec77dbcf6b550eb3e0 (diff)
ChefAdapterImpl cleanup
- Switched usage of RequestContext class for wrapping the SvcLogicContext to using the SvcLogicContext directly - Removed unused RequestContext class Change-Id: I7376e3d6f5602887cf5a25f1f90ff45805c620b2 Issue-ID: APPC-437 Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
Diffstat (limited to 'appc-adapters/appc-chef-adapter')
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java73
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/RequestContext.java252
2 files changed, 24 insertions, 301 deletions
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java
index f5b6b6751..28facf3df 100644
--- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java
+++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java
@@ -119,7 +119,7 @@ public class ChefAdapterImpl implements ChefAdapter {
String env = params.get("Environment");
logger.info("Environmnet" + env);
if (env.equals(StringUtils.EMPTY)) {
- chefServerResult(new RequestContext(ctx), 200, "Skip Environment block ");
+ chefServerResult(ctx, 200, "Skip Environment block ");
} else {
String message;
if (privateKeyChecker.doesExist(clientPrivatekey)) {
@@ -138,7 +138,7 @@ public class ChefAdapterImpl implements ChefAdapter {
message = chefResponse.getBody();
logger.info("requestbody {}", chefResponse.getBody());
}
- chefServerResult(new RequestContext(ctx), code, message);
+ chefServerResult(ctx, code, message);
} catch (JSONException e) {
code = 401;
logger.error(POSTING_REQUEST_JSON_ERROR_STR, e);
@@ -171,8 +171,6 @@ public class ChefAdapterImpl implements ChefAdapter {
nodeListS = nodeListS.replace("\"", StringUtils.EMPTY);
nodeListS = nodeListS.replace(" ", StringUtils.EMPTY);
List<String> nodes = Arrays.asList(nodeListS.split("\\s*,\\s*"));
- RequestContext rc = new RequestContext(ctx);
- rc.isAlive();
code = 200;
String message = null;
if (privateKeyChecker.doesExist(clientPrivatekey)) {
@@ -196,7 +194,7 @@ public class ChefAdapterImpl implements ChefAdapter {
message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
doFailure(ctx, code, message);
}
- chefServerResult(rc, code, message);
+ chefServerResult(ctx, code, message);
} else {
throw new SvcLogicException("Missing Mandatory param(s) Node , NodeList ");
}
@@ -232,10 +230,6 @@ public class ChefAdapterImpl implements ChefAdapter {
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 = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
ChefResponse chefResponse = cac.post(chefAction, pushRequest);
code = chefResponse.getStatusCode();
@@ -246,10 +240,10 @@ public class ChefAdapterImpl implements ChefAdapter {
int startIndex = message.indexOf("jobs") + 5;
int endIndex = message.length() - 2;
String jobID = message.substring(startIndex, endIndex);
- svcLogic.setAttribute("jobID", jobID);
+ ctx.setAttribute("jobID", jobID);
logger.info(jobID);
}
- chefServerResult(rc, code, message);
+ chefServerResult(ctx, code, message);
} else {
throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
}
@@ -323,9 +317,7 @@ public class ChefAdapterImpl implements ChefAdapter {
}
}
- RequestContext rc = new RequestContext(ctx);
- rc.isAlive();
- chefServerResult(rc, code, returnMessage);
+ chefServerResult(ctx, code, returnMessage);
} else {
throw new SvcLogicException("Missing Mandatory param(s) NodeList ");
}
@@ -430,8 +422,6 @@ public class ChefAdapterImpl implements ChefAdapter {
logger.info("chef get method");
chefInfo(params, ctx);
String chefAction = params.get(CHEF_ACTION_STR);
- RequestContext rc = new RequestContext(ctx);
- rc.isAlive();
int code;
String message;
@@ -443,7 +433,7 @@ public class ChefAdapterImpl implements ChefAdapter {
code = 500;
message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
}
- chefServerResult(rc, code, message);
+ chefServerResult(ctx, code, message);
}
/**
@@ -456,8 +446,6 @@ public class ChefAdapterImpl implements ChefAdapter {
chefInfo(params, ctx);
String chefAction = params.get(CHEF_ACTION_STR);
String chefNodeStr = params.get("chefRequestBody");
- RequestContext rc = new RequestContext(ctx);
- rc.isAlive();
int code;
String message;
if (privateKeyChecker.doesExist(clientPrivatekey)) {
@@ -471,7 +459,7 @@ public class ChefAdapterImpl implements ChefAdapter {
message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
}
logger.info(code + " " + message);
- chefServerResult(rc, code, message);
+ chefServerResult(ctx, code, message);
}
/**
@@ -485,8 +473,6 @@ public class ChefAdapterImpl implements ChefAdapter {
String chefNodeStr = params.get("chefRequestBody");
String chefAction = params.get(CHEF_ACTION_STR);
- RequestContext rc = new RequestContext(ctx);
- rc.isAlive();
int code;
String message;
// should load pem from somewhere else
@@ -503,7 +489,7 @@ public class ChefAdapterImpl implements ChefAdapter {
message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
}
logger.info(code + " " + message);
- chefServerResult(rc, code, message);
+ chefServerResult(ctx, code, message);
}
/**
@@ -514,8 +500,6 @@ public class ChefAdapterImpl implements ChefAdapter {
logger.info("chef delete method");
chefInfo(params, ctx);
String chefAction = params.get(CHEF_ACTION_STR);
- RequestContext rc = new RequestContext(ctx);
- rc.isAlive();
int code;
String message;
if (privateKeyChecker.doesExist(clientPrivatekey)) {
@@ -528,7 +512,7 @@ public class ChefAdapterImpl implements ChefAdapter {
message = CANNOT_FIND_PRIVATE_KEY_STR + clientPrivatekey;
}
logger.info(code + " " + message);
- chefServerResult(rc, code, message);
+ chefServerResult(ctx, code, message);
}
/**
@@ -538,12 +522,10 @@ public class ChefAdapterImpl implements ChefAdapter {
public void trigger(Map<String, String> params, SvcLogicContext svcLogicContext) {
logger.info("Run trigger method");
String tVmIp = params.get("ip");
- RequestContext rc = new RequestContext(svcLogicContext);
- rc.isAlive();
try {
ChefResponse chefResponse = chefApiClientFactory.create(tVmIp).get("");
- chefClientResult(rc, chefResponse.getStatusCode(), chefResponse.getBody());
+ chefClientResult(svcLogicContext, chefResponse.getStatusCode(), chefResponse.getBody());
svcLogicContext.setAttribute("chefAgent.code", "200");
} catch (Exception e) {
logger.error("An error occurred when executing trigger method", e);
@@ -568,9 +550,6 @@ public class ChefAdapterImpl implements ChefAdapter {
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++) {
@@ -585,7 +564,7 @@ public class ChefAdapterImpl implements ChefAdapter {
break;
}
}
- resolveSvcLogicAttributes(svcLogic, message, status);
+ resolveSvcLogicAttributes(ctx, message, status);
} else {
throw new SvcLogicException("Missing Mandatory param(s) retryTimes , retryInterval ");
}
@@ -625,9 +604,6 @@ public class ChefAdapterImpl implements ChefAdapter {
chefInfo(params, ctx);
String pushRequest = params.get("pushRequest");
String chefAction = "/pushy/jobs";
- RequestContext rc = new RequestContext(ctx);
- rc.isAlive();
- SvcLogicContext svcLogic = rc.getSvcLogicContext();
ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey);
ChefResponse chefResponse = chefApiClient.post(chefAction, pushRequest);
@@ -637,10 +613,10 @@ public class ChefAdapterImpl implements ChefAdapter {
int startIndex = message.indexOf("jobs") + 6;
int endIndex = message.length() - 2;
String jobID = message.substring(startIndex, endIndex);
- svcLogic.setAttribute("jobID", jobID);
+ ctx.setAttribute("jobID", jobID);
logger.info(jobID);
}
- chefServerResult(rc, code, message);
+ chefServerResult(ctx, code, message);
} catch (Exception e) {
code = 401;
logger.error("An error occurred when executing pushJob method", e);
@@ -649,26 +625,25 @@ public class ChefAdapterImpl implements ChefAdapter {
}
@SuppressWarnings("static-method")
- private void chefServerResult(RequestContext rc, int code, String message) {
- initSvcLogic(rc, code, message, "server");
+ private void chefServerResult(SvcLogicContext svcLogicContext, int code, String message) {
+ initSvcLogic(svcLogicContext, code, message, "server");
}
@SuppressWarnings("static-method")
- private void chefClientResult(RequestContext rc, int code, String message) {
- initSvcLogic(rc, code, message, "client");
+ private void chefClientResult(SvcLogicContext svcLogicContext, int code, String message) {
+ initSvcLogic(svcLogicContext, code, message, "client");
}
- private void initSvcLogic(RequestContext rc, int code, String message, String target) {
+ private void initSvcLogic(SvcLogicContext svcLogicContext, int code, String message, String target) {
- SvcLogicContext svcLogic = rc.getSvcLogicContext();
String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR;
String messageStr = "client".equals(target) ? CHEF_CLIENT_RESULT_MSG_STR : CHEF_SERVER_RESULT_MSG_STR;
- svcLogic.setStatus(OUTCOME_SUCCESS);
- svcLogic.setAttribute(codeStr, Integer.toString(code));
- svcLogic.setAttribute(messageStr, message);
- logger.info(codeStr + ": " + svcLogic.getAttribute(codeStr));
- logger.info(messageStr + ": " + svcLogic.getAttribute(messageStr));
+ svcLogicContext.setStatus(OUTCOME_SUCCESS);
+ svcLogicContext.setAttribute(codeStr, Integer.toString(code));
+ svcLogicContext.setAttribute(messageStr, message);
+ logger.info(codeStr + ": " + svcLogicContext.getAttribute(codeStr));
+ logger.info(messageStr + ": " + svcLogicContext.getAttribute(messageStr));
}
@SuppressWarnings("static-method")
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/RequestContext.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/RequestContext.java
deleted file mode 100644
index 3c0e00a6a..000000000
--- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/RequestContext.java
+++ /dev/null
@@ -1,252 +0,0 @@
-/*-
- * ============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 org.onap.appc.Constants;
-import org.onap.appc.configuration.Configuration;
-import org.onap.appc.configuration.ConfigurationFactory;
-import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
-
-/**
- * This class is used to track and maintain recovery and time-to-live information for a request as it is being
- * processed.
- */
-public class RequestContext {
- /**
- * The number of seconds of wait time between successive attempts to connect to the provider. This is used to
- * recover from provider outages or failures. It is not used to recover from logical errors, such as an invalid
- * request, server not found, etc.
- */
- private Integer retryDelay;
-
- /**
- * The number of times we will attempt to connect to the provider. This is used to recover from provider outages or
- * failures. It is not used to recover from logical errors, such as an invalid request, server not found, etc.
- */
- private Integer retryLimit;
-
- /**
- * The total time, in milliseconds, that the provider can have to process this request. If the accumulated time
- * exceeds the time to live, then the request is failed with a timeout exception, regardless of the state of the
- * provider. Note that the caller may supply this as a value in seconds, in which case it must be converted to
- * milliseconds for the request context.
- */
- private Long timeToLive;
-
- /**
- * The accumulated time, in milliseconds, that has been used so far to process the request. This is compared to the
- * time to live each time it is updated. If the accumulated time exceeds the time to live, then the request is
- * failed with a timeout exception, regardless of the state of the provider.
- */
- private long accumulatedTime;
-
- /**
- * The total number of retries attempted so far
- */
- private int attempt;
-
- /**
- * The time when the stopwatch was started
- */
- private long startTime = -1;
-
- /**
- * The service logic (DG) context from the SLI
- */
- private SvcLogicContext svcLogicContext;
-
- /**
- * The configuration
- */
- private Configuration configuration = ConfigurationFactory.getConfiguration();
-
- /**
- * Set to true whenever the retry limit has been exceeded, reset to false when reset() is called.
- */
- private boolean retryFailed;
-
- /**
- * Creates the request context
- *
- * @param context
- * The service logic (SLI) context associated with the current DG
- */
- public RequestContext(SvcLogicContext context) {
- setSvcLogicContext(context);
- }
-
- /**
- * @return The retry delay, in seconds. If zero, then no retry is to be performed
- */
- public int getRetryDelay() {
- if (retryDelay == null) {
- int value = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_DELAY);
- retryDelay = Integer.valueOf(value);
- }
-
- return retryDelay.intValue();
- }
-
- /**
- * This method is a helper that allows the caller to delay for the retry interval time and not have to handle the
- * thread interruption, timer handling, etc.
- */
- public void delay() {
- long time = getRetryDelay() * 1000L;
- long future = System.currentTimeMillis() + time;
- if (time != 0) {
- while (System.currentTimeMillis() < future && time > 0) {
- try {
- Thread.sleep(time);
- } catch (InterruptedException e) {
- /*
- * This is rare, but it can happen if another thread interrupts us while we are sleeping. In that
- * case, the thread is resumed before the delay time has actually expired, so re-calculate the
- * amount of delay time needed and reenter the sleep until we get to the future time.
- */
- time = future - System.currentTimeMillis();
- }
- }
- }
- }
-
- /**
- * @return The number of retries that are allowed per connection
- */
- public int getRetryLimit() {
- if (retryLimit == null) {
- int value = configuration.getIntegerProperty(Constants.PROPERTY_RETRY_LIMIT);
- retryLimit = Integer.valueOf(value);
- }
-
- return retryLimit.intValue();
- }
-
- /**
- * Check and count the connection attempt.
- *
- * @return True if the connection should be attempted. False indicates that the number of retries has been exhausted
- * and it should NOT be attempted.
- */
- public boolean attempt() {
- if (retryFailed || attempt >= getRetryLimit()) {
- retryFailed = true;
- return false;
- }
- attempt++;
-
- return true;
- }
-
- /**
- * @return The number of retry attempts so far
- */
- public int getAttempts() {
- return attempt;
- }
-
- /**
- * @return True if the retry limit has been exceeded, false otherwise
- */
- public boolean isFailed() {
- return retryFailed;
- }
-
- /**
- * This method both checks the time to live to see if it has been exceeded and accumulates the total time used so
- * far.
- * <p>
- * Each time this method is called it accumulates the total duration since the last time it was called to the total
- * time accumulator. It then checks the total time to the time to live and if greater, it returns false. As long as
- * the total time used is less than or equal to the time to live limit, the method returns true. It is important to
- * call this method at the very beginning of the process so that all parts of the process are tracked.
- * </p>
- *
- * @return True if the total time to live has not been exceeded. False indicates that the total time to live has
- * been exceeded and no further processing should be performed.
- */
- public boolean isAlive() {
- long now = System.currentTimeMillis();
- if (startTime == -1) {
- startTime = now;
- return true;
- }
- accumulatedTime += (now - startTime);
- startTime = now;
- if (accumulatedTime > timeToLive) {
- return false;
- }
- return true;
- }
-
- /**
- * @return The total amount of time used, in milliseconds.
- */
- public long getTotalDuration() {
- return accumulatedTime;
- }
-
- /**
- * This method is called to reset the retry counters. It has no effect on the time to live accumulator.
- */
- public void reset() {
- attempt = 0;
- }
-
- /**
- * Sets the time to live to the value, expressed in seconds
- *
- * @param time
- * The time to live, in seconds
- */
- public void setTimeToLiveSeconds(int time) {
- setTimeToLiveMS(time * 1000L);
- }
-
- /**
- * Sets the time to live to the value, expressed in milliseconds
- *
- * @param time
- * The time to live, in milliseconds
- */
- public void setTimeToLiveMS(long time) {
- this.timeToLive = time;
- }
-
- /**
- * @return The service logic context associated with this request
- */
- public SvcLogicContext getSvcLogicContext() {
- return svcLogicContext;
- }
-
- /**
- * @param svcLogicContext
- * The service logic context to be associated with this request
- */
- public void setSvcLogicContext(SvcLogicContext svcLogicContext) {
- this.svcLogicContext = svcLogicContext;
- }
-}