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=========================================================
20 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
22 import fj.data.Either;
23 import java.util.HashMap;
24 import java.util.List;
26 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
29 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
30 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
34 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.openecomp.sdc.common.util.ValidationUtils;
38 @org.springframework.stereotype.Component("category-operation")
39 public class CategoryOperation extends BaseOperation {
41 private static final Logger log = Logger.getLogger(CategoryOperation.class.getName());
48 public Either<GraphVertex, StorageOperationStatus> getCategory(String name, VertexTypeEnum type) {
50 String categoryUid = UniqueIdBuilder.buildComponentCategoryUid(name, type);
51 Map<GraphPropertyEnum, Object> props = new HashMap<>();
52 props.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(name));
53 Either<List<GraphVertex>, JanusGraphOperationStatus> either = janusGraphDao.getByCriteria(type, props);
54 if (either.isRight()) {
55 JanusGraphOperationStatus janusGraphOperationStatus = either.right().value();
56 log.debug("Problem while geting category with id {}. reason - {}", categoryUid, janusGraphOperationStatus.name());
57 if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
58 return Either.right(StorageOperationStatus.CATEGORY_NOT_FOUND);
60 return Either.right(StorageOperationStatus.GENERAL_ERROR);
63 return Either.left(either.left().value().get(0));
65 return Either.right(StorageOperationStatus.GENERAL_ERROR);
74 public Either<GraphVertex, StorageOperationStatus> getSubCategoryForCategory(GraphVertex categoryV, String name) {
75 Either<List<GraphVertex>, JanusGraphOperationStatus> childrenVertecies = janusGraphDao
76 .getChildrenVertices(categoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
77 if (childrenVertecies.isRight()) {
78 log.debug("Failed to fetch children verticies for category {} error {}", categoryV.getUniqueId(), childrenVertecies.right().value());
79 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
81 for (GraphVertex childV : childrenVertecies.left().value()) {
82 if (childV.getMetadataProperty(GraphPropertyEnum.NAME).equals(name)) {
83 return Either.left(childV);
86 return Either.right(StorageOperationStatus.NOT_FOUND);