43fabd325327f40be8c44bf015ee45355ff50a18
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / ExtractServiceFromUserParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.so.client.cds;
21
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;
28 import java.util.Map;
29
30 @Component
31 public class ExtractServiceFromUserParameters {
32
33     private static final String SERVICE_KEY = "service";
34
35     @Autowired
36     private ObjectMapper objectMapper;
37
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);
42     }
43
44     private Service getServiceObjectFromServiceMap(Map<String, Object> serviceMap) throws PayloadGenerationException {
45         try {
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",
50                     e);
51         }
52     }
53 }