re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / CategoryOperation.java
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.be.model.jsontitan.operations;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
25 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
26 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
27 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
28 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
29 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
30 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
31 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
32 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.openecomp.sdc.common.util.ValidationUtils;
35
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 @org.springframework.stereotype.Component("category-operation")
41 public class CategoryOperation extends BaseOperation{
42
43     private static final Logger log = Logger.getLogger(CategoryOperation.class.getName());
44
45     /**
46      *
47      * @param name
48      * @param type
49      * @return
50      */
51     public  Either<GraphVertex, StorageOperationStatus> getCategory(String name, VertexTypeEnum type) {
52         if (name != null) {
53             String categoryUid = UniqueIdBuilder.buildComponentCategoryUid(name, type);
54             Map<GraphPropertyEnum, Object> props = new HashMap<>();
55             props.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(name));
56             Either<List<GraphVertex>, TitanOperationStatus> either = titanDao.getByCriteria(type, props);
57
58             if (either.isRight()) {
59                 TitanOperationStatus titanOperationStatus = either.right().value();
60                 log.debug("Problem while geting category with id {}. reason - {}", categoryUid, titanOperationStatus.name());
61                 if (titanOperationStatus == TitanOperationStatus.NOT_FOUND) {
62                     return Either.right(StorageOperationStatus.CATEGORY_NOT_FOUND);
63                 } else {
64                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
65                 }
66             }
67             return Either.left(either.left().value().get(0));
68         } else {
69             return Either.right(StorageOperationStatus.GENERAL_ERROR);
70         }
71     }
72     /**
73      *
74      * @param categoryV
75      * @param name
76      * @return
77      */
78     public  Either<GraphVertex, StorageOperationStatus> getSubCategoryForCategory(GraphVertex categoryV, String name ) {
79         Either<List<GraphVertex>, TitanOperationStatus> childrenVertecies = titanDao.getChildrenVertecies(categoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
80         if ( childrenVertecies.isRight() ){
81             log.debug("Failed to fetch children verticies for category {} error {}", categoryV.getUniqueId(), childrenVertecies.right().value());
82             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(childrenVertecies.right().value()));
83         }
84         for ( GraphVertex childV : childrenVertecies.left().value() ){
85             if ( childV.getMetadataProperty(GraphPropertyEnum.NAME).equals(name) ){
86                 return Either.left(childV);
87             }
88         }
89         return Either.right(StorageOperationStatus.NOT_FOUND);
90     }
91 }