2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.asdctool.impl.migration.v1707;
23 import java.util.EnumMap;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.stream.Collectors;
30 import org.apache.commons.collections.MapUtils;
31 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
32 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
33 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
34 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
35 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
36 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
37 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
38 import org.openecomp.sdc.be.model.ComponentParametersView;
39 import org.openecomp.sdc.be.model.DistributionStatusEnum;
40 import org.openecomp.sdc.be.model.LifecycleStateEnum;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
43 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
44 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.stereotype.Component;
50 import fj.data.Either;
52 @Component("distributionStatusUpdate")
53 public class DistributionStatusUpdate {
54 private static Logger LOGGER = LoggerFactory.getLogger(DistributionStatusUpdate.class);
57 private ToscaOperationFacade toscaOperationFacade;
59 private TitanDao titanDao;
62 public boolean migrate() {
63 boolean result = true;
64 Either<Map<GraphVertex, org.openecomp.sdc.be.model.Service>, StorageOperationStatus> getAllServiceComponentsRes = getAllServiceComponents();
65 if(getAllServiceComponentsRes.isRight()){
68 if(result && MapUtils.isNotEmpty(getAllServiceComponentsRes.left().value())){
69 updateDistributionStatusFromMetadata(getAllServiceComponentsRes.left().value());
70 updateDistributionStatusToNotDistributed(getAllServiceComponentsRes.left().value());
73 toscaOperationFacade.commit();
79 private void updateDistributionStatusToNotDistributed(Map<GraphVertex, org.openecomp.sdc.be.model.Service> components) {
81 Map<GraphVertex, org.openecomp.sdc.be.model.Service> filteredComponents = components.entrySet()
83 .filter(e -> e.getValue().getLifecycleState() != LifecycleStateEnum.CERTIFIED)
84 .collect(Collectors.toMap(e -> e.getKey(), e -> (Service)e.getValue()));
87 Either<GraphVertex, TitanOperationStatus> updateResponse;
88 GraphVertex metadataV;
90 for(Entry<GraphVertex, Service> currComponent : filteredComponents.entrySet()){
91 metadataV = currComponent.getKey();
92 service = currComponent.getValue();
94 metadataV.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
95 updateResponse = titanDao.updateVertex(metadataV);
97 if (updateResponse.isRight()) {
98 LOGGER.debug("failed to updateDistributionStatusToNotDistributed service {} error {}", service.getUniqueId(), updateResponse.right().value());
101 } catch (Exception e) {
102 LOGGER.debug("failed to updateDistributionStatusToNotDistributed service {} error {}", service.getUniqueId(), e.toString());
107 private void updateDistributionStatusFromMetadata(Map<GraphVertex, org.openecomp.sdc.be.model.Service> components) {
109 String statusFromMetadata;
110 Either<GraphVertex, TitanOperationStatus> updateResponse;
111 GraphVertex metadataV;
113 for(Entry<GraphVertex, Service> currComponent : components.entrySet()){
114 metadataV = currComponent.getKey();
115 service = currComponent.getValue();
117 statusFromMetadata = (String) metadataV.getJsonMetadataField(JsonPresentationFields.DISTRIBUTION_STATUS);
118 metadataV.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, statusFromMetadata);
119 updateResponse = titanDao.updateVertex(metadataV);
121 if (updateResponse.isRight()) {
122 LOGGER.debug("failed to updateDistributionStatusFromMetadata service {} error {}", service.getUniqueId(), updateResponse.right().value());
125 } catch (Exception e) {
126 LOGGER.debug("failed to read distribution status of service {} error {}", service.getUniqueId(), e.toString());
133 public Either<Map<GraphVertex, org.openecomp.sdc.be.model.Service>, StorageOperationStatus> getAllServiceComponents() {
135 Map<GraphVertex, org.openecomp.sdc.be.model.Service> components = new HashMap<>();
136 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
137 Map<GraphPropertyEnum, Object> propertiesNotMatch = new EnumMap<>(GraphPropertyEnum.class);
138 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
139 propertiesNotMatch.put(GraphPropertyEnum.IS_DELETED, true);
140 Either<List<GraphVertex>, TitanOperationStatus> getVerticiesRes = toscaOperationFacade.getTitanDao().getByCriteria(null, propertiesToMatch, propertiesNotMatch, JsonParseFlagEnum.ParseAll);
142 if (getVerticiesRes.isRight() && getVerticiesRes.right().value() != TitanOperationStatus.NOT_FOUND) {
143 LOGGER.debug("Failed to fetch all service components. Status is {}", getVerticiesRes.right().value());
144 return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVerticiesRes.right().value()));
146 if(getVerticiesRes.isLeft()){
147 List<GraphVertex> componentVerticies = getVerticiesRes.left().value();
148 for (GraphVertex componentV : componentVerticies) {
149 ComponentParametersView filters = new ComponentParametersView(true);
150 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getComponentsRes = toscaOperationFacade.getToscaElement(componentV.getUniqueId(), filters);
151 if (getComponentsRes.isRight()) {
152 return Either.right(getComponentsRes.right().value());
154 components.put(componentV, (Service) getComponentsRes.left().value());
157 return Either.left(components);