aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java
blob: b57c57a9cc5abd29ddceb87606d9367080145b07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.model.operations.impl;

import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DATA_TYPES;
import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.PROPERTIES;
import static org.openecomp.sdc.common.api.Constants.ADDITIONAL_TYPE_DEFINITIONS;

import fj.data.Either;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.onap.sdc.tosca.services.YamlUtil;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.cassandra.ToscaModelImportCassandraDao;
import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
import org.openecomp.sdc.be.data.model.ToscaImportByModel;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.model.Model;
import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
import org.openecomp.sdc.be.model.normatives.ElementTypeEnum;
import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.resources.data.ModelData;
import org.openecomp.sdc.be.utils.TypeUtils;
import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

@Component("model-operation")
public class ModelOperation {

    static final Path ADDITIONAL_TYPE_DEFINITIONS_PATH = Path.of(ADDITIONAL_TYPE_DEFINITIONS);
    private static final Logger log = Logger.getLogger(ModelOperation.class);
    private final JanusGraphGenericDao janusGraphGenericDao;
    private final JanusGraphDao janusGraphDao;
    private final ToscaModelImportCassandraDao toscaModelImportCassandraDao;
    private final DerivedFromOperation derivedFromOperation;
    private ModelElementOperation modelElementOperation;

    @Autowired
    public ModelOperation(final JanusGraphGenericDao janusGraphGenericDao,
                          final JanusGraphDao janusGraphDao,
                          final ToscaModelImportCassandraDao toscaModelImportCassandraDao,
                          final DerivedFromOperation derivedFromOperation) {
        this.janusGraphGenericDao = janusGraphGenericDao;
        this.janusGraphDao = janusGraphDao;
        this.toscaModelImportCassandraDao = toscaModelImportCassandraDao;
        this.derivedFromOperation = derivedFromOperation;
    }

    public Model createModel(final Model model, final boolean inTransaction) {
        Model result = null;
        final var modelData = new ModelData(model.getName(), UniqueIdBuilder.buildModelUid(model.getName()), model.getModelType());
        try {
            final Either<ModelData, JanusGraphOperationStatus> createNode = janusGraphGenericDao.createNode(modelData, ModelData.class);
            if (createNode.isRight()) {
                final var janusGraphOperationStatus = createNode.right().value();
                log.error(EcompLoggerErrorCode.DATA_ERROR, ModelOperation.class.getName(), "Problem while creating model, reason {}",
                    janusGraphOperationStatus);
                if (janusGraphOperationStatus == JanusGraphOperationStatus.JANUSGRAPH_SCHEMA_VIOLATION) {
                    throw ModelOperationExceptionSupplier.modelAlreadyExists(model.getName()).get();
                }
                throw new OperationException(ActionStatus.GENERAL_ERROR,
                    String.format("Failed to create model %s on JanusGraph with %s error", model, janusGraphOperationStatus));
            }
            addDerivedFromRelation(model);
            result = new Model(createNode.left().value().getName(), model.getDerivedFrom(), model.getModelType());
            return result;
        } finally {
            if (!inTransaction) {
                if (Objects.nonNull(result)) {
                    janusGraphGenericDao.commit();
                } else {
                    janusGraphGenericDao.rollback();
                }
            }
        }
    }

    private void addDerivedFromRelation(final Model model) {
        final String derivedFrom = model.getDerivedFrom();
        if (derivedFrom == null) {
            return;
        }
        log.debug("Adding derived from relation between model {} to its parent {}",
            model.getName(), derivedFrom);
        final Optional<Model> derivedFromModelOptional = this.findModelByName(derivedFrom);
        if (derivedFromModelOptional.isPresent()) {
            final Either<GraphRelation, StorageOperationStatus> result = derivedFromOperation.addDerivedFromRelation(
                UniqueIdBuilder.buildModelUid(model.getName()),
                UniqueIdBuilder.buildModelUid(derivedFromModelOptional.get().getName()), NodeTypeEnum.Model);
            if (result.isRight()) {
                throw new OperationException(ActionStatus.GENERAL_ERROR,
                    String.format("Failed to create relationship from model %s to derived from model %s on JanusGraph with %s error", model,
                        derivedFrom, result.right().value()));
            }
        }
    }

