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