Add lombok support to simple classes
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / 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.jsonjanusgraph.operations;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
25 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
26 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
27 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
28 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
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>, JanusGraphOperationStatus> either = janusGraphDao
57                 .getByCriteria(type, props);
58
59             if (either.isRight()) {
60                 JanusGraphOperationStatus janusGraphOperationStatus = either.right().value();
61                 log.debug("Problem while geting category with id {}. reason - {}", categoryUid, janusGraphOperationStatus
62                     .name());
63                 if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
64                     return Either.right(StorageOperationStatus.CATEGORY_NOT_FOUND);
65                 } else {
66                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
67                 }
68             }
69             return Either.left(either.left().value().get(0));
70         } else {
71             return Either.right(StorageOperationStatus.GENERAL_ERROR);
72         }
73     }
74     /**
75      *
76      * @param categoryV
77      * @param name
78      * @return
79      */
80     public  Either<GraphVertex, StorageOperationStatus> getSubCategoryForCategory(GraphVertex categoryV, String name ) {
81         Either<List<GraphVertex>, JanusGraphOperationStatus> childrenVertecies = janusGraphDao
82             .getChildrenVertecies(categoryV, EdgeLabelEnum.SUB_CATEGORY, JsonParseFlagEnum.NoParse);
83         if ( childrenVertecies.isRight() ){
84             log.debug("Failed to fetch children verticies for category {} error {}", categoryV.getUniqueId(), childrenVertecies.right().value());
85             return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(childrenVertecies.right().value()));
86         }
87         for ( GraphVertex childV : childrenVertecies.left().value() ){
88             if ( childV.getMetadataProperty(GraphPropertyEnum.NAME).equals(name) ){
89                 return Either.left(childV);
90             }
91         }
92         return Either.right(StorageOperationStatus.NOT_FOUND);
93     }
94 }