2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with 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
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.nbi.apis.serviceinventory;
16 import java.util.ArrayList;
17 import java.util.LinkedHashMap;
18 import java.util.List;
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.CollectionUtils;
29 import org.springframework.util.MultiValueMap;
30 import org.springframework.util.StringUtils;
33 public class ServiceInventoryService {
42 GetServiceInventoryJsonTransformer getServiceInventoryJsonTransformer;
45 FindServiceInventoryJsonTransformer findServiceInventoryJsonTransformer;
47 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInventoryService.class);
50 public Map get(String serviceId, MultiValueMap<String, String> params) {
52 Map serviceResponse = aaiClient.getService(serviceId);
53 if (serviceResponse != null) {
54 addVnfsToResponse(serviceResponse);
55 LinkedHashMap serviceInventoryResponse =
56 (LinkedHashMap) getServiceInventoryJsonTransformer.transform(serviceResponse);
57 addrelatedPartyIdIdandSpecName(serviceId, serviceInventoryResponse);
58 String href = "service/" + serviceId;
59 serviceInventoryResponse.put("href", href );
60 return serviceInventoryResponse;
62 throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found",
63 "no catalog service found");
69 private String getCustomerId(String clientId) {
71 if (StringUtils.isEmpty(clientId)) {
79 private String getServiceName(String serviceSpecificationName, String serviceSpecificationId) {
81 if (StringUtils.isEmpty(serviceSpecificationName)) {
82 Map serviceSpecification = nbiClient.getServiceSpecification(serviceSpecificationId);
83 return (String) serviceSpecification.get("name");
85 return serviceSpecificationName;
90 private void addrelatedPartyIdIdandSpecName(String serviceId,
91 LinkedHashMap serviceInventoryResponse) {
94 String serviceSpecName;
95 LinkedHashMap relatedParty = (LinkedHashMap) serviceInventoryResponse.get("relatedParty");
96 LinkedHashMap serviceSpecification =
97 (LinkedHashMap) serviceInventoryResponse.get("serviceSpecification");
98 Map servicecustomerResponse = aaiClient.getServiceCustomer(serviceId);
99 if (servicecustomerResponse != null) {
100 List<LinkedHashMap> serviceCustomerResults =
101 (List<LinkedHashMap>) servicecustomerResponse.get("results");
103 if (!CollectionUtils.isEmpty(serviceCustomerResults)) {
104 for (LinkedHashMap serviceCustomerResult : serviceCustomerResults) {
105 String url = (String) serviceCustomerResult.get("url");
106 String[] pathObjects = url.split("/");
107 customerId = pathObjects[6];
108 serviceSpecName = pathObjects[9];
109 relatedParty.put("id", customerId);
110 serviceSpecification.put("name", serviceSpecName);
113 LOGGER.warn("no service instance found for serviceId {}", serviceId);
116 LOGGER.warn("no service instance found for serviceId {}", serviceId);
120 private void addVnfsToResponse(Map serviceResponse) {
122 List<Map> vnfs = new ArrayList<>();
123 LinkedHashMap relationShip = (LinkedHashMap) serviceResponse.get("relationship-list");
124 if (relationShip != null) {
125 List<LinkedHashMap> relationsList = (List<LinkedHashMap>) relationShip.get("relationship");
126 if (relationsList != null) {
127 for (LinkedHashMap relation : relationsList) {
128 String relatedLink = (String) relation.get("related-link");
129 Map vnf = aaiClient.getVNF(relatedLink);
134 serviceResponse.put("vnfs", vnfs);
140 public List<LinkedHashMap> find(MultiValueMap<String, String> params) {
142 String clientId = params.getFirst("relatedParty.id");
143 String serviceSpecId = params.getFirst("serviceSpecification.id");
144 String serviceSpecName = params.getFirst("serviceSpecification.name");
145 String customerId = getCustomerId(clientId);
147 List<LinkedHashMap> serviceInstances = new ArrayList<>();
148 if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
149 handleFindWithNoServiceParam(customerId, serviceInstances);
151 serviceName = getServiceName(serviceSpecName, serviceSpecId);
152 buildServiceInstances(serviceInstances, customerId, serviceName);
154 List<LinkedHashMap> serviceInventoryResponse = new ArrayList<>();
155 if (!CollectionUtils.isEmpty(serviceInstances)) {
156 serviceInventoryResponse = findServiceInventoryJsonTransformer.transform(serviceInstances);
157 for (LinkedHashMap serviceInventory : serviceInventoryResponse) {
158 String href = "service/" + serviceInventory.get("id");
159 serviceInventory.put("href", href);
160 LinkedHashMap party = (LinkedHashMap) serviceInventory.get("relatedParty");
161 party.put("id", customerId);
164 LOGGER.warn("no service instance found for customer {} ", customerId);
166 return serviceInventoryResponse;
171 private void handleFindWithNoServiceParam(String customerId,
172 List<LinkedHashMap> serviceInstances) {
173 Map servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId);
174 if (servicesInAaiForCustomer != null) {
175 List<LinkedHashMap> servicesInAAI =
176 (List<LinkedHashMap>) servicesInAaiForCustomer.get("service-subscription");
177 for (LinkedHashMap service : servicesInAAI) {
178 String serviceType = (String) service.get("service-type");
179 buildServiceInstances(serviceInstances, customerId, serviceType);
182 LOGGER.warn("no service instance found for customer {} ", customerId);
186 private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId,
187 String serviceType) {
189 Map serviceInstancesInAaiForCustomer =
190 aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType);
191 if (serviceInstancesInAaiForCustomer != null) {
192 List<LinkedHashMap> serviceInstancesForServiceType =
193 (List<LinkedHashMap>) serviceInstancesInAaiForCustomer.get("service-instance");
195 if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) {
196 // add service type for jolt
197 for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) {
198 serviceInstanceForServiceType.put("service-type", serviceType);
200 serviceInstances.addAll(serviceInstancesForServiceType);
202 LOGGER.warn("no service instance found for customer {} and service type {}", customerId,
206 LOGGER.warn("no service instance found for customer {} and service type {}", customerId,