Consider component model when retrieving relationship types
Issue-ID: SDC-3640
Signed-off-by: MichaelMorris <michael.morris@est.tech>
Change-Id: Ic06a9085b8aa2f44b8d33d7de12eadf691106131
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech>
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeBusinessLogic.java
index 9a83566..195e43d 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeBusinessLogic.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeBusinessLogic.java
@@ -39,9 +39,9 @@
this.componentsUtils = componentsUtils;
}
- public Either<Map<String, RelationshipTypeDefinition>, ResponseFormat> getAllRelationshipTypes() {
+ public Either<Map<String, RelationshipTypeDefinition>, ResponseFormat> getAllRelationshipTypes(final String model) {
Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> allRelationshipTypes = relationshipTypeOperation
- .getAllRelationshipTypes();
+ .getAllRelationshipTypes(model);
if (allRelationshipTypes.isRight()) {
JanusGraphOperationStatus operationStatus = allRelationshipTypes.right().value();
if (JanusGraphOperationStatus.NOT_FOUND == operationStatus) {
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java
index 6fbdbaf..cafcbfd 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java
@@ -22,6 +22,7 @@
import com.jcabi.aspects.Loggable;
import fj.data.Either;
import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
@@ -42,10 +43,12 @@
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.collections4.ListUtils;
+import org.glassfish.jersey.media.multipart.FormDataParam;
import org.openecomp.sdc.be.components.impl.CapabilitiesBusinessLogic;
import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
@@ -223,7 +226,8 @@
@ApiResponse(responseCode = "404", description = "Relationship types not found")})
@PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
public Response getAllRelationshipTypesServlet(@Context final HttpServletRequest request,
- @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
+ @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
+ @Parameter(description = "model", required = false) @QueryParam("model") String modelName) {
Wrapper<Response> responseWrapper = new Wrapper<>();
Wrapper<User> userWrapper = new Wrapper<>();
try {
@@ -233,7 +237,7 @@
String url = request.getMethod() + " " + request.getRequestURI();
log.debug("Start handle request of {} | modifier id is {}", url, userId);
Either<Map<String, RelationshipTypeDefinition>, ResponseFormat> allDataTypes = relationshipTypeBusinessLogic
- .getAllRelationshipTypes();
+ .getAllRelationshipTypes(modelName);
if (allDataTypes.isRight()) {
log.info("Failed to get all relationship types. Reason - {}", allDataTypes.right().value());
Response errorResponse = buildErrorResponse(allDataTypes.right().value());
diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java
index c6ad5f2..b2492cd 100644
--- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java
+++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java
@@ -805,10 +805,47 @@
public <T extends GraphNode> Either<List<T>, JanusGraphOperationStatus> getByCriteria(NodeTypeEnum type, Map<String, Object> props,
Class<T> clazz) {
- Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
+ return getByCriteriaForModel(type, props, null, clazz);
+ }
+
+ public <T extends GraphNode> Either<List<T>, JanusGraphOperationStatus> getByCriteriaForModel(final NodeTypeEnum type, final Map<String, Object> props,
+ final String model, final Class<T> clazz) {
+ try {
+ final Either<Iterable<JanusGraphVertex>, JanusGraphOperationStatus> vertices = getVerticesByCriteria(type, props);
+
+ if (vertices.isLeft()) {
+ final Predicate<? super JanusGraphVertex> filterPredicate = StringUtils.isEmpty(model) ? this::vertexNotConnectedToAnyModel : vertex -> vertexValidForModel(vertex, model);
+ final List<JanusGraphVertex> verticesForModel = StreamSupport.stream(vertices.left().value().spliterator(), false).filter(filterPredicate).collect(Collectors.toList());
+
+ if (CollectionUtils.isEmpty(verticesForModel)) {
+ log.debug("No vertex in graph for props ={} ", props);
+ return Either.right(JanusGraphOperationStatus.NOT_FOUND);
+ }
+
+ final Iterator<JanusGraphVertex> iterator = verticesForModel.iterator();
+ final List<T> result = new ArrayList<>();
+ while (iterator.hasNext()) {
+ Vertex vertex = iterator.next();
+ Map<String, Object> newProp = getProperties(vertex);
+ T element = GraphElementFactory.createElement(type.getName(), GraphElementTypeEnum.Node, newProp, clazz);
+ result.add(element);
+ }
+ log.debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size());
+ return Either.left(result);
+
+ }
+ return Either.right(vertices.right().value());
+ } catch (Exception e) {
+ log.debug("Failed get by criteria for type = {} and properties = {}", type, props, e);
+ return Either.right(JanusGraphClient.handleJanusGraphException(e));
+ }
+ }
+
+ private Either<Iterable<JanusGraphVertex>, JanusGraphOperationStatus> getVerticesByCriteria(final NodeTypeEnum type, final Map<String, Object> props) {
+ final Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
if (graph.isLeft()) {
try {
- JanusGraph tGraph = graph.left().value();
+ final JanusGraph tGraph = graph.left().value();
JanusGraphQuery<? extends JanusGraphQuery> query = tGraph.query();
query = query.has(GraphPropertiesDictionary.LABEL.getProperty(), type.getName());
if (props != null && !props.isEmpty()) {
@@ -816,25 +853,11 @@
query = query.has(entry.getKey(), entry.getValue());
}
}
- Iterable<JanusGraphVertex> vertices = query.vertices();
- if (vertices == null) {
+ final Iterable<JanusGraphVertex> vertices = query.vertices();
+ if (vertices == null || !vertices.iterator().hasNext()) {
return Either.right(JanusGraphOperationStatus.NOT_FOUND);
}
- Iterator<JanusGraphVertex> iterator = vertices.iterator();
- List<T> result = new ArrayList<>();
- while (iterator.hasNext()) {
- Vertex vertex = iterator.next();
- Map<String, Object> newProp = getProperties(vertex);
- T element = GraphElementFactory.createElement(type.getName(), GraphElementTypeEnum.Node, newProp, clazz);
- result.add(element);
- }
- if (log.isDebugEnabled()) {
- log.debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size());
- }
- if (result.size() == 0) {
- return Either.right(JanusGraphOperationStatus.NOT_FOUND);
- }
- return Either.left(result);
+ return Either.left(vertices);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Failed get by criteria for type = {} and properties = {}", type, props, e);
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java
index f02fc2d..1b3fee9 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperation.java
@@ -20,8 +20,6 @@
package org.openecomp.sdc.be.model.operations.impl;
import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;
-
-import fj.data.Either;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -47,6 +45,7 @@
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
+import fj.data.Either;
@Component("relationship-type-operation")
public class RelationshipTypeOperation extends AbstractOperation {
@@ -72,7 +71,7 @@
return getRelationshipTypeDefinition(relationshipTypesRes.left().value());
}
- public Either<RelationshipTypeDefinition, JanusGraphOperationStatus> getRelationshipTypeDefinition(final RelationshipTypeData relationshipTypeData) {
+ private Either<RelationshipTypeDefinition, JanusGraphOperationStatus> getRelationshipTypeDefinition(final RelationshipTypeData relationshipTypeData) {
RelationshipTypeDefinition relationshipTypeDefinition = new RelationshipTypeDefinition(
relationshipTypeData.getRelationshipTypeDataDefinition());
Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> propertiesStatus = OperationUtils
@@ -364,11 +363,11 @@
DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
}
- public Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> getAllRelationshipTypes() {
+ public Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> getAllRelationshipTypes(final String model) {
Map<String, RelationshipTypeDefinition> relationshipTypeDefinitionMap = new HashMap<>();
Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> result = Either.left(relationshipTypeDefinitionMap);
Either<List<RelationshipTypeData>, JanusGraphOperationStatus> getAllRelationshipTypes = janusGraphGenericDao
- .getByCriteria(NodeTypeEnum.RelationshipType, null, RelationshipTypeData.class);
+ .getByCriteriaForModel(NodeTypeEnum.RelationshipType, null, model, RelationshipTypeData.class);
if (getAllRelationshipTypes.isRight()) {
JanusGraphOperationStatus status = getAllRelationshipTypes.right().value();
if (status != JanusGraphOperationStatus.NOT_FOUND) {
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java
index 1cc1336..6551a13 100644
--- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java
+++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/RelationshipTypeOperationTest.java
@@ -32,6 +32,7 @@
import java.util.Map.Entry;
import fj.data.Either;
+import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.Before;
@@ -155,9 +156,9 @@
@Test
public void testGetAllRelationshipTypesNotFound() {
Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)).when(
- janusGraphGenericDao).getByCriteria(NodeTypeEnum.RelationshipType, null,
+ janusGraphGenericDao).getByCriteriaForModel(NodeTypeEnum.RelationshipType, null, null,
RelationshipTypeData.class);
- Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> either = relationshipTypeOperation.getAllRelationshipTypes();
+ Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> either = relationshipTypeOperation.getAllRelationshipTypes(null);
assertTrue(either.isLeft() && MapUtils.isEmpty(either.left().value()));
}
@@ -165,9 +166,9 @@
@Test
public void testGetAllRelationshipTypesNotConnnected() {
Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_CONNECTED)).when(
- janusGraphGenericDao).getByCriteria(NodeTypeEnum.RelationshipType, null,
+ janusGraphGenericDao).getByCriteriaForModel(NodeTypeEnum.RelationshipType, null, null,
RelationshipTypeData.class);
- Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> either = relationshipTypeOperation.getAllRelationshipTypes();
+ Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> either = relationshipTypeOperation.getAllRelationshipTypes(null);
assertTrue(either.isRight() && JanusGraphOperationStatus.NOT_CONNECTED == either.right().value());
}
@@ -185,7 +186,7 @@
relationshipTypeDataList.add(relationshipTypeData1);
Mockito.doReturn(Either.left(relationshipTypeDataList))
- .when(janusGraphGenericDao).getByCriteria(NodeTypeEnum.RelationshipType, null,
+ .when(janusGraphGenericDao).getByCriteriaForModel(NodeTypeEnum.RelationshipType, null, null,
RelationshipTypeData.class);
Mockito.doReturn(Either.left(relationshipTypeData1)).when(janusGraphGenericDao)
@@ -203,11 +204,18 @@
.getDerivedFromChild("tosca.relationships.Root1", NodeTypeEnum.RelationshipType, RelationshipTypeData.class);
Either<Map<String, RelationshipTypeDefinition>, JanusGraphOperationStatus> either =
- relationshipTypeOperation.getAllRelationshipTypes();
+ relationshipTypeOperation.getAllRelationshipTypes(null);
assertTrue(either.isLeft());
RelationshipTypeDefinition relationshipTypeDefinition = either.left().value().get("tosca.relationships.Root1");
assertEquals("tosca.relationships.Parent", relationshipTypeDefinition.getDerivedFrom());
+
+ Mockito.doReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND))
+ .when(janusGraphGenericDao).getByCriteriaForModel(NodeTypeEnum.RelationshipType, null, "modelA",
+ RelationshipTypeData.class);
+ either = relationshipTypeOperation.getAllRelationshipTypes("modelA");
+ assertTrue(either.isLeft());
+ assertTrue(MapUtils.isEmpty(either.left().value()));
}
public RelationshipTypeDefinition createRelationship(String relationshipTypeName) {
diff --git a/catalog-ui/src/app/ng2/pages/workspace/req-and-capabilities/req-and-capabilities.service.ts b/catalog-ui/src/app/ng2/pages/workspace/req-and-capabilities/req-and-capabilities.service.ts
index 470aac7..f9b98c6 100644
--- a/catalog-ui/src/app/ng2/pages/workspace/req-and-capabilities/req-and-capabilities.service.ts
+++ b/catalog-ui/src/app/ng2/pages/workspace/req-and-capabilities/req-and-capabilities.service.ts
@@ -52,7 +52,7 @@
if (initInputsFor === 'INPUTS_FOR_REQUIREMENTS') {
if (!this.requirementsListUpdated){
this.relationshipTypesList = [];
- let relationshipTypesResult = await this.toscaTypesServiceNg2.fetchRelationshipTypes();
+ let relationshipTypesResult = await this.toscaTypesServiceNg2.fetchRelationshipTypes(this.workspaceService.metadata.model);
Object.keys(relationshipTypesResult).forEach(key => {this.relationshipTypesList.push(relationshipTypesResult[key])});
this.requirementsListUpdated = true;
}
diff --git a/catalog-ui/src/app/ng2/services/tosca-types.service.ts b/catalog-ui/src/app/ng2/services/tosca-types.service.ts
index 83b833b..2fc2e7a 100644
--- a/catalog-ui/src/app/ng2/services/tosca-types.service.ts
+++ b/catalog-ui/src/app/ng2/services/tosca-types.service.ts
@@ -14,18 +14,18 @@
* permissions and limitations under the License.
*/
-import { HttpClient } from '@angular/common/http';
-import { Inject, Injectable } from '@angular/core';
-import { Response } from '@angular/http';
+import {HttpClient} from '@angular/common/http';
+import {Inject, Injectable} from '@angular/core';
+import {Response} from '@angular/http';
import {
- CapabilityTypeModel,
- CapabilityTypesMap,
- IComponentsArray,
- NodeTypesMap,
- RelationshipTypesMap
+ CapabilityTypeModel,
+ CapabilityTypesMap,
+ IComponentsArray,
+ NodeTypesMap,
+ RelationshipTypesMap
} from 'app/models';
-import { Observable } from 'rxjs/Observable';
-import { ISdcConfig, SdcConfigToken } from '../config/sdc-config.config';
+import {Observable} from 'rxjs/Observable';
+import {ISdcConfig, SdcConfigToken} from '../config/sdc-config.config';
import 'rxjs/add/operator/toPromise';
declare var angular: angular.IAngularStatic;
@@ -33,22 +33,22 @@
@Injectable()
export class ToscaTypesServiceNg2 {
- protected baseUrl;
+ protected baseUrl;
- constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
- this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
- }
+ constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
+ this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
+ }
- async fetchRelationshipTypes(): Promise<RelationshipTypesMap> {
- return this.http.get<RelationshipTypesMap>(this.baseUrl + 'relationshipTypes').toPromise();
- }
+ async fetchRelationshipTypes(modelName: string): Promise<RelationshipTypesMap> {
+ return this.http.get<RelationshipTypesMap>(this.baseUrl + 'relationshipTypes', {params: {model: modelName}}).toPromise();
+ }
- async fetchNodeTypes(): Promise<NodeTypesMap> {
- return this.http.get<NodeTypesMap>(this.baseUrl + 'nodeTypes').toPromise();
- }
+ async fetchNodeTypes(): Promise<NodeTypesMap> {
+ return this.http.get<NodeTypesMap>(this.baseUrl + 'nodeTypes').toPromise();
+ }
- async fetchCapabilityTypes(): Promise<CapabilityTypesMap>{
- return this.http.get<CapabilityTypesMap>(this.baseUrl + 'capabilityTypes').toPromise();
- }
+ async fetchCapabilityTypes(): Promise<CapabilityTypesMap> {
+ return this.http.get<CapabilityTypesMap>(this.baseUrl + 'capabilityTypes').toPromise();
+ }
}