2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.be.model.jsonjanusgraph.operations;
23 import org.janusgraph.core.JanusGraphVertex;
24 import fj.data.Either;
25 import org.apache.tinkerpop.gremlin.structure.Direction;
26 import org.apache.tinkerpop.gremlin.structure.Edge;
27 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
29 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
30 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
31 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
32 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
34 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
35 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
36 import org.openecomp.sdc.be.model.ComponentDependency;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.springframework.stereotype.Component;
44 import java.util.function.Function;
45 import java.util.stream.Collectors;
48 public class UpgradeOperation extends BaseOperation {
49 private static final Logger log = Logger.getLogger(UpgradeOperation.class.getName());
51 public Either<List<ComponentDependency>, StorageOperationStatus> getComponentDependencies(String componentId) {
52 Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
53 if (vertexById.isRight()) {
54 log.debug("Failed to fetch vertex with id {} error {}", componentId, vertexById.right().value());
55 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexById.right().value()));
57 List<ComponentDependency> dependencies = new ArrayList<>();
59 GraphVertex vertex = vertexById.left().value();
61 StorageOperationStatus status = fillDependenciesByVertex(componentId, dependencies, vertex);
62 if (status != StorageOperationStatus.OK) {
63 return Either.right(status);
66 GraphVertex vertexToStart = vertex;
67 Function<GraphVertex, Either<GraphVertex, JanusGraphOperationStatus>> getNextElement = vertexP -> janusGraphDao
68 .getParentVertex(vertexP, EdgeLabelEnum.VERSION, JsonParseFlagEnum.ParseAll);
69 status = handleVersionChain(componentId, dependencies, vertex, getNextElement);
70 if (status != StorageOperationStatus.OK) {
71 return Either.right(status);
73 vertex = vertexToStart;
74 getNextElement = vertexP -> janusGraphDao
75 .getChildVertex(vertexP, EdgeLabelEnum.VERSION, JsonParseFlagEnum.ParseAll);
76 status = handleVersionChain(componentId, dependencies, vertex, getNextElement);
78 return status == StorageOperationStatus.OK ? Either.left(dependencies) : Either.right(status);
81 private StorageOperationStatus handleVersionChain(String componentId, List<ComponentDependency> dependencies, GraphVertex vertexToStart, Function<GraphVertex, Either<GraphVertex, JanusGraphOperationStatus>> getNextElement) {
83 StorageOperationStatus status;
84 boolean nextInChain = true;
85 GraphVertex vertex = vertexToStart;
86 Either<GraphVertex, JanusGraphOperationStatus> nextInChainV;
88 nextInChainV = getNextElement.apply(vertex);
89 if (nextInChainV.isRight()) {
92 vertex = nextInChainV.left().value();
93 status = fillDependenciesByVertex(componentId, dependencies, vertex);
94 if (status != StorageOperationStatus.OK) {
99 return StorageOperationStatus.OK;
102 private StorageOperationStatus fillDependenciesByVertex(String componentId, List<ComponentDependency> dependencies, GraphVertex vertex) {
103 StorageOperationStatus status = StorageOperationStatus.OK;
104 if ( needToAddToDependency(vertex) ) {
105 ComponentDependency dependency = fillDataFromVertex(vertex, null, null);
107 List<EdgeLabelEnum> dependList = Arrays.asList(EdgeLabelEnum.INSTANCE_OF, EdgeLabelEnum.PROXY_OF, EdgeLabelEnum.ALLOTTED_OF);
108 for (EdgeLabelEnum label : dependList) {
109 status = fillDependenciesByLabel(componentId, vertex, dependency, label);
110 if (status != StorageOperationStatus.OK) {
111 log.debug("Failed to create dependencies for component {} and label {} status {}", componentId, label, status);
115 if (status == StorageOperationStatus.OK) {
116 dependencies.add(dependency);
122 private boolean needToAddToDependency(GraphVertex vertex){
123 Boolean isDeleted = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
124 Boolean isArchived = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_ARCHIVED);
125 return !Boolean.TRUE.equals(isDeleted) && !Boolean.TRUE.equals(isArchived);
128 private StorageOperationStatus fillDependenciesByLabel(String componentId, GraphVertex vertex, ComponentDependency dependency, EdgeLabelEnum label) {
129 Either<List<GraphVertex>, JanusGraphOperationStatus> parentVertices = janusGraphDao.getParentVertices(vertex, label, JsonParseFlagEnum.ParseAll);
130 if (parentVertices.isRight() && parentVertices.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
131 log.debug("Failed to fetch parent verticies by label INSTANCE_OF for vertex with id {} error {}", componentId, parentVertices.right().value());
132 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertices.right().value());
134 if (parentVertices.isLeft()) {
135 List<ComponentDependency> existIn = new ArrayList<>( );
136 parentVertices.left().value().forEach(v -> handleHighestVersion(vertex, label, existIn, v) );
137 dependency.addDependencies(existIn);
139 return StorageOperationStatus.OK;
142 private void handleHighestVersion(GraphVertex vertexOrigin, EdgeLabelEnum label, List<ComponentDependency> exisIn, GraphVertex containerVertex) {
143 Boolean isHighest = (Boolean) containerVertex.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
144 if ( isHighest && needToAddToDependency(containerVertex) ) {
145 JanusGraphVertex janusGraphVertex = containerVertex.getVertex();
146 Iterator<Edge> edges = janusGraphVertex.edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
147 //verify that it is a last version - highest by version number
148 if ( edges == null || !edges.hasNext() ){
149 ComponentDependency container = fillDataFromVertex(containerVertex, vertexOrigin.getUniqueId(), label);
150 boolean addToDependency = true;
151 if (label == EdgeLabelEnum.ALLOTTED_OF) {
152 //in case of not full allotted chain not add to dependency list
153 addToDependency = findAllottedChain(containerVertex, container);
155 if ( addToDependency ){
156 exisIn.add(container);
162 private boolean findAllottedChain(GraphVertex vertex, ComponentDependency container) {
163 Either<List<GraphVertex>, JanusGraphOperationStatus> parentVertecies = janusGraphDao.getParentVertices(vertex, EdgeLabelEnum.INSTANCE_OF, JsonParseFlagEnum.ParseAll);
164 if (parentVertecies.isLeft()) {
165 List<ComponentDependency> existIn = new ArrayList<>();
166 parentVertecies.left().value().forEach(v -> {
167 Boolean isHighest = (Boolean) v.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
168 if ( isHighest && needToAddToDependency(v) ) {
169 JanusGraphVertex janusGraphVertex = v.getVertex();
170 Iterator<Edge> edges = janusGraphVertex.edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
171 //verify that it is a last version - highest by version number
172 if ( edges == null || !edges.hasNext() ){
173 ComponentDependency parentContainer = fillDataFromVertex(v, vertex.getUniqueId(), EdgeLabelEnum.INSTANCE_OF);
174 existIn.add(parentContainer);
178 if ( !existIn.isEmpty() ){
179 container.setDependencies(existIn);
186 private ComponentDependency fillDataFromVertex(GraphVertex v, String originId, EdgeLabelEnum edgeLabel) {
187 ComponentDependency container = new ComponentDependency();
188 container.setName((String) v.getMetadataProperty(GraphPropertyEnum.NAME));
189 container.setVersion((String) v.getMetadataProperty(GraphPropertyEnum.VERSION));
190 container.setUniqueId(v.getUniqueId());
191 container.setType((String) v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
192 container.setIcon((String) v.getJsonMetadataField(JsonPresentationFields.ICON));
193 container.setState((String) v.getMetadataProperty(GraphPropertyEnum.STATE));
195 if (edgeLabel == EdgeLabelEnum.PROXY_OF || edgeLabel == EdgeLabelEnum.ALLOTTED_OF) {
196 findInstanceNames(v, originId, edgeLabel, container);
201 private void findInstanceNames(GraphVertex v, String originId, EdgeLabelEnum edgeLabel, ComponentDependency container) {
202 Map<String, CompositionDataDefinition> jsonComposition = (Map<String, CompositionDataDefinition>) v.getJson();
203 CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
204 JanusGraphVertex vertex = v.getVertex();
205 Iterator<Edge> edges = vertex.edges(Direction.OUT, edgeLabel.name());
206 while (edges != null && edges.hasNext()) {
207 Edge edge = edges.next();
208 JanusGraphVertex inVertex = (JanusGraphVertex) edge.inVertex();
209 String id = (String) janusGraphDao.getProperty(inVertex, GraphPropertyEnum.UNIQUE_ID.getProperty());
210 if (id.equals(originId)) {
211 List<String> instanceOnEdge = (List<String>) janusGraphDao
212 .getProperty(edge, EdgePropertyEnum.INSTANCES);
213 Map<String, ComponentInstanceDataDefinition> componentInstances = compositionDataDefinition.getComponentInstances();
215 if (componentInstances != null) {
216 List<String> ciNames = componentInstances
219 .filter(ci -> instanceOnEdge.contains(ci.getUniqueId()))
220 .map(ComponentInstanceDataDefinition::getName)
221 .collect(Collectors.toList());
222 if (ciNames != null && !ciNames.isEmpty()) {
223 container.setInstanceNames(ciNames);
231 public List<String> getInstanceIdFromAllottedEdge(String resourceId, String serviceInvariantUUID) {
232 Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(resourceId);
233 if ( vertexById.isLeft() ){
234 GraphVertex vertexG = vertexById.left().value();
235 JanusGraphVertex vertex = vertexG.getVertex();
236 Iterator<Edge> edges = vertex.edges(Direction.OUT, EdgeLabelEnum.ALLOTTED_OF.name());
237 while ( edges != null && edges.hasNext() ){
238 Edge edge = edges.next();
239 JanusGraphVertex inVertex = (JanusGraphVertex)edge.inVertex();
240 String vertexInInvUUID = (String) janusGraphDao
241 .getProperty(inVertex, GraphPropertyEnum.INVARIANT_UUID.getProperty());
242 if ( vertexInInvUUID.equals(serviceInvariantUUID) ){
243 return (List<String>) janusGraphDao.getProperty(edge, EdgePropertyEnum.INSTANCES) ;
247 return new ArrayList<>();