85fd6cc279d4692dd721d4d763fa7b0b81f09c3c
[so.git] /
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
28 import org.onap.so.openstack.beans.HeatStatus;
29 import org.onap.so.openstack.beans.StackInfo;
30
31 import com.woorea.openstack.heat.model.Stack;
32 import com.woorea.openstack.heat.model.Stack.Output;
33
34 public class StackInfoMapper {
35
36         private final Stack stack;
37         private final Map<String,HeatStatus> heatStatusMap = new HashMap<>();
38         public StackInfoMapper(Stack stack) {
39                 this.stack = stack;
40                 configureHeatStatusMap();
41         }
42         
43         public StackInfo map() {
44                 final StackInfo info = new StackInfo();
45                 if (stack == null) {
46                         info.setStatus(HeatStatus.NOTFOUND);
47                 } else {
48                         info.setName(stack.getStackName());
49                         info.setCanonicalName(stack.getStackName() + "/" + stack.getId());
50                         info.setStatus(this.mapStatus(stack.getStackStatus()));
51                         
52                         info.setStatusMessage(stack.getStackStatusReason());
53                         
54                         Optional<Map<String, Object>> result = this.mapOutputToMap(stack.getOutputs());
55                         if (result.isPresent()) {
56                                 info.setOutputs(result.get());
57                         }
58                         
59                         info.setParameters(stack.getParameters());
60                 }
61                 
62                 return info;
63         }
64         
65         protected HeatStatus mapStatus(String status) {
66                 final HeatStatus result;
67                 if (status == null) {
68                         result = HeatStatus.INIT;
69                 } else {
70                         result = heatStatusMap.getOrDefault(status, HeatStatus.UNKNOWN);
71                 }
72                 
73                 return result;
74         }
75         
76         protected Optional<Map<String, Object>> mapOutputToMap(List<Output> outputs) {
77                 Optional<Map<String, Object>> result = Optional.empty();
78                 if (outputs != null) {
79                         final HashMap<String, Object> map = new HashMap<>();
80                         for (Output output : outputs) {
81                                 map.put(output.getOutputKey(), output.getOutputValue());
82                         }
83                         result = Optional.of(map);
84                 }
85                 
86                 return result;
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         }
99 }