2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.healing.healers;
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;
29 import java.util.Collection;
30 import java.util.Objects;
32 public class ComponentDataHealer implements Healer {
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;
40 public ComponentDataHealer() {
41 this.componentDao = ComponentDaoFactory.getInstance().createInterface();
45 ComponentDataHealer(ComponentDao componentDao) {
46 this.componentDao = componentDao;
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);
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));
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);
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,
87 moveAttribute(json, questionnaireJson, questionnaireJson.getAsJsonObject(GENERAL), 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());
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();
102 general.addProperty(questAttrName, compositionJsonObj.get(compositionAttrName).getAsString());
103 questJsonObject.add(GENERAL, general);
104 compositionJsonObj.remove(compositionAttrName);