1 package org.openecomp.sdcrests.vendorlicense.rest.mapping;
3 import org.openecomp.sdc.common.errors.CoreException;
4 import org.openecomp.sdc.common.errors.ErrorCode;
5 import org.openecomp.sdc.datatypes.error.ErrorLevel;
6 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
7 import org.openecomp.sdc.logging.types.LoggerConstants;
8 import org.openecomp.sdc.logging.types.LoggerServiceName;
9 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
10 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
11 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementMetric;
12 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementTime;
13 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
14 import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder;
15 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
16 import org.openecomp.sdc.vendorsoftwareproduct.errors.ImageErrorBuilder;
17 import org.openecomp.sdcrests.mapping.MappingBase;
18 import org.openecomp.sdcrests.vendorlicense.types.LimitRequestDto;
19 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
22 public class MapLimitRequestDtoToLimitEntity extends MappingBase<LimitRequestDto, LimitEntity> {
24 public void doMapping(LimitRequestDto source, LimitEntity target) {
25 target.setName(source.getName());
26 target.setDescription(source.getDescription());
28 LimitType type = LimitType.valueOf(source.getType());
31 catch (IllegalArgumentException exception) {
32 throwInvalidValueError("type", VendorLicenseErrorCodes.LIMIT_INVALID_TYPE);
36 EntitlementMetric metric = EntitlementMetric.valueOf(source.getMetric());
37 target.setMetric(metric);
39 catch (IllegalArgumentException exception) {
40 throwInvalidValueError("metric", VendorLicenseErrorCodes.LIMIT_INVALID_METRIC);
44 AggregationFunction function = source.getAggregationFunction() != null ?
45 AggregationFunction.valueOf(source.getAggregationFunction()) : null;
46 target.setAggregationFunction(function);
48 catch (IllegalArgumentException exception) {
49 throwInvalidValueError("aggregationFunction",
50 VendorLicenseErrorCodes.LIMIT_INVALID_AGGREGATIONFUNCTION);
54 EntitlementTime time = source.getTime() != null ?
55 EntitlementTime.valueOf(source.getTime()) : null;
58 catch (IllegalArgumentException exception) {
59 throwInvalidValueError("time", VendorLicenseErrorCodes.LIMIT_INVALID_TIME);
62 target.setValue(source.getValue());
63 target.setUnit(source.getUnit());
67 private void throwInvalidValueError(String attribute, String vendorLicenseErrorCode) {
68 ErrorCode errorCode = LimitErrorBuilder.getInvalidValueErrorBuilder(attribute,
69 vendorLicenseErrorCode);
70 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
71 LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(),
72 errorCode.id(), errorCode.message() );
73 throw new CoreException(errorCode);