cc9e6ec9726c10bfc4114a5eeab19ebb80b4387c
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / migration / v1707 / jsonmodel / NormativesResolver.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.impl.migration.v1707.jsonmodel;
22
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;
28
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;
35
36 public class NormativesResolver {
37
38     @javax.annotation.Resource(name = "resource-operation")
39     private IResourceOperation resourceOperation;
40
41     /**
42      *
43      * @return list of all normatives sorted by neighboring order
44      */
45     public Either<List<Resource>, StorageOperationStatus> getAllNodeTypeNormatives() {
46         Either<List<Resource>, StorageOperationStatus> rootNormatives = resourceOperation.getRootResources();
47         return rootNormatives.either(this::getAllNormatives,
48                                      Either::right);
49
50     }
51
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());
58             }
59             allNormatives.addAll(normativesOfRoot.left().value());
60         }
61         return Either.left(allNormatives);
62     }
63
64     private Either<List<Resource>, StorageOperationStatus> getAllNodeTypeNormatives(Resource root) {
65         List<Resource> normativeResources = new ArrayList<>();
66         Queue<Resource> resources = new ArrayDeque<>();
67         resources.add(root);
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());
74             }
75             List<Resource> derivedResources = allDerivedResources.left().value();
76             replaceDerivedNameWithDerivedUniqueId(currentResource, derivedResources);
77             resources.addAll(derivedResources);
78         }
79         return Either.left(normativeResources);
80     }
81
82     private void replaceDerivedNameWithDerivedUniqueId(Resource currentResource, List<Resource> derivedResources) {
83         derivedResources.forEach(resource -> resource.setDerivedFrom(Collections.singletonList(currentResource.getUniqueId())));
84     }
85
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);
90     }
91
92     private List<Resource> filterNonVFResources(List<Resource> resources) {
93         return resources.stream().filter(resource -> resource.getResourceType() != ResourceTypeEnum.VF).collect(Collectors.toList());
94     }
95
96     private Either<List<Resource>, StorageOperationStatus> resolveEmptyListOrErrorStatus(StorageOperationStatus storageOperationStatus) {
97         return storageOperationStatus == StorageOperationStatus.NOT_FOUND ? Either.left(Collections.emptyList()) : Either.right(storageOperationStatus);
98     }
99
100
101 }