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.onap.nbi.apis.serviceinventory.jolt.FindServiceInventoryJsonTransformer;
20 import org.onap.nbi.apis.serviceinventory.jolt.GetServiceInventoryJsonTransformer;
21 import org.onap.nbi.exceptions.BackendFunctionalException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.http.HttpStatus;
26 import org.springframework.stereotype.Service;
27 import org.springframework.util.CollectionUtils;
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) {
52 Map serviceResponse = aaiClient.getService(serviceId);
54 if (serviceResponse != null) {
55 addVnfsToResponse(serviceResponse);
56 LinkedHashMap serviceInventoryResponse =
57 (LinkedHashMap) getServiceInventoryJsonTransformer.transform(serviceResponse);
58 return serviceInventoryResponse;
60 throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found","no catalog service found");
66 private String getCustomerId(String clientId) {
68 if (StringUtils.isEmpty(clientId)) {
76 private String getServiceName(String serviceSpecificationName, String serviceSpecificationId) {
78 if (StringUtils.isEmpty(serviceSpecificationName)) {
79 Map serviceSpecification = nbiClient.getServiceSpecification(serviceSpecificationId);
80 return (String) serviceSpecification.get("name");
82 return serviceSpecificationName;
87 private void addRelatedPartyId(String customerId, LinkedHashMap serviceInventoryResponse) {
89 LinkedHashMap relatedParty = (LinkedHashMap) serviceInventoryResponse.get("relatedParty");
90 relatedParty.put("id", customerId);
94 private void addVnfsToResponse(Map serviceResponse) {
96 List<Map> vnfs = new ArrayList<>();
97 LinkedHashMap relationShip = (LinkedHashMap) serviceResponse.get("relationship-list");
98 if (relationShip != null) {
99 List<LinkedHashMap> relationsList = (List<LinkedHashMap>) relationShip.get("relationship");
100 if (relationsList != null) {
101 for (LinkedHashMap relation : relationsList) {
102 String relatedLink = (String) relation.get("related-link");
103 Map vnf = aaiClient.getVNF(relatedLink);
108 serviceResponse.put("vnfs", vnfs);
114 public List<LinkedHashMap> find(MultiValueMap<String, String> params) {
116 String clientId = params.getFirst("relatedParty.id");
117 String serviceSpecId = params.getFirst("serviceSpecification.id");
118 String serviceSpecName = params.getFirst("serviceSpecification.name");
119 String customerId = getCustomerId(clientId);
121 List<LinkedHashMap> serviceInstances = new ArrayList<>();
122 if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
123 handleFindWithNoServiceParam(customerId, serviceInstances);
125 serviceName = getServiceName(serviceSpecName, serviceSpecId);
126 buildServiceInstances(serviceInstances, customerId, serviceName);
128 List<LinkedHashMap> serviceInventoryResponse = new ArrayList<>();
129 if(!CollectionUtils.isEmpty(serviceInstances)){
130 serviceInventoryResponse =
131 findServiceInventoryJsonTransformer.transform(serviceInstances);
132 for (LinkedHashMap serviceInventory : serviceInventoryResponse) {
133 LinkedHashMap party = (LinkedHashMap) serviceInventory.get("relatedParty");
134 party.put("id", customerId);
137 LOGGER.warn("no service instance found for customer {} ",customerId);
139 return serviceInventoryResponse;
144 private void handleFindWithNoServiceParam(String customerId, List<LinkedHashMap> serviceInstances) {
145 Map servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId);
146 if(servicesInAaiForCustomer!=null){
147 List<LinkedHashMap> servicesInAAI =
148 (List<LinkedHashMap>) servicesInAaiForCustomer.get("service-subscription");
149 for (LinkedHashMap service : servicesInAAI) {
150 String serviceType = (String) service.get("service-type");
151 buildServiceInstances(serviceInstances, customerId, serviceType);
154 LOGGER.warn("no service instance found for customer {} ",customerId);
158 private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId, String serviceType) {
160 Map serviceInstancesInAaiForCustomer =
161 aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType);
162 if (serviceInstancesInAaiForCustomer != null) {
163 List<LinkedHashMap> serviceInstancesForServiceType =
164 (List<LinkedHashMap>) serviceInstancesInAaiForCustomer.get("service-instance");
166 if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) {
167 // add service type for jolt
168 for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) {
169 serviceInstanceForServiceType.put("service-type", serviceType);
171 serviceInstances.addAll(serviceInstancesForServiceType);
173 LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);
176 LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);