[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-license-rest / vendor-license-rest-services / src / main / java / org / openecomp / sdcrests / vendorlicense / rest / mapping / MapLimitRequestDtoToLimitEntity.java
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.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;
20
21
22 public class MapLimitRequestDtoToLimitEntity extends MappingBase<LimitRequestDto, LimitEntity> {
23   @Override
24   public void doMapping(LimitRequestDto source, LimitEntity target) {
25     target.setName(source.getName());
26     target.setDescription(source.getDescription());
27     try {
28       LimitType type = LimitType.valueOf(source.getType());
29       target.setType(type);
30     }
31     catch (IllegalArgumentException exception) {
32       throwInvalidValueError("type", VendorLicenseErrorCodes.LIMIT_INVALID_TYPE);
33     }
34
35     try {
36       EntitlementMetric metric = EntitlementMetric.valueOf(source.getMetric());
37       target.setMetric(metric);
38     }
39     catch (IllegalArgumentException exception) {
40       throwInvalidValueError("metric", VendorLicenseErrorCodes.LIMIT_INVALID_METRIC);
41     }
42
43     try {
44       AggregationFunction function = source.getAggregationFunction() != null ?
45               AggregationFunction.valueOf(source.getAggregationFunction()) : null;
46       target.setAggregationFunction(function);
47     }
48     catch (IllegalArgumentException exception) {
49       throwInvalidValueError("aggregationFunction",
50           VendorLicenseErrorCodes.LIMIT_INVALID_AGGREGATIONFUNCTION);
51     }
52
53     try {
54       EntitlementTime time = source.getTime() != null ?
55               EntitlementTime.valueOf(source.getTime()) : null;
56       target.setTime(time);
57     }
58     catch (IllegalArgumentException exception) {
59       throwInvalidValueError("time", VendorLicenseErrorCodes.LIMIT_INVALID_TIME);
60     }
61
62     target.setValue(source.getValue());
63     target.setUnit(source.getUnit());
64
65   }
66
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);
74   }
75 }