Skip to content

Commit

Permalink
[#1509] Sync Target type with SW and DS types (Mgmt Layer) (#1513)
Browse files Browse the repository at this point in the history
Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm authored Dec 13, 2023
1 parent 71a5319 commit 4b5a7d6
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ public enum SpServerError {
SP_TARGET_TYPE_INCOMPATIBLE("hawkbit.server.error.target.type.incompatible",
"Target type of target is not compatible with distribution set."),

SP_STOP_ROLLOUT_FAILED("hawkbit.server.error.stopRolloutFailed", "Stopping the rollout failed");
SP_TARGET_TYPE_KEY_OR_NAME_REQUIRED("hawkbit.server.error.target.type.keyOrNameRequired",
"Target type key or name is required."),

SP_STOP_ROLLOUT_FAILED("hawkbit.server.error.stopRolloutFailed", "Stopping the rollout failed.");

private final String key;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.NamedEntity;
import org.eclipse.hawkbit.repository.model.TargetType;
import org.eclipse.hawkbit.repository.model.Type;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -42,12 +43,19 @@ public interface TargetTypeCreate {
*/
TargetTypeCreate description(@Size(max = NamedEntity.DESCRIPTION_MAX_SIZE) String description);

/**
* @param key
* for {@link TargetType#getName()}
* @return updated builder instance
*/
TargetTypeCreate key(@Size(min = 1, max = Type.KEY_MAX_SIZE) @NotEmpty String key);

/**
* @param colour
* for {@link TargetType#getColour()}
* @return updated builder instance
*/
TargetTypeCreate colour(@Size(max = TargetType.COLOUR_MAX_SIZE) String colour);
TargetTypeCreate colour(@Size(max = Type.COLOUR_MAX_SIZE) String colour);

/**
* @param compatible
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2023 Bosch.IO GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.exception;

import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.exception.SpServerError;

import java.io.Serial;

/**
* Thrown if tried creation of type with no key nor name.
*/
public class TargetTypeKeyOrNameRequiredException extends AbstractServerRtException {

@Serial
private static final long serialVersionUID = 1L;
private static final SpServerError THIS_ERROR = SpServerError.SP_TARGET_TYPE_KEY_OR_NAME_REQUIRED;

/**
* Default constructor.
*/
public TargetTypeKeyOrNameRequiredException(final String message) {
super(message, THIS_ERROR);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Optional;

import org.eclipse.hawkbit.repository.ValidString;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
Expand All @@ -22,11 +21,7 @@
* @param <T>
* update or create builder interface
*/
public abstract class AbstractDistributionSetTypeUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
@ValidString
protected String colour;
@ValidString
protected String key;
public abstract class AbstractDistributionSetTypeUpdateCreate<T> extends AbstractTypeUpdateCreate<T> {

protected Collection<Long> mandatory;
protected Collection<Long> optional;
Expand All @@ -48,23 +43,4 @@ public Optional<Collection<Long>> getMandatory() {
public Optional<Collection<Long>> getOptional() {
return Optional.ofNullable(optional);
}

public T colour(final String colour) {
this.colour = StringUtils.trimWhitespace(colour);
return (T) this;
}

public Optional<String> getColour() {
return Optional.ofNullable(colour);
}

public T key(final String key) {
this.key = StringUtils.trimWhitespace(key);
return (T) this;
}

public Optional<String> getKey() {
return Optional.ofNullable(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@
*/
package org.eclipse.hawkbit.repository.builder;

import java.util.Optional;

import org.eclipse.hawkbit.repository.ValidString;
import org.springframework.util.StringUtils;

/**
* Create and update builder DTO.
*
* @param <T>
* update or create builder interface
*/
public abstract class AbstractSoftwareModuleTypeUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
@ValidString
protected String colour;
@ValidString
protected String key;
public abstract class AbstractSoftwareModuleTypeUpdateCreate<T> extends AbstractTypeUpdateCreate<T> {

protected int maxAssignments = 1;

Expand All @@ -36,23 +27,4 @@ public T maxAssignments(final int maxAssignments) {
public int getMaxAssignments() {
return maxAssignments;
}

public T colour(final String colour) {
this.colour = StringUtils.trimWhitespace(colour);
return (T) this;
}

public Optional<String> getColour() {
return Optional.ofNullable(colour);
}

public T key(final String key) {
this.key = StringUtils.trimWhitespace(key);
return (T) this;
}

public Optional<String> getKey() {
return Optional.ofNullable(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
* @param <T>
* update or create builder interface
*/
public abstract class AbstractTargetTypeUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {
@ValidString
protected String colour;
public abstract class AbstractTargetTypeUpdateCreate<T> extends AbstractTypeUpdateCreate<T> {

protected Collection<Long> compatible;

Expand All @@ -43,22 +41,4 @@ public T compatible(final Collection<Long> compatible) {
public Optional<Collection<Long>> getCompatible() {
return Optional.ofNullable(compatible);
}

/**
* @param colour
* Colour value
* @return generic type
*/
public T colour(final String colour) {
this.colour = StringUtils.trimWhitespace(colour);
return (T) this;
}

/**
* @return colour
*/
public Optional<String> getColour() {
return Optional.ofNullable(colour);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2023 Bosch.IO GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.builder;

import org.eclipse.hawkbit.repository.ValidString;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
* Create and update builder DTO.
*
* @param <T>
* update or create builder interface
*/
public abstract class AbstractTypeUpdateCreate<T> extends AbstractNamedEntityBuilder<T> {

@ValidString
protected String colour;
@ValidString
protected String key;

public T colour(final String colour) {
this.colour = StringUtils.trimWhitespace(colour);
return (T) this;
}

public Optional<String> getColour() {
return Optional.ofNullable(colour);
}

public T key(final String key) {
this.key = StringUtils.trimWhitespace(key);
return (T) this;
}

public Optional<String> getKey() {
return Optional.ofNullable(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.eclipse.hawkbit.repository.builder.AbstractTargetTypeUpdateCreate;
import org.eclipse.hawkbit.repository.builder.TargetTypeCreate;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.TargetTypeKeyOrNameRequiredException;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetType;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
Expand Down Expand Up @@ -42,10 +43,12 @@ public class JpaTargetTypeCreate extends AbstractTargetTypeUpdateCreate<TargetTy

@Override
public JpaTargetType build() {
final JpaTargetType result = new JpaTargetType(name, description, colour);
if (key == null && name == null) {
throw new TargetTypeKeyOrNameRequiredException("Key or name of the target type shall be specified!");
}

final JpaTargetType result = new JpaTargetType(key == null ? name : key, name == null ? key : name, description, colour);
findDistributionSetTypeWithExceptionIfNotFound(compatible).forEach(result::addCompatibleDistributionSetType);

return result;
}

Expand All @@ -62,5 +65,4 @@ private Collection<DistributionSetType> findDistributionSetTypeWithExceptionIfNo

return type;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public JpaTargetType() {
}

/**
* Constructor
* Constructor, legacy support where <code>key</code> is set to passed <code>name</code>.
*
* @param name
* of the type
Expand All @@ -76,6 +76,7 @@ public JpaTargetType() {
* @param colour
* of the type
*/
@Deprecated
public JpaTargetType(final String name, final String description, final String colour) {
this(name, name, description, colour);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class ResponseExceptionHandler {
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_NO_WEIGHT_PROVIDED_IN_MULTIASSIGNMENT_MODE, HttpStatus.BAD_REQUEST);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_TARGET_TYPE_IN_USE, HttpStatus.CONFLICT);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_TARGET_TYPE_INCOMPATIBLE, HttpStatus.BAD_REQUEST);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_TARGET_TYPE_KEY_OR_NAME_REQUIRED, HttpStatus.BAD_REQUEST);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_DS_INVALID, HttpStatus.BAD_REQUEST);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_DS_INCOMPLETE, HttpStatus.BAD_REQUEST);
ERROR_TO_HTTP_STATUS.put(SpServerError.SP_STOP_ROLLOUT_FAILED, HttpStatus.LOCKED);
Expand Down

0 comments on commit 4b5a7d6

Please sign in to comment.