2 * ============LICENSE_START=======================================================
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
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.onap.vid.services;
23 import static java.util.Collections.emptyMap;
24 import static java.util.Objects.requireNonNull;
25 import static java.util.stream.Collectors.toList;
27 import java.util.Collection;
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;
44 public class InstantiationTemplatesService {
46 private final ModelUtil modelUtil;
47 private final AsyncInstantiationRepository asyncInstantiationRepository;
48 private FeatureManager featureManager;
52 public InstantiationTemplatesService(ModelUtil modelUtil,
53 AsyncInstantiationRepository asyncInstantiationRepository,
54 FeatureManager featureManager) {
55 this.modelUtil = modelUtil;
56 this.asyncInstantiationRepository = asyncInstantiationRepository;
57 this.featureManager = featureManager;
60 public ServiceInstantiationTemplate getJobRequestAsTemplate(UUID jobId) {
61 ServiceInstantiation jobRequest = requireNonNull(asyncInstantiationRepository.getJobRequest(jobId));
63 return new ServiceInstantiationTemplate(
65 counterMap(jobRequest.getVnfs()),
66 counterMap(jobRequest.getPnfs()),
67 counterMap(jobRequest.getNetworks()),
68 counterMap(jobRequest.getVnfGroups()),
69 emptyMap() // model info for VRF is not stored
73 private <T extends BaseResource> Map<String, Long> counterMap(Map<String, T> nodesToCount) {
74 return modelUtil.getExistingCounterMap(
75 nodesToCount, BaseResource::getModelInfo
79 public Collection<Service> setOnEachServiceIsTemplateExists(Collection<Service> services){
80 if (!featureManager.isActive(Features.FLAG_2004_INSTANTIATION_TEMPLATES_POPUP)){
81 return unsetTemplateExistsToAllServices(services);
85 Set<String> serviceModelIdsFromDB = asyncInstantiationRepository.getAllTemplatesServiceModelIds();
87 return services.stream().map(it -> setTemplateExistForService(it, serviceModelIdsFromDB)).collect(toList());
91 protected Collection<Service> unsetTemplateExistsToAllServices(Collection<Service> services) {
92 services.forEach(it -> it.setIsInstantiationTemplateExists(false));
96 protected Service setTemplateExistForService(Service service, Set<String> serviceModelIdsFromDb) {
97 service.setIsInstantiationTemplateExists(serviceModelIdsFromDb.contains(service.getUuid()));