Update vulnerable package dependencies
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-impl / src / main / java / org / openecomp / sdc / healing / healers / ComponentDataHealer.java
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 package org.openecomp.sdc.healing.healers;
17
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.gson.JsonObject;
20 import com.google.gson.JsonParser;
21 import java.util.Collection;
22 import java.util.Objects;
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.sdc.healing.interfaces.Healer;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
28 import org.openecomp.sdc.versioning.dao.types.Version;
29
30 public class ComponentDataHealer implements Healer {
31
32     private static final String VFC_CODE = "vfcCode"; //earlier present in composition data
33     private static final String NFC_FUNCTION = "nfcFunction";
34     private static final String NFC_NAMING_CODE = "nfcNamingCode";
35     private static final String GENERAL = "general";
36     private final ComponentDao componentDao;
37
38     public ComponentDataHealer() {
39         this.componentDao = ComponentDaoFactory.getInstance().createInterface();
40     }
41
42     @VisibleForTesting
43     ComponentDataHealer(ComponentDao componentDao) {
44         this.componentDao = componentDao;
45     }
46
47     private static void moveAttribute(JsonObject compositionJsonObj, JsonObject questJsonObject, JsonObject general, String compositionAttrName,
48                                       String questAttrName) {
49         if (Objects.nonNull(compositionJsonObj.get(compositionAttrName))) {
50             if (general == null) {
51                 general = new JsonObject();
52             }
53             general.addProperty(questAttrName, compositionJsonObj.get(compositionAttrName).getAsString());
54             questJsonObject.add(GENERAL, general);
55             compositionJsonObj.remove(compositionAttrName);
56         }
57     }
58
59     @Override
60     public boolean isHealingNeeded(String itemId, Version version) {
61         final Collection<ComponentEntity> componentEntities = componentDao.listCompositionAndQuestionnaire(itemId, version);
62         return Objects.nonNull(componentEntities) && !componentEntities.isEmpty() && componentEntities.stream().anyMatch(this::checkNfcParams);
63     }
64
65     private boolean checkNfcParams(ComponentEntity componentEntity) {
66         final String compositionData = componentEntity.getCompositionData();
67         if (!StringUtils.isEmpty(compositionData)) {
68             JsonParser jsonParser = new JsonParser();
69             JsonObject json = (JsonObject) jsonParser.parse(compositionData);
70             return Objects.nonNull(json.get(VFC_CODE)) || Objects.nonNull(json.get(NFC_FUNCTION));
71         }
72         return false;
73     }
74
75     @Override
76     public void heal(String itemId, Version version) throws Exception {
77         final Collection<ComponentEntity> componentEntities = componentDao.listCompositionAndQuestionnaire(itemId, version);
78         if (Objects.nonNull(componentEntities) && !componentEntities.isEmpty()) {
79             componentEntities.forEach(componentEntity -> {
80                 final String compositionData = componentEntity.getCompositionData();
81                 updateComponentData(itemId, version, componentEntity, componentEntity.getQuestionnaireData(), compositionData);
82             });
83         }
84     }
85
86     private void updateComponentData(String itemId, Version version, ComponentEntity componentEntity, String questionnaireData,
87                                      String compositionData) {
88         if (!StringUtils.isEmpty(compositionData)) {
89             JsonParser jsonParser = new JsonParser();
90             JsonObject json = (JsonObject) jsonParser.parse(compositionData);
91             JsonObject questionnaireJson = (JsonObject) jsonParser.parse(questionnaireData);
92             moveAttribute(json, questionnaireJson, questionnaireJson.getAsJsonObject(GENERAL), VFC_CODE, NFC_NAMING_CODE);
93             moveAttribute(json, questionnaireJson, questionnaireJson.getAsJsonObject(GENERAL), NFC_FUNCTION, NFC_FUNCTION);
94             componentEntity.setCompositionData(json.toString());
95             componentDao.update(componentEntity);
96             componentEntity.setQuestionnaireData(questionnaireJson.toString());
97             componentDao.updateQuestionnaireData(itemId, version, componentEntity.getId(), questionnaireJson.toString());
98         }
99     }
100 }