17520eef485e8d88cc10dcac589dbd77c8179096
[vid.git] / vid-app-common / src / main / java / org / onap / vid / services / InstantiationTemplatesService.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.services;
22
23 import static java.util.Collections.emptyMap;
24 import static java.util.Objects.requireNonNull;
25 import static java.util.stream.Collectors.toList;
26
27 import java.util.Collection;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31 import javax.inject.Inject;
32 import org.jetbrains.annotations.NotNull;
33 import org.onap.vid.asdc.beans.Service;
34 import org.onap.vid.dal.AsyncInstantiationRepository;
35 import org.onap.vid.model.ModelUtil;
36 import org.onap.vid.model.serviceInstantiation.BaseResource;
37 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
38 import org.onap.vid.model.serviceInstantiation.ServiceInstantiationTemplate;
39 import org.onap.vid.properties.Features;
40 import org.springframework.stereotype.Component;
41 import org.togglz.core.manager.FeatureManager;
42
43 @Component
44 public class InstantiationTemplatesService {
45
46     private final ModelUtil modelUtil;
47     private final AsyncInstantiationRepository asyncInstantiationRepository;
48     private FeatureManager featureManager;
49
50
51     @Inject
52     public InstantiationTemplatesService(ModelUtil modelUtil,
53         AsyncInstantiationRepository asyncInstantiationRepository,
54         FeatureManager featureManager) {
55         this.modelUtil = modelUtil;
56         this.asyncInstantiationRepository = asyncInstantiationRepository;
57         this.featureManager = featureManager;
58     }
59
60     public ServiceInstantiationTemplate getJobRequestAsTemplate(UUID jobId) {
61         ServiceInstantiation jobRequest = requireNonNull(asyncInstantiationRepository.getJobRequest(jobId));
62
63         return new ServiceInstantiationTemplate(
64             jobRequest,
65             counterMap(jobRequest.getVnfs()),
66             counterMap(jobRequest.getNetworks()),
67             counterMap(jobRequest.getVnfGroups()),
68             emptyMap() // model info for VRF is not stored
69         );
70     }
71
72     private <T extends BaseResource> Map<String, Long> counterMap(Map<String, T> nodesToCount) {
73         return modelUtil.getExistingCounterMap(
74             nodesToCount, BaseResource::getModelInfo
75         );
76     }
77
78     public Collection<Service> setOnEachServiceIsTemplateExists(Collection<Service> services){
79         if (!featureManager.isActive(Features.FLAG_2004_CREATE_ANOTHER_INSTANCE_FROM_TEMPLATE)){
80             return unsetTemplateExistsToAllServices(services);
81
82         }
83
84         Set<String> serviceModelIdsFromDB  = asyncInstantiationRepository.getAllTemplatesServiceModelIds();
85
86         return services.stream().map(it -> setTemplateExistForService(it, serviceModelIdsFromDB)).collect(toList());
87     }
88
89     @NotNull
90     protected Collection<Service> unsetTemplateExistsToAllServices(Collection<Service> services) {
91         services.forEach(it -> it.setIsInstantiationTemplateExists(false));
92         return services;
93     }
94
95     protected Service setTemplateExistForService(Service service, Set<String> serviceModelIdsFromDb) {
96         service.setIsInstantiationTemplateExists(serviceModelIdsFromDb.contains(service.getUuid()));
97         return service;
98     }
99 }