diff options
author | sheetalm <sheetal.mudholkar@amdocs.com> | 2018-06-12 17:32:56 +0530 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-06-13 08:16:41 +0000 |
commit | 67e400cc929314f1d66accb2f2f47d489f6b0c4f (patch) | |
tree | 1f92e95dd1165944ec57de9e7318850a587cb1c4 /openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java | |
parent | d932a21e9f99ef5e706975a73c4f17a145445fe1 (diff) |
Fix for nfcparameters in component questionnaire
issue - nfc naming code and nfc function fields' values are wiped out
with a VSP update
Moved the above fields from composition to questionnaire
Add BDD test. Add license to java files
Change-Id: I2b746fedc17c19b716df35bf0dad2c212f15df30
Issue-ID: SDC-1419
Signed-off-by: sheetalm <sheetal.mudholkar@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java')
-rw-r--r-- | openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentDataHealer.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentDataHealer.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentDataHealer.java new file mode 100644 index 0000000000..da3ad81d58 --- /dev/null +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ComponentDataHealer.java @@ -0,0 +1,101 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.healing.healers; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.commons.lang.StringUtils; +import org.openecomp.sdc.healing.interfaces.Healer; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.util.Collection; +import java.util.Objects; + +public class ComponentDataHealer implements Healer { + + private static final String VFC_CODE = "vfcCode"; //earlier present in composition data + private static final String NFC_FUNCTION = "nfcFunction"; + private static final String NFC_NAMING_CODE = "nfcNamingCode"; + private static final String GENERAL = "general"; + private final ComponentDao componentDao; + + public ComponentDataHealer() { + this.componentDao = ComponentDaoFactory.getInstance().createInterface(); + } + + @Override + public boolean isHealingNeeded(String itemId, Version version) { + final Collection<ComponentEntity> componentEntities = + componentDao.listCompositionAndQuestionnaire(itemId, version); + return Objects.nonNull(componentEntities) && !componentEntities.isEmpty() && + componentEntities.stream().anyMatch(this::checkNfcParams); + } + + private boolean checkNfcParams(ComponentEntity componentEntity) { + final String compositionData = componentEntity.getCompositionData(); + if (!StringUtils.isEmpty(compositionData)) { + JsonParser jsonParser = new JsonParser(); + JsonObject json = (JsonObject) jsonParser.parse(compositionData); + return Objects.nonNull(json.get(VFC_CODE)) || Objects.nonNull(json.get(NFC_FUNCTION)); + } + return false; + } + + @Override + public void heal(String itemId, Version version) throws Exception { + final Collection<ComponentEntity> componentEntities = + componentDao.listCompositionAndQuestionnaire(itemId, version); + if (Objects.nonNull(componentEntities) && !componentEntities.isEmpty()) { + componentEntities.forEach(componentEntity -> { + final String compositionData = componentEntity.getCompositionData(); + updateComponentData(itemId, version, componentEntity, componentEntity.getQuestionnaireData(), compositionData); + }); + } + } + + private void updateComponentData(String itemId, Version version, ComponentEntity componentEntity, + String questionnaireData, String compositionData) { + if (!StringUtils.isEmpty(compositionData)) { + JsonParser jsonParser = new JsonParser(); + JsonObject json = (JsonObject) jsonParser.parse(compositionData); + JsonObject questionnaireJson = (JsonObject) jsonParser.parse(questionnaireData); + moveAttribute(json, questionnaireJson, questionnaireJson.getAsJsonObject(GENERAL), VFC_CODE, + NFC_NAMING_CODE); + moveAttribute(json, questionnaireJson, questionnaireJson.getAsJsonObject(GENERAL), NFC_FUNCTION, + NFC_FUNCTION); + componentEntity.setCompositionData(json.toString()); + componentDao.update(componentEntity); + componentEntity.setQuestionnaireData(questionnaireJson.toString()); + componentDao.updateQuestionnaireData(itemId,version,componentEntity.getId(), questionnaireJson.toString()); + } + } + + private static void moveAttribute(JsonObject compositionJsonObj, JsonObject questJsonObject, + JsonObject general, String compositionAttrName, String questAttrName ) { + if (Objects.nonNull(compositionJsonObj.get(compositionAttrName))) { + if (general == null) { + general = new JsonObject(); + } + general.addProperty(questAttrName, compositionJsonObj.get(compositionAttrName).getAsString()); + questJsonObject.add(GENERAL, general); + compositionJsonObj.remove(compositionAttrName); + } + } +} |