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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
* ============LICENSE_END=========================================================
* Modifications copyright (c) 2019 Nokia
* ================================================================================
*/
package org.openecomp.sdc.be.components.impl;
import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType;
import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName;
import fj.data.Either;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.servlet.ServletContext;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
import org.openecomp.sdc.be.auditing.impl.AuditingManager;
import org.openecomp.sdc.be.auditing.impl.resourceadmin.AuditImportResourceAdminEventFactory;
import org.openecomp.sdc.be.components.csar.CsarInfo;
import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum;
import org.openecomp.sdc.be.components.impl.ImportUtils.Constants;
import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.impl.WebAppContextWrapper;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.AttributeDefinition;
import org.openecomp.sdc.be.model.CapabilityDefinition;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.RequirementDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.UploadResourceInfo;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.be.resources.data.auditing.model.CommonAuditData;
import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
import org.openecomp.sdc.be.utils.TypeUtils;
import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.ThreadLocalsHolder;
import org.openecomp.sdc.common.util.ValidationUtils;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.yaml.snakeyaml.Yaml;
@Component("resourceImportManager")
public class ResourceImportManager {
static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("[\\w\\-\\_\\d\\:]+");
private static final Logger log = Logger.getLogger(ResourceImportManager.class);
private final InterfaceDefinitionHandler interfaceDefinitionHandler;
private final ComponentsUtils componentsUtils;
private final CapabilityTypeOperation capabilityTypeOperation;
private ServletContext servletContext;
private AuditingManager auditingManager;
private ResourceBusinessLogic resourceBusinessLogic;
@Autowired
private ServiceBusinessLogic serviceBusinessLogic;
private IGraphLockOperation graphLockOperation;
private ToscaOperationFacade toscaOperationFacade;
private ResponseFormatManager responseFormatManager;
@Autowired
public ResourceImportManager(ComponentsUtils componentsUtils, CapabilityTypeOperation capabilityTypeOperation,
final InterfaceDefinitionHandler interfaceDefinitionHandler) {
this.componentsUtils = componentsUtils;
this.capabilityTypeOperation = capabilityTypeOperation;
this.interfaceDefinitionHandler = interfaceDefinitionHandler;
}
public ServiceBusinessLogic getServiceBusinessLogic() {
return serviceBusinessLogic;
}
public void setServiceBusinessLogic(ServiceBusinessLogic serviceBusinessLogic) {
this.serviceBusinessLogic = serviceBusinessLogic;
}
@Autowired
public void setToscaOperationFacade(ToscaOperationFacade toscaOperationFacade) {
this.toscaOperationFacade = toscaOperationFacade;
}
public ImmutablePair<Resource, ActionStatus> importNormativeResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator,
boolean createNewVersion, boolean needLock) {
LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
lifecycleChangeInfo.setUserRemarks("certification on import");
Function<Resource, Boolean> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock,
null, null, false, null, null, false);
}
public ImmutablePair<Resource, ActionStatus> importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData,
User creator, boolean createNewVersion, boolean needLock) {
LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction();
lifecycleChangeInfo.setUserRemarks("certification on import");
Function<Resource, Boolean> validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource);
return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock,
null, null, false, null, null, false);
}
public ImmutablePair<Resource, ActionStatus> importCertifiedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator,
Function<Resource, Boolean> validationFunction,
LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean isInTransaction,
boolean createNewVersion, boolean needLock,
Map<ArtifactOperationEnum, List<ArtifactDefinition>> nodeTypeArtifactsToHandle,
List<ArtifactDefinition> nodeTypesNewCreatedArtifacts,
boolean forceCertificationAllowed, CsarInfo csarInfo, String nodeName,
boolean isNested) {
Resource resource = new Resource();
ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
Either<ImmutablePair<Resource, ActionStatus>, ResponseFormat> response = Either.left(responsePair);
String latestCertifiedResourceId = null;
try {
boolean shouldBeCertified = nodeTypeArtifactsToHandle == null || nodeTypeArtifactsToHandle.isEmpty();
setConstantMetaData(resource, shouldBeCertified);
setMetaDataFromJson(resourceMetaData, resource);
populateResourceFromYaml(resourceYml, resource);
validationFunction.apply(resource);
checkResourceExistsBeforeCreate(createNewVersion, csarInfo, resource);
resource = resourceBusinessLogic
.createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo, nodeName, isNested).left;
Resource changeStateResponse;
if (nodeTypeArtifactsToHandle != null && !nodeTypeArtifactsToHandle.isEmpty()) {
Either<List<ArtifactDefinition>, ResponseFormat> handleNodeTypeArtifactsRes = resourceBusinessLogic
.handleNodeTypeArtifacts(resource, nodeTypeArtifactsToHandle, nodeTypesNewCreatedArtifacts, creator, isInTransaction, false);
if (handleNodeTypeArtifactsRes.isRight()) {
//TODO: should be used more correct action
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
}
}
latestCertifiedResourceId = getLatestCertifiedResourceId(resource);
changeStateResponse = resourceBusinessLogic
.propagateStateToCertified(creator, resource, lifecycleChangeInfo, isInTransaction, needLock, forceCertificationAllowed);
responsePair = new ImmutablePair<>(changeStateResponse, response.left().value().right);
} catch (RuntimeException e) {
handleImportResourceException(resourceMetaData, creator, true, e);
} finally {
if (latestCertifiedResourceId != null && needLock) {
log.debug("unlock resource {}", latestCertifiedResourceId);
graphLockOperation.unlockComponent(latestCertifiedResourceId, NodeTypeEnum.Resource);
}
}
return responsePair;
}
private void checkResourceExistsBeforeCreate(final boolean createNewVersion, final CsarInfo csarInfo, final Resource resource) {
final String resourceName = resource.getName();
final String model = resource.getModel();
final Either<Resource, StorageOperationStatus> latestByToscaName = toscaOperationFacade
.getLatestByToscaResourceNameAndModel(resourceName, model);
if (latestByToscaName.isLeft()) {
final Resource foundResource = latestByToscaName.left().value();
validateComponentWithModelExist(resourceName, model, foundResource);
if (!createNewVersion) {
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, resourceName);
}
if (!isCsarPresent(csarInfo)) {
final Either<Resource, StorageOperationStatus> component = toscaOperationFacade
.getComponentByNameAndVendorRelease(resource.getComponentType(), resource.getName(), resource.getVendorRelease(),
JsonParseFlagEnum.ParseAll);
if (component.isLeft()) {
validateComponentWithModelExist(resourceName, model, foundResource);
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_VERSION_ALREADY_EXIST, resource.getName());
}
}
}
}
private void validateComponentWithModelExist(final String resourceName, final String model, final Resource foundResource) {
if (model != null && toscaOperationFacade.isNodeAssociatedToModel(model, foundResource).isPresent()) {
throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_MODEL_ALREADY_EXIST, resourceName, model);
}
}
private boolean isCsarPresent(final CsarInfo csarInfo) {
return csarInfo != null && StringUtils.isNotEmpty(csarInfo.getCsarUUID());
}
private String getLatestCertifiedResourceId(Resource resource) {
Map<String, String> allVersions = resource.getAllVersions();
Double latestCertifiedVersion = 0.0;
if (allVersions != null) {
for (String version : allVersions.keySet()) {
Double dVersion = Double.valueOf(version);
if ((dVersion > latestCertifiedVersion) && (version.endsWith(".0"))) {
latestCertifiedVersion = dVersion;
}
}
return allVersions.get(String.valueOf(latestCertifiedVersion));
} else {
return null;
}
}
public void populateResourceMetadata(UploadResourceInfo resourceMetaData, Resource resource) {
if (resource != null && resourceMetaData != null) {
resource.setDescription(resourceMetaData.getDescription());
resource.setTags(resourceMetaData.getTags());
resource.setCategories(resourceMetaData.getCategories());
resource.setContactId(resourceMetaData.getContactId());
resource.setName(resourceMetaData.getName());
resource.setIcon(resourceMetaData.getResourceIconPath());
resource.setResourceVendorModelNumber(resourceMetaData.getResourceVendorModelNumber());
resource.setResourceType(ResourceTypeEnum.valueOf(resourceMetaData.getResourceType()));
if (resourceMetaData.getVendorName() != null) {
resource.setVendorName(resourceMetaData.getVendorName());
}
if (resourceMetaData.getVendorRelease() != null) {
resource.setVendorRelease(resourceMetaData.getVendorRelease());
}
if (resourceMetaData.getModel() != null) {
resource.setModel(resourceMetaData.getModel());
}
}
}
public ImmutablePair<Resource, ActionStatus> importUserDefinedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator,
boolean isInTransaction) {
Resource resource = new Resource();
ImmutablePair<Resource, ActionStatus> responsePair = new ImmutablePair<>(resource, ActionStatus.CREATED);
try {
setMetaDataFromJson(resourceMetaData, resource);
populateResourceFromYaml(resourceYml, resource);
// currently import VF isn't supported. In future will be supported
// import VF only with CSAR file!!
if (ResourceTypeEnum.VF == resource.getResourceType()) {
log.debug("Now import VF isn't supported. It will be supported in future with CSAR file only");
throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION);
}
resourceBusinessLogic.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE);
Boolean validatePropertiesTypes = resourceBusinessLogic.validatePropertiesDefaultValues(resource);
responsePair = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, false, isInTransaction, true, null, null, false);
} catch (RuntimeException e) {
handleImportResourceException(resourceMetaData, creator, false, e);
}
return responsePair;
}
private void populateResourceFromYaml(String resourceYml, Resource resource) {
@SuppressWarnings("unchecked") Object ymlObj = new Yaml().load(resourceYml);
if (ymlObj instanceof Map) {
Map<String, Object> toscaJsonAll = (Map<String, Object>) ymlObj;
Map<String, Object> toscaJson = toscaJsonAll;
// Checks if exist and builds the node_types map
if (toscaJsonAll.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName())
&& resource.getResourceType() != ResourceTypeEnum.CVFC) {
toscaJson = new HashMap<>();
toscaJson.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(),
toscaJsonAll.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName()));
}
final List<Object> foundElements = new ArrayList<>();
final Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils
.findToscaElements(toscaJsonAll, ToscaTagNamesEnum.DATA_TYPES.getElementName(), ToscaElementTypeEnum.MAP, foundElements);
if (toscaElements.isLeft()) {
final Map<String, Object> toscaAttributes = (Map<String, Object>) foundElements.get(0);
if (MapUtils.isNotEmpty(toscaAttributes)) {
resource.setDataTypes(extractDataTypeFromJson(resourceBusinessLogic, toscaAttributes));
}
}
// Derived From
Resource parentResource = setDerivedFrom(toscaJson, resource);
if (StringUtils.isEmpty(resource.getToscaResourceName())) {
setToscaResourceName(toscaJson, resource);
}
setCapabilities(toscaJson, resource, parentResource);
setProperties(toscaJson, resource);
setAttributes(toscaJson, resource);
setRequirements(toscaJson, resource, parentResource);
setInterfaceLifecycle(toscaJson, resource);
} else {
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
}
}
private void setToscaResourceName(Map<String, Object> toscaJson, Resource resource) {
Either<Map<String, Object>, ResultStatusEnum> toscaElement = ImportUtils
.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
if (toscaElement.isLeft() || toscaElement.left().value().size() == 1) {
String toscaResourceName = toscaElement.left().value().keySet().iterator().next();
resource.setToscaResourceName(toscaResourceName);
}
}
private void setInterfaceLifecycle(Map<String, Object> toscaJson, Resource resource) {
Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = ImportUtils
.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.INTERFACES);
if (toscaInterfaces.isLeft()) {
Map<String, Object> jsonInterfaces = toscaInterfaces.left().value();
Map<String, InterfaceDefinition> moduleInterfaces = new HashMap<>();
for (final Entry<String, Object> interfaceNameValue : jsonInterfaces.entrySet()) {
final Either<InterfaceDefinition, ResultStatusEnum> eitherInterface = createModuleInterface(interfaceNameValue.getValue());
if (eitherInterface.isRight()) {
log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName());
} else {
final InterfaceDefinition interfaceDefinition = eitherInterface.left().value();
moduleInterfaces.put(interfaceNameValue.getKey(), interfaceDefinition);
}
}
if (!moduleInterfaces.isEmpty()) {
resource.setInterfaces(moduleInterfaces);
}
}
}
private Either<InterfaceDefinition, ResultStatusEnum> createModuleInterface(final Object interfaceJson) {
try {
if (interfaceJson instanceof String) {
final InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
interfaceDefinition.setType((String) interfaceJson);
return Either.left(interfaceDefinition);
}
if (interfaceJson instanceof Map) {
final Map<String, Object> interfaceJsonMap = (Map<String, Object>) interfaceJson;
final InterfaceDefinition interfaceDefinition = interfaceDefinitionHandler.create(interfaceJsonMap);
return Either.left(interfaceDefinition);
}
return Either.right(ResultStatusEnum.GENERAL_ERROR);
} catch (final Exception e) {
BeEcompErrorManager.getInstance().logBeSystemError("Import Resource- create interface");
log.debug("error when creating interface, message:{}", e.getMessage(), e);
return Either.right(ResultStatusEnum.GENERAL_ERROR);
}
}
private void setRequirements(Map<String, Object> toscaJson, Resource resource,
Resource parentResource) {// Note that parentResource can be null
Either<List<Object>, ResultStatusEnum> toscaRequirements = ImportUtils
.findFirstToscaListElement(toscaJson, TypeUtils.ToscaTagNamesEnum.REQUIREMENTS);
if (toscaRequirements.isLeft()) {
List<Object> jsonRequirements = toscaRequirements.left().value();
Map<String, List<RequirementDefinition>> moduleRequirements = new HashMap<>();
// Checking for name duplication
Set<String> reqNames = new HashSet<>();
// Getting flattened list of capabilities of parent node - cap name
// to cap type
Map<String, String> reqName2TypeMap = getReqName2Type(parentResource);
for (Object jsonRequirementObj : jsonRequirements) {
// Requirement
Map<String, Object> requirementJsonWrapper = (Map<String, Object>) jsonRequirementObj;
String requirementName = requirementJsonWrapper.keySet().iterator().next();
String reqNameLowerCase = requirementName.toLowerCase();
if (reqNames.contains(reqNameLowerCase)) {
log.debug("More than one requirement with same name {} (case-insensitive) in imported TOSCA file is invalid", reqNameLowerCase);
throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "requirement", reqNameLowerCase);
}
reqNames.add(reqNameLowerCase);
RequirementDefinition requirementDef = createRequirementFromImportFile(requirementJsonWrapper.get(requirementName));
requirementDef.setName(requirementName);
if (moduleRequirements.containsKey(requirementDef.getCapability())) {
moduleRequirements.get(requirementDef.getCapability()).add(requirementDef);
} else {
List<RequirementDefinition> list = new ArrayList<>();
list.add(requirementDef);
moduleRequirements.put(requirementDef.getCapability(), list);
}
// Validating against req/cap of "derived from" node
Boolean validateVsParentCap = validateCapNameVsDerived(reqName2TypeMap, requirementDef.getCapability(), requirementDef.getName());
if (!validateVsParentCap) {
String parentResourceName = parentResource != null ? parentResource.getName() : "";
log.debug("Requirement with name {} already exists in parent {}", requirementDef.getName(), parentResourceName);
throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "requirement",
requirementDef.getName().toLowerCase(), parentResourceName);
}
}
if (moduleRequirements.size() > 0) {
resource.setRequirements(moduleRequirements);
}
}
}
private RequirementDefinition createRequirementFromImportFile(Object requirementJson) {
RequirementDefinition requirement = new RequirementDefinition();
if (requirementJson instanceof String) {
String requirementJsonString = (String) requirementJson;
requirement.setCapability(requirementJsonString);
} else if (requirementJson instanceof Map) {
Map<String, Object> requirementJsonMap = (Map<String, Object>) requirementJson;
if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName())) {
requirement.setCapability((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.CAPABILITY.getElementName()));
}
if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE.getElementName())) {
requirement.setNode((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE.getElementName()));
}
if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName())) {
requirement.setRelationship((String) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.RELATIONSHIP.getElementName()));
}
if (requirementJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
List<Object> occurrencesList = (List) requirementJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
validateOccurrences(occurrencesList);
requirement.setMinOccurrences(occurrencesList.get(0).toString());
requirement.setMaxOccurrences(occurrencesList.get(1).toString());
}
} else {
throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML);
}
return requirement;
}
private void setProperties(Map<String, Object> toscaJson, Resource resource) {
Map<String, Object> reducedToscaJson = new HashMap<>(toscaJson);
ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities");
Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson);
if (properties.isLeft()) {
List<PropertyDefinition> propertiesList = new ArrayList<>();
Map<String, PropertyDefinition> value = properties.left().value();
if (value != null) {
for (Entry<String, PropertyDefinition> entry : value.entrySet()) {
String name = entry.getKey();
if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
log.debug("The property with invalid name {} occured upon import resource {}. ", name, resource.getName());
throw new ByActionStatusComponentException(
componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY));
}
PropertyDefinition propertyDefinition = entry.getValue();
propertyDefinition.setName(name);
propertiesList.add(propertyDefinition);
}
}
resource.setProperties(propertiesList);
} else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) {
throw new ByActionStatusComponentException(
componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY));
}
}
private void setAttributes(final Map<String, Object> originalToscaJsonMap, final Resource resource) {
final Map<String, Object> toscaJsonMap = new HashMap<>(originalToscaJsonMap);
ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities");
final Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributeEither = ImportUtils.getAttributes(toscaJsonMap);
if (getAttributeEither.isRight()) {
final ResultStatusEnum resultStatus = getAttributeEither.right().value();
if (resultStatus == ResultStatusEnum.ELEMENT_NOT_FOUND) {
return;
}
throw new ByActionStatusComponentException(componentsUtils.convertFromResultStatusEnum(resultStatus, JsonPresentationFields.ATTRIBUTES));
}
final List<AttributeDefinition> attributeDefinitionList = new ArrayList<>();
final Map<String, AttributeDefinition> attributeMap = getAttributeEither.left().value();
if (MapUtils.isEmpty(attributeMap)) {
return;
}
for (final Entry<String, AttributeDefinition> entry : attributeMap.entrySet()) {
final String name = entry.getKey();
if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) {
log.debug("Detected attribute with invalid name '{}' during resource '{}' import. ", name, resource.getName());
throw new ByActionStatusComponentException(
componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_ATTRIBUTE_NAME, JsonPresentationFields.ATTRIBUTES));
}
final AttributeDefinition attributeDefinition = entry.getValue();
attributeDefinition.setName(name);
attributeDefinitionList.add(attributeDefinition);
}
resource.setAttributes(attributeDefinitionList);
}
private Resource setDerivedFrom(Map<String, Object> toscaJson, Resource resource) {
Either<String, ResultStatusEnum> toscaDerivedFromElement = ImportUtils
.findFirstToscaStringElement(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM);
Resource derivedFromResource = null;
if (toscaDerivedFromElement.isLeft()) {
String derivedFrom = toscaDerivedFromElement.left().value();
log.debug("Derived from TOSCA name is {}", derivedFrom);
resource.setDerivedFrom(Arrays.asList(new String[]{derivedFrom}));
Either<Resource, StorageOperationStatus> latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom);
if (latestByToscaResourceName.isRight()) {
StorageOperationStatus operationStatus = latestByToscaResourceName.right().value();
if (operationStatus == StorageOperationStatus.NOT_FOUND) {
operationStatus = StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND;
}
log.debug("Error when fetching parent resource {}, error: {}", derivedFrom, operationStatus);
ActionStatus convertFromStorageResponse = componentsUtils.convertFromStorageResponse(operationStatus);
BeEcompErrorManager.getInstance().logBeComponentMissingError("Import TOSCA YAML", "resource", derivedFrom);
throw new ByActionStatusComponentException(convertFromStorageResponse, derivedFrom);
}
derivedFromResource = latestByToscaResourceName.left().value();
}
return derivedFromResource;
}
private void setCapabilities(Map<String, Object> toscaJson, Resource resource,
Resource parentResource) {// Note that parentResource can be null
Either<Map<String, Object>, ResultStatusEnum> toscaCapabilities = ImportUtils
.findFirstToscaMapElement(toscaJson, TypeUtils.ToscaTagNamesEnum.CAPABILITIES);
if (toscaCapabilities.isLeft()) {
Map<String, Object> jsonCapabilities = toscaCapabilities.left().value();
Map<String, List<CapabilityDefinition>> moduleCapabilities = new HashMap<>();
Iterator<Entry<String, Object>> capabilitiesNameValue = jsonCapabilities.entrySet().iterator();
Set<String> capNames = new HashSet<>();
// Getting flattened list of capabilities of parent node - cap name
// to cap type
Map<String, String> capName2TypeMap = getCapName2Type(parentResource);
while (capabilitiesNameValue.hasNext()) {
Entry<String, Object> capabilityNameValue = capabilitiesNameValue.next();
// Validating that no req/cap duplicates exist in imported YAML
String capNameLowerCase = capabilityNameValue.getKey().toLowerCase();
if (capNames.contains(capNameLowerCase)) {
log.debug("More than one capability with same name {} (case-insensitive) in imported TOSCA file is invalid", capNameLowerCase);
throw new ByActionStatusComponentException(ActionStatus.IMPORT_DUPLICATE_REQ_CAP_NAME, "capability", capNameLowerCase);
}
capNames.add(capNameLowerCase);
CapabilityDefinition capabilityDef = createCapabilityFromImportFile(capabilityNameValue.getValue());
capabilityDef.setName(capabilityNameValue.getKey());
if (moduleCapabilities.containsKey(capabilityDef.getType())) {
moduleCapabilities.get(capabilityDef.getType()).add(capabilityDef);
} else {
List<CapabilityDefinition> list = new ArrayList<>();
list.add(capabilityDef);
moduleCapabilities.put(capabilityDef.getType(), list);
}
// Validating against req/cap of "derived from" node
Boolean validateVsParentCap = validateCapNameVsDerived(capName2TypeMap, capabilityDef.getType(), capabilityDef.getName());
if (!validateVsParentCap) {
// Here parentResource is for sure not null, so it's
// null-safe
// Check added to avoid sonar warning
String parentResourceName = parentResource != null ? parentResource.getName() : "";
log.debug("Capability with name {} already exists in parent {}", capabilityDef.getName(), parentResourceName);
throw new ByActionStatusComponentException(ActionStatus.IMPORT_REQ_CAP_NAME_EXISTS_IN_DERIVED, "capability",
capabilityDef.getName().toLowerCase(), parentResourceName);
}
}
if (moduleCapabilities.size() > 0) {
resource.setCapabilities(moduleCapabilities);
}
}
}
private Map<String, String> getCapName2Type(Resource parentResource) {
Map<String, String> capName2type = new HashMap<>();
if (parentResource != null) {
Map<String, List<CapabilityDefinition>> capabilities = parentResource.getCapabilities();
if (capabilities != null) {
for (List<CapabilityDefinition> capDefinitions : capabilities.values()) {
for (CapabilityDefinition capDefinition : capDefinitions) {
String nameLowerCase = capDefinition.getName().toLowerCase();
if (capName2type.get(nameLowerCase) != null) {
String parentResourceName = parentResource.getName();
log.debug("Resource with name {} has more than one capability with name {}, ignoring case", parentResourceName,
nameLowerCase);
BeEcompErrorManager.getInstance().logInternalDataError("Import resource",
"Parent resource " + parentResourceName + " of imported resource has one or more capabilities with name "
+ nameLowerCase, ErrorSeverity.ERROR);
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
}
capName2type.put(nameLowerCase, capDefinition.getType());
}
}
}
}
return capName2type;
}
private Map<String, String> getReqName2Type(Resource parentResource) {
Map<String, String> reqName2type = new HashMap<>();
if (parentResource != null) {
Map<String, List<RequirementDefinition>> requirements = parentResource.getRequirements();
if (requirements != null) {
for (List<RequirementDefinition> reqDefinitions : requirements.values()) {
for (RequirementDefinition reqDefinition : reqDefinitions) {
String nameLowerCase = reqDefinition.getName().toLowerCase();
if (reqName2type.get(nameLowerCase) != null) {
String parentResourceName = parentResource.getName();
log.debug("Resource with name {} has more than one requirement with name {}, ignoring case", parentResourceName,
nameLowerCase);
BeEcompErrorManager.getInstance().logInternalDataError("Import resource",
"Parent resource " + parentResourceName + " of imported resource has one or more requirements with name "
+ nameLowerCase, ErrorSeverity.ERROR);
throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
}
reqName2type.put(nameLowerCase, reqDefinition.getCapability());
}
}
}
}
return reqName2type;
}
private Boolean validateCapNameVsDerived(Map<String, String> parentCapName2Type, String childCapabilityType, String reqCapName) {
String capNameLowerCase = reqCapName.toLowerCase();
log.trace("Validating capability {} vs parent resource", capNameLowerCase);
String parentCapType = parentCapName2Type.get(capNameLowerCase);
if (parentCapType != null) {
if (childCapabilityType.equals(parentCapType)) {
log.debug("Capability with name {} is of same type {} for imported resource and its parent - this is OK", capNameLowerCase,
childCapabilityType);
return true;
}
Either<Boolean, StorageOperationStatus> capabilityTypeDerivedFrom = capabilityTypeOperation
.isCapabilityTypeDerivedFrom(childCapabilityType, parentCapType);
if (capabilityTypeDerivedFrom.isRight()) {
log.debug("Couldn't check whether imported resource capability derives from its parent's capability");
throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom.right().value()));
}
return capabilityTypeDerivedFrom.left().value();
}
return true;
}
private CapabilityDefinition createCapabilityFromImportFile(Object capabilityJson) {
CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
if (capabilityJson instanceof String) {
String capabilityJsonString = (String) capabilityJson;
capabilityDefinition.setType(capabilityJsonString);
} else if (capabilityJson instanceof Map) {
Map<String, Object> capabilityJsonMap = (Map<String, Object>) capabilityJson;
// Type
if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) {
capabilityDefinition.setType((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName()));
}
// ValidSourceTypes
if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) {
capabilityDefinition
.setValidSourceTypes((List<String>) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName()));
}
// ValidSourceTypes
if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName())) {
capabilityDefinition.setDescription((String) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName()));
}
if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
List<Object> occurrencesList = (List) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
validateOccurrences(occurrencesList);
capabilityDefinition.setMinOccurrences(occurrencesList.get(0).toString());
capabilityDefinition.setMaxOccurrences(occurrencesList.get(1).toString());
}
if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.PROPERTIES.getElementName())) {
Either<Map<String, PropertyDefinition>, ResultStatusEnum> propertiesRes = ImportUtils.getProperties(capabilityJsonMap);
if (propertiesRes.isRight()) {
throw new ByActionStatusComponentException(ActionStatus.PROPERTY_NOT_FOUND);
} else {
propertiesRes.left().value().entrySet().stream().forEach(e -> e.getValue().setName(e.getKey().toLowerCase()));
List<ComponentInstanceProperty> capabilityProperties = propertiesRes.left().value().values().stream()
.map(p -> new ComponentInstanceProperty(p, p.getDefaultValue(), null)).collect(Collectors.toList());
capabilityDefinition.setProperties(capabilityProperties);
}
}
} else if (!(capabilityJson instanceof List)) {
throw new ByActionStatusComponentException(ActionStatus.INVALID_YAML);
}
return capabilityDefinition;
}
private void handleImportResourceException(UploadResourceInfo resourceMetaData, User user, boolean isNormative, RuntimeException e) {
ResponseFormat responseFormat;
ComponentException newException;
if (e instanceof ComponentException) {
ComponentException componentException = (ComponentException) e;
responseFormat = componentException.getResponseFormat();
if (responseFormat == null) {
responseFormat = getResponseFormatManager().getResponseFormat(componentException.getActionStatus(), componentException.getParams());
}
newException = componentException;
} else {
responseFormat = getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR);
newException = new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
}
String payloadName = (resourceMetaData != null) ? resourceMetaData.getPayloadName() : "";
BeEcompErrorManager.getInstance().logBeSystemError("Import Resource " + payloadName);
log.debug("Error when importing resource from payload:{} Exception text: {}", payloadName, e.getMessage(), e);
auditErrorImport(resourceMetaData, user, responseFormat, isNormative);
throw newException;
}
private void auditErrorImport(UploadResourceInfo resourceMetaData, User user, ResponseFormat errorResponseWrapper, boolean isNormative) {
String version, lifeCycleState;
if (isNormative) {
version = TypeUtils.getFirstCertifiedVersionVersion();
lifeCycleState = LifecycleStateEnum.CERTIFIED.name();
} else {
version = "";
lifeCycleState = LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT.name();
}
String message = "";
if (errorResponseWrapper.getMessageId() != null) {
message = errorResponseWrapper.getMessageId() + ": ";
}
message += errorResponseWrapper.getFormattedMessage();
AuditEventFactory factory = new AuditImportResourceAdminEventFactory(
CommonAuditData.newBuilder().status(errorResponseWrapper.getStatus()).description(message).requestId(ThreadLocalsHolder.getUuid())
.build(), new ResourceCommonInfo(resourceMetaData.getName(), ComponentTypeEnum.RESOURCE.getValue()),
ResourceVersionInfo.newBuilder().state(lifeCycleState).version(version).build(),
ResourceVersionInfo.newBuilder().state("").version("").build(), "", user, "");
getAuditingManager().auditEvent(factory);
}
private void setMetaDataFromJson(final UploadResourceInfo resourceMetaData, final Resource resource) {
this.populateResourceMetadata(resourceMetaData, resource);
resource.setCreatorUserId(resourceMetaData.getContactId());
final String payloadData = resourceMetaData.getPayloadData();
if (payloadData != null) {
resource.setToscaVersion(getToscaVersion(payloadData));
}
final List<CategoryDefinition> categories = resourceMetaData.getCategories();
calculateResourceIsAbstract(resource, categories);
}
private Map<String, Object> decodePayload(final String payloadData) {
final String decodedPayload = new String(Base64.decodeBase64(payloadData));
return (Map<String, Object>) new Yaml().load(decodedPayload);
}
private String getToscaVersion(final String payloadData) {
final Map<String, Object> mappedToscaTemplate = decodePayload(payloadData);
final Either<String, ResultStatusEnum> findFirstToscaStringElement = ImportUtils
.findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION);
if (findFirstToscaStringElement.isLeft()) {
return findFirstToscaStringElement.left().value();
} else {
return null;
}
}
private void calculateResourceIsAbstract(Resource resource, List<CategoryDefinition> categories) {
if (categories != null && !categories.isEmpty()) {
CategoryDefinition categoryDef = categories.get(0);
resource.setAbstract(false);
if (categoryDef != null && categoryDef.getName() != null && categoryDef.getName().equals(Constants.ABSTRACT_CATEGORY_NAME)) {
SubCategoryDefinition subCategoryDef = categoryDef.getSubcategories().get(0);
if (subCategoryDef != null && subCategoryDef.getName().equals(Constants.ABSTRACT_SUBCATEGORY)) {
resource.setAbstract(true);
}
}
}
}
private void setConstantMetaData(Resource resource, boolean shouldBeCertified) {
String version;
LifecycleStateEnum state;
if (shouldBeCertified) {
version = TypeUtils.getFirstCertifiedVersionVersion();
state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE;
} else {
version = ImportUtils.Constants.FIRST_NON_CERTIFIED_VERSION;
state = ImportUtils.Constants.NORMATIVE_TYPE_LIFE_CYCLE_NOT_CERTIFIED_CHECKOUT;
}
resource.setVersion(version);
resource.setLifecycleState(state);
resource.setHighestVersion(ImportUtils.Constants.NORMATIVE_TYPE_HIGHEST_VERSION);
resource.setVendorName(ImportUtils.Constants.VENDOR_NAME);
resource.setVendorRelease(ImportUtils.Constants.VENDOR_RELEASE);
}
private void validateOccurrences(List<Object> occurrensesList) {
if (!ValidationUtils.validateListNotEmpty(occurrensesList)) {
log.debug("Occurrenses list empty");
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
if (occurrensesList.size() < 2) {
log.debug("Occurrenses list size not 2");
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
Object minObj = occurrensesList.get(0);
Object maxObj = occurrensesList.get(1);
Integer minOccurrences;
Integer maxOccurrences;
if (minObj instanceof Integer) {
minOccurrences = (Integer) minObj;
} else {
log.debug("Invalid occurrenses format. low_bound occurrense must be Integer {}", minObj);
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
if (minOccurrences < 0) {
log.debug("Invalid occurrenses format.low_bound occurrense negative {}", minOccurrences);
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
if (maxObj instanceof String) {
if (!"UNBOUNDED".equals(maxObj)) {
log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
} else {
if (maxObj instanceof Integer) {
maxOccurrences = (Integer) maxObj;
} else {
log.debug("Invalid occurrenses format. Max occurrence is {}", maxObj);
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
if (maxOccurrences <= 0 || maxOccurrences < minOccurrences) {
log.debug("Invalid occurrenses format. min occurrence is {}, Max occurrence is {}", minOccurrences, maxOccurrences);
throw new ByActionStatusComponentException(ActionStatus.INVALID_OCCURRENCES);
}
}
}
public synchronized void init(ServletContext servletContext) {
if (this.servletContext == null) {
this.servletContext = servletContext;
responseFormatManager = ResponseFormatManager.getInstance();
resourceBusinessLogic = getResourceBL(servletContext);
}
}
public boolean isResourceExist(String resourceName) {
return resourceBusinessLogic.isResourceExist(resourceName);
}
private ResourceBusinessLogic getResourceBL(ServletContext context) {
WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context
.getAttribute(org.openecomp.sdc.common.api.Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
return webApplicationContext.getBean(ResourceBusinessLogic.class);
}
public ServletContext getServletContext() {
return servletContext;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
public AuditingManager getAuditingManager() {
return auditingManager;
}
@Autowired
public void setAuditingManager(AuditingManager auditingManager) {
this.auditingManager = auditingManager;
}
public ResponseFormatManager getResponseFormatManager() {
return responseFormatManager;
}
public void setResponseFormatManager(ResponseFormatManager responseFormatManager) {
this.responseFormatManager = responseFormatManager;
}
public ResourceBusinessLogic getResourceBusinessLogic() {
return resourceBusinessLogic;
}
@Autowired
public void setResourceBusinessLogic(ResourceBusinessLogic resourceBusinessLogic) {
this.resourceBusinessLogic = resourceBusinessLogic;
}
public IGraphLockOperation getGraphLockOperation() {
return graphLockOperation;
}
@Autowired
public void setGraphLockOperation(IGraphLockOperation graphLockOperation) {
this.graphLockOperation = graphLockOperation;
}
private List<DataTypeDefinition> extractDataTypeFromJson(final ResourceBusinessLogic resourceBusinessLogic,
final Map<String, Object> foundElements) {
final List<DataTypeDefinition> dataTypeDefinitionList = new ArrayList<>();
if (MapUtils.isNotEmpty(foundElements)) {
final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheAll = resourceBusinessLogic.dataTypeCache.getAll();
if (dataTypeCacheAll.isLeft()) {
for (final Entry<String, Object> attributeNameValue : foundElements.entrySet()) {
final Object value = attributeNameValue.getValue();
if (value instanceof Map) {
final DataTypeDefinition dataTypeDefinition = createDataTypeDefinitionWithName(attributeNameValue);
final DataTypeDefinition dataTypeDefinitionParent = dataTypeCacheAll.left().value()
.get(dataTypeDefinition.getDerivedFromName());
dataTypeDefinition.setDerivedFrom(dataTypeDefinitionParent);
dataTypeDefinitionList.add(dataTypeDefinition);
} else {
dataTypeDefinitionList.add(createDataType(String.valueOf(value)));
}
}
}
}
return dataTypeDefinitionList;
}
}
|