    public Optional<GraphVertex> findModelVertexByName(final String name) {
        if (StringUtils.isEmpty(name)) {
            return Optional.empty();
        }
        final Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
        props.put(GraphPropertyEnum.NAME, name);
        props.put(GraphPropertyEnum.UNIQUE_ID, UniqueIdBuilder.buildModelUid(name));
        final List<GraphVertex> modelVerticesList = findModelVerticesByCriteria(props);
        if (modelVerticesList.isEmpty()) {
            return Optional.empty();
        }
        return Optional.ofNullable(modelVerticesList.get(0));
    }

    public Optional<Model> findModelByName(final String name) {
        if (StringUtils.isEmpty(name)) {
            return Optional.empty();
        }
        final Optional<GraphVertex> modelVertexOpt = findModelVertexByName(name);
        if (modelVertexOpt.isEmpty()) {
            return Optional.empty();
        }

        final GraphVertex graphVertex = modelVertexOpt.get();
        return Optional.of(convertToModel(graphVertex));
    }

    public void createModelImports(final String modelId, final Map<String, byte[]> zipContent) {
        if (MapUtils.isEmpty(zipContent)) {
            return;
        }
        final List<ToscaImportByModel> toscaImportByModelList = zipContent.entrySet().stream()
            .map(entry -> {
                final String path = entry.getKey();
                final byte[] bytes = entry.getValue();
                final String content = new String(bytes, StandardCharsets.UTF_8);
                final var toscaImportByModel = new ToscaImportByModel();
                toscaImportByModel.setModelId(modelId);
                toscaImportByModel.setFullPath(path);
                toscaImportByModel.setContent(content);
                return toscaImportByModel;
            }).collect(Collectors.toList());
        toscaModelImportCassandraDao.replaceImports(modelId, toscaImportByModelList);
    }

    /**
     * Find all the model default imports, with the option to include the default imports from the parent model.
     *
     * @param modelId       the model id
     * @param includeParent a flag to include the parent model imports.
     * @return the list of model default imports, or an empty list if no imports were found.
     */
    public List<ToscaImportByModel> findAllModelImports(final String modelId, final boolean includeParent) {
        final List<ToscaImportByModel> toscaImportByModelList = toscaModelImportCassandraDao.findAllByModel(modelId);
        if (includeParent) {
            findModelByName(modelId).ifPresent(model -> {
                if (model.getDerivedFrom() != null) {
                    toscaImportByModelList.addAll(toscaModelImportCassandraDao.findAllByModel(model.getDerivedFrom()));
                }
            });
        }
        toscaImportByModelList.sort((o1, o2) -> {
            final int modelIdComparison = o1.getModelId().compareTo(o2.getModelId());
            if (modelIdComparison == 0) {
                return o1.getFullPath().compareTo(o2.getFullPath());
            }
            return modelIdComparison;
        });
        return toscaImportByModelList;
    }

    /**
     * Finds all the models.
     *
     * @return the list of models
     */
    public List<Model> findAllModels() {
        return findModelsByCriteria(Collections.emptyMap());
    }

    public List<Model> findModels(final ModelTypeEnum modelType) {
        final Map<GraphPropertyEnum, Object> propertyCriteria = new EnumMap<>(GraphPropertyEnum.class);
        propertyCriteria.put(GraphPropertyEnum.MODEL_TYPE, modelType.getValue());

        return findModelsByCriteria(propertyCriteria);
    }

