Merge "add new functionality to aai client"
[so.git] / adapters / mso-adapter-utils / src / main / java / org / onap / so / openstack / utils / MsoHeatUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.openstack.utils;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Calendar;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Optional;
33 import java.util.Set;
34
35 import org.onap.so.adapters.vdu.CloudInfo;
36 import org.onap.so.adapters.vdu.PluginAction;
37 import org.onap.so.adapters.vdu.VduArtifact;
38 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
39 import org.onap.so.adapters.vdu.VduException;
40 import org.onap.so.adapters.vdu.VduInstance;
41 import org.onap.so.adapters.vdu.VduModelInfo;
42 import org.onap.so.adapters.vdu.VduPlugin;
43 import org.onap.so.adapters.vdu.VduStateType;
44 import org.onap.so.adapters.vdu.VduStatus;
45 import org.onap.so.cloud.CloudConfig;
46 import org.onap.so.cloud.authentication.AuthenticationMethodFactory;
47 import org.onap.so.cloud.authentication.KeystoneAuthHolder;
48 import org.onap.so.cloud.authentication.KeystoneV3Authentication;
49 import org.onap.so.cloud.authentication.ServiceEndpointNotFoundException;
50 import org.onap.so.db.catalog.beans.CloudIdentity;
51 import org.onap.so.db.catalog.beans.CloudSite;
52 import org.onap.so.db.catalog.beans.HeatTemplate;
53 import org.onap.so.db.catalog.beans.HeatTemplateParam;
54 import org.onap.so.db.catalog.beans.ServerType;
55 import org.onap.so.logger.MessageEnum;
56
57 import org.onap.so.logger.MsoLogger;
58 import org.onap.so.openstack.beans.HeatCacheEntry;
59 import org.onap.so.openstack.beans.HeatStatus;
60 import org.onap.so.openstack.beans.StackInfo;
61 import org.onap.so.openstack.exceptions.MsoAdapterException;
62 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
63 import org.onap.so.openstack.exceptions.MsoException;
64 import org.onap.so.openstack.exceptions.MsoIOException;
65 import org.onap.so.openstack.exceptions.MsoOpenstackException;
66 import org.onap.so.openstack.exceptions.MsoStackAlreadyExists;
67 import org.onap.so.openstack.exceptions.MsoTenantNotFound;
68 import org.onap.so.openstack.mappers.StackInfoMapper;
69 import org.onap.so.utils.CryptoUtils;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.context.annotation.Primary;
72 import org.springframework.core.env.Environment;
73 import org.springframework.stereotype.Component;
74
75 import com.fasterxml.jackson.core.type.TypeReference;
76 import com.fasterxml.jackson.databind.JsonNode;
77 import com.fasterxml.jackson.databind.ObjectMapper;
78 import com.woorea.openstack.base.client.OpenStackConnectException;
79 import com.woorea.openstack.base.client.OpenStackRequest;
80 import com.woorea.openstack.base.client.OpenStackResponseException;
81 import com.woorea.openstack.heat.Heat;
82 import com.woorea.openstack.heat.model.CreateStackParam;
83 import com.woorea.openstack.heat.model.Resources;
84 import com.woorea.openstack.heat.model.Stack;
85 import com.woorea.openstack.heat.model.Stack.Output;
86 import com.woorea.openstack.heat.model.Stacks;
87 import com.woorea.openstack.keystone.Keystone;
88 import com.woorea.openstack.keystone.model.Access;
89 import com.woorea.openstack.keystone.model.Authentication;
90 import com.woorea.openstack.keystone.utils.KeystoneUtils;
91
92 @Primary
93 @Component
94 public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{
95
96     private static final String TOKEN_AUTH = "TokenAuth";
97
98     private static final String QUERY_ALL_STACKS = "QueryAllStacks";
99
100     private static final String DELETE_STACK = "DeleteStack";
101
102     protected static final String HEAT_ERROR = "HeatError";
103
104     protected static final String CREATE_STACK = "CreateStack";
105
106     // Fetch cloud configuration each time (may be cached in CloudConfig class)
107     @Autowired
108     protected CloudConfig cloudConfig;
109
110     @Autowired
111     private Environment environment;
112
113     @Autowired
114     private AuthenticationMethodFactory authenticationMethodFactory;
115
116     @Autowired
117     private MsoTenantUtilsFactory tenantUtilsFactory;
118     
119     @Autowired
120     private KeystoneV3Authentication keystoneV3Authentication;
121     
122     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, MsoHeatUtils.class);
123
124     // Properties names and variables (with default values)
125     protected String createPollIntervalProp = "org.onap.so.adapters.po.pollInterval";
126     private String deletePollIntervalProp = "org.onap.so.adapters.po.pollInterval";
127     private String deletePollTimeoutProp = "org.onap.so.adapters.po.pollTimeout";
128
129     protected static final String createPollIntervalDefault = "15";
130     private static final String deletePollIntervalDefault = "15";
131
132     private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
133
134     /**
135      * keep this old method signature here to maintain backwards compatibility. keep others as well.
136      * this method does not include environment, files, or heatFiles
137      */
138     public StackInfo createStack (String cloudSiteId,
139                                   String tenantId,
140                                   String stackName,
141                                   String heatTemplate,
142                                   Map <String, ?> stackInputs,
143                                   boolean pollForCompletion,
144                                   int timeoutMinutes) throws MsoException {
145         // Just call the new method with the environment & files variable set to null
146         return this.createStack (cloudSiteId,
147                                  tenantId,
148                                  stackName,
149                                  heatTemplate,
150                                  stackInputs,
151                                  pollForCompletion,
152                                  timeoutMinutes,
153                                  null,
154                                  null,
155                                  null,
156                                  true);
157     }
158
159     // This method has environment, but not files or heatFiles
160     public StackInfo createStack (String cloudSiteId,
161                                   String tenantId,
162                                   String stackName,
163                                   String heatTemplate,
164                                   Map <String, ?> stackInputs,
165                                   boolean pollForCompletion,
166                                   int timeoutMinutes,
167                                   String environment) throws MsoException {
168         // Just call the new method with the files/heatFiles variables set to null
169         return this.createStack (cloudSiteId,
170                                  tenantId,
171                                  stackName,
172                                  heatTemplate,
173                                  stackInputs,
174                                  pollForCompletion,
175                                  timeoutMinutes,
176                                  environment,
177                                  null,
178                                  null,
179                                  true);
180     }
181
182     // This method has environment and files, but not heatFiles.
183     public StackInfo createStack (String cloudSiteId,
184                                   String tenantId,
185                                   String stackName,
186                                   String heatTemplate,
187                                   Map <String, ?> stackInputs,
188                                   boolean pollForCompletion,
189                                   int timeoutMinutes,
190                                   String environment,
191                                   Map <String, Object> files) throws MsoException {
192         return this.createStack (cloudSiteId,
193                                  tenantId,
194                                  stackName,
195                                  heatTemplate,
196                                  stackInputs,
197                                  pollForCompletion,
198                                  timeoutMinutes,
199                                  environment,
200                                  files,
201                                  null,
202                                  true);
203     }
204
205     // This method has environment, files, heatfiles
206     public StackInfo createStack (String cloudSiteId,
207                                   String tenantId,
208                                   String stackName,
209                                   String heatTemplate,
210                                   Map <String, ?> stackInputs,
211                                   boolean pollForCompletion,
212                                   int timeoutMinutes,
213                                   String environment,
214                                   Map <String, Object> files,
215                                   Map <String, Object> heatFiles) throws MsoException {
216         return this.createStack (cloudSiteId,
217                                  tenantId,
218                                  stackName,
219                                  heatTemplate,
220                                  stackInputs,
221                                  pollForCompletion,
222                                  timeoutMinutes,
223                                  environment,
224                                  files,
225                                  heatFiles,
226                                  true);
227     }
228
229     /**
230      * Create a new Stack in the specified cloud location and tenant. The Heat template
231      * and parameter map are passed in as arguments, along with the cloud access credentials.
232      * It is expected that parameters have been validated and contain at minimum the required
233      * parameters for the given template with no extra (undefined) parameters..
234      *
235      * The Stack name supplied by the caller must be unique in the scope of this tenant.
236      * However, it should also be globally unique, as it will be the identifier for the
237      * resource going forward in Inventory. This latter is managed by the higher levels
238      * invoking this function.
239      *
240      * The caller may choose to let this function poll Openstack for completion of the
241      * stack creation, or may handle polling itself via separate calls to query the status.
242      * In either case, a StackInfo object will be returned containing the current status.
243      * When polling is enabled, a status of CREATED is expected. When not polling, a
244      * status of BUILDING is expected.
245      *
246      * An error will be thrown if the requested Stack already exists in the specified
247      * Tenant and Cloud.
248      *
249      * For 1510 - add "environment", "files" (nested templates), and "heatFiles" (get_files) as
250      * parameters for createStack. If environment is non-null, it will be added to the stack.
251      * The nested templates and get_file entries both end up being added to the "files" on the
252      * stack. We must combine them before we add them to the stack if they're both non-null.
253      *
254      * @param cloudSiteId The cloud (may be a region) in which to create the stack.
255      * @param tenantId The Openstack ID of the tenant in which to create the Stack
256      * @param stackName The name of the stack to create
257      * @param heatTemplate The Heat template
258      * @param stackInputs A map of key/value inputs
259      * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
260      * @param environment An optional yaml-format string to specify environmental parameters
261      * @param files a Map<String, Object> that lists the child template IDs (file is the string, object is an int of
262      *        Template id)
263      * @param heatFiles a Map<String, Object> that lists the get_file entries (fileName, fileBody)
264      * @param backout Donot delete stack on create Failure - defaulted to True
265      * @return A StackInfo object
266      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
267      */
268
269     @SuppressWarnings("unchecked")
270     public StackInfo createStack (String cloudSiteId,
271                                   String tenantId,
272                                   String stackName,
273                                   String heatTemplate,
274                                   Map <String, ?> stackInputs,
275                                   boolean pollForCompletion,
276                                   int timeoutMinutes,
277                                   String environment,
278                                   Map <String, Object> files,
279                                   Map <String, Object> heatFiles,
280                                   boolean backout) throws MsoException {
281
282         // Take out the multicloud inputs, if present.
283         for (String key : MsoMulticloudUtils.MULTICLOUD_INPUTS) {
284             if (stackInputs.containsKey(key)) {
285                 stackInputs.remove(key);
286                 if (stackInputs.isEmpty()) {
287                     break;
288                 }
289             }
290         }
291
292         CreateStackParam stack = createStackParam(stackName, heatTemplate, stackInputs, timeoutMinutes, environment, files, heatFiles);
293
294         // Obtain the cloud site information where we will create the stack
295         CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
296                 () -> new MsoCloudSiteNotFound(cloudSiteId));
297         LOGGER.debug("Found: " + cloudSite.toString());
298         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
299         // This could throw MsoTenantNotFound or MsoOpenstackException (both propagated)
300         Heat heatClient = getHeatClient (cloudSite, tenantId);
301         if (heatClient != null) {
302             LOGGER.debug("Found: " + heatClient.toString());
303         }
304
305         LOGGER.debug ("Ready to Create Stack (" + heatTemplate + ") with input params: " + stackInputs);
306
307         Stack heatStack = null;
308         try {
309             OpenStackRequest <Stack> request = heatClient.getStacks ().create (stack);
310             CloudIdentity cloudIdentity = cloudSite.getIdentityService();
311             request.header ("X-Auth-User", cloudIdentity.getMsoId ());
312             request.header ("X-Auth-Key", CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass ()));
313             heatStack = executeAndRecordOpenstackRequest (request);
314         } catch (OpenStackResponseException e) {
315             if (e.getStatus () == 409) {                
316                 MsoStackAlreadyExists me = new MsoStackAlreadyExists (stackName, tenantId, cloudSiteId);
317                 me.addContext (CREATE_STACK);
318                 throw me;
319         } else {
320                 LOGGER.debug("ERROR STATUS = " + e.getStatus() + ",\n" + e.getMessage() + "\n" + e.getLocalizedMessage());
321                 throw heatExceptionToMsoException (e, CREATE_STACK);
322             }
323         } catch (OpenStackConnectException e) {          
324             throw heatExceptionToMsoException (e, CREATE_STACK);
325         } catch (RuntimeException e) {           
326             throw runtimeExceptionToMsoException (e, CREATE_STACK);
327         }
328
329         // Subsequent access by the canonical name "<stack name>/<stack-id>".
330         // Otherwise, simple query by name returns a 302 redirect.
331         // NOTE: This is specific to the v1 Orchestration API.
332         String canonicalName = stackName + "/" + heatStack.getId ();
333        
334         if (pollForCompletion) {
335             heatStack = pollStackForCompletion(cloudSiteId, tenantId, stackName, timeoutMinutes, backout, heatClient,
336                                         heatStack, canonicalName);
337         } else {
338             // Get initial status, since it will have been null after the create.
339             heatStack = queryHeatStack (heatClient, canonicalName);
340             LOGGER.debug (heatStack.getStackStatus ());
341         }
342         return new StackInfoMapper(heatStack).map();
343     }
344
345         private Stack pollStackForCompletion(String cloudSiteId, String tenantId, String stackName, int timeoutMinutes,
346                         boolean backout, Heat heatClient, Stack heatStack, String canonicalName)
347                         throws MsoException, MsoOpenstackException {
348                 int createPollInterval = Integer.parseInt(this.environment.getProperty(createPollIntervalProp, createPollIntervalDefault));
349                 int pollTimeout = (timeoutMinutes * 60) + createPollInterval;
350                 int deletePollInterval = createPollInterval;
351                 int deletePollTimeout = pollTimeout;
352                 boolean createTimedOut = false;
353                 StringBuilder stackErrorStatusReason = new StringBuilder("");
354                 LOGGER.debug("createPollInterval=" + createPollInterval + ", pollTimeout=" + pollTimeout);
355
356                 while (true) {
357                     try {
358                         heatStack = queryHeatStack (heatClient, canonicalName);
359                         LOGGER.debug (heatStack.getStackStatus () + " (" + canonicalName + ")");
360                         try {
361                             LOGGER.debug("Current stack " + this.getOutputsAsStringBuilder(heatStack).toString());
362                         } catch (Exception e) {
363                             LOGGER.debug("an error occurred trying to print out the current outputs of the stack", e);
364                         }
365
366                         if ("CREATE_IN_PROGRESS".equals (heatStack.getStackStatus ())) {                       
367                             if (pollTimeout <= 0) {
368                                 LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName, heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Create stack timeout");
369                                 createTimedOut = true;
370                                 break;
371                             }
372                             sleep(createPollInterval * 1000L);
373                             pollTimeout -= createPollInterval;
374                                 LOGGER.debug("pollTimeout remaining: " + pollTimeout);
375                         } else {                    
376                                 stackErrorStatusReason.append("Stack error (" + heatStack.getStackStatus() + "): " + heatStack.getStackStatusReason());
377                             break;
378                         }
379                     } catch (MsoException me) {
380                         // Cannot query the stack status. Something is wrong.
381                         // Try to roll back the stack
382                         if (!backout)
383                         {
384                                 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in Create Stack, stack deletion suppressed");
385                         }
386                         else
387                         {
388                                 try {
389                                         LOGGER.debug("Create Stack error - unable to query for stack status - attempting to delete stack: " + canonicalName + " - This will likely fail and/or we won't be able to query to see if delete worked");
390                                         OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
391                                         executeAndRecordOpenstackRequest (request);
392                                         boolean deleted = false;
393                                         while (!deleted) {
394                                                 try {
395                                                         heatStack = queryHeatStack(heatClient, canonicalName);
396                                                         if (heatStack != null) {
397                                                                 LOGGER.debug(heatStack.getStackStatus());
398                                                                 if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
399                                                                         if (deletePollTimeout <= 0) {
400                                                                                 LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
401                                                                                                 heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError,
402                                                                                                 "Rollback: DELETE stack timeout");
403                                                                                 break;
404                                                                         } else {
405                                                                                 sleep(deletePollInterval * 1000L);
406                                                                                 deletePollTimeout -= deletePollInterval;
407                                                                         }
408                                                                 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
409                                                                         LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
410                                                                         deleted = true;
411                                                                         continue;
412                                                                 } else {
413                                                                         //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
414                                                                         break;
415                                                                 }
416                                                         } else {
417                                                                 // assume if we can't find it - it's deleted
418                                                                 LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
419                                                                 deleted = true;
420                                                                 continue;
421                                                         }
422
423                                                 } catch (Exception e3) {
424                                                         // Just log this one. We will report the original exception.
425                                                         LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack: Nested exception rolling back stack: " + e3, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack: Nested exception rolling back stack on error on query");
426
427                                                 }
428                                         }
429                                 } catch (Exception e2) {
430                                         // Just log this one. We will report the original exception.
431                                         LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack: Nested exception rolling back stack: " + e2, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack: Nested exception rolling back stack");
432                                 }
433                         }
434
435                         // Propagate the original exception from Stack Query.
436                         me.addContext (CREATE_STACK);
437                         throw me;
438                     }
439                 }
440
441                 if (!"CREATE_COMPLETE".equals (heatStack.getStackStatus ())) {
442                     LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack error:  Polling complete with non-success status: "
443                                   + heatStack.getStackStatus () + ", " + heatStack.getStackStatusReason (), "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error");
444
445                     // Rollback the stack creation, since it is in an indeterminate state.
446                     if (!backout)
447                     {
448                         LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error, stack deletion suppressed");
449                     }
450                     else
451                     {
452                         try {
453                                 LOGGER.debug("Create Stack errored - attempting to DELETE stack: " + canonicalName);
454                                 LOGGER.debug("deletePollInterval=" + deletePollInterval + ", deletePollTimeout=" + deletePollTimeout);
455                                 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
456                                 executeAndRecordOpenstackRequest (request);
457                                 boolean deleted = false;
458                                 while (!deleted) {
459                                         try {
460                                                 heatStack = queryHeatStack(heatClient, canonicalName);
461                                                 if (heatStack != null) {
462                                                         LOGGER.debug(heatStack.getStackStatus() + " (" + canonicalName + ")");
463                                                         if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
464                                                                 if (deletePollTimeout <= 0) {
465                                                                         LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
466                                                                                         heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError,
467                                                                                         "Rollback: DELETE stack timeout");
468                                                                         break;
469                                                                 } else {
470                                                                         sleep(deletePollInterval * 1000L);
471                                                                         deletePollTimeout -= deletePollInterval;
472                                                                         LOGGER.debug("deletePollTimeout remaining: " + deletePollTimeout);
473                                                                 }
474                                                         } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
475                                                                 LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
476                                                                 deleted = true;
477                                                                 continue;
478                                                         } else if ("DELETE_FAILED".equals(heatStack.getStackStatus())) {
479                                                                 // Warn about this (?) - but still throw the original exception
480                                                                 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion FAILED", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error, stack deletion FAILED");
481                                                                 LOGGER.debug("Stack deletion FAILED on a rollback of a create - " + canonicalName + ", status=" + heatStack.getStackStatus() + ", reason=" + heatStack.getStackStatusReason());
482                                                                 break;
483                                                         } else {
484                                                                 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
485                                                                 break;
486                                                         }
487                                                 } else {
488                                                         // assume if we can't find it - it's deleted
489                                                         LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
490                                                         deleted = true;
491                                                         continue;
492                                                 }
493
494                                         } catch (MsoException me2) {
495                                                 // We got an exception on the delete - don't throw this exception - throw the original - just log.
496                                                 LOGGER.debug("Exception thrown trying to delete " + canonicalName + " on a create->rollback: " + me2.getContextMessage(), me2);
497                                                 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, then stack deletion FAILED - exception thrown", "", "", MsoLogger.ErrorCode.BusinessProcesssError, me2.getContextMessage());
498                                         }
499
500                                 } // end while !deleted
501                                 StringBuilder errorContextMessage;
502                                 if (createTimedOut) {
503                                         errorContextMessage = new StringBuilder("Stack Creation Timeout");
504                                 } else {
505                                         errorContextMessage  = stackErrorStatusReason;
506                                 }
507                                 if (deleted) {
508                                         errorContextMessage.append(" - stack successfully deleted");
509                                 } else {
510                                         errorContextMessage.append(" - encountered an error trying to delete the stack");
511                                 }
512                         } catch (Exception e2) {
513                                 // shouldn't happen - but handle
514                                 LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack: Nested exception rolling back stack: " + e2, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in Create Stack: rolling back stack");
515                         }
516                     }
517                     MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
518                     me.addContext(CREATE_STACK);
519                     throw me;
520                 }
521                 return heatStack;
522         }
523
524     /**
525      * Query for a single stack (by Name) in a tenant. This call will always return a
526      * StackInfo object. If the stack does not exist, an "empty" StackInfo will be
527      * returned - containing only the stack name and a status of NOTFOUND.
528      *
529      * @param tenantId The Openstack ID of the tenant in which to query
530      * @param cloudSiteId The cloud identifier (may be a region) in which to query
531      * @param stackName The name of the stack to query (may be simple or canonical)
532      * @return A StackInfo object
533      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
534      */
535     public StackInfo queryStack (String cloudSiteId, String tenantId, String stackName) throws MsoException {
536         LOGGER.debug ("Query HEAT stack: " + stackName + " in tenant " + tenantId);
537
538         // Obtain the cloud site information where we will create the stack
539         CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
540                 () -> new MsoCloudSiteNotFound(cloudSiteId));
541         LOGGER.debug("Found: " + cloudSite.toString());
542
543         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
544         Heat heatClient = null;
545         try {
546             heatClient = getHeatClient (cloudSite, tenantId);
547             if (heatClient != null) {
548                 LOGGER.debug("Found: " + heatClient.toString());
549             }
550         } catch (MsoTenantNotFound e) {
551             // Tenant doesn't exist, so stack doesn't either
552             LOGGER.debug ("Tenant with id " + tenantId + "not found.", e);
553             return new StackInfo (stackName, HeatStatus.NOTFOUND);
554         } catch (MsoException me) {
555             // Got an Openstack error. Propagate it
556             LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Exception on Token request: " + me, "Openstack", "", MsoLogger.ErrorCode.AvailabilityError, "Connection Exception");
557             me.addContext ("QueryStack");
558             throw me;
559         }
560
561         // Query the Stack.
562         // An MsoException will propagate transparently to the caller.
563         Stack heatStack = queryHeatStack (heatClient, stackName);
564
565         if (heatStack == null) {
566             // Stack does not exist. Return a StackInfo with status NOTFOUND
567             return new StackInfo (stackName, HeatStatus.NOTFOUND);
568         }
569
570         return new StackInfoMapper(heatStack).map();
571     }
572
573     /**
574      * Delete a stack (by Name/ID) in a tenant. If the stack is not found, it will be
575      * considered a successful deletion. The return value is a StackInfo object which
576      * contains the current stack status.
577      *
578      * The client may choose to let the adapter poll Openstack for completion of the
579      * stack deletion, or may handle polling itself via separate query calls. In either
580      * case, a StackInfo object will be returned. When polling is enabled, a final
581      * status of NOTFOUND is expected. When not polling, a status of DELETING is expected.
582      *
583      * There is no rollback from a successful stack deletion. A deletion failure will
584      * also result in an undefined stack state - the components may or may not have been
585      * all or partially deleted, so the resulting stack must be considered invalid.
586      *
587      * @param tenantId The Openstack ID of the tenant in which to perform the delete
588      * @param cloudSiteId The cloud identifier (may be a region) from which to delete the stack.
589      * @param stackName The name/id of the stack to delete. May be simple or canonical
590      * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
591      * @return A StackInfo object
592      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
593      * @throws MsoCloudSiteNotFound
594      */
595     public StackInfo deleteStack (String tenantId,
596                                   String cloudSiteId,
597                                   String stackName,
598                                   boolean pollForCompletion) throws MsoException {
599         // Obtain the cloud site information where we will create the stack
600         CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
601                 () -> new MsoCloudSiteNotFound(cloudSiteId));
602         LOGGER.debug("Found: " + cloudSite.toString());
603
604         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
605         Heat heatClient = null;
606         try {
607             heatClient = getHeatClient (cloudSite, tenantId);
608             if (heatClient != null) {
609                 LOGGER.debug("Found: " + heatClient.toString());
610             }
611         } catch (MsoTenantNotFound e) {
612             // Tenant doesn't exist, so stack doesn't either
613             LOGGER.debug ("Tenant with id " + tenantId + "not found.", e);
614             return new StackInfo (stackName, HeatStatus.NOTFOUND);
615         } catch (MsoException me) {
616             // Got an Openstack error. Propagate it
617             LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack", "Openstack Exception on Token request: " + me, "Openstack", "", MsoLogger.ErrorCode.AvailabilityError, "Connection Exception");
618             me.addContext (DELETE_STACK);
619             throw me;
620         }
621
622         // OK if stack not found, perform a query first
623         Stack heatStack = queryHeatStack (heatClient, stackName);
624         if (heatStack == null || "DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
625             // Not found. Return a StackInfo with status NOTFOUND
626             return new StackInfo (stackName, HeatStatus.NOTFOUND);
627         }
628
629         // Delete the stack.
630
631         // Use canonical name "<stack name>/<stack-id>" to delete.
632         // Otherwise, deletion by name returns a 302 redirect.
633         // NOTE: This is specific to the v1 Orchestration API.
634         String canonicalName = heatStack.getStackName () + "/" + heatStack.getId ();
635
636         try {
637             OpenStackRequest <Void> request = null;
638             if(null != heatClient) {
639                 request = heatClient.getStacks ().deleteByName (canonicalName);
640             }
641             else {
642                 LOGGER.debug ("Heat Client is NULL" );
643             }
644
645             executeAndRecordOpenstackRequest (request);
646         } catch (OpenStackResponseException e) {
647             if (e.getStatus () == 404) {
648                 // Not found. We are OK with this. Return a StackInfo with status NOTFOUND
649                 return new StackInfo (stackName, HeatStatus.NOTFOUND);
650             } else {
651                 // Convert the OpenStackResponseException to an MsoOpenstackException
652                 throw heatExceptionToMsoException (e, DELETE_STACK);
653             }
654         } catch (OpenStackConnectException e) {
655             // Error connecting to Openstack instance. Convert to an MsoException
656             throw heatExceptionToMsoException (e, DELETE_STACK);
657         } catch (RuntimeException e) {
658             // Catch-all
659             throw runtimeExceptionToMsoException (e, DELETE_STACK);
660         }
661
662         // Requery the stack for current status.
663         // It will probably still exist with "DELETE_IN_PROGRESS" status.
664         heatStack = queryHeatStack (heatClient, canonicalName);
665
666         if (pollForCompletion) {
667             // Set a timeout on polling
668
669             int pollInterval = Integer.parseInt(this.environment.getProperty(deletePollIntervalProp, "" + deletePollIntervalDefault));
670             int pollTimeout = Integer.parseInt(this.environment.getProperty(deletePollTimeoutProp, "" + deletePollIntervalDefault));
671
672             // When querying by canonical name, Openstack returns DELETE_COMPLETE status
673             // instead of "404" (which would result from query by stack name).
674             while (heatStack != null && !"DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
675                 LOGGER.debug ("Stack status: " + heatStack.getStackStatus ());
676
677                 if ("DELETE_FAILED".equals (heatStack.getStackStatus ())) {
678                     // Throw a 'special case' of MsoOpenstackException to report the Heat status
679                     String error = "Stack delete error (" + heatStack.getStackStatus ()
680                                    + "): "
681                                    + heatStack.getStackStatusReason ();
682                     MsoOpenstackException me = new MsoOpenstackException (0, "", error);
683                     me.addContext (DELETE_STACK);
684
685                     // Alarm this condition, stack deletion failed
686
687
688                     throw me;
689                 }
690
691                 if (pollTimeout <= 0) {
692                     LOGGER.error (MessageEnum.RA_DELETE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName, heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Delete Stack Timeout");
693
694                     // Throw a 'special case' of MsoOpenstackException to report the Heat status
695                     MsoOpenstackException me = new MsoOpenstackException (0, "", "Stack Deletion Timeout");
696                     me.addContext (DELETE_STACK);
697
698                     // Alarm this condition, stack deletion failed
699
700
701                     throw me;
702                 }
703
704                 sleep(pollInterval * 1000L);
705
706                 pollTimeout -= pollInterval;
707                 LOGGER.debug("pollTimeout remaining: " + pollTimeout);
708
709                 heatStack = queryHeatStack (heatClient, canonicalName);
710             }
711
712             // The stack is gone when this point is reached
713             return new StackInfo (stackName, HeatStatus.NOTFOUND);
714         }
715
716         // Return the current status (if not polling, the delete may still be in progress)
717         StackInfo stackInfo = new StackInfoMapper(heatStack).map();
718         stackInfo.setName (stackName);
719
720         return stackInfo;
721     }
722
723     /**
724      * Query for all stacks in a tenant site. This call will return a List of StackInfo
725      * objects, one for each deployed stack.
726      *
727      * Note that this is limited to a single site. To ensure that a tenant is truly
728      * empty would require looping across all tenant endpoints.
729      *
730      * @param tenantId The Openstack ID of the tenant to query
731      * @param cloudSiteId The cloud identifier (may be a region) in which to query.
732      * @return A List of StackInfo objects
733      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
734      * @throws MsoCloudSiteNotFound
735      */
736     public List <StackInfo> queryAllStacks (String tenantId, String cloudSiteId) throws MsoException {
737         // Obtain the cloud site information where we will create the stack
738         CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
739                 () -> new MsoCloudSiteNotFound(cloudSiteId));
740         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
741         Heat heatClient = getHeatClient (cloudSite, tenantId);
742
743         try {
744             OpenStackRequest <Stacks> request = heatClient.getStacks ().list ();
745             Stacks stacks = executeAndRecordOpenstackRequest (request);
746
747             List <StackInfo> stackList = new ArrayList <> ();
748
749             // Not sure if returns an empty list or null if no stacks exist
750             if (stacks != null) {
751                 for (Stack stack : stacks) {
752                     stackList.add (new StackInfoMapper(stack).map());
753                 }
754             }
755
756             return stackList;
757         } catch (OpenStackResponseException e) {
758             if (e.getStatus () == 404) {
759                 // Not sure if this can happen, but return an empty list
760                 LOGGER.debug ("queryAllStacks - stack not found: ");
761                 return new ArrayList <> ();
762             } else {
763                 // Convert the OpenStackResponseException to an MsoOpenstackException
764                 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
765             }
766         } catch (OpenStackConnectException e) {
767             // Error connecting to Openstack instance. Convert to an MsoException
768             throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
769         } catch (RuntimeException e) {
770             // Catch-all
771             throw runtimeExceptionToMsoException (e, QUERY_ALL_STACKS);
772         }
773     }
774
775     /**
776      * Validate parameters to be passed to Heat template. This method performs
777      * three functions:
778      * 1. Apply default values to parameters which have them defined
779      * 2. Report any required parameters that are missing. This will generate an
780      * exception in the caller, since stack create/update operations would fail.
781      * 3. Report and remove any extraneous parameters. This will allow clients to
782      * pass supersets of parameters and not get errors.
783      *
784      * These functions depend on the HeatTemplate definition from the MSO Catalog DB,
785      * along with the input parameter Map. The output is an updated parameter map.
786      * If the parameters are invalid for the template, an IllegalArgumentException
787      * is thrown.
788      */
789     public Map <String, Object> validateStackParams (Map <String, Object> inputParams,
790                                                      HeatTemplate heatTemplate) {
791         // Check that required parameters have been supplied for this template type
792         StringBuilder missingParams = null;
793         List <String> paramList = new ArrayList <> ();
794
795         // TODO: Enhance DB to support defaults for Heat Template parameters
796
797         for (HeatTemplateParam parm : heatTemplate.getParameters ()) {
798             if (parm.isRequired () && !inputParams.containsKey (parm.getParamName ())) {
799                 if (missingParams == null) {
800                     missingParams = new StringBuilder(parm.getParamName());
801                 } else {
802                     missingParams.append("," + parm.getParamName());
803                 }
804             }
805             paramList.add (parm.getParamName ());
806         }
807         if (missingParams != null) {
808             // Problem - missing one or more required parameters
809             String error = "Missing Required inputs for HEAT Template: " + missingParams;
810             LOGGER.error (MessageEnum.RA_MISSING_PARAM, missingParams + " for HEAT Template", "", "", MsoLogger.ErrorCode.SchemaError, "Missing Required inputs for HEAT Template: " + missingParams);
811             throw new IllegalArgumentException (error);
812         }
813
814         // Remove any extraneous parameters (don't throw an error)
815         Map <String, Object> updatedParams = new HashMap <> ();
816         List <String> extraParams = new ArrayList <> ();
817
818         for (Entry<String, Object> entry : inputParams.entrySet()) {
819                 if (!paramList.contains(entry.getKey())) {
820                         // This is not a valid parameter for this template
821                         extraParams.add(entry.getKey());
822                 } else {
823                         updatedParams.put(entry.getKey(), entry.getValue());
824                 }
825         }
826
827         if (!extraParams.isEmpty ()) {
828             LOGGER.warn (MessageEnum.RA_GENERAL_WARNING, "Heat Stack (" + heatTemplate.getTemplateName ()
829                          + ") extra input params received: "
830                          + extraParams, "", "", MsoLogger.ErrorCode.DataError, "Heat Stack (" + heatTemplate.getTemplateName () + ") extra input params received: "+ extraParams);
831         }
832
833         return updatedParams;
834     }
835
836     // ---------------------------------------------------------------
837     // PRIVATE FUNCTIONS FOR USE WITHIN THIS CLASS
838
839     /**
840      * Get a Heat client for the Openstack Identity service.
841      * This requires a 'member'-level userId + password, which will be retrieved from
842      * properties based on the specified cloud Id. The tenant in which to operate
843      * must also be provided.
844      * <p>
845      * On successful authentication, the Heat object will be cached for the
846      * tenantID + cloudId so that it can be reused without reauthenticating with
847      * Openstack every time.
848      *
849      * @return an authenticated Heat object
850      */
851     public Heat getHeatClient (CloudSite cloudSite, String tenantId) throws MsoException {
852         String cloudId = cloudSite.getId();
853         // For DCP/LCP, the region should be the cloudId.
854         String region = cloudSite.getRegionId ();
855
856         // Obtain an MSO token for the tenant
857         CloudIdentity cloudIdentity = cloudSite.getIdentityService();
858         LOGGER.debug("Found: " + cloudIdentity.toString());
859         MsoTenantUtils tenantUtils = tenantUtilsFactory.getTenantUtilsByServerType(cloudIdentity.getIdentityServerType());
860         String keystoneUrl = tenantUtils.getKeystoneUrl(cloudId, cloudIdentity);
861         LOGGER.debug("keystoneUrl=" + keystoneUrl);
862         String heatUrl = null;
863         String tokenId = null;
864         Calendar expiration = null;
865             try {
866                 if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
867                         Keystone keystoneTenantClient = new Keystone (keystoneUrl);
868                         Access access = null;
869                 
870                         Authentication credentials = authenticationMethodFactory.getAuthenticationFor(cloudIdentity);
871         
872                         OpenStackRequest <Access> request = keystoneTenantClient.tokens ()
873                                .authenticate (credentials).withTenantId (tenantId);
874         
875                     access = executeAndRecordOpenstackRequest (request);
876                 
877                         try {
878                                 // Isolate trying to printout the region IDs
879                                 try {
880                                         LOGGER.debug("access=" + access.toString());
881                                         for (Access.Service service : access.getServiceCatalog()) {
882                                                 List<Access.Service.Endpoint> endpoints = service.getEndpoints();
883                                                 for (Access.Service.Endpoint endpoint : endpoints) {
884                                                         LOGGER.debug("AIC returned region=" + endpoint.getRegion());
885                                                 }
886                                         }
887                                 } catch (Exception e) {
888                                         LOGGER.debug("Encountered an error trying to printout Access object returned from AIC. " + e.getMessage());
889                                 }
890                             heatUrl = KeystoneUtils.findEndpointURL (access.getServiceCatalog (), "orchestration", region, "public");
891                             LOGGER.debug("heatUrl=" + heatUrl + ", region=" + region);
892                         } catch (RuntimeException e) {
893                             // This comes back for not found (probably an incorrect region ID)
894                             String error = "AIC did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
895                             throw new MsoAdapterException (error, e);
896                         }
897                         tokenId = access.getToken ().getId ();
898                         expiration = access.getToken ().getExpires ();
899                 } else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
900                         try {
901                                 KeystoneAuthHolder holder = keystoneV3Authentication.getToken(cloudSite, tenantId, "orchestration");
902                                 tokenId = holder.getId();
903                                 expiration = holder.getexpiration();
904                                 heatUrl = holder.getServiceUrl();
905                         } catch (ServiceEndpointNotFoundException e) {
906                                 // This comes back for not found (probably an incorrect region ID)
907                             String error = "cloud did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
908                             throw new MsoAdapterException (error, e);
909                         }
910                 }
911             } catch (OpenStackResponseException e) {
912             if (e.getStatus () == 401) {
913                 // Authentication error.
914                 String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId ();
915
916                 throw new MsoAdapterException (error);
917             } else {
918                 throw keystoneErrorToMsoException (e, TOKEN_AUTH);
919             }
920         } catch (OpenStackConnectException e) {
921             // Connection to Openstack failed
922             MsoIOException me = new MsoIOException (e.getMessage (), e);
923             me.addContext (TOKEN_AUTH);
924             throw me;
925         } catch (RuntimeException e) {
926             // Catch-all
927             throw runtimeExceptionToMsoException (e, TOKEN_AUTH);
928         }
929         Heat heatClient = new Heat (heatUrl);
930         heatClient.token (tokenId);
931         return heatClient;
932     }
933
934     /*
935      * Query for a Heat Stack. This function is needed in several places, so
936      * a common method is useful. This method takes an authenticated Heat Client
937      * (which internally identifies the cloud & tenant to search), and returns
938      * a Stack object if found, Null if not found, or an MsoOpenstackException
939      * if the Openstack API call fails.
940      *
941      * The stack name may be a simple name or a canonical name ("{name}/{id}").
942      * When simple name is used, Openstack always returns a 302 redirect which
943      * results in a 2nd request (to the canonical name). Note that query by
944      * canonical name for a deleted stack returns a Stack object with status
945      * "DELETE_COMPLETE" while query by simple name for a deleted stack returns
946      * HTTP 404.
947      *
948      * @param heatClient an authenticated Heat client
949      *
950      * @param stackName the stack name to query
951      *
952      * @return a Stack object that describes the current stack or null if the
953      * requested stack doesn't exist.
954      *
955      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
956      */
957     protected Stack queryHeatStack (Heat heatClient, String stackName) throws MsoException {
958         if (stackName == null) {
959             return null;
960         }
961         try {
962             OpenStackRequest <Stack> request = heatClient.getStacks ().byName (stackName);
963             return executeAndRecordOpenstackRequest (request);
964         } catch (OpenStackResponseException e) {
965             if (e.getStatus () == 404) {
966                 LOGGER.debug ("queryHeatStack - stack not found: " + stackName);
967                 return null;
968             } else {
969                 // Convert the OpenStackResponseException to an MsoOpenstackException
970                 throw heatExceptionToMsoException (e, "QueryStack");
971             }
972         } catch (OpenStackConnectException e) {
973             // Connection to Openstack failed
974             throw heatExceptionToMsoException (e, "QueryAllStack");
975         }
976     }
977
978
979         public Map<String, Object> queryStackForOutputs(String cloudSiteId,
980                         String tenantId, String stackName) throws MsoException {
981                 LOGGER.debug("MsoHeatUtils.queryStackForOutputs)");
982                 StackInfo heatStack = this.queryStack(cloudSiteId, tenantId, stackName);
983                 if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
984                         return null;
985                 }
986                 return heatStack.getOutputs();
987         }
988
989         public void copyStringOutputsToInputs(Map<String, String> inputs,
990                         Map<String, Object> otherStackOutputs, boolean overWrite) {
991                 if (inputs == null || otherStackOutputs == null)
992                         return;
993                 for (String key : otherStackOutputs.keySet()) {
994                         if (!inputs.containsKey(key)) {
995                                 Object obj = otherStackOutputs.get(key);
996                                 if (obj instanceof String) {
997                                         inputs.put(key, (String) otherStackOutputs.get(key));
998                                 } else if (obj instanceof JsonNode ){
999                                         // This is a bit of mess - but I think it's the least impacting
1000                                         // let's convert it BACK to a string - then it will get converted back later
1001                                         try {
1002                                                 String str = this.convertNode((JsonNode) obj);
1003                                                 inputs.put(key, str);
1004                                         } catch (Exception e) {
1005                                                 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for JsonNode "+ key, e);
1006                                                 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1007                                         }
1008                                 } else if (obj instanceof java.util.LinkedHashMap) {
1009                                         LOGGER.debug("LinkedHashMap - this is showing up as a LinkedHashMap instead of JsonNode");
1010                                         try {
1011                                                 String str = JSON_MAPPER.writeValueAsString(obj);
1012                                                 inputs.put(key, str);
1013                                         } catch (Exception e) {
1014                                                 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for LinkedHashMap "+ key, e);
1015                                         }
1016                                 } else if (obj instanceof Integer) {
1017                                         try {
1018                                                 String str = "" + obj;
1019                                                 inputs.put(key, str);
1020                                         } catch (Exception e) {
1021                                                 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for Integer "+ key, e);
1022                                         }
1023                                 } else {
1024                                         try {
1025                                                 String str = obj.toString();
1026                                                 inputs.put(key, str);
1027                                         } catch (Exception e) {
1028                                                 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for Other "+ key +" (" + e.getMessage() + ")", e);
1029                                                 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1030                                         }
1031                                 }
1032                         }
1033                 }
1034                 return;
1035         }
1036         public StringBuilder requestToStringBuilder(CreateStackParam stack) {
1037                 StringBuilder sb = new StringBuilder();
1038                 sb.append("Stack:\n");
1039                 sb.append("\tStackName: " + stack.getStackName());
1040                 sb.append("\tTemplateUrl: " + stack.getTemplateUrl());
1041                 sb.append("\tTemplate: " + stack.getTemplate());
1042                 sb.append("\tEnvironment: " + stack.getEnvironment());
1043                 sb.append("\tTimeout: " + stack.getTimeoutMinutes());
1044                 sb.append("\tParameters:\n");
1045                 Map<String, Object> params = stack.getParameters();
1046                 if (params == null || params.size() < 1) {
1047                         sb.append("\nNONE");
1048                 } else {
1049                         for (String key : params.keySet()) {
1050                                 if (params.get(key) instanceof String) {
1051                                         sb.append("\n").append(key).append("=").append((String) params.get(key));
1052                                 } else if (params.get(key) instanceof JsonNode) {
1053                                         String jsonStringOut = this.convertNode((JsonNode)params.get(key));
1054                                         sb.append("\n").append(key).append("=").append(jsonStringOut);
1055                                 } else if (params.get(key) instanceof Integer) {
1056                                         String integerOut = "" + params.get(key);
1057                                         sb.append("\n").append(key).append("=").append(integerOut);
1058
1059                                 } else {
1060                                         try {
1061                                                 String str = params.get(key).toString();
1062                                                 sb.append("\n").append(key).append("=").append(str);
1063                                         } catch (Exception e) {
1064                                                 LOGGER.debug("Exception :",e);
1065                                         }
1066                                 }
1067                         }
1068                 }
1069                 return sb;
1070         }
1071
1072         private String convertNode(final JsonNode node) {
1073                 try {
1074                         final Object obj = JSON_MAPPER.treeToValue(node, Object.class);
1075                         final String json = JSON_MAPPER.writeValueAsString(obj);
1076                         return json;
1077                 } catch (Exception e) {
1078                         LOGGER.debug("Error converting json to string " + e.getMessage(), e);
1079                 }
1080                 return "[Error converting json to string]";
1081         }
1082
1083
1084         protected StringBuilder getOutputsAsStringBuilder(Stack heatStack) {
1085                 // This should only be used as a utility to print out the stack outputs
1086                 // to the log
1087                 StringBuilder sb = new StringBuilder("");
1088                 if (heatStack == null) {
1089                         sb.append("(heatStack is null)");
1090                         return sb;
1091                 }
1092                 List<Output> outputList = heatStack.getOutputs();
1093                 if (outputList == null || outputList.isEmpty()) {
1094                         sb.append("(outputs is empty)");
1095                         return sb;
1096                 }
1097                 Map<String, Object> outputs = new HashMap<>();
1098                 for (Output outputItem : outputList) {
1099                         outputs.put(outputItem.getOutputKey(), outputItem.getOutputValue());
1100                 }
1101                 int counter = 0;
1102                 sb.append("OUTPUTS:\n");
1103                 for (String key : outputs.keySet()) {
1104                         sb.append("outputs[").append(counter++).append("]: ").append(key).append("=");
1105                         Object obj = outputs.get(key);
1106                         if (obj instanceof String) {
1107                                 sb.append((String) obj).append(" (a string)");
1108                         } else if (obj instanceof JsonNode) {
1109                                 sb.append(this.convertNode((JsonNode) obj)).append(" (a JsonNode)");
1110                         } else if (obj instanceof java.util.LinkedHashMap) {
1111                                 try {
1112                                         String str = JSON_MAPPER.writeValueAsString(obj);
1113                                         sb.append(str).append(" (a java.util.LinkedHashMap)");
1114                                 } catch (Exception e) {
1115                                         LOGGER.debug("Exception :",e);
1116                                         sb.append("(a LinkedHashMap value that would not convert nicely)");
1117                                 }
1118                         } else if (obj instanceof Integer) {
1119                                 String str = "";
1120                                 try {
1121                                         str = obj.toString() + " (an Integer)\n";
1122                                 } catch (Exception e) {
1123                                         LOGGER.debug("Exception :",e);
1124                                         str = "(an Integer unable to call .toString() on)";
1125                                 }
1126                                 sb.append(str);
1127                         } else if (obj instanceof ArrayList) {
1128                                 String str = "";
1129                                 try {
1130                                         str = obj.toString() + " (an ArrayList)";
1131                                 } catch (Exception e) {
1132                                         LOGGER.debug("Exception :",e);
1133                                         str = "(an ArrayList unable to call .toString() on?)";
1134                                 }
1135                                 sb.append(str);
1136                         } else if (obj instanceof Boolean) {
1137                                 String str = "";
1138                                 try {
1139                                         str = obj.toString() + " (a Boolean)";
1140                                 } catch (Exception e) {
1141                                         LOGGER.debug("Exception :",e);
1142                                         str = "(an Boolean unable to call .toString() on?)";
1143                                 }
1144                                 sb.append(str);
1145                         }
1146                         else {
1147                                 String str = "";
1148                                 try {
1149                                         str = obj.toString() + " (unknown Object type)";
1150                                 } catch (Exception e) {
1151                                         LOGGER.debug("Exception :",e);
1152                                         str = "(a value unable to call .toString() on?)";
1153                                 }
1154                                 sb.append(str);
1155                         }
1156                         sb.append("\n");
1157                 }
1158                 sb.append("[END]");
1159                 return sb;
1160         }
1161  
1162
1163         public void copyBaseOutputsToInputs(Map<String, Object> inputs,
1164                         Map<String, Object> otherStackOutputs, List<String> paramNames, Map<String, String> aliases) {
1165                 if (inputs == null || otherStackOutputs == null)
1166                         return;
1167                 for (String key : otherStackOutputs.keySet()) {
1168                         if (paramNames != null) {
1169                                 if (!paramNames.contains(key) && !aliases.containsKey(key)) {
1170                                         LOGGER.debug("\tParameter " + key + " is NOT defined to be in the template - do not copy to inputs");
1171                                         continue;
1172                                 }
1173                                 if (aliases.containsKey(key)) {
1174                                         LOGGER.debug("Found an alias! Will move " + key + " to " + aliases.get(key));
1175                                         Object obj = otherStackOutputs.get(key);
1176                                         key = aliases.get(key);
1177                                         otherStackOutputs.put(key, obj);
1178                                 }
1179                         }
1180                         if (!inputs.containsKey(key)) {
1181                                 Object obj = otherStackOutputs.get(key);
1182                                 LOGGER.debug("\t**Adding " + key + " to inputs (.toString()=" + obj.toString());
1183                                 if (obj instanceof String) {
1184                                         LOGGER.debug("\t\t**A String");
1185                                         inputs.put(key, obj);
1186                                 } else if (obj instanceof Integer) {
1187                                         LOGGER.debug("\t\t**An Integer");
1188                                         inputs.put(key, obj);
1189                                 } else if (obj instanceof JsonNode) {
1190                                         LOGGER.debug("\t\t**A JsonNode");
1191                                         inputs.put(key, obj);
1192                                 } else if (obj instanceof Boolean) {
1193                                         LOGGER.debug("\t\t**A Boolean");
1194                                         inputs.put(key, obj);
1195                                 } else if (obj instanceof java.util.LinkedHashMap) {
1196                                         LOGGER.debug("\t\t**A java.util.LinkedHashMap **");
1197                                         inputs.put(key, obj);
1198                                 } else if (obj instanceof java.util.ArrayList) {
1199                                         LOGGER.debug("\t\t**An ArrayList");
1200                                         inputs.put(key, obj);
1201                                 } else {
1202                                         LOGGER.debug("\t\t**UNKNOWN OBJECT TYPE");
1203                                         inputs.put(key, obj);
1204                                 }
1205                         } else {
1206                                 LOGGER.debug("key=" + key + " is already in the inputs - will not overwrite");
1207                         }
1208                 }
1209                 return;
1210         }
1211
1212         public List<String> convertCdlToArrayList(String cdl) {
1213                 String cdl2 = cdl.trim();
1214                 String cdl3;
1215                 if (cdl2.startsWith("[") && cdl2.endsWith("]")) {
1216                         cdl3 = cdl2.substring(1, cdl2.lastIndexOf("]"));
1217                 } else {
1218                         cdl3 = cdl2;
1219                 }
1220                 return new ArrayList<>(Arrays.asList(cdl3.split(",")));
1221         }
1222
1223     /**
1224      * New with 1707 - this method will convert all the String *values* of the inputs
1225      * to their "actual" object type (based on the param type: in the db - which comes from the template):
1226      * (heat variable type) -> java Object type
1227      * string -> String
1228      * number -> Integer
1229      * json -> JsonNode XXX Removed with MSO-1475 / 1802
1230      * comma_delimited_list -> ArrayList
1231      * boolean -> Boolean
1232      * if any of the conversions should fail, we will default to adding it to the inputs
1233      * as a string - see if Openstack can handle it.
1234      * Also, will remove any params that are extra.
1235      * Any aliases will be converted to their appropriate name (anyone use this feature?)
1236      * @param inputs - the Map<String, String> of the inputs received on the request
1237      * @param template the HeatTemplate object - this is so we can also verify if the param is valid for this template
1238      * @return HashMap<String, Object> of the inputs, cleaned and converted
1239      */
1240         public Map<String, Object> convertInputMap(Map<String, String> inputs, HeatTemplate template) {
1241                 HashMap<String, Object> newInputs = new HashMap<>();
1242                 HashMap<String, HeatTemplateParam> params = new HashMap<>();
1243                 HashMap<String, HeatTemplateParam> paramAliases = new HashMap<>();
1244
1245                 if (inputs == null) {
1246                         LOGGER.debug("convertInputMap - inputs is null - nothing to do here");
1247                         return new HashMap<>();
1248                 }
1249
1250                 LOGGER.debug("convertInputMap in MsoHeatUtils called, with " + inputs.size() + " inputs, and template " + template.getArtifactUuid());
1251                 try {
1252                         LOGGER.debug(template.toString());
1253                         Set<HeatTemplateParam> paramSet = template.getParameters();
1254                         LOGGER.debug("paramSet has " + paramSet.size() + " entries");
1255                 } catch (Exception e) {
1256                         LOGGER.debug("Exception occurred in convertInputMap:" + e.getMessage(), e);
1257                 }
1258
1259                 for (HeatTemplateParam htp : template.getParameters()) {
1260                         LOGGER.debug("Adding " + htp.getParamName());
1261                         params.put(htp.getParamName(), htp);
1262                         if (htp.getParamAlias() != null && !"".equals(htp.getParamAlias())) {
1263                                 LOGGER.debug("\tFound ALIAS " + htp.getParamName() + "->" + htp.getParamAlias());
1264                                 paramAliases.put(htp.getParamAlias(), htp);
1265                         }
1266                 }
1267                 LOGGER.debug("Now iterate through the inputs...");
1268                 for (String key : inputs.keySet()) {
1269                         LOGGER.debug("key=" + key);
1270                         boolean alias = false;
1271                         String realName = null;
1272                         if (!params.containsKey(key)) {
1273                                 LOGGER.debug(key + " is not a parameter in the template! - check for an alias");
1274                                 // add check here for an alias
1275                                 if (!paramAliases.containsKey(key)) {
1276                                         LOGGER.debug("The parameter " + key + " is in the inputs, but it's not a parameter for this template - omit");
1277                                         continue;
1278                                 } else {
1279                                         alias = true;
1280                                         realName = paramAliases.get(key).getParamName();
1281                                         LOGGER.debug("FOUND AN ALIAS! Will use " + realName + " in lieu of give key/alias " + key);
1282                                 }
1283                         }
1284                         String type = params.get(key).getParamType();
1285                         if (type == null || "".equals(type)) {
1286                                 LOGGER.debug("**PARAM_TYPE is null/empty for " + key + ", will default to string");
1287                                 type = "string";
1288                         }
1289                         LOGGER.debug("Parameter: " + key + " is of type " + type);
1290                         if ("string".equalsIgnoreCase(type)) {
1291                                 // Easiest!
1292                                 String str = inputs.get(key);
1293                                 if (alias)
1294                                         newInputs.put(realName, str);
1295                                 else
1296                                         newInputs.put(key, str);
1297                         } else if ("number".equalsIgnoreCase(type)) {
1298                                 String integerString = inputs.get(key);
1299                                 Integer anInteger = null;
1300                                 try {
1301                                         anInteger = Integer.parseInt(integerString);
1302                                 } catch (Exception e) {
1303                                         LOGGER.debug("Unable to convert " + integerString + " to an integer!!", e);
1304                                         anInteger = null;
1305                                 }
1306                                 if (anInteger != null) {
1307                                         if (alias)
1308                                                 newInputs.put(realName, anInteger);
1309                                         else
1310                                                 newInputs.put(key, anInteger);
1311                                 }
1312                                 else {
1313                                         if (alias)
1314                                                 newInputs.put(realName, integerString);
1315                                         else
1316                                                 newInputs.put(key, integerString);
1317                                 }
1318                         } else if ("json".equalsIgnoreCase(type)) {
1319                                 // MSO-1475 - Leave this as a string now
1320                                 String jsonString = inputs.get(key);
1321                                 LOGGER.debug("Skipping conversion to jsonNode...");
1322                         if (alias)
1323                                 newInputs.put(realName, jsonString);
1324                         else
1325                                 newInputs.put(key, jsonString);
1326                         //}
1327                         } else if ("comma_delimited_list".equalsIgnoreCase(type)) {
1328                                 String commaSeparated = inputs.get(key);
1329                                 try {
1330                                         List<String> anArrayList = this.convertCdlToArrayList(commaSeparated);
1331                                         if (alias)
1332                                                 newInputs.put(realName, anArrayList);
1333                                         else
1334                                                 newInputs.put(key, anArrayList);
1335                                 } catch (Exception e) {
1336                                         LOGGER.debug("Unable to convert " + commaSeparated + " to an ArrayList!!", e);
1337                                         if (alias)
1338                                                 newInputs.put(realName, commaSeparated);
1339                                         else
1340                                                 newInputs.put(key, commaSeparated);
1341                                 }
1342                         } else if ("boolean".equalsIgnoreCase(type)) {
1343                                 String booleanString = inputs.get(key);
1344                                 Boolean aBool = Boolean.valueOf(booleanString);
1345                                 if (alias)
1346                                         newInputs.put(realName, aBool);
1347                                 else
1348                                         newInputs.put(key, aBool);
1349                         } else {
1350                                 // it's null or something undefined - just add it back as a String
1351                                 String str = inputs.get(key);
1352                                 if (alias)
1353                                         newInputs.put(realName, str);
1354                                 else
1355                                         newInputs.put(key, str);
1356                         }
1357                 }
1358                 return newInputs;
1359         }
1360
1361         /*
1362          * This helpful method added for Valet
1363          */
1364         public String getCloudSiteKeystoneUrl(String cloudSiteId) throws MsoCloudSiteNotFound {
1365                 String keystone_url = null;
1366                 try {
1367                         CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1368                         CloudIdentity cloudIdentity = cloudSite.getIdentityService();
1369                         keystone_url = cloudIdentity.getIdentityUrl();
1370                 } catch (Exception e) {
1371                         throw new MsoCloudSiteNotFound(cloudSiteId);
1372                 }
1373                 if (keystone_url == null || keystone_url.isEmpty()) {
1374                         throw new MsoCloudSiteNotFound(cloudSiteId);
1375                 }
1376                 return keystone_url;
1377         }
1378
1379         /*
1380          * Create a string suitable for being dumped to a debug log that creates a
1381          * pseudo-JSON request dumping what's being sent to Openstack API in the create or update request
1382          */
1383
1384         private String printStackRequest(String tenantId,
1385                         Map<String, Object> heatFiles,
1386                         Map<String, Object> nestedTemplates,
1387                         String environment,
1388                         Map<String, Object> inputs,
1389                         String vfModuleName,
1390                         String template,
1391                         int timeoutMinutes,
1392                         boolean backout,
1393                         String cloudSiteId) {
1394                 StringBuilder sb = new StringBuilder();
1395                 sb.append("CREATE STACK REQUEST (formatted for readability)\n");
1396                 sb.append("tenant=" + tenantId + ", cloud=" + cloudSiteId);
1397                 sb.append("{\n");
1398                 sb.append("  \"stack_name\": \"" + vfModuleName + "\",\n");
1399                 sb.append("  \"disable_rollback\": " + backout + ",\n");
1400                 sb.append("  \"timeout_mins\": " + timeoutMinutes + ",\n");
1401                 sb.append("  \"template\": {\n");
1402                 sb.append(template);
1403                 sb.append("  },\n");
1404                 sb.append("  \"environment\": {\n");
1405                 if (environment == null)
1406                         sb.append("<none>");
1407                 else
1408                         sb.append(environment);
1409                 sb.append("  },\n");
1410                 sb.append("  \"files\": {\n");
1411                 int filesCounter = 0;
1412                 if (heatFiles != null) {
1413                         for (String key : heatFiles.keySet()) {
1414                                 filesCounter++;
1415                                 if (filesCounter > 1) {
1416                                         sb.append(",\n");
1417                                 }
1418                                 sb.append("    \"" + key + "\": {\n");
1419                                 sb.append(heatFiles.get(key).toString() + "\n    }");
1420                         }
1421                 }
1422                 if (nestedTemplates != null) {
1423                         for (String key : nestedTemplates.keySet()) {
1424                                 filesCounter++;
1425                                 if (filesCounter > 1) {
1426                                         sb.append(",\n");
1427                                 }
1428                                 sb.append("    \"" + key + "\": {\n");
1429                                 sb.append(nestedTemplates.get(key).toString() + "\n    }");
1430                         }
1431                 }
1432                 sb.append("\n  },\n");
1433                 sb.append("  \"parameters\": {\n");
1434                 int paramCounter = 0;
1435                 for (String name : inputs.keySet()) {
1436                         paramCounter++;
1437                         if (paramCounter > 1) {
1438                                 sb.append(",\n");
1439                         }
1440                         Object o = inputs.get(name);
1441                         if (o instanceof java.lang.String) {
1442                                 sb.append("    \"" + name + "\": \"" + inputs.get(name).toString() + "\"");
1443                         } else if (o instanceof Integer) {
1444                                 sb.append("    \"" + name + "\": " + inputs.get(name).toString() );
1445                         } else if (o instanceof ArrayList) {
1446                                 sb.append("    \"" + name + "\": " + inputs.get(name).toString() );
1447                         } else if (o instanceof Boolean) {
1448                                 sb.append("    \"" + name + "\": " + inputs.get(name).toString() );
1449                         } else {
1450                                 sb.append("    \"" + name + "\": " + "\"(there was an issue trying to dump this value...)\"" );
1451                         }
1452                 }
1453                 sb.append("\n  }\n}\n");
1454
1455                 return sb.toString();
1456         }
1457
1458         /*******************************************************************************
1459      *
1460      * Methods (and associated utilities) to implement the VduPlugin interface
1461      *
1462      *******************************************************************************/
1463
1464     /**
1465      * VduPlugin interface for instantiate function.
1466      *
1467      * Translate the VduPlugin parameters to the corresponding 'createStack' parameters,
1468      * and then invoke the existing function.
1469      */
1470     @Override
1471         public VduInstance instantiateVdu (
1472                         CloudInfo cloudInfo,
1473                         String instanceName,
1474                         Map<String,Object> inputs,
1475                         VduModelInfo vduModel,
1476                         boolean rollbackOnFailure)
1477         throws VduException
1478     {
1479         String cloudSiteId = cloudInfo.getCloudSiteId();
1480         String tenantId = cloudInfo.getTenantId();
1481
1482         // Translate the VDU ModelInformation structure to that which is needed for
1483         // creating the Heat stack.  Loop through the artifacts, looking specifically
1484         // for MAIN_TEMPLATE and ENVIRONMENT.  Any other artifact will
1485         // be attached as a FILE.
1486         String heatTemplate = null;
1487         Map<String,Object> nestedTemplates = new HashMap<>();
1488         Map<String,Object> files = new HashMap<>();
1489         String heatEnvironment = null;
1490
1491         for (VduArtifact vduArtifact: vduModel.getArtifacts()) {
1492                 if (vduArtifact.getType() == ArtifactType.MAIN_TEMPLATE) {
1493                         heatTemplate = new String(vduArtifact.getContent());
1494                 }
1495                 else if (vduArtifact.getType() == ArtifactType.NESTED_TEMPLATE) {
1496                         nestedTemplates.put(vduArtifact.getName(), new String(vduArtifact.getContent()));
1497                 }
1498                 else if (vduArtifact.getType() == ArtifactType.ENVIRONMENT) {
1499                         heatEnvironment = new String(vduArtifact.getContent());
1500                 }
1501         }
1502
1503         try {
1504             StackInfo stackInfo = createStack (cloudSiteId,
1505                     tenantId,
1506                     instanceName,
1507                     heatTemplate,
1508                     inputs,
1509                     true,       // poll for completion
1510                     vduModel.getTimeoutMinutes(),
1511                     heatEnvironment,
1512                     nestedTemplates,
1513                     files,
1514                     rollbackOnFailure);
1515
1516             // Populate a vduInstance from the StackInfo
1517                 return stackInfoToVduInstance(stackInfo);
1518         }
1519         catch (Exception e) {
1520                 throw new VduException ("MsoHeatUtils (instantiateVDU): createStack Exception", e);
1521         }
1522     }
1523
1524
1525     /**
1526      * VduPlugin interface for query function.
1527      */
1528     @Override
1529         public VduInstance queryVdu (CloudInfo cloudInfo, String instanceId)
1530         throws VduException
1531     {
1532         String cloudSiteId = cloudInfo.getCloudSiteId();
1533         String tenantId = cloudInfo.getTenantId();
1534
1535         try {
1536                 // Query the Cloudify Deployment object and  populate a VduInstance
1537                 StackInfo stackInfo = queryStack (cloudSiteId, tenantId, instanceId);
1538
1539                 return stackInfoToVduInstance(stackInfo);
1540         }
1541         catch (Exception e) {
1542                 throw new VduException ("MsoHeatUtile (queryVdu): queryStack Exception ", e);
1543         }
1544     }
1545
1546
1547     /**
1548      * VduPlugin interface for delete function.
1549      */
1550     @Override
1551         public VduInstance deleteVdu (CloudInfo cloudInfo, String instanceId, int timeoutMinutes)
1552         throws VduException
1553     {
1554         String cloudSiteId = cloudInfo.getCloudSiteId();
1555         String tenantId = cloudInfo.getTenantId();
1556
1557         try {
1558                 // Delete the Heat stack
1559                 StackInfo stackInfo = deleteStack (tenantId, cloudSiteId, instanceId, true);
1560
1561                 // Populate a VduInstance based on the deleted Cloudify Deployment object
1562                 VduInstance vduInstance = stackInfoToVduInstance(stackInfo);
1563
1564                 // Override return state to DELETED (HeatUtils sets to NOTFOUND)
1565                 vduInstance.getStatus().setState(VduStateType.DELETED);
1566
1567                 return vduInstance;
1568         }
1569         catch (Exception e) {
1570                 throw new VduException ("Delete VDU Exception", e);
1571         }
1572     }
1573
1574
1575     /**
1576      * VduPlugin interface for update function.
1577      *
1578      * Update is currently not supported in the MsoHeatUtils implementation of VduPlugin.
1579      * Just return a VduException.
1580      *
1581      */
1582     @Override
1583         public VduInstance updateVdu (
1584                         CloudInfo cloudInfo,
1585                         String instanceId,
1586                         Map<String,Object> inputs,
1587                         VduModelInfo vduModel,
1588                         boolean rollbackOnFailure)
1589         throws VduException
1590     {
1591         throw new VduException ("MsoHeatUtils: updateVdu interface not supported");
1592     }
1593
1594
1595     /*
1596      * Convert the local DeploymentInfo object (Cloudify-specific) to a generic VduInstance object
1597      */
1598     protected VduInstance stackInfoToVduInstance (StackInfo stackInfo)
1599     {
1600         VduInstance vduInstance = new VduInstance();
1601
1602         // The full canonical name as the instance UUID
1603         vduInstance.setVduInstanceId(stackInfo.getCanonicalName());
1604         vduInstance.setVduInstanceName(stackInfo.getName());
1605
1606         // Copy inputs and outputs
1607         vduInstance.setInputs(stackInfo.getParameters());
1608         vduInstance.setOutputs(stackInfo.getOutputs());
1609
1610         // Translate the status elements
1611         vduInstance.setStatus(stackStatusToVduStatus (stackInfo));
1612
1613         return vduInstance;
1614     }
1615
1616     private VduStatus stackStatusToVduStatus (StackInfo stackInfo)
1617     {
1618         VduStatus vduStatus = new VduStatus();
1619
1620         // Map the status fields to more generic VduStatus.
1621         // There are lots of HeatStatus values, so this is a bit long...
1622         HeatStatus heatStatus = stackInfo.getStatus();
1623         String statusMessage = stackInfo.getStatusMessage();
1624
1625         if (heatStatus == HeatStatus.INIT  ||  heatStatus == HeatStatus.BUILDING) {
1626                 vduStatus.setState(VduStateType.INSTANTIATING);
1627                 vduStatus.setLastAction((new PluginAction ("create", "in_progress", statusMessage)));
1628         }
1629         else if (heatStatus == HeatStatus.NOTFOUND) {
1630                 vduStatus.setState(VduStateType.NOTFOUND);
1631         }
1632         else if (heatStatus == HeatStatus.CREATED) {
1633                 vduStatus.setState(VduStateType.INSTANTIATED);
1634                 vduStatus.setLastAction((new PluginAction ("create", "complete", statusMessage)));
1635         }
1636         else if (heatStatus == HeatStatus.UPDATED) {
1637                 vduStatus.setState(VduStateType.INSTANTIATED);
1638                 vduStatus.setLastAction((new PluginAction ("update", "complete", statusMessage)));
1639         }
1640         else if (heatStatus == HeatStatus.UPDATING) {
1641                 vduStatus.setState(VduStateType.UPDATING);
1642                 vduStatus.setLastAction((new PluginAction ("update", "in_progress", statusMessage)));
1643         }
1644         else if (heatStatus == HeatStatus.DELETING) {
1645                 vduStatus.setState(VduStateType.DELETING);
1646                 vduStatus.setLastAction((new PluginAction ("delete", "in_progress", statusMessage)));
1647         }
1648         else if (heatStatus == HeatStatus.FAILED) {
1649                 vduStatus.setState(VduStateType.FAILED);
1650                 vduStatus.setErrorMessage(stackInfo.getStatusMessage());
1651         } else {
1652                 vduStatus.setState(VduStateType.UNKNOWN);
1653         }
1654
1655         return vduStatus;
1656     }
1657     
1658         public Resources queryStackResources(String cloudSiteId, String tenantId, String stackName) throws MsoException {
1659                 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1660                                 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1661                 Heat heatClient = getHeatClient(cloudSite, tenantId);
1662                 OpenStackRequest<Resources> request = heatClient.getResources().listResources(stackName);
1663                 return executeAndRecordOpenstackRequest(request);
1664         }
1665
1666         public <R> R executeHeatClientRequest(String url, String cloudSiteId, String tenantId, Class<R> returnType) throws MsoException {
1667                 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1668                                 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1669                 Heat heatClient = getHeatClient(cloudSite, tenantId);
1670                 OpenStackRequest<R> request = heatClient.get(url, returnType);
1671                 return executeAndRecordOpenstackRequest(request);
1672         }
1673
1674     protected void sleep(long time) {
1675         try {
1676             Thread.sleep(time);
1677         } catch (InterruptedException e) {
1678             LOGGER.debug ("Thread interrupted while sleeping", e);
1679             Thread.currentThread().interrupt();
1680         }
1681     }
1682
1683 }