e26a01e1b4beaa11886d12f6c463aff99403886e
[sdc.git] /
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 org.openecomp.sdc.be.model.Component;
24 import org.openecomp.sdc.be.model.operations.migration.MigrationMalformedDataLogger;
25
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Optional;
29
30 public class InvariantUUIDResolver <T extends Component> {
31
32     public String resolveInvariantUUID(List<T> components, T missingInvariantCmpt) {
33         String uuid = missingInvariantCmpt.getUUID();
34         String systemName = missingInvariantCmpt.getSystemName();
35         String invariantUid = findInvariantUUidByAllVersionsMap(missingInvariantCmpt, components).orElseGet(() -> findInvariantUUidByUUIDOrSystemName(components, uuid, systemName));
36         if (invariantUid == null) {
37             MigrationMalformedDataLogger.logMalformedDataMsg(String.format("could not find invariant uuid for component %s with id %s", missingInvariantCmpt.getName(), missingInvariantCmpt.getUniqueId()));
38         }
39         return invariantUid;
40     }
41
42     private String findInvariantUUidByUUIDOrSystemName(List<T> components, String uuid, String systemName) {
43         return components.stream()
44                 .filter(c -> c.getUUID().equals(uuid) || c.getSystemName().equals(systemName))
45                 .map(Component::getInvariantUUID)
46                 .filter(c -> c != null)
47                 .findAny().orElse(null);
48     }
49
50     private Optional<String> findInvariantUUidByAllVersionsMap(T component, List<T> allComponents) {
51         if (component.getAllVersions() == null) return Optional.empty();
52         Collection<String> allVersionsComponentIds = component.getAllVersions().values();
53         return allComponents.stream().filter(c -> allVersionsComponentIds.contains(c.getUniqueId()))
54                 .map(Component::getInvariantUUID)
55                 .filter(c -> c != null)
56                 .findAny();
57
58
59     }
60
61 }