    private List<Model> findModelsByCriteria(final Map<GraphPropertyEnum, Object> propertyCriteria) {
        final List<GraphVertex> modelVerticesByCriteria = findModelVerticesByCriteria(propertyCriteria);
        if (modelVerticesByCriteria.isEmpty()) {
            return Collections.emptyList();
        }

        return modelVerticesByCriteria.stream().map(this::convertToModel).collect(Collectors.toList());
    }

    private List<GraphVertex> findModelVerticesByCriteria(final Map<GraphPropertyEnum, Object> propertyCriteria) {
        final Either<List<GraphVertex>, JanusGraphOperationStatus> result = janusGraphDao.getByCriteria(VertexTypeEnum.MODEL, propertyCriteria);
        if (result.isRight()) {
            final var janusGraphOperationStatus = result.right().value();
            if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
                return Collections.emptyList();
            }
            final var operationException = ModelOperationExceptionSupplier.failedToRetrieveModels(janusGraphOperationStatus).get();
            log.error(EcompLoggerErrorCode.DATA_ERROR, this.getClass().getName(), operationException.getMessage());
            throw operationException;
        }
        return result.left().value();
    }

    private Model convertToModel(final GraphVertex modelGraphVertex) {
        final String modelName = (String) modelGraphVertex.getMetadataProperty(GraphPropertyEnum.NAME);
        final String modelTypeProperty = (String) modelGraphVertex.getMetadataProperty(GraphPropertyEnum.MODEL_TYPE);
        ModelTypeEnum modelType = ModelTypeEnum.NORMATIVE;
        final Optional<ModelTypeEnum> optionalModelTypeEnum = ModelTypeEnum.findByValue(modelTypeProperty);
        if (optionalModelTypeEnum.isPresent()) {
            modelType = optionalModelTypeEnum.get();
        }

        final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> parentNode =
            janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName),
                GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class);
        log.debug("After retrieving DERIVED_FROM node of {}. status is {}", modelName, parentNode);
        if (parentNode.isRight()) {
            final JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value();
            if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) {
                final var operationException = ModelOperationExceptionSupplier.failedToRetrieveModels(janusGraphOperationStatus).get();
                log.error(EcompLoggerErrorCode.DATA_ERROR, this.getClass().getName(), operationException.getMessage());
                throw operationException;
            }
            return new Model((String) modelGraphVertex.getMetadataProperty(GraphPropertyEnum.NAME), modelType);
        } else {
            final ModelData parentModel = parentNode.left().value().getKey();
            return new Model((String) modelGraphVertex.getMetadataProperty(GraphPropertyEnum.NAME), parentModel.getName(), modelType);
        }
    }

    public void addTypesToDefaultImports(final ElementTypeEnum elementTypeEnum, final String typesYaml, final String modelName) {
        final List<ToscaImportByModel> modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName);
        final Optional<ToscaImportByModel> additionalTypeDefinitionsImportOptional = modelImportList.stream()
            .filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny();
        final ToscaImportByModel additionalTypeDefinitionsImport;
        final List<ToscaImportByModel> rebuiltModelImportList;
        if (additionalTypeDefinitionsImportOptional.isPresent()) {
            additionalTypeDefinitionsImport = additionalTypeDefinitionsImportOptional.get();
            rebuiltModelImportList = modelImportList.stream()
                .filter(toscaImportByModel -> !ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(toscaImportByModel.getFullPath())))
                .collect(Collectors.toList());
        } else {
            additionalTypeDefinitionsImport = new ToscaImportByModel();
            additionalTypeDefinitionsImport.setModelId(modelName);
            additionalTypeDefinitionsImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString());
            additionalTypeDefinitionsImport.setContent(createAdditionalTypeDefinitionsHeader());
            rebuiltModelImportList = new ArrayList<>(modelImportList);
        }

        Map<String, Object> typesYamlMap = new Yaml().loadAs(typesYaml, Map.class);
        if (typesYamlMap.containsKey(DATA_TYPES.getElementName())) {
            typesYamlMap = (Map<String, Object>) typesYamlMap.get(DATA_TYPES.getElementName());
        }
        removeExistingTypesFromDefaultImports(elementTypeEnum, typesYamlMap, rebuiltModelImportList);

        final Map<String, Object> originalContent = new Yaml().load(additionalTypeDefinitionsImport.getContent());
        additionalTypeDefinitionsImport.setContent(buildAdditionalTypeDefinitionsContent(elementTypeEnum, typesYamlMap, originalContent));
        rebuiltModelImportList.add(additionalTypeDefinitionsImport);

        toscaModelImportCassandraDao.saveAll(modelName, rebuiltModelImportList);
    }

    private void removeExistingTypesFromDefaultImports(final ElementTypeEnum elementTypeEnum, final Map<String, Object> typesYaml,
                                                       final List<ToscaImportByModel> defaultImportList) {
        defaultImportList.forEach(toscaImportByModel -> {
            final Map<String, Object> existingImportYamlMap = new Yaml().load(toscaImportByModel.getContent());
            final Map<String, Object> currentTypeYamlMap = (Map<String, Object>) existingImportYamlMap.get(elementTypeEnum.getToscaEntryName());
            if (MapUtils.isNotEmpty(currentTypeYamlMap)) {
                typesYaml.keySet().forEach(currentTypeYamlMap::remove);
            }
            toscaImportByModel.setContent(new YamlUtil().objectToYaml(existingImportYamlMap));
        });
    }

    private String buildAdditionalTypeDefinitionsContent(final ElementTypeEnum elementTypeEnum, final Map<String, Object> typesYamlMap,
                                                         final Map<String, Object> originalContent) {
        final Map<String, Object> originalTypeContent = (Map<String, Object>) originalContent.get(elementTypeEnum.getToscaEntryName());
        if (MapUtils.isEmpty(originalTypeContent)) {
            originalContent.put(elementTypeEnum.getToscaEntryName(), new LinkedHashMap<>(typesYamlMap));
        } else {
            originalTypeContent.putAll(typesYamlMap);
        }
        return new YamlUtil().objectToYaml(originalContent);
    }

    private String createAdditionalTypeDefinitionsHeader() {
        return "tosca_definitions_version: tosca_simple_yaml_1_3" + "\n"
            + "description: Auto-generated file that contains package custom types or types added after system installation." + "\n";
    }

    /**
     * Deletes the given model if it exists, along with its MODEL_ELEMENT edges and import files.
     *
     * @param model         the model
     * @param inTransaction if the operation is called in the middle of a janusgraph transaction
     */
    public void deleteModel(final Model model, final boolean inTransaction) {
        boolean rollback = false;

        try {
            final GraphVertex modelVertexByName = findModelVertexByName(model.getName()).orElse(null);
            if (modelVertexByName == null) {
                return;
            }
            toscaModelImportCassandraDao.deleteAllByModel(model.getName());
            modelElementOperation.deleteModelElements(model, inTransaction);
            deleteModel(model);
        } catch (final OperationException e) {
            rollback = true;
            throw e;
        } catch (final Exception e) {
            rollback = true;
            throw new OperationException(e, ActionStatus.COULD_NOT_DELETE_MODEL, model.getName());
        } finally {
            if (!inTransaction) {
                if (rollback) {
                    janusGraphGenericDao.rollback();
                } else {
                    janusGraphGenericDao.commit();
                }
            }
        }
    }

    private void deleteModel(final Model model) {
        final var modelData = new ModelData(model.getName(), UniqueIdBuilder.buildModelUid(model.getName()), model.getModelType());
        final Either<ModelData, JanusGraphOperationStatus> deleteParentNodeByModel = janusGraphGenericDao.deleteNode(modelData, ModelData.class);
        if (deleteParentNodeByModel.isRight()) {
            final var janusGraphOperationStatus = deleteParentNodeByModel.right().value();
            log.error(EcompLoggerErrorCode.DATA_ERROR, ModelOperation.class.getName(),
                "Failed to delete model {} on JanusGraph with status {}", model.getName(), janusGraphOperationStatus);
            throw new OperationException(ActionStatus.COULD_NOT_DELETE_MODEL, model.getName());
        }
    }

    @Autowired
    public void setModelElementOperation(final ModelElementOperation modelElementOperation) {
        this.modelElementOperation = modelElementOperation;
    }

    @SuppressWarnings("unchecked")
    public void updateTypesInAdditionalTypesImport(final ElementTypeEnum elementTypeEnum, final String typesYaml, final String modelName) {
        final Optional<ToscaImportByModel> additionalTypeDefinitionsImportOptional = getAdditionalTypes(modelName);

        if (additionalTypeDefinitionsImportOptional.isPresent()) {

            final Map<String, Object> existingTypeContent = getExistingTypes(elementTypeEnum, additionalTypeDefinitionsImportOptional.get());
            final Set<String> existingTypeNames = existingTypeContent.keySet();

            Map<String, Object> newTypesYaml = new Yaml().load(typesYaml);
            if (newTypesYaml.containsKey(DATA_TYPES.getElementName())) {
                newTypesYaml = (Map<String, Object>) newTypesYaml.get(DATA_TYPES.getElementName());
            }
            final Map<String, Object> typesToUpate = new HashMap<>();
            newTypesYaml.entrySet().stream().filter(entry -> existingTypeNames.contains(entry.getKey())).forEach(newTypeToUpdate -> {

                final Map<String, Object> propertiesInNewDef =
                    (Map<String, Object>) ((Map<String, Object>) newTypeToUpdate.getValue()).get(PROPERTIES.getElementName());
                final Map<String, Object> existingProperties =
                    (Map<String, Object>) ((Map<String, Object>) existingTypeContent.get(newTypeToUpdate.getKey())).get(PROPERTIES.getElementName());

                final List<Entry<String, Object>> propertiesMissingFromNewDef = MapUtils.isEmpty(existingProperties) ? Collections.emptyList() :
                    existingProperties.entrySet().stream()
                        .filter(existingPropEntry -> !propertiesInNewDef.keySet().contains(existingPropEntry.getKey())).collect(Collectors.toList());

                if (CollectionUtils.isNotEmpty(propertiesMissingFromNewDef)) {
                    typesToUpate.put(newTypeToUpdate.getKey(), newTypeToUpdate.getValue());

                    propertiesMissingFromNewDef
                        .forEach(existingPropToAdd -> propertiesInNewDef.put(existingPropToAdd.getKey(), existingPropToAdd.getValue()));
                }
            });
            if (MapUtils.isNotEmpty(typesToUpate)) {
                addTypesToDefaultImports(elementTypeEnum, new Yaml().dumpAsMap(typesToUpate), modelName);
            }
        }
    }

    private Optional<ToscaImportByModel> getAdditionalTypes(final String modelName) {
        final List<ToscaImportByModel> modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName);
        return modelImportList.stream().filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny();
    }

    private Map<String, Object> getExistingTypes(final ElementTypeEnum elementTypeEnum, final ToscaImportByModel additionalTypeDefinitionsImport) {
        final Map<String, Object> existingContent = new Yaml().load(additionalTypeDefinitionsImport.getContent());
        return (Map<String, Object>) existingContent.get(elementTypeEnum.getToscaEntryName());
    }

    public void updatePropertyInAdditionalType(final ElementTypeEnum elementTypeEnum, final PropertyDefinitionDto property,
                                               final String modelName, final String name, boolean isAdd) {
        final List<ToscaImportByModel> modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName);
        final Optional<ToscaImportByModel> additionalTypeDefinitionsImportOptional = modelImportList.stream()
            .filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny();
        if (additionalTypeDefinitionsImportOptional.isEmpty()) {
            return;
        }
        final ToscaImportByModel additionalTypeDefinitionsImport = additionalTypeDefinitionsImportOptional.get();
        final List<ToscaImportByModel> rebuiltModelImportList = modelImportList.stream()
            .filter(toscaImportByModel -> !ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(toscaImportByModel.getFullPath())))
            .collect(Collectors.toList());
        final Map<String, Object> originalContent = new Yaml().load(additionalTypeDefinitionsImport.getContent());
        additionalTypeDefinitionsImport.setContent(
            buildPropertyAdditionalTypeDefinitionContent(elementTypeEnum, name, property, originalContent, isAdd));
        rebuiltModelImportList.add(additionalTypeDefinitionsImport);
        toscaModelImportCassandraDao.saveAll(modelName, rebuiltModelImportList);
    }

    public void removeDataTypeFromAdditionalType(final ElementTypeEnum elementTypeEnum, final String modelName, final String name) {
        final List<ToscaImportByModel> modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName);
        final Optional<ToscaImportByModel> additionalTypeDefinitionsImportOptional = modelImportList.stream()
            .filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny();
        if (additionalTypeDefinitionsImportOptional.isEmpty()) {
            return;
        }
        final ToscaImportByModel additionalTypeDefinitionsImport = additionalTypeDefinitionsImportOptional.get();
        removeExistingTypesFromDefaultImports(elementTypeEnum, Collections.singletonMap(name, null),
            Collections.singletonList(additionalTypeDefinitionsImport));
        final List<ToscaImportByModel> rebuiltModelImportList = modelImportList.stream()
            .filter(toscaImportByModel -> !ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(toscaImportByModel.getFullPath())))
            .collect(Collectors.toList());
        rebuiltModelImportList.add(additionalTypeDefinitionsImport);
        toscaModelImportCassandraDao.saveAll(modelName, rebuiltModelImportList);
    }

    private String buildPropertyAdditionalTypeDefinitionContent(final ElementTypeEnum elementTypeEnum, final String name,
                                                                final PropertyDefinitionDto property, final Map<String, Object> originalContent,
                                                                boolean isAdd) {
        final Map<String, Object> originalTypeContent = (Map<String, Object>) originalContent.get(elementTypeEnum.getToscaEntryName());
        Map<String, Object> typeContent = (Map<String, Object>) originalTypeContent.get(name);
        Map<String, Object> typeProperties = (Map<String, Object>) typeContent.get(PROPERTIES.getElementName());
        if (MapUtils.isEmpty(typeProperties)) {
            typeProperties = new HashMap<>();
        }
        if (isAdd) {
            typeProperties.put(property.getName(), constructProperty(property));
        } else {
            typeProperties.remove(property.getName());
        }
        typeContent.put(PROPERTIES.getElementName(), typeProperties);
        return new YamlUtil().objectToYaml(originalContent);
    }

    private Map<String, Object> constructProperty(final PropertyDefinitionDto property) {
        Map<String, Object> typeProp = new HashMap<>();
        if (property.getType() != null) {
            typeProp.put(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName(), property.getType());
        }
        if (property.getDescription() != null) {
            typeProp.put(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), property.getDescription());
        }
        Map<String, Object> schema = new HashMap<>();
        if (property.getSchemaType() != null) {
            schema.put(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName(), property.getSchemaType());
            typeProp.put(TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA.getElementName(), schema);
        }
        if (property.getDefaultValue() != null) {
            typeProp.put(TypeUtils.ToscaTagNamesEnum.DEFAULT.getElementName(), property.getDefaultValue());
        }
        if (property.getRequired() != null) {
            typeProp.put(TypeUtils.ToscaTagNamesEnum.REQUIRED.getElementName(), property.getRequired());
        }
        if (property.getConstraints() != null) {
            typeProp.put(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName(), property.getConstraints());
        }
        return typeProp;
    }

}