Fix multicloud query of non-existant stack
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / openstack / mappers / StackInfoMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
21 package org.onap.so.openstack.mappers;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import org.onap.so.openstack.beans.HeatStatus;
28 import org.onap.so.openstack.beans.StackInfo;
29 import com.woorea.openstack.heat.model.Stack;
30 import com.woorea.openstack.heat.model.Stack.Output;
31
32 public class StackInfoMapper {
33
34     private final Stack stack;
35     private final Map<String, HeatStatus> heatStatusMap = new HashMap<>();
36
37     public StackInfoMapper(Stack stack) {
38         this.stack = stack;
39         configureHeatStatusMap();
40     }
41
42     public StackInfo map() {
43         final StackInfo info = new StackInfo();
44         if (stack == null) {
45             info.setStatus(HeatStatus.NOTFOUND);
46         } else {
47             info.setName(stack.getStackName());
48             info.setCanonicalName(stack.getStackName() + "/" + stack.getId());
49             info.setStatus(this.mapStatus(stack.getStackStatus()));
50
51             info.setStatusMessage(stack.getStackStatusReason());
52
53             Optional<Map<String, Object>> result = this.mapOutputToMap(stack.getOutputs());
54             if (result.isPresent()) {
55                 info.setOutputs(result.get());
56             }
57
58             info.setParameters(stack.getParameters());
59         }
60
61         return info;
62     }
63
64     protected HeatStatus mapStatus(String status) {
65         final HeatStatus result;
66         if (status == null) {
67             result = HeatStatus.INIT;
68         } else {
69             result = heatStatusMap.getOrDefault(status, HeatStatus.UNKNOWN);
70         }
71
72         return result;
73     }
74
75     protected Optional<Map<String, Object>> mapOutputToMap(List<Output> outputs) {
76         Optional<Map<String, Object>> result = Optional.empty();
77         if (outputs != null) {
78             final HashMap<String, Object> map = new HashMap<>();
79             for (Output output : outputs) {
80                 map.put(output.getOutputKey(), output.getOutputValue());
81             }
82             result = Optional.of(map);
83         }
84
85         return result;
86     }
87
88     private void configureHeatStatusMap() {
89         heatStatusMap.put("CREATE_IN_PROGRESS", HeatStatus.BUILDING);
90         heatStatusMap.put("CREATE_COMPLETE", HeatStatus.CREATED);
91         heatStatusMap.put("CREATE_FAILED", HeatStatus.FAILED);
92         heatStatusMap.put("DELETE_IN_PROGRESS", HeatStatus.DELETING);
93         heatStatusMap.put("DELETE_COMPLETE", HeatStatus.NOTFOUND);
94         heatStatusMap.put("DELETE_FAILED", HeatStatus.FAILED);
95         heatStatusMap.put("UPDATE_IN_PROGRESS", HeatStatus.UPDATING);
96         heatStatusMap.put("UPDATE_FAILED", HeatStatus.FAILED);
97         heatStatusMap.put("UPDATE_COMPLETE", HeatStatus.UPDATED);
98         heatStatusMap.put("NOT_FOUND", HeatStatus.NOTFOUND);
99     }
100 }