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
|
/*
* ============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 fj.data.Either;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
import org.openecomp.sdc.be.datatypes.elements.ArtifactTypeDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.model.ArtifactTypeDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;
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.operations.api.DerivedFromOperation;
import org.openecomp.sdc.be.model.operations.api.IArtifactTypeOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.resources.data.ArtifactTypeData;
import org.openecomp.sdc.be.resources.data.ModelData;
import org.openecomp.sdc.be.resources.data.PropertyData;
import org.openecomp.sdc.be.resources.data.UniqueIdData;
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 java.util.*;
import java.util.stream.Collectors;
/**
* This class is responsible for handling all operations for the TOSCA artifact types
*/
@Component("artifact-type-operation")
public class ArtifactTypeOperation extends AbstractOperation implements IArtifactTypeOperation {
private static final Logger LOGGER = Logger.getLogger(ArtifactTypeOperation.class.getName());
private final PropertyOperation propertyOperation;
private final DerivedFromOperation derivedFromOperation;
private final ModelOperation modelOperation;
@Autowired
public ArtifactTypeOperation(final PropertyOperation propertyOperation,
final DerivedFromOperation derivedFromOperation,
final ModelOperation modelOperation) {
this.propertyOperation = propertyOperation;
this.derivedFromOperation = derivedFromOperation;
this.modelOperation = modelOperation;
}
/**
* Creates a TOSCA artifact types
* @param artifactType the TOSCA artifact types definition to be created
* @return the created TOSCA artifact types definition
*/
@Override
public ArtifactTypeDefinition createArtifactType(final ArtifactTypeDefinition artifactType) {
return createArtifactType(artifactType, false);
}
@Override
public ArtifactTypeDefinition createArtifactType(final ArtifactTypeDefinition artifactType,
final boolean inTransaction) {
Either<ArtifactTypeDefinition, StorageOperationStatus> createNodeResult = null;
try {
artifactType.setUniqueId(UniqueIdBuilder.buildArtifactTypeUid(artifactType.getModel(), artifactType.getType()));
final ArtifactTypeData artifactTypeData = new ArtifactTypeData(artifactType);
final Either<ArtifactTypeData, JanusGraphOperationStatus> existArtifact = janusGraphGenericDao
.getNode(artifactTypeData.getUniqueIdKey(), artifactTypeData.getUniqueId(), ArtifactTypeData.class);
if (!existArtifact.isLeft()) {
LOGGER.debug("Artifact type did not exist, creating new one {}", artifactType);
createNodeResult = janusGraphGenericDao.createNode(artifactTypeData, ArtifactTypeData.class).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus).left()
.map(createdDerivedFrom -> artifactType);
addPropertiesToArtifactType(artifactType);
addModelRelation(artifactType);
addDerivedFromRelation(artifactType);
}
else {
LOGGER.debug("Artifact type {} already exists", artifactType);
createNodeResult = janusGraphGenericDao.updateNode(artifactTypeData, ArtifactTypeData.class).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus).left()
.bind(updatedNode -> updateArtifactProperties(artifactType.getUniqueId(), artifactType.getProperties())).left()
.bind(updatedProperties -> updateArtifactDerivedFrom(artifactType)).left()
.map(updatedDerivedFrom -> artifactType);
}
if (createNodeResult.isRight()) {
final StorageOperationStatus operationStatus = createNodeResult.right().value();
LOGGER.error(EcompLoggerErrorCode.DATA_ERROR,
"Failed to add artifact type {} to graph. Operation status {}", artifactType.getType(), operationStatus);
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed to create artifact type %s with status %s", artifactType.getType(),
operationStatus));
}
return createNodeResult.left().value();
} finally {
if (!inTransaction) {
if (createNodeResult == null || createNodeResult.isRight()) {
LOGGER.debug("Rollback on graph.");
janusGraphGenericDao.rollback();
} else {
janusGraphGenericDao.commit();
}
}
}
}
/**
* Finds all TOSCA artifact types applicable to the given model name
* @param model model name
* @return all the TOSCA artifact types found
*/
@Override
public Map<String, ArtifactTypeDefinition> getAllArtifactTypes(final String model) {
final Either<List<ArtifactTypeData>, JanusGraphOperationStatus> artifactTypes =
janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.ArtifactType, Collections.emptyMap(), model, ArtifactTypeData.class);
if (artifactTypes.isRight()) {
if (JanusGraphOperationStatus.NOT_FOUND == artifactTypes.right().value()) {
return Collections.emptyMap();
}
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed to find artifact on JanusGraph with status %s",
DaoStatusConverter.convertJanusGraphStatusToStorageStatus(artifactTypes.right().value())));
}
final Map<String, ArtifactTypeDefinition> artifactTypeDefinitionTypes = new HashMap<>();
final List<ArtifactTypeDefinition> artifactTypeList = artifactTypes.left().value().stream()
.map(this::convertArtifactDataDefinition)
.filter(artifactTypeDefinition -> artifactTypeDefinition.getUniqueId().equalsIgnoreCase(UniqueIdBuilder
.buildArtifactTypeUid(artifactTypeDefinition.getModel(), artifactTypeDefinition.getType())))
.collect(Collectors.toList());
for (final ArtifactTypeDefinition type : artifactTypeList) {
artifactTypeDefinitionTypes.put(type.getUniqueId(), type);
}
return artifactTypeDefinitionTypes;
}
private Either<Map<String, PropertyData>, StorageOperationStatus> updateArtifactProperties(final String artifactId, final List<PropertyDefinition> properties) {
LOGGER.debug("#updateArtifactProperties - updating artifact type properties for artifact type with id {}", artifactId);
return propertyOperation.deletePropertiesAssociatedToNode(NodeTypeEnum.ArtifactType, artifactId).left()
.bind(deleteProps -> addPropertiesToArtifact(artifactId, properties));
}
private Either<GraphRelation, StorageOperationStatus> updateArtifactDerivedFrom(ArtifactTypeDefinition updatedArtifactType) {
String artifactTypeId = updatedArtifactType.getUniqueId();
Either<ArtifactTypeData, StorageOperationStatus> currentDerivedFrom = derivedFromOperation.getDerivedFromChild(updatedArtifactType.getUniqueId(), NodeTypeEnum.ArtifactType, ArtifactTypeData.class);
LOGGER.debug(
"#updateArtifactDerivedFrom - updating artifact derived from relation for artifact type with id {}. old derived type {}. new derived type {}",
artifactTypeId, currentDerivedFrom.isLeft() ? currentDerivedFrom: "", updatedArtifactType.getDerivedFrom());
if (currentDerivedFrom.isLeft()) {
StorageOperationStatus deleteDerivedRelationStatus = deleteDerivedFromArtifactType(artifactTypeId, currentDerivedFrom.left().value().getArtifactTypeDataDefinition().getType(), updatedArtifactType.getModel());
if (deleteDerivedRelationStatus != StorageOperationStatus.OK) {
return Either.right(deleteDerivedRelationStatus);
}
}
return addDerivedFromRelation(updatedArtifactType, artifactTypeId);
}
private StorageOperationStatus deleteDerivedFromArtifactType(String artifactTypeId, String derivedFromType, String model) {
if (derivedFromType == null) {
return StorageOperationStatus.OK;
}
LOGGER.debug("#deleteDerivedFromArtifactType - deleting derivedFrom relation for artifact type with id {} and its derived type {}", artifactTypeId,
derivedFromType);
return getProjectionLatestArtifactTypeByType(derivedFromType, model).either(
derivedFromNode -> derivedFromOperation.removeDerivedFromRelation(artifactTypeId, derivedFromNode.getUniqueId(), NodeTypeEnum.ArtifactType),
err -> err);
}
private Either<Map<String, PropertyData>, StorageOperationStatus> addPropertiesToArtifact(String artifactId,
List<PropertyDefinition> properties) {
LOGGER.debug("#addPropertiesToArtifact - adding artifact type properties for artifact type with id {}", artifactId);
return propertyOperation.addPropertiesToElementType(artifactId, NodeTypeEnum.ArtifactType, properties).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
}
/**
* Coverts the given artifact type data into a TOSCA artifact types definition
* @param artifactTypeData artifact type data representation
* @return the TOSCA artifact types definition
*/
private ArtifactTypeDefinition convertArtifactDataDefinition(final ArtifactTypeData artifactTypeData) {
LOGGER.debug("The object returned after create tosca artifact type is {}", artifactTypeData);
final ArtifactTypeDefinition artifactType = new ArtifactTypeDefinition(artifactTypeData.getArtifactTypeDataDefinition());
final var modelAssociatedToArtifactOptional = getModelAssociatedToArtifact(artifactTypeData.getUniqueId());
if (!modelAssociatedToArtifactOptional.isEmpty()) {
artifactType.setModel(modelAssociatedToArtifactOptional.get());
}
artifactType.setType(artifactTypeData.getArtifactTypeDataDefinition().getType());
final ArtifactTypeData derivedFromNode = fillDerivedFrom(artifactType);
fillProperties(artifactType, derivedFromNode);
return artifactType;
}
/**
* Finds an artifact type data on JanusGraph based on the given parameters
* @param type the artifact type derived from
* @param model the model name
* @return the optional artifact type data found
*/
private Optional<ArtifactTypeData> getLatestArtifactTypeByType(final String type, final String model) {
final Map<String, Object> mapCriteria = new HashMap<>();
mapCriteria.put(GraphPropertiesDictionary.TYPE.getProperty(), type);
final Either<List<ArtifactTypeData>, JanusGraphOperationStatus> result = janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.ArtifactType,
mapCriteria, model, ArtifactTypeData.class);
if (result.isRight()) {
final JanusGraphOperationStatus status = result.right().value();
if (JanusGraphOperationStatus.NOT_FOUND == status) {
return Optional.empty();
}
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed to find artifact by type on JanusGraph with status %s",
DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)));
}
return Optional.of(result.left().value().get(0));
}
private Either<ArtifactTypeDefinition, StorageOperationStatus> getProjectionLatestArtifactTypeByType(String type, String model) {
Map<String, Object> mapCriteria = new HashMap<>();
mapCriteria.put(GraphPropertiesDictionary.TYPE.getProperty(), type);
return getArtifactTypeByCriteria(type, mapCriteria, model);
}
/**
* Creates the derivedFrom relation for the given TOSCA artifact types
* @param artifactType the TOSCA artifact types definition
*/
private void addDerivedFromRelation(final ArtifactTypeDefinition artifactType) {
final String derivedFrom = artifactType.getDerivedFrom();
final String artifactId = artifactType.getUniqueId();
if (derivedFrom == null || derivedFrom.isEmpty()) {
return;
}
final var getArtifactTypeOptional = getLatestArtifactTypeByType(derivedFrom, artifactType.getModel());
if (getArtifactTypeOptional.isPresent()) {
if (derivedFromOperation.addDerivedFromRelation(artifactId, getArtifactTypeOptional.get().getUniqueId(),
NodeTypeEnum.ArtifactType).isRight()) {
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed creating derivedFrom relation for artifact type %s", artifactType.getType()));
}
}
}
private Either<GraphRelation, StorageOperationStatus> addDerivedFromRelation(ArtifactTypeDefinition artifactType, String atUniqueId) {
String derivedFrom = artifactType.getDerivedFrom();
if (derivedFrom == null) {
return Either.left(null);
}
LOGGER.debug("#addDerivedFromRelationBefore - adding derived from relation between artifact type {} to its parent {}", artifactType.getType(),
derivedFrom);
return this.getProjectionLatestArtifactTypeByType(derivedFrom, artifactType.getModel()).left().bind(
derivedFromArtifact -> derivedFromOperation.addDerivedFromRelation(atUniqueId, derivedFromArtifact.getUniqueId(), NodeTypeEnum.ArtifactType));
}
private Either<ArtifactTypeDefinition, StorageOperationStatus> getArtifactTypeByCriteria(String type, Map<String, Object> properties, String model) {
Either<ArtifactTypeDefinition, StorageOperationStatus> result;
if (type == null || type.isEmpty()) {
LOGGER.error("type is empty");
result = Either.right(StorageOperationStatus.INVALID_ID);
return result;
}
Either<List<ArtifactTypeData>, JanusGraphOperationStatus> eitherArtifactData = janusGraphGenericDao
.getByCriteriaForModel(NodeTypeEnum.ArtifactType, properties, model, ArtifactTypeData.class);
if (eitherArtifactData.isRight()) {
result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(eitherArtifactData.right().value()));
} else {
ArtifactTypeDataDefinition dataDefinition = eitherArtifactData.left().value().stream().map(ArtifactTypeData::getArtifactTypeDataDefinition)
.findFirst().get();
result = getArtifactTypeByUid(dataDefinition.getUniqueId());
}
return result;
}
private Either<ArtifactTypeDefinition, StorageOperationStatus> getArtifactTypeByUid(String uniqueId) {
LOGGER.debug("#getArtifactTypeByUid - fetching artifact type with id {}", uniqueId);
return janusGraphGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ArtifactType), uniqueId, ArtifactTypeData.class).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus).left()
.bind(artifactType -> createArtifactTypeDefinition(uniqueId, artifactType));
}
private Either<ArtifactTypeDefinition, StorageOperationStatus> createArtifactTypeDefinition(String uniqueId, ArtifactTypeData artifactTypeNode) {
ArtifactTypeDefinition artifactType = new ArtifactTypeDefinition(artifactTypeNode.getArtifactTypeDataDefinition());
Optional<String> modelName = getAssociatedModelName(uniqueId);
if (modelName.isPresent()) {
artifactType.setModel(modelName.get());
}
return fillDerivedFrom(uniqueId, artifactType).left().map(derivedFrom -> fillProperties(uniqueId, artifactType, derivedFrom)).left()
.map(props -> artifactType);
}
private Optional<String> getAssociatedModelName(String uniqueId) {
final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> modelName = janusGraphGenericDao.getParentNode(
UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ArtifactType), uniqueId, GraphEdgeLabels.MODEL_ELEMENT,
NodeTypeEnum.Model, ModelData.class);
if (modelName.isRight()) {
return Optional.empty();
}
return Optional.ofNullable(modelName.left().value().getLeft().getName());
}
/**
* Adds a property definition to the given TOSCA artifact types definition
* @param artifactType the TOSCA artifact types
*/
private void addPropertiesToArtifactType(final ArtifactTypeDefinition artifactType) {
final List<PropertyDefinition> properties = artifactType.getProperties();
final Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToArtifactType =
propertyOperation.addPropertiesToElementType(artifactType.getUniqueId(), NodeTypeEnum.ArtifactType, properties);
if (addPropertiesToArtifactType.isRight()) {
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed creating properties for artifact type %s with status %s",
artifactType.getType(), DaoStatusConverter.convertJanusGraphStatusToStorageStatus(addPropertiesToArtifactType.right().value())));
}
}
/**
* Creates an edge between the given TOSCA artifact types and it`s model
* @param artifactType the TOSCA artifact types
*/
private void addModelRelation(final ArtifactTypeDefinition artifactType) {
final String model = artifactType.getModel();
if (StringUtils.isNotEmpty(model)) {
final GraphNode from = new UniqueIdData(NodeTypeEnum.Model, UniqueIdBuilder.buildModelUid(model));
final GraphNode to = new UniqueIdData(NodeTypeEnum.ArtifactType, artifactType.getUniqueId());
LOGGER.info("Connecting model {} to type {}", from, to);
final Either<GraphRelation, JanusGraphOperationStatus> status = janusGraphGenericDao.createRelation(from,
to, GraphEdgeLabels.MODEL_ELEMENT, Collections.emptyMap());
if (status.isRight()) {
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed creating relation between model %s and artifact type %s with status %s",
model, artifactType.getType(), DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status.right().value())));
}
}
}
/**
* Finds a model associated to the given artifact type unique id
* @param uid the TOSCA artifact types unique id
* @return
*/
private Optional<String> getModelAssociatedToArtifact(final String uid) {
final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> model =
janusGraphGenericDao.getParentNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Interface), uid,
GraphEdgeLabels.MODEL_ELEMENT, NodeTypeEnum.Model, ModelData.class);
if (model.isLeft()) {
return Optional.ofNullable(model.left().value().getLeft().getName());
}
return Optional.empty();
}
/**
* Finds the derived from for teh given TOSCA artifact types
* @param artifactType
* @return
*/
private ArtifactTypeData fillDerivedFrom(final ArtifactTypeDefinition artifactType) {
final Either<ArtifactTypeData, StorageOperationStatus> result = derivedFromOperation
.getDerivedFromChild(artifactType.getUniqueId(), NodeTypeEnum.ArtifactType, ArtifactTypeData.class)
.right().bind(this::handleDerivedFromNotExist).left()
.map(derivedFrom -> setDerivedFrom(artifactType, derivedFrom));
if (result.isRight()) {
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed fetching derivedFrom for artifact type %s with status %s",
artifactType.getType(), result.right().value()));
}
return result.left().value();
}
private Either<ArtifactTypeData, StorageOperationStatus> fillDerivedFrom(String uniqueId, ArtifactTypeDefinition artifactType) {
LOGGER.debug("#fillDerivedFrom - fetching artifact type {} derived node", artifactType.getType());
return derivedFromOperation.getDerivedFromChild(uniqueId, NodeTypeEnum.ArtifactType, ArtifactTypeData.class).right()
.bind(this::handleDerivedFromNotExist).left().map(derivedFrom -> setDerivedFrom(artifactType, derivedFrom));
}
private Either<ArtifactTypeData, StorageOperationStatus> handleDerivedFromNotExist(final StorageOperationStatus storageOperationStatus) {
if (storageOperationStatus == StorageOperationStatus.NOT_FOUND) {
return Either.left(null);
}
return Either.right(storageOperationStatus);
}
private ArtifactTypeData setDerivedFrom(final ArtifactTypeDefinition artifactType, final ArtifactTypeData derivedFrom) {
if (derivedFrom != null) {
artifactType.setDerivedFrom(derivedFrom.getArtifactTypeDataDefinition().getType());
}
return derivedFrom;
}
/**
* Finds all properties for the given TOSCA artifact types
* @param artifactType the TOSCA artifact types
* @param derivedFromNode the TOSCA artifact types derived from
*/
private void fillProperties(final ArtifactTypeDefinition artifactType, final ArtifactTypeData derivedFromNode) {
final Either<List<PropertyDefinition>, StorageOperationStatus> result =
propertyOperation.findPropertiesOfNode(NodeTypeEnum.ArtifactType, artifactType.getUniqueId()).right()
.bind(this::handleNoProperties).left().bind(propsMap -> fillDerivedFromProperties(artifactType,
derivedFromNode, new ArrayList<>(propsMap.values())));
if (result.isRight()) {
throw new OperationException(ActionStatus.GENERAL_ERROR,
String.format("Failed fetching properties for artifact type %s with status %s",
artifactType.getType(), result.right().value()));
}
}
private Either<List<PropertyDefinition>, StorageOperationStatus> fillProperties(String uniqueId, ArtifactTypeDefinition artifactType,
ArtifactTypeData derivedFromNode) {
LOGGER.debug("#fillProperties - fetching all properties for artifact type {}", artifactType.getType());
return propertyOperation.findPropertiesOfNode(NodeTypeEnum.ArtifactType, uniqueId).right().bind(this::handleArtifactTypeHasNoProperties).left()
.bind(propsMap -> fillDerivedFromProperties(artifactType, derivedFromNode, new ArrayList<>(propsMap.values())));
}
private Either<Map<String, PropertyDefinition>, StorageOperationStatus> handleArtifactTypeHasNoProperties(JanusGraphOperationStatus err) {
if (err == JanusGraphOperationStatus.NOT_FOUND) {
return Either.left(new HashMap<>());
}
return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(err));
}
private Either<Map<String, PropertyDefinition>, StorageOperationStatus> handleNoProperties(
final JanusGraphOperationStatus janusGraphOperationStatus) {
if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
return Either.left(new HashMap<>());
}
return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphOperationStatus));
}
private Either<List<PropertyDefinition>, StorageOperationStatus> fillDerivedFromProperties(final ArtifactTypeDefinition artifactType,
final ArtifactTypeData derivedFromNode,
final List<PropertyDefinition> artifactTypeProperties)
{
if (derivedFromNode == null) {
artifactType.setProperties(artifactTypeProperties);
return Either.left(artifactTypeProperties);
}
return propertyOperation
.getAllPropertiesRec(derivedFromNode.getUniqueId(), NodeTypeEnum.ArtifactType, ArtifactTypeData.class)
.left().map(derivedFromProps -> {
artifactTypeProperties.addAll(derivedFromProps);
return artifactTypeProperties;
}).left().map(allProps -> {
artifactType.setProperties(allProps);
return allProps;
});
}
/**
* The Model field is an optional entry when uploading a resource. If the field is present, it validates if the Model name exists.
* @param modelName Model names declared on the resource json representation
*/
public void validateModel(final String modelName) {
if (modelOperation.findModelByName(modelName).isEmpty()) {
LOGGER.error(EcompLoggerErrorCode.DATA_ERROR,"Could not find model name {}", modelName);
throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
}
}
}
|