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
15 package org.onap.nbi.apis.serviceinventory;
17 import java.util.ArrayList;
18 import java.util.LinkedHashMap;
19 import java.util.List;
21 import org.onap.nbi.apis.serviceinventory.jolt.FindServiceInventoryJsonTransformer;
22 import org.onap.nbi.apis.serviceinventory.jolt.GetServiceInventoryJsonTransformer;
23 import org.onap.nbi.exceptions.BackendFunctionalException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.stereotype.Service;
29 import org.springframework.util.CollectionUtils;
30 import org.springframework.util.MultiValueMap;
31 import org.springframework.util.StringUtils;
34 public class ServiceInventoryService {
43 GetServiceInventoryJsonTransformer getServiceInventoryJsonTransformer;
46 FindServiceInventoryJsonTransformer findServiceInventoryJsonTransformer;
48 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");
68 private String getCustomerId(String clientId) {
70 if (StringUtils.isEmpty(clientId)) {
78 private String getServiceName(String serviceSpecificationName, String serviceSpecificationId) {
80 if (StringUtils.isEmpty(serviceSpecificationName)) {
81 Map serviceSpecification = nbiClient.getServiceSpecification(serviceSpecificationId);
82 return (String) serviceSpecification.get("name");
84 return serviceSpecificationName;
89 private void addrelatedPartyIdIdandSpecName(String serviceId, LinkedHashMap serviceInventoryResponse) {
92 String serviceSpecName;
93 LinkedHashMap relatedParty = (LinkedHashMap) serviceInventoryResponse.get("relatedParty");
94 LinkedHashMap serviceSpecification = (LinkedHashMap) serviceInventoryResponse.get("serviceSpecification");
95 Map servicecustomerResponse = aaiClient.getServiceCustomer(serviceId);
96 if (servicecustomerResponse != null) {
97 List<LinkedHashMap> serviceCustomerResults = (List<LinkedHashMap>) servicecustomerResponse.get("results");
99 if (!CollectionUtils.isEmpty(serviceCustomerResults)) {
100 for (LinkedHashMap serviceCustomerResult : serviceCustomerResults) {
101 String url = (String) serviceCustomerResult.get("url");
102 String[] pathObjects = url.split("/");
103 customerId = pathObjects[6];
104 serviceSpecName = pathObjects[9];
105 relatedParty.put("id", customerId);
106 serviceSpecification.put("name", serviceSpecName);
109 LOGGER.warn("no service instance found for serviceId {}", serviceId);
112 LOGGER.warn("no service instance found for serviceId {}", serviceId);
116 private void addVnfsToResponse(Map serviceResponse) {
118 List<Map> vnfs = new ArrayList<>();
119 LinkedHashMap relationShip = (LinkedHashMap) serviceResponse.get("relationship-list");
120 if (relationShip != null) {
121 List<LinkedHashMap> relationsList = (List<LinkedHashMap>) relationShip.get("relationship");
122 if (relationsList != null) {
123 for (LinkedHashMap relation : relationsList) {
124 String relatedLink = (String) relation.get("related-link");
125 Map vnf = aaiClient.getVNF(relatedLink);
130 serviceResponse.put("vnfs", vnfs);
135 public List<LinkedHashMap> find(MultiValueMap<String, String> params) {
137 String clientId = params.getFirst("relatedParty.id");
138 String serviceSpecId = params.getFirst("serviceSpecification.id");
139 String serviceSpecName = params.getFirst("serviceSpecification.name");
140 String customerId = getCustomerId(clientId);
142 List<LinkedHashMap> serviceInstances = new ArrayList<>();
143 if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
144 handleFindWithNoServiceParam(customerId, serviceInstances);
146 serviceName = getServiceName(serviceSpecName, serviceSpecId);
147 buildServiceInstances(serviceInstances, customerId, serviceName);
149 List<LinkedHashMap> serviceInventoryResponse = new ArrayList<>();
150 if (!CollectionUtils.isEmpty(serviceInstances)) {
151 serviceInventoryResponse = findServiceInventoryJsonTransformer.transform(serviceInstances);
152 for (LinkedHashMap serviceInventory : serviceInventoryResponse) {
153 String href = "service/" + serviceInventory.get("id");
154 serviceInventory.put("href", href);
155 LinkedHashMap party = (LinkedHashMap) serviceInventory.get("relatedParty");
156 party.put("id", customerId);
159 LOGGER.warn("no service instance found for customer {} ", customerId);
161 return serviceInventoryResponse;
165 private void handleFindWithNoServiceParam(String customerId, List<LinkedHashMap> serviceInstances) {
166 Map servicesInAaiForCustomer = aaiClient.getServicesInAaiForCustomer(customerId);
167 if (servicesInAaiForCustomer != null) {
168 List<LinkedHashMap> servicesInAAI =
169 (List<LinkedHashMap>) servicesInAaiForCustomer.get("service-subscription");
170 for (LinkedHashMap service : servicesInAAI) {
171 String serviceType = (String) service.get("service-type");
172 buildServiceInstances(serviceInstances, customerId, serviceType);
175 LOGGER.warn("no service instance found for customer {} ", customerId);
179 private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId, String serviceType) {
181 Map serviceInstancesInAaiForCustomer = aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType);
182 if (serviceInstancesInAaiForCustomer != null) {
183 List<LinkedHashMap> serviceInstancesForServiceType =
184 (List<LinkedHashMap>) serviceInstancesInAaiForCustomer.get("service-instance");
186 if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) {
187 // add service type for jolt
188 for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) {
189 serviceInstanceForServiceType.put("service-type", serviceType);
191 serviceInstances.addAll(serviceInstancesForServiceType);
193 LOGGER.warn("no service instance found for customer {} and service type {}", customerId, serviceType);
196 LOGGER.warn("no service instance found for customer {} and service type {}", customerId, serviceType);