Enable ControllerExecutionBB for service scope
[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 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;
29 import java.util.Map;
30
31 @Component
32 public class ExtractServiceFromUserParameters {
33
34     private static final String SERVICE_KEY = "service";
35
36     @Autowired
37     private ObjectMapper objectMapper;
38
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()));
44         }
45         return Optional.empty();
46     }
47
48     private Service getServiceObjectFromServiceMap(Map<String, Object> serviceMap) throws PayloadGenerationException {
49         try {
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",
54                     e);
55         }
56     }
57 }