Help NBI user to get information
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceinventory / ServiceInventoryService.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
12  */
13 package org.onap.nbi.apis.serviceinventory;
14
15 import java.util.ArrayList;
16 import java.util.LinkedHashMap;
17 import java.util.List;
18 import java.util.Map;
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;
30
31 @Service
32 public class ServiceInventoryService {
33
34     @Autowired
35     NbiClient nbiClient;
36
37     @Autowired
38     AaiClient aaiClient;
39
40     @Autowired
41     GetServiceInventoryJsonTransformer getServiceInventoryJsonTransformer;
42
43     @Autowired
44     FindServiceInventoryJsonTransformer findServiceInventoryJsonTransformer;
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInventoryService.class);
47
48
49     public Map get(String serviceId, MultiValueMap<String, String> params) {
50
51         String clientId = params.getFirst("relatedParty.id");
52         String serviceSpecId = params.getFirst("serviceSpecification.id");
53         String serviceSpecName = params.getFirst("serviceSpecification.name");
54
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");
58         }
59
60         String customerId = getCustomerId(clientId);
61         String serviceName = getServiceName(serviceSpecName, serviceSpecId);
62         Map serviceResponse = aaiClient.getCatalogService(customerId, serviceName, serviceId);
63
64         if (serviceResponse != null) {
65             addVnfsToResponse(serviceResponse);
66             LinkedHashMap serviceInventoryResponse =
67                 (LinkedHashMap) getServiceInventoryJsonTransformer.transform(serviceResponse);
68             addRelatedPartyId(customerId, serviceInventoryResponse);
69             return serviceInventoryResponse;
70         } else {
71             throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found","no catalog service found");
72         }
73
74     }
75
76
77     private String getCustomerId(String clientId) {
78
79         if (StringUtils.isEmpty(clientId)) {
80             return "generic";
81         } else {
82             return clientId;
83         }
84
85     }
86
87     private String getServiceName(String serviceSpecificationName, String serviceSpecificationId) {
88
89         if (StringUtils.isEmpty(serviceSpecificationName)) {
90             Map serviceSpecification = nbiClient.getServiceSpecification(serviceSpecificationId);
91             return (String) serviceSpecification.get("name");
92         } else {
93             return serviceSpecificationName;
94         }
95
96     }
97
98     private void addRelatedPartyId(String customerId, LinkedHashMap serviceInventoryResponse) {
99
100         LinkedHashMap relatedParty = (LinkedHashMap) serviceInventoryResponse.get("relatedParty");
101         relatedParty.put("id", customerId);
102
103     }
104
105     private void addVnfsToResponse(Map serviceResponse) {
106
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);
115                     if (vnf != null) {
116                         vnfs.add(vnf);
117                     }
118                 }
119                 serviceResponse.put("vnfs", vnfs);
120             }
121         }
122     }
123
124
125     public List<LinkedHashMap> find(MultiValueMap<String, String> params) {
126
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);
131         String serviceName;
132         List<LinkedHashMap> serviceInstances = new ArrayList<>();
133         if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
134             handleFindWithNoServiceParam(customerId, serviceInstances);
135         } else {
136             serviceName = getServiceName(serviceSpecName, serviceSpecId);
137             buildServiceInstances(serviceInstances, customerId, serviceName);
138         }
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);
146             }
147         }else {
148             LOGGER.warn("no service instance found for customer {} ",customerId);
149         }
150         return serviceInventoryResponse;
151
152
153     }
154
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);
163             }
164         }else {
165             LOGGER.warn("no service instance found for customer {} ",customerId);
166         }
167     }
168
169     private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId, String serviceType) {
170
171         Map serviceInstancesInAaiForCustomer =
172             aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType);
173         if (serviceInstancesInAaiForCustomer != null) {
174             List<LinkedHashMap> serviceInstancesForServiceType =
175                 (List<LinkedHashMap>) serviceInstancesInAaiForCustomer.get("service-instance");
176
177             if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) {
178                 // add service type for jolt
179                 for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) {
180                     serviceInstanceForServiceType.put("service-type", serviceType);
181                 }
182                 serviceInstances.addAll(serviceInstancesForServiceType);
183             } else {
184                 LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);
185             }
186         } else {
187             LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);
188         }
189
190
191     }
192
193 }