744965ff68b3a5d051be23dd90e95e1e4f168d84
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.impl.migration.v1707;
22
23 import java.util.EnumMap;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.stream.Collectors;
29
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;
49
50 import fj.data.Either;
51
52 @Component("distributionStatusUpdate")
53 public class DistributionStatusUpdate {
54         private static Logger LOGGER = LoggerFactory.getLogger(DistributionStatusUpdate.class);
55
56         @Autowired
57     private ToscaOperationFacade toscaOperationFacade;
58         @Autowired
59     private TitanDao titanDao;
60         
61     
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()){
66                         result = false;
67                 }
68                 if(result && MapUtils.isNotEmpty(getAllServiceComponentsRes.left().value())){
69                         updateDistributionStatusFromMetadata(getAllServiceComponentsRes.left().value());
70                         updateDistributionStatusToNotDistributed(getAllServiceComponentsRes.left().value());
71                 }
72                 
73                 toscaOperationFacade.commit();
74                 
75                 return result;
76         }
77
78         
79         private void updateDistributionStatusToNotDistributed(Map<GraphVertex, org.openecomp.sdc.be.model.Service> components) {
80                 
81                 Map<GraphVertex, org.openecomp.sdc.be.model.Service> filteredComponents = components.entrySet()
82                                 .stream()
83                                 .filter(e -> e.getValue().getLifecycleState() != LifecycleStateEnum.CERTIFIED)
84                                 .collect(Collectors.toMap(e -> e.getKey(), e -> (Service)e.getValue()));
85                 
86                 Service service;
87                 Either<GraphVertex, TitanOperationStatus> updateResponse;
88                 GraphVertex metadataV;
89                 
90                 for(Entry<GraphVertex, Service> currComponent : filteredComponents.entrySet()){
91                         metadataV = currComponent.getKey();
92                         service = currComponent.getValue();
93                         try {
94                                 metadataV.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED.name());
95                                 updateResponse = titanDao.updateVertex(metadataV);
96                                 
97                                 if (updateResponse.isRight()) {
98                                         LOGGER.debug("failed to updateDistributionStatusToNotDistributed service  {} error {}", service.getUniqueId(), updateResponse.right().value());
99                                 }
100                                 
101                         } catch (Exception e) {
102                                 LOGGER.debug("failed to updateDistributionStatusToNotDistributed service  {} error {}", service.getUniqueId(), e.toString());
103                         }
104                 }
105         }
106         
107         private void updateDistributionStatusFromMetadata(Map<GraphVertex, org.openecomp.sdc.be.model.Service> components) {
108                 Service service;
109                 String statusFromMetadata;
110                 Either<GraphVertex, TitanOperationStatus> updateResponse;
111                 GraphVertex metadataV;
112                 
113                 for(Entry<GraphVertex, Service> currComponent : components.entrySet()){
114                         metadataV = currComponent.getKey();
115                         service = currComponent.getValue();
116                         try {
117                                 statusFromMetadata = (String) metadataV.getJsonMetadataField(JsonPresentationFields.DISTRIBUTION_STATUS);
118                                 metadataV.addMetadataProperty(GraphPropertyEnum.DISTRIBUTION_STATUS, statusFromMetadata);
119                                 updateResponse = titanDao.updateVertex(metadataV);
120                                 
121                                 if (updateResponse.isRight()) {
122                                         LOGGER.debug("failed to updateDistributionStatusFromMetadata service  {} error {}", service.getUniqueId(), updateResponse.right().value());
123                                 }
124                                 
125                         } catch (Exception e) {
126                                 LOGGER.debug("failed to read distribution status of service {} error {}", service.getUniqueId(), e.toString());
127                         }
128                         
129                 }
130         }
131         
132         
133         public Either<Map<GraphVertex, org.openecomp.sdc.be.model.Service>, StorageOperationStatus> getAllServiceComponents() {
134
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);
141
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()));
145                 }
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());
153                                 }
154                                 components.put(componentV, (Service) getComponentsRes.left().value());
155                         }
156                 }
157                 return Either.left(components);
158         }
159         
160 }