Implement 'Update Service by importing Tosca Model'-story
[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 package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
21
22 import fj.data.Either;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
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;
37
38 @org.springframework.stereotype.Component("category-operation")
39 public class CategoryOperation extends BaseOperation {
40
41     private static final Logger log = Logger.getLogger(CategoryOperation.class.getName());
42
43     /**
44      * @param name
45      * @param type
46      * @return
47      */
48     public Either<GraphVertex, StorageOperationStatus> getCategory(String name, VertexTypeEnum type) {
49         if (name != null) {
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);
59                 } else {
60                     return Either.right(StorageOperationStatus.GENERAL_ERROR);
61                 }
62             }
63             return Either.left(either.left().value().get(0));
64         } else {
65             return Either.right(StorageOperationStatus.GENERAL_ERROR);
66         }
67     }
68
69     /**
70      * @param categoryV
71      * @param name
72      * @return
73      */
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()));
80         }
81         for (GraphVertex childV : childrenVertecies.left().value()) {
82             if (childV.getMetadataProperty(GraphPropertyEnum.NAME).equals(name)) {
83                 return Either.left(childV);
84             }
85         }
86         return Either.right(StorageOperationStatus.NOT_FOUND);
87     }
88 }