d123924ec3afad6d08507d258835d16d82795d36
[sdc.git] /
1 package org.openecomp.sdcrests.vendorlicense.rest.mapping;
2
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.api.Logger;
7 import org.openecomp.sdc.logging.api.LoggerFactory;
8 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
9 import org.openecomp.sdc.logging.types.LoggerConstants;
10 import org.openecomp.sdc.logging.types.LoggerServiceName;
11 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
12 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
13 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
14 import org.openecomp.sdc.vendorlicense.errors.LimitErrorBuilder;
15 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
16 import org.openecomp.sdcrests.mapping.MappingBase;
17 import org.openecomp.sdcrests.vendorlicense.types.LimitRequestDto;
18
19
20 public class MapLimitRequestDtoToLimitEntity extends MappingBase<LimitRequestDto, LimitEntity> {
21
22     private static final Logger logger =
23             LoggerFactory.getLogger(MapLimitRequestDtoToLimitEntity.class);
24
25     @Override
26     public void doMapping(LimitRequestDto source, LimitEntity target) {
27         target.setName(source.getName());
28         target.setDescription(source.getDescription());
29         try {
30             LimitType type = LimitType.valueOf(source.getType());
31             target.setType(type);
32         } catch (IllegalArgumentException exception) {
33             logger.error(exception.getMessage(), exception);
34             throwInvalidValueError("type", VendorLicenseErrorCodes.LIMIT_INVALID_TYPE);
35         }
36
37         try {
38             AggregationFunction function = source.getAggregationFunction() != null ?
39                     AggregationFunction.valueOf(source.getAggregationFunction()) : null;
40             target.setAggregationFunction(function);
41         } catch (IllegalArgumentException exception) {
42             logger.error(exception.getMessage(), exception);
43             throwInvalidValueError("aggregationFunction",
44                     VendorLicenseErrorCodes.LIMIT_INVALID_AGGREGATIONFUNCTION);
45         }
46
47         target.setTime(source.getTime());
48         target.setMetric(source.getMetric());
49         target.setValue(source.getValue());
50         target.setUnit(source.getUnit());
51
52     }
53
54     private void throwInvalidValueError(String attribute, String vendorLicenseErrorCode) {
55         ErrorCode errorCode = LimitErrorBuilder.getInvalidValueErrorBuilder(attribute,
56                 vendorLicenseErrorCode);
57         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
58                 LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(),
59                 errorCode.id(), errorCode.message());
60         throw new CoreException(errorCode);
61     }
62 }