2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
13 package org.onap.nbi.apis.serviceinventory;
15 import java.util.ArrayList;
16 import java.util.LinkedHashMap;
17 import java.util.List;
19 import org.apache.commons.collections.CollectionUtils;
20 import org.onap.nbi.apis.serviceinventory.jolt.FindServiceInventoryJsonTransformer;
21 import org.onap.nbi.apis.serviceinventory.jolt.GetServiceInventoryJsonTransformer;
22 import org.onap.nbi.exceptions.BackendFunctionalException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.stereotype.Service;
28 import org.springframework.util.MultiValueMap;
29 import org.springframework.util.StringUtils;
32 public class ServiceInventoryService {
41 GetServiceInventoryJsonTransformer getServiceInventoryJsonTransformer;
44 FindServiceInventoryJsonTransformer findServiceInventoryJsonTransformer;
46 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInventoryService.class);
49 public Map get(String serviceId, MultiValueMap<String, String> params) {
51 String clientId = params.getFirst("relatedParty.id");
52 String serviceSpecId = params.getFirst("serviceSpecification.id");
53 String serviceSpecName = params.getFirst("serviceSpecification.name");
55 if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
56 throw new BackendFunctionalException(HttpStatus.NOT_FOUND,
57 "serviceSpecName or serviceSpecId must be provided","serviceSpecName or serviceSpecId must be provided");
60 String customerId = getCustomerId(clientId);
61 String serviceName = getServiceName(serviceSpecName, serviceSpecId);
62 Map serviceResponse = aaiClient.getCatalogService(customerId, serviceName, serviceId);
64 if (serviceResponse != null) {
65 addVnfsToResponse(serviceResponse);
66 LinkedHashMap serviceInventoryResponse =
67 (LinkedHashMap) getServiceInventoryJsonTransformer.transform(serviceResponse);
68 addRelatedPartyId(customerId, serviceInventoryResponse);
69 return serviceInventoryResponse;
71 throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found","no catalog service found");
77 private String getCustomerId(String clientId) {
79 if (StringUtils.isEmpty(clientId)) {
87 private String getServiceName(String serviceSpecificationName, String serviceSpecificationId) {
89 if (StringUtils.isEmpty(serviceSpecificationName)) {
90 Map serviceSpecification = nbiClient.getServiceSpecification(serviceSpecificationId);
91 return (String) serviceSpecification.get("name");
93 return serviceSpecificationName;
98 private void addRelatedPartyId(String customerId, LinkedHashMap serviceInventoryResponse) {
100 LinkedHashMap relatedParty = (LinkedHashMap) serviceInventoryResponse.get("relatedParty");
101 relatedParty.put("id", customerId);
105 private void addVnfsToResponse(Map serviceResponse) {
107 List<Map> vnfs = new ArrayList<>();
108 LinkedHashMap relationShip = (LinkedHashMap) serviceResponse.get("relationship-list");
109 if (relationShip != null) {
110 List<LinkedHashMap> relationsList = (List<LinkedHashMap>) relationShip.get("relationship");
111 if (relationsList != null) {
112 for (LinkedHashMap relation : relationsList) {
113 String relatedLink = (String) relation.get("related-link");
114 Map vnf = aaiClient.getVNF(relatedLink);
119 serviceResponse.put("vnfs", vnfs);
125 public List<LinkedHashMap> find(MultiValueMap<String, String> params) {
127 String clientId = params.getFirst("relatedParty.id");
128 String serviceSpecId = params.getFirst("serviceSpecification.id");
129 String serviceSpecName = params.getFirst("serviceSpecification.name");
130 String customerId = getCustomerId(clientId);
132 List<LinkedHashMap> serviceInstances = new ArrayList<>();
133 if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
134 handleFindWithNoServiceParam(customerId, serviceInstances);
136 serviceName = getServiceName(serviceSpecName, serviceSpecId);
137 buildServiceInstances(serviceInstances, customerId, serviceName);
139 List<LinkedHashMap> serviceInventoryResponse = new ArrayList<>();
140 if(CollectionUtils.isNotEmpty(serviceInstances)){
141 serviceInventoryResponse =
142 findServiceInventoryJsonTransformer.transform(serviceInstances);
143 for (LinkedHashMap serviceInventory : serviceInventoryResponse) {
144 LinkedHashMap party = (LinkedHashMap) serviceInventory.get("relatedParty");
145 party.put("id", customerId);
148 LOGGER.warn("no service instance found for customer {} ",customerId);
150 return serviceInventoryResponse;
155 private void handleFindWithNoServiceParam(String customerId, List<LinkedHashMap> serviceInstances) {
156 Map servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId);
157 if(servicesInAaiForCustomer!=null){
158 List<LinkedHashMap> servicesInAAI =
159 (List<LinkedHashMap>) servicesInAaiForCustomer.get("service-subscription");
160 for (LinkedHashMap service : servicesInAAI) {
161 String serviceType = (String) service.get("service-type");
162 buildServiceInstances(serviceInstances, customerId, serviceType);
165 LOGGER.warn("no service instance found for customer {} ",customerId);
169 private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId, String serviceType) {
171 Map serviceInstancesInAaiForCustomer =
172 aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType);
173 if (serviceInstancesInAaiForCustomer != null) {
174 List<LinkedHashMap> serviceInstancesForServiceType =
175 (List<LinkedHashMap>) serviceInstancesInAaiForCustomer.get("service-instance");
177 if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) {
178 // add service type for jolt
179 for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) {
180 serviceInstanceForServiceType.put("service-type", serviceType);
182 serviceInstances.addAll(serviceInstancesForServiceType);
184 LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);
187 LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);