Add Support for creating model type
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / ModelOperation.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.model.operations.impl;
20
21 import fj.data.Either;
22 import java.util.Objects;
23 import org.openecomp.sdc.be.dao.api.ActionStatus;
24 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
25 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
26 import org.openecomp.sdc.be.model.Model;
27 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
28 import org.openecomp.sdc.be.resources.data.ModelData;
29 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 @Component("model-operation")
35 public class ModelOperation extends AbstractOperation {
36
37     private static final Logger log = Logger.getLogger(ModelOperation.class);
38
39     private final JanusGraphGenericDao genericDao;
40
41     @Autowired
42     public ModelOperation(final JanusGraphGenericDao janusGraphGenericDao) {
43         this.genericDao = janusGraphGenericDao;
44     }
45
46     public Model createModel(final Model model, final boolean inTransaction) {
47         Model result = null;
48         final ModelData modelData = new ModelData(model.getName(), UniqueIdBuilder.buildModelUid(model.getName()));
49         try {
50             final Either<ModelData, JanusGraphOperationStatus> createNode = genericDao.createNode(modelData, ModelData.class);
51             if (createNode.isRight()) {
52                 final JanusGraphOperationStatus janusGraphOperationStatus = createNode.right().value();
53                 log.error(EcompLoggerErrorCode.DATA_ERROR, ModelOperation.class.getName(), "Problem while creating model, reason {}",
54                     janusGraphOperationStatus);
55                 if (janusGraphOperationStatus == JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION) {
56                     throw new OperationException(ActionStatus.MODEL_ALREADY_EXISTS, model.getName());
57                 }
58                 throw new OperationException(ActionStatus.GENERAL_ERROR,
59                     String.format("Failed to create model %s on JanusGraph with %s error", model, janusGraphOperationStatus));
60             }
61             result = new Model(createNode.left().value().getName());
62             return result;
63         } finally {
64             if (!inTransaction) {
65                 if (Objects.nonNull(result)) {
66                     genericDao.commit();
67                 } else {
68                     genericDao.rollback();
69                 }
70             }
71         }
72     }
73
74 }
75
76