2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Bell Canada
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.so.client.cds;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.util.Optional;
24 import org.onap.so.client.exception.PayloadGenerationException;
25 import org.onap.so.serviceinstancebeans.Service;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Component;
28 import java.util.List;
32 public class ExtractServiceFromUserParameters {
34 private static final String SERVICE_KEY = "service";
37 private ObjectMapper objectMapper;
39 public Optional<Service> getServiceFromRequestUserParams(List<Map<String, Object>> userParams) throws Exception {
40 Optional<Map<String, Object>> serviceMap =
41 userParams.stream().filter(key -> key.containsKey(SERVICE_KEY)).findFirst();
42 if (serviceMap.isPresent()) {
43 return Optional.of(getServiceObjectFromServiceMap(serviceMap.get()));
45 return Optional.empty();
48 private Service getServiceObjectFromServiceMap(Map<String, Object> serviceMap) throws PayloadGenerationException {
50 String serviceFromJson = objectMapper.writeValueAsString(serviceMap.get(SERVICE_KEY));
51 return objectMapper.readValue(serviceFromJson, Service.class);
52 } catch (Exception e) {
53 throw new PayloadGenerationException("An exception occurred while converting json object to Service object",