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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
19 package org.openecomp.sdc.be.model.operations.impl;
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;
34 @Component("model-operation")
35 public class ModelOperation extends AbstractOperation {
37 private static final Logger log = Logger.getLogger(ModelOperation.class);
39 private final JanusGraphGenericDao genericDao;
42 public ModelOperation(final JanusGraphGenericDao janusGraphGenericDao) {
43 this.genericDao = janusGraphGenericDao;
46 public Model createModel(final Model model, final boolean inTransaction) {
48 final ModelData modelData = new ModelData(model.getName(), UniqueIdBuilder.buildModelUid(model.getName()));
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());
58 throw new OperationException(ActionStatus.GENERAL_ERROR,
59 String.format("Failed to create model %s on JanusGraph with %s error", model, janusGraphOperationStatus));
61 result = new Model(createNode.left().value().getName());
65 if (Objects.nonNull(result)) {
68 genericDao.rollback();