Merge "Fix release notes"
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / util / DeprecationHelper.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.util;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import lombok.RequiredArgsConstructor;
26 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
27 import org.onap.cps.ncmp.api.models.ConditionApiProperties;
28 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
29 import org.onap.cps.utils.JsonObjectMapper;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 @RequiredArgsConstructor
34 public class DeprecationHelper {
35
36     private final JsonObjectMapper jsonObjectMapper;
37
38     /**
39      * Convert the old condition properties to the new schema.
40      * !!! remove it after the old condition removed !!!
41      * it only works for module names
42      *
43      * @deprecated this method will be removed in Release 12 (No Name know yet)
44      *
45      * @param cmHandleQueryParameters the original input parameter
46      */
47     @Deprecated
48     public CmHandleQueryApiParameters mapOldConditionProperties(
49                                            final CmHandleQueryParameters cmHandleQueryParameters) {
50         final CmHandleQueryApiParameters cmHandleQueryApiParameters =
51                 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
52         if (cmHandleQueryParameters.getConditions() != null
53                 && cmHandleQueryApiParameters.getCmHandleQueryParameters().isEmpty()) {
54             cmHandleQueryApiParameters.setCmHandleQueryParameters(new ArrayList<>());
55             cmHandleQueryParameters.getConditions().parallelStream().forEach(
56                 oldConditionProperty -> {
57                     if (oldConditionProperty.getConditionParameters() != null
58                             && oldConditionProperty.getName() != null) {
59                         final ConditionApiProperties conditionApiProperties = new ConditionApiProperties();
60                         conditionApiProperties.setConditionName(oldConditionProperty.getName());
61                         conditionApiProperties.setConditionParameters(new ArrayList<>());
62                         oldConditionProperty.getConditionParameters().parallelStream().forEach(
63                             oldConditionParameter ->
64                                 conditionApiProperties.getConditionParameters().add(Collections
65                                     .singletonMap("moduleName", oldConditionParameter.getModuleName()))
66                         );
67                         cmHandleQueryApiParameters.getCmHandleQueryParameters().add(conditionApiProperties);
68                     }
69                 }
70             );
71         }
72
73         return cmHandleQueryApiParameters;
74     }
75 }