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