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 ( needToAddToDepenedency(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);
121 private boolean needToAddToDepenedency(GraphVertex vertex){
122 Boolean isDeleted = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_DELETED);
123 Boolean isArchived = (Boolean) vertex.getMetadataProperty(GraphPropertyEnum.IS_ARCHIVED);
124 return ( isDeleted == Boolean.TRUE || isArchived == Boolean.TRUE) ? false : true;
127 private StorageOperationStatus fillDependenciesByLabel(String componentId, GraphVertex vertex, ComponentDependency dependency, EdgeLabelEnum label) {
128 Either<List<GraphVertex>, JanusGraphOperationStatus> parentVertecies = janusGraphDao
129 .getParentVertecies(vertex, label, JsonParseFlagEnum.ParseAll);
130 if (parentVertecies.isRight() && parentVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
131 log.debug("Failed to fetch parent verticies by label INSTANCE_OF for vertex with id {} error {}", componentId, parentVertecies.right().value());
132 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertecies.right().value());
134 if (parentVertecies.isLeft()) {
135 List<ComponentDependency> existIn = new ArrayList<>( );
136 parentVertecies.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 && needToAddToDepenedency(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
164 .getParentVertecies(vertex, EdgeLabelEnum.INSTANCE_OF, JsonParseFlagEnum.ParseAll);
165 if (parentVertecies.isLeft()) {
166 List<ComponentDependency> existIn = new ArrayList<>();
167 parentVertecies.left().value().forEach(v -> {
168 Boolean isHighest = (Boolean) v.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
169 if ( isHighest && needToAddToDepenedency(v) ) {
170 JanusGraphVertex janusGraphVertex = v.getVertex();
171 Iterator<Edge> edges = janusGraphVertex.edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
172 //verify that it is a last version - highest by version number
173 if ( edges == null || !edges.hasNext() ){
174 ComponentDependency parentContainer = fillDataFromVertex(v, vertex.getUniqueId(), EdgeLabelEnum.INSTANCE_OF);
175 existIn.add(parentContainer);
179 if ( !existIn.isEmpty() ){
180 container.setDependencies(existIn);
187 private ComponentDependency fillDataFromVertex(GraphVertex v, String originId, EdgeLabelEnum edgeLabel) {
188 ComponentDependency container = new ComponentDependency();
189 container.setName((String) v.getMetadataProperty(GraphPropertyEnum.NAME));
190 container.setVersion((String) v.getMetadataProperty(GraphPropertyEnum.VERSION));
191 container.setUniqueId(v.getUniqueId());
192 container.setType((String) v.getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
193 container.setIcon((String) v.getJsonMetadataField(JsonPresentationFields.ICON));
194 container.setState((String) v.getMetadataProperty(GraphPropertyEnum.STATE));
196 if (edgeLabel == EdgeLabelEnum.PROXY_OF || edgeLabel == EdgeLabelEnum.ALLOTTED_OF) {
197 findInstanceNames(v, originId, edgeLabel, container);
202 private void findInstanceNames(GraphVertex v, String originId, EdgeLabelEnum edgeLabel, ComponentDependency container) {
203 Map<String, CompositionDataDefinition> jsonComposition = (Map<String, CompositionDataDefinition>) v.getJson();
204 CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
205 JanusGraphVertex vertex = v.getVertex();
206 Iterator<Edge> edges = vertex.edges(Direction.OUT, edgeLabel.name());
207 while (edges != null && edges.hasNext()) {
208 Edge edge = edges.next();
209 JanusGraphVertex inVertex = (JanusGraphVertex) edge.inVertex();
210 String id = (String) janusGraphDao.getProperty(inVertex, GraphPropertyEnum.UNIQUE_ID.getProperty());
211 if (id.equals(originId)) {
212 List<String> instanceOnEdge = (List<String>) janusGraphDao
213 .getProperty(edge, EdgePropertyEnum.INSTANCES);
214 Map<String, ComponentInstanceDataDefinition> componentInstances = compositionDataDefinition.getComponentInstances();
216 if (componentInstances != null) {
217 List<String> ciNames = componentInstances
220 .filter(ci -> instanceOnEdge.contains(ci.getUniqueId()))
221 .map(ComponentInstanceDataDefinition::getName)
222 .collect(Collectors.toList());
223 if (ciNames != null && !ciNames.isEmpty()) {
224 container.setInstanceNames(ciNames);
232 public List<String> getInstanceIdFromAllottedEdge(String resourceId, String serviceInvariantUUID) {
233 Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(resourceId);
234 if ( vertexById.isLeft() ){
235 GraphVertex vertexG = vertexById.left().value();
236 JanusGraphVertex vertex = vertexG.getVertex();
237 Iterator<Edge> edges = vertex.edges(Direction.OUT, EdgeLabelEnum.ALLOTTED_OF.name());
238 while ( edges != null && edges.hasNext() ){
239 Edge edge = edges.next();
240 JanusGraphVertex inVertex = (JanusGraphVertex)edge.inVertex();
241 String vertexInInvUUID = (String) janusGraphDao
242 .getProperty(inVertex, GraphPropertyEnum.INVARIANT_UUID.getProperty());
243 if ( vertexInInvUUID.equals(serviceInvariantUUID) ){
244 return (List<String>) janusGraphDao.getProperty(edge, EdgePropertyEnum.INSTANCES) ;
248 return new ArrayList<>();