Merge "Merge 2 'query' end points in NCMP"
[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      * @param cmHandleQueryParameters the original input parameter
44      */
45     @Deprecated //this method wil be removed in Release 12 (No Name know yet)
46     public CmHandleQueryApiParameters mapOldConditionProperties(
47                                            final CmHandleQueryParameters cmHandleQueryParameters) {
48         final CmHandleQueryApiParameters cmHandleQueryApiParameters =
49                 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
50         if (cmHandleQueryParameters.getConditions() != null
51                 && cmHandleQueryApiParameters.getCmHandleQueryParameters().isEmpty()) {
52             cmHandleQueryApiParameters.setCmHandleQueryParameters(new ArrayList<>());
53             cmHandleQueryParameters.getConditions().parallelStream().forEach(
54                 oldConditionProperty -> {
55                     if (oldConditionProperty.getConditionParameters() != null
56                             && oldConditionProperty.getName() != null) {
57                         final ConditionApiProperties conditionApiProperties = new ConditionApiProperties();
58                         conditionApiProperties.setConditionName(oldConditionProperty.getName());
59                         conditionApiProperties.setConditionParameters(new ArrayList<>());
60                         oldConditionProperty.getConditionParameters().parallelStream().forEach(
61                             oldConditionParameter ->
62                                 conditionApiProperties.getConditionParameters().add(Collections
63                                     .singletonMap("moduleName", oldConditionParameter.getModuleName()))
64                         );
65                         cmHandleQueryApiParameters.getCmHandleQueryParameters().add(conditionApiProperties);
66                     }
67                 }
68             );
69         }
70
71         return cmHandleQueryApiParameters;
72     }
73 }