Format Java code with respect to ONAP Code Style
[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
5  * in compliance with 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
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
12  * the License.
13  */
14
15 package org.onap.nbi.apis.serviceinventory;
16
17 import java.util.ArrayList;
18 import java.util.LinkedHashMap;
19 import java.util.List;
20 import java.util.Map;
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;
32
33 @Service
34 public class ServiceInventoryService {
35
36     @Autowired
37     NbiClient nbiClient;
38
39     @Autowired
40     AaiClient aaiClient;
41
42     @Autowired
43     GetServiceInventoryJsonTransformer getServiceInventoryJsonTransformer;
44
45     @Autowired
46     FindServiceInventoryJsonTransformer findServiceInventoryJsonTransformer;
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceInventoryService.class);
49
50     public Map get(String serviceId, MultiValueMap<String, String> params) {
51
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;
61         } else {
62             throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found",
63                     "no catalog service found");
64         }
65
66     }
67
68     private String getCustomerId(String clientId) {
69
70         if (StringUtils.isEmpty(clientId)) {
71             return "generic";
72         } else {
73             return clientId;
74         }
75
76     }
77
78     private String getServiceName(String serviceSpecificationName, String serviceSpecificationId) {
79
80         if (StringUtils.isEmpty(serviceSpecificationName)) {
81             Map serviceSpecification = nbiClient.getServiceSpecification(serviceSpecificationId);
82             return (String) serviceSpecification.get("name");
83         } else {
84             return serviceSpecificationName;
85         }
86
87     }
88
89     private void addrelatedPartyIdIdandSpecName(String serviceId, LinkedHashMap serviceInventoryResponse) {
90
91         String customerId;
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");
98
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);
107                 }
108             } else {
109                 LOGGER.warn("no service instance found for serviceId {}", serviceId);
110             }
111         } else {
112             LOGGER.warn("no service instance found for serviceId {}", serviceId);
113         }
114     }
115
116     private void addVnfsToResponse(Map serviceResponse) {
117
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);
126                     if (vnf != null) {
127                         vnfs.add(vnf);
128                     }
129                 }
130                 serviceResponse.put("vnfs", vnfs);
131             }
132         }
133     }
134
135     public List<LinkedHashMap> find(MultiValueMap<String, String> params) {
136
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);
141         String serviceName;
142         List<LinkedHashMap> serviceInstances = new ArrayList<>();
143         if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
144             handleFindWithNoServiceParam(customerId, serviceInstances);
145         } else {
146             serviceName = getServiceName(serviceSpecName, serviceSpecId);
147             buildServiceInstances(serviceInstances, customerId, serviceName);
148         }
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);
157             }
158         } else {
159             LOGGER.warn("no service instance found for customer {} ", customerId);
160         }
161         return serviceInventoryResponse;
162
163     }
164
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);
173             }
174         } else {
175             LOGGER.warn("no service instance found for customer {} ", customerId);
176         }
177     }
178
179     private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId, String serviceType) {
180
181         Map serviceInstancesInAaiForCustomer = aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType);
182         if (serviceInstancesInAaiForCustomer != null) {
183             List<LinkedHashMap> serviceInstancesForServiceType =
184                     (List<LinkedHashMap>) serviceInstancesInAaiForCustomer.get("service-instance");
185
186             if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) {
187                 // add service type for jolt
188                 for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) {
189                     serviceInstanceForServiceType.put("service-type", serviceType);
190                 }
191                 serviceInstances.addAll(serviceInstancesForServiceType);
192             } else {
193                 LOGGER.warn("no service instance found for customer {} and service type {}", customerId, serviceType);
194             }
195         } else {
196             LOGGER.warn("no service instance found for customer {} and service type {}", customerId, serviceType);
197         }
198
199     }
200
201 }