node query for service instance query
[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.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;
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                 
52         Map serviceResponse = aaiClient.getService(serviceId);
53
54         if (serviceResponse != null) {
55             addVnfsToResponse(serviceResponse);
56             LinkedHashMap serviceInventoryResponse =
57                 (LinkedHashMap) getServiceInventoryJsonTransformer.transform(serviceResponse);
58             return serviceInventoryResponse;
59         } else {
60             throw new BackendFunctionalException(HttpStatus.NOT_FOUND, "no catalog service found","no catalog service found");
61         }
62
63     }
64
65
66     private String getCustomerId(String clientId) {
67
68         if (StringUtils.isEmpty(clientId)) {
69             return "generic";
70         } else {
71             return clientId;
72         }
73
74     }
75
76     private String getServiceName(String serviceSpecificationName, String serviceSpecificationId) {
77
78         if (StringUtils.isEmpty(serviceSpecificationName)) {
79             Map serviceSpecification = nbiClient.getServiceSpecification(serviceSpecificationId);
80             return (String) serviceSpecification.get("name");
81         } else {
82             return serviceSpecificationName;
83         }
84
85     }
86
87     private void addRelatedPartyId(String customerId, LinkedHashMap serviceInventoryResponse) {
88
89         LinkedHashMap relatedParty = (LinkedHashMap) serviceInventoryResponse.get("relatedParty");
90         relatedParty.put("id", customerId);
91
92     }
93
94     private void addVnfsToResponse(Map serviceResponse) {
95
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);
104                     if (vnf != null) {
105                         vnfs.add(vnf);
106                     }
107                 }
108                 serviceResponse.put("vnfs", vnfs);
109             }
110         }
111     }
112
113
114     public List<LinkedHashMap> find(MultiValueMap<String, String> params) {
115
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);
120         String serviceName;
121         List<LinkedHashMap> serviceInstances = new ArrayList<>();
122         if (StringUtils.isEmpty(serviceSpecId) && StringUtils.isEmpty(serviceSpecName)) {
123             handleFindWithNoServiceParam(customerId, serviceInstances);
124         } else {
125             serviceName = getServiceName(serviceSpecName, serviceSpecId);
126             buildServiceInstances(serviceInstances, customerId, serviceName);
127         }
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);
135             }
136         }else {
137             LOGGER.warn("no service instance found for customer {} ",customerId);
138         }
139         return serviceInventoryResponse;
140
141
142     }
143
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);
152             }
153         }else {
154             LOGGER.warn("no service instance found for customer {} ",customerId);
155         }
156     }
157
158     private void buildServiceInstances(List<LinkedHashMap> serviceInstances, String customerId, String serviceType) {
159
160         Map serviceInstancesInAaiForCustomer =
161             aaiClient.getServiceInstancesInAaiForCustomer(customerId, serviceType);
162         if (serviceInstancesInAaiForCustomer != null) {
163             List<LinkedHashMap> serviceInstancesForServiceType =
164                 (List<LinkedHashMap>) serviceInstancesInAaiForCustomer.get("service-instance");
165
166             if (!CollectionUtils.isEmpty(serviceInstancesForServiceType)) {
167                 // add service type for jolt
168                 for (LinkedHashMap serviceInstanceForServiceType : serviceInstancesForServiceType) {
169                     serviceInstanceForServiceType.put("service-type", serviceType);
170                 }
171                 serviceInstances.addAll(serviceInstancesForServiceType);
172             } else {
173                 LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);
174             }
175         } else {
176             LOGGER.warn("no service instance found for customer {} and service type {}",customerId,serviceType);
177         }
178
179
180     }
181
182 }