Introduce action status scoped custom code (#1277)

* Allow providing a custom code with an action status feedback to give more fine grained device specific details.
* Add ddi rest docs for new optional status code value.
* Provide new code value via mgmt api. Fix review findings.
* Fix failing tests

Signed-off-by: Michael Herdt <Michael.Herdt@bosch.io>
Co-authored-by: Stefan Behl <stefan.behl@bosch.io>
This commit is contained in:
Michael Herdt
2022-09-21 15:20:34 +02:00
committed by GitHub
parent 32718676a4
commit 5e963f8308
28 changed files with 338 additions and 43 deletions

View File

@@ -8,11 +8,13 @@
*/
package org.eclipse.hawkbit.rest.util;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.RolloutGroup;
@@ -183,6 +185,101 @@ public abstract class JsonBuilder {
return builder.toString();
}
/**
* builds a json string for the feedback for the execution "proceeding".
*
* @param id
* of the Action feedback refers to
* @return the built string
* @throws JSONException
*/
public static String deploymentActionInProgressFeedback(final String id) throws JSONException {
return deploymentActionFeedback(id, "proceeding");
}
/**
* builds a certain json string for a action feedback.
*
* @param id
* of the action the feedback refers to
* @param execution
* see ExecutionStatus
* @return the build json string
* @throws JSONException
*/
public static String deploymentActionFeedback(final String id, final String execution) throws JSONException {
return deploymentActionFeedback(id, execution, "none", null,
Arrays.asList(RandomStringUtils.randomAlphanumeric(1000)));
}
public static String deploymentActionFeedback(final String id, final String execution, final String message)
throws JSONException {
return deploymentActionFeedback(id, execution, "none", null, Arrays.asList(message));
}
public static String deploymentActionFeedback(final String id, final String execution, final String finished,
final String message) throws JSONException {
return deploymentActionFeedback(id, execution, finished, null, message);
}
public static String deploymentActionFeedback(final String id, final String execution, final String finished,
final Integer code, final String message) throws JSONException {
return deploymentActionFeedback(id, execution, finished, code, Arrays.asList(message));
}
public static String deploymentActionFeedback(final String id, final String execution, final String finished,
final Integer code, final Collection<String> messages) throws JSONException {
final JSONObject statusJson = new JSONObject().put("execution", execution).put("result",
new JSONObject().put("finished", finished).put("progress", new JSONObject().put("cnt", 2).put("of", 5)))
.put("details", new JSONArray(messages));
if (code != null) {
statusJson.put("code", code);
}
return new JSONObject().put("id", id).put("status", statusJson).toString();
}
/**
* Build an invalid request body with missing result for feedback message.
*
* @param id
* id of the action
* @param execution
* the execution
* @param message
* the message
* @return a invalid request body
* @throws JSONException
*/
public static String missingResultInFeedback(final String id, final String execution, final String message)
throws JSONException {
return new JSONObject().put("id", id).put("time", "20140511T121314")
.put("status",
new JSONObject().put("execution", execution).put("details", new JSONArray().put(message)))
.toString();
}
/**
* Build an invalid request body with missing finished result for feedback
* message.
*
* @param id
* id of the action
* @param execution
* the execution
* @param message
* the message
* @return a invalid request body
* @throws JSONException
*/
public static String missingFinishedResultInFeedback(final String id, final String execution, final String message)
throws JSONException {
return new JSONObject().put("id", id).put("time", "20140511T121314")
.put("status", new JSONObject().put("execution", execution).put("result", new JSONObject())
.put("details", new JSONArray().put(message)))
.toString();
}
public static String distributionSetTypes(final List<DistributionSetType> types) throws JSONException {
final JSONArray result = new JSONArray();
@@ -545,15 +642,27 @@ public abstract class JsonBuilder {
return json.toString();
}
public static String cancelActionFeedback(final String id, final String execution) throws JSONException {
return cancelActionFeedback(id, execution, null, RandomStringUtils.randomAlphanumeric(1000));
}
public static String cancelActionFeedback(final String id, final String execution, final String message)
throws JSONException {
return new JSONObject().put("id", id)
.put("status",
new JSONObject().put("execution", execution)
.put("result", new JSONObject().put("finished", "success"))
.put("details", new JSONArray().put(message)))
.toString();
return cancelActionFeedback(id, execution, null, message);
}
public static String cancelActionFeedback(final String id, final String execution, final Integer code,
final String message) throws JSONException {
final JSONObject statusJson = new JSONObject().put("execution", execution)
.put("result", new JSONObject().put("finished", "success"))
.put("details", new JSONArray().put(message));
if (code != null) {
statusJson.put("code", code);
}
return new JSONObject().put("id", id).put("status", statusJson).toString();
}
public static JSONObject configData(final Map<String, String> attributes) throws JSONException {