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 org.onap.so.client.exception.PayloadGenerationException;
24 import org.onap.so.serviceinstancebeans.Service;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Component;
27 import java.util.List;
31 public class ExtractServiceFromUserParameters {
33 private static final String SERVICE_KEY = "service";
36 private ObjectMapper objectMapper;
38 public Service getServiceFromRequestUserParams(List<Map<String, Object>> userParams) throws Exception {
39 Map<String, Object> serviceMap = userParams.stream().filter(key -> key.containsKey(SERVICE_KEY)).findFirst()
40 .orElseThrow(() -> new Exception("Can not find service in userParams section in generalBuildingBlock"));
41 return getServiceObjectFromServiceMap(serviceMap);
44 private Service getServiceObjectFromServiceMap(Map<String, Object> serviceMap) throws PayloadGenerationException {
46 String serviceFromJson = objectMapper.writeValueAsString(serviceMap.get(SERVICE_KEY));
47 return objectMapper.readValue(serviceFromJson, Service.class);
48 } catch (Exception e) {
49 throw new PayloadGenerationException("An exception occurred while converting json object to Service object",