summaryrefslogtreecommitdiffstats
path: root/heat-model/src/main/java/com/woorea/openstack/heat/model/Stack.java
blob: 340f3d1f70bf672ecebeed30432c7c0fa8ca3f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*-
 * ============LICENSE_START=======================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package com.woorea.openstack.heat.model;

/*
 * Modifications copyright (c) 2017 AT&T Intellectual Property
 */

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName("stack")
public class Stack {
    @JsonProperty("description")
    private String description;

    @JsonProperty("links")
    private List<Link> links;

    @JsonProperty("stack_status_reason")
    private String stackStatusReason;

    @JsonProperty("stack_name")
    private String stackName;

    @JsonProperty("updated_time")
    private Date updatedTime;

    @JsonProperty("creation_time")
    private Date creationTime;

    @JsonProperty("stack_status")
    private String stackStatus;

    @JsonProperty("id")
    private String id;
    
    @JsonProperty("files")
    private Map<String, Object> files = null;
    
    // ObjectMapper instance to parse Json stack outputs
    @JsonIgnore
    private static ObjectMapper mapper = new ObjectMapper();

    public Date getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Date updatedTime) {
        this.updatedTime = updatedTime;
    }

    public String getStackStatus() {
        return stackStatus;
    }

    public void setStackStatus(String stackStatus) {
        this.stackStatus = stackStatus;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    public String getStackName() {
        return stackName;
    }

    public void setStackName(String stackName) {
        this.stackName = stackName;
    }

    public String getStackStatusReason() {
        return stackStatusReason;
    }

    public void setStackStatusReason(String stackStatusReason) {
        this.stackStatusReason = stackStatusReason;
    }

    public List<Link> getLinks() {
        return links;
    }

    public void setLinks(List<Link> links) {
        this.links = links;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
    
    public Map<String, Object> getFiles() {
        return this.files;
    }
    public void setFiles(Map<String, Object> files) {
        this.files = files;
    }


    @Override
    public String toString() {
        return "Stack{" +
                "description='" + description + '\'' +
                ", links=" + links +
                ", stackStatusReason='" + stackStatusReason + '\'' +
                ", stackName='" + stackName + '\'' +
                ", updatedTime=" + updatedTime +
                ", creationTime=" + creationTime +
                ", stackStatus='" + stackStatus + '\'' +
                ", id='" + id + '\'' +
                ", outputs='" + outputs + '\'' +
                ", parameters='" + parameters + '\'' +
                ", files='" + files + '\'' +
                '}';
    }
    
    @JsonIgnoreProperties(ignoreUnknown=true)
    public static final class Output {
        @JsonProperty("output_value")
        private Object outputValue;
        
        private String description;
        
        @JsonProperty("output_key")
        private String outputKey;
        
        public Object getOutputValue() {
            return outputValue;
        }

        public String getDescription() {
            return description;
        }

        public String getOutputKey() {
            return outputKey;
        }

        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "Output [key=" + outputKey + ", value="
                    + outputValue + "]";
        }
    }
    
    private List<Output> outputs;

    public List<Output> getOutputs() {
        return outputs;
    }
    
    private Object _findOutputValue (String key) {
        for (Output o : outputs) {
            if (o.getOutputKey().equals(key)) {
                return o.getOutputValue();
            }
        }
        return null;
    }
    
    /*
     * Return a stack output as a String.
     * Generally speaking, most outputs will be Strings.
     */
    public String getOutputValue (String key)
    {
        Object value = _findOutputValue(key);
        if (value != null)
            return value.toString();
        else
            return null;
    }
    
    /*
     * Return a stack output as a Json-mapped Object of the provided type.
     * This is useful for json-object stack outputs.
     */
    public <T> T getOutputValue (String key, Class<T> type)
    {
        try {
            String s = mapper.writeValueAsString(_findOutputValue(key));
            return mapper.readValue(s, type);
        }
        catch (IOException e) {
            return null;
        }
    }
    
    @JsonProperty("parameters")
    private Map<String,Object> parameters = new HashMap<>();
    
    public void setParameters (Map<String,Object> params)
    {
        // Need to "fix" comma-delimited-list parameters for pre-Juno Heat
        // (see https://bugs.launchpad.net/heat/+bug/1367393)
        parameters = params;
        
        for (Entry<String,Object> param : parameters.entrySet())
        {
            // CDL params are returned as a string with format:
            // "[u'<value1>',u'<value2>',...]"
            String value = param.getValue().toString();
            if (value.startsWith("[") && value.endsWith("]"))
            {
                param.setValue(value.substring(1,value.length()-1).replaceAll("u'([^\']+)'","$1"));
            }
        }
    }
    
    public Map<String,Object> getParameters() {
        return parameters;
    }
}