da3ad81d5839317ac8251eac79c72dedc4e954d5
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.healing.healers;
18
19 import com.google.gson.JsonObject;
20 import com.google.gson.JsonParser;
21 import org.apache.commons.lang.StringUtils;
22 import org.openecomp.sdc.healing.interfaces.Healer;
23 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
24 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
26 import org.openecomp.sdc.versioning.dao.types.Version;
27
28 import java.util.Collection;
29 import java.util.Objects;
30
31 public class ComponentDataHealer implements Healer {
32
33     private static final String VFC_CODE = "vfcCode"; //earlier present in composition data
34     private static final String NFC_FUNCTION = "nfcFunction";
35     private  static final String NFC_NAMING_CODE = "nfcNamingCode";
36     private static final String GENERAL = "general";
37     private final ComponentDao componentDao;
38
39     public ComponentDataHealer() {
40         this.componentDao = ComponentDaoFactory.getInstance().createInterface();
41     }
42
43     @Override
44     public boolean isHealingNeeded(String itemId, Version version) {
45         final Collection<ComponentEntity> componentEntities =
46                 componentDao.listCompositionAndQuestionnaire(itemId, version);
47         return Objects.nonNull(componentEntities) && !componentEntities.isEmpty() &&
48                        componentEntities.stream().anyMatch(this::checkNfcParams);
49     }
50
51     private boolean checkNfcParams(ComponentEntity componentEntity) {
52         final String compositionData = componentEntity.getCompositionData();
53         if (!StringUtils.isEmpty(compositionData)) {
54             JsonParser jsonParser = new JsonParser();
55             JsonObject json = (JsonObject) jsonParser.parse(compositionData);
56             return Objects.nonNull(json.get(VFC_CODE)) || Objects.nonNull(json.get(NFC_FUNCTION));
57         }
58         return false;
59     }
60
61     @Override
62     public void heal(String itemId, Version version) throws Exception {
63         final Collection<ComponentEntity> componentEntities =
64                 componentDao.listCompositionAndQuestionnaire(itemId, version);
65         if (Objects.nonNull(componentEntities) && !componentEntities.isEmpty()) {
66             componentEntities.forEach(componentEntity -> {
67                 final String compositionData = componentEntity.getCompositionData();
68                 updateComponentData(itemId, version, componentEntity, componentEntity.getQuestionnaireData(), compositionData);
69             });
70         }
71     }
72
73     private void updateComponentData(String itemId, Version version, ComponentEntity componentEntity,
74                                             String questionnaireData, String compositionData) {
75         if (!StringUtils.isEmpty(compositionData)) {
76             JsonParser jsonParser = new JsonParser();
77             JsonObject json = (JsonObject) jsonParser.parse(compositionData);
78             JsonObject questionnaireJson = (JsonObject) jsonParser.parse(questionnaireData);
79             moveAttribute(json, questionnaireJson, questionnaireJson.getAsJsonObject(GENERAL), VFC_CODE,
80                     NFC_NAMING_CODE);
81             moveAttribute(json, questionnaireJson, questionnaireJson.getAsJsonObject(GENERAL), NFC_FUNCTION,
82                     NFC_FUNCTION);
83             componentEntity.setCompositionData(json.toString());
84             componentDao.update(componentEntity);
85             componentEntity.setQuestionnaireData(questionnaireJson.toString());
86             componentDao.updateQuestionnaireData(itemId,version,componentEntity.getId(), questionnaireJson.toString());
87         }
88     }
89
90     private static void moveAttribute(JsonObject compositionJsonObj, JsonObject questJsonObject,
91                                       JsonObject general, String compositionAttrName, String questAttrName ) {
92         if (Objects.nonNull(compositionJsonObj.get(compositionAttrName))) {
93             if (general == null) {
94                 general = new JsonObject();
95             }
96             general.addProperty(questAttrName, compositionJsonObj.get(compositionAttrName).getAsString());
97             questJsonObject.add(GENERAL, general);
98             compositionJsonObj.remove(compositionAttrName);
99         }
100     }
101 }