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