2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel;
23 import fj.data.Either;
24 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
25 import org.openecomp.sdc.be.model.Resource;
26 import org.openecomp.sdc.be.model.operations.api.IResourceOperation;
27 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
29 import java.util.ArrayDeque;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Queue;
34 import java.util.stream.Collectors;
36 public class NormativesResolver {
38 @javax.annotation.Resource(name = "resource-operation")
39 private IResourceOperation resourceOperation;
43 * @return list of all normatives sorted by neighboring order
45 public Either<List<Resource>, StorageOperationStatus> getAllNodeTypeNormatives() {
46 Either<List<Resource>, StorageOperationStatus> rootNormatives = resourceOperation.getRootResources();
47 return rootNormatives.either(this::getAllNormatives,
52 private Either<List<Resource>, StorageOperationStatus> getAllNormatives(List<Resource> rootResources) {
53 List<Resource> allNormatives = new ArrayList<>();
54 for (Resource rootResource : rootResources) {
55 Either<List<Resource>, StorageOperationStatus> normativesOfRoot = getAllNodeTypeNormatives(rootResource);
56 if (normativesOfRoot.isRight()) {
57 return Either.right(normativesOfRoot.right().value());
59 allNormatives.addAll(normativesOfRoot.left().value());
61 return Either.left(allNormatives);
64 private Either<List<Resource>, StorageOperationStatus> getAllNodeTypeNormatives(Resource root) {
65 List<Resource> normativeResources = new ArrayList<>();
66 Queue<Resource> resources = new ArrayDeque<>();
68 while (!resources.isEmpty()) {
69 Resource currentResource = resources.poll();
70 normativeResources.add(currentResource);
71 Either<List<Resource>, StorageOperationStatus> allDerivedResources = getAllNonVFDerivedResources(currentResource);
72 if (allDerivedResources.isRight()) {
73 return Either.right(allDerivedResources.right().value());
75 List<Resource> derivedResources = allDerivedResources.left().value();
76 replaceDerivedNameWithDerivedUniqueId(currentResource, derivedResources);
77 resources.addAll(derivedResources);
79 return Either.left(normativeResources);
82 private void replaceDerivedNameWithDerivedUniqueId(Resource currentResource, List<Resource> derivedResources) {
83 derivedResources.forEach(resource -> resource.setDerivedFrom(Collections.singletonList(currentResource.getUniqueId())));
86 private Either<List<Resource>, StorageOperationStatus> getAllNonVFDerivedResources(Resource resource) {
87 Either<List<Resource>, StorageOperationStatus> childrenNodes = resourceOperation.getAllDerivedResources(resource);
88 return childrenNodes.either(resourceList -> Either.left(filterNonVFResources(resourceList)),
89 this::resolveEmptyListOrErrorStatus);
92 private List<Resource> filterNonVFResources(List<Resource> resources) {
93 return resources.stream().filter(resource -> resource.getResourceType() != ResourceTypeEnum.VF).collect(Collectors.toList());
96 private Either<List<Resource>, StorageOperationStatus> resolveEmptyListOrErrorStatus(StorageOperationStatus storageOperationStatus) {
97 return storageOperationStatus == StorageOperationStatus.NOT_FOUND ? Either.left(Collections.emptyList()) : Either.right(storageOperationStatus);