isVfModuleBaseModule() will not throw on model mismatch
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / CommandUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.onap.vid.job.command;
22
23 import static org.apache.commons.collections4.MapUtils.emptyIfNull;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
27 import org.onap.vid.aai.model.ModelVer;
28 import org.onap.vid.model.Group;
29 import org.onap.vid.model.GroupProperties;
30 import org.onap.vid.model.ServiceModel;
31 import org.onap.vid.model.VfModule;
32 import org.onap.vid.mso.model.ModelInfo;
33 import org.onap.vid.services.AaiService;
34 import org.onap.vid.services.VidService;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class CommandUtils {
40
41     private static final EELFLoggerDelegate Logger = EELFLoggerDelegate.getLogger(CommandUtils.class);
42
43     private final VidService vidService;
44     private final AaiService aaiService;
45
46     @Autowired
47     public CommandUtils(VidService vidService, AaiService aaiService) {
48         this.vidService = vidService;
49         this.aaiService = aaiService;
50     }
51
52     public boolean isVfModuleBaseModule(String serviceModelUuid, ModelInfo vfModuleModelInfo) {
53         ServiceModel serviceModel = getServiceModel(serviceModelUuid);
54
55         return emptyIfNull(serviceModel.getVfModules())
56             .values().stream()
57             .filter(toscaModelInfo -> modelsMatch(vfModuleModelInfo, toscaModelInfo))
58             .map(Group::getProperties)
59             .map(GroupProperties::getBaseModule)
60             .findAny().orElseGet(() -> {
61                 Logger.debug(EELFLoggerDelegate.debugLogger,
62                     "Could not find vfModule in model with uuid {}, assuming not base module ({})",
63                     serviceModelUuid, vfModuleModelInfo);
64                 return false;
65             });
66     }
67
68     private boolean modelsMatch(ModelInfo instanceModelInfo, VfModule toscaModelInfo) {
69         return StringUtils.equals(toscaModelInfo.getCustomizationUuid(), instanceModelInfo.getModelCustomizationId())
70             || StringUtils.equals(toscaModelInfo.getModelCustomizationName(), instanceModelInfo.getModelCustomizationName());
71     }
72
73     public ServiceModel getServiceModel(String serviceModelUuid) {
74         return vidService.getServiceModelOrThrow(serviceModelUuid);
75     }
76
77     public String getNewestModelUuid(String serviceModelInvariantId) {
78         ModelVer serviceModelLatestVersion = aaiService.getNewestModelVersionByInvariantId(serviceModelInvariantId);
79         return serviceModelLatestVersion.getModelVersionId();
80     }
81
82 }