[MSO-8] Update the maven dependency
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / openstack / utils / MsoHeatUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.openstack.utils;
22
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Calendar;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.codehaus.jackson.map.ObjectMapper;
31 import org.codehaus.jackson.JsonNode;
32 import org.codehaus.jackson.JsonParseException;
33
34 import org.openecomp.mso.cloud.CloudConfig;
35 import org.openecomp.mso.cloud.CloudConfigFactory;
36 import org.openecomp.mso.cloud.CloudIdentity;
37 import org.openecomp.mso.cloud.CloudSite;
38 import org.openecomp.mso.db.catalog.beans.HeatTemplate;
39 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
40 import org.openecomp.mso.logger.MessageEnum;
41 import org.openecomp.mso.logger.MsoAlarmLogger;
42 import org.openecomp.mso.logger.MsoLogger;
43 import org.openecomp.mso.openstack.beans.HeatStatus;
44 import org.openecomp.mso.openstack.beans.StackInfo;
45 import org.openecomp.mso.openstack.exceptions.MsoAdapterException;
46 import org.openecomp.mso.openstack.exceptions.MsoCloudSiteNotFound;
47 import org.openecomp.mso.openstack.exceptions.MsoException;
48 import org.openecomp.mso.openstack.exceptions.MsoIOException;
49 import org.openecomp.mso.openstack.exceptions.MsoOpenstackException;
50 import org.openecomp.mso.openstack.exceptions.MsoStackAlreadyExists;
51 import org.openecomp.mso.openstack.exceptions.MsoTenantNotFound;
52 import org.openecomp.mso.properties.MsoJavaProperties;
53 import org.openecomp.mso.properties.MsoPropertiesException;
54 import org.openecomp.mso.properties.MsoPropertiesFactory;
55 import com.woorea.openstack.base.client.OpenStackConnectException;
56 import com.woorea.openstack.base.client.OpenStackRequest;
57 import com.woorea.openstack.base.client.OpenStackResponseException;
58 import com.woorea.openstack.heat.Heat;
59 import com.woorea.openstack.heat.model.CreateStackParam;
60 import com.woorea.openstack.heat.model.Stack;
61 import com.woorea.openstack.heat.model.Stack.Output;
62 import com.woorea.openstack.heat.model.Stacks;
63 import com.woorea.openstack.keystone.Keystone;
64 import com.woorea.openstack.keystone.model.Access;
65 import com.woorea.openstack.keystone.model.Authentication;
66 import com.woorea.openstack.keystone.utils.KeystoneUtils;
67
68 public class MsoHeatUtils extends MsoCommonUtils {
69
70         private MsoPropertiesFactory msoPropertiesFactory;
71
72         private CloudConfigFactory cloudConfigFactory;
73
74     private static final String TOKEN_AUTH = "TokenAuth";
75
76     private static final String QUERY_ALL_STACKS = "QueryAllStacks";
77
78     private static final String DELETE_STACK = "DeleteStack";
79
80     private static final String HEAT_ERROR = "HeatError";
81
82     private static final String CREATE_STACK = "CreateStack";
83
84     // Cache Heat Clients statically. Since there is just one MSO user, there is no
85     // benefit to re-authentication on every request (or across different flows). The
86     // token will be used until it expires.
87     //
88     // The cache key is "tenantId:cloudId"
89     private static Map <String, HeatCacheEntry> heatClientCache = new HashMap <String, HeatCacheEntry> ();
90
91     // Fetch cloud configuration each time (may be cached in CloudConfig class)
92     protected CloudConfig cloudConfig;
93
94     private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
95
96     protected MsoJavaProperties msoProps = null;
97
98     // Properties names and variables (with default values)
99     protected String createPollIntervalProp = "ecomp.mso.adapters.heat.create.pollInterval";
100     private String deletePollIntervalProp = "ecomp.mso.adapters.heat.delete.pollInterval";
101     private String deletePollTimeoutProp = "ecomp.mso.adapters.heat.delete.pollTimeout";
102
103     protected int createPollIntervalDefault = 15;
104     private int deletePollIntervalDefault = 15;
105     private int deletePollTimeoutDefault = 300;
106     private String msoPropID;
107
108     private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
109
110     /**
111      * This constructor MUST be used ONLY in the JUNIT tests, not for real code.
112      * The MsoPropertiesFactory will be added by EJB injection.
113      *
114      * @param msoPropID ID of the mso pro config as defined in web.xml
115      * @param msoPropFactory The mso properties factory instanciated by EJB injection
116      * @param cloudConfFactory the Cloud Config instantiated by EJB injection
117      */
118     public MsoHeatUtils (String msoPropID, MsoPropertiesFactory msoPropFactory, CloudConfigFactory cloudConfFactory) {
119         msoPropertiesFactory = msoPropFactory;
120         cloudConfigFactory = cloudConfFactory;
121         this.msoPropID = msoPropID;
122         // Dynamically get properties each time (in case reloaded).
123
124         try {
125                         msoProps = msoPropertiesFactory.getMsoJavaProperties (msoPropID);
126                 } catch (MsoPropertiesException e) {
127                         LOGGER.error (MessageEnum.LOAD_PROPERTIES_FAIL, "Unknown. Mso Properties ID not found in cache: " + msoPropID, "", "", MsoLogger.ErrorCode.DataError, "Exception - Mso Properties ID not found in cache", e);
128                 }
129         cloudConfig = cloudConfigFactory.getCloudConfig ();
130         LOGGER.debug("MsoHeatUtils:" + msoPropID);
131
132     }
133
134
135     /**
136      * keep this old method signature here to maintain backwards compatibility. keep others as well.
137      * this method does not include environment, files, or heatFiles
138      */
139     public StackInfo createStack (String cloudSiteId,
140                                   String tenantId,
141                                   String stackName,
142                                   String heatTemplate,
143                                   Map <String, ? extends Object> stackInputs,
144                                   boolean pollForCompletion,
145                                   int timeoutMinutes) throws MsoException {
146         // Just call the new method with the environment & files variable set to null
147         return this.createStack (cloudSiteId,
148                                  tenantId,
149                                  stackName,
150                                  heatTemplate,
151                                  stackInputs,
152                                  pollForCompletion,
153                                  timeoutMinutes,
154                                  null,
155                                  null,
156                                  null,
157                                  true);
158     }
159
160     // This method has environment, but not files or heatFiles
161     public StackInfo createStack (String cloudSiteId,
162                                   String tenantId,
163                                   String stackName,
164                                   String heatTemplate,
165                                   Map <String, ? extends Object> stackInputs,
166                                   boolean pollForCompletion,
167                                   int timeoutMinutes,
168                                   String environment) throws MsoException {
169         // Just call the new method with the files/heatFiles variables set to null
170         return this.createStack (cloudSiteId,
171                                  tenantId,
172                                  stackName,
173                                  heatTemplate,
174                                  stackInputs,
175                                  pollForCompletion,
176                                  timeoutMinutes,
177                                  environment,
178                                  null,
179                                  null,
180                                  true);
181     }
182
183     // This method has environment and files, but not heatFiles.
184     public StackInfo createStack (String cloudSiteId,
185                                   String tenantId,
186                                   String stackName,
187                                   String heatTemplate,
188                                   Map <String, ? extends Object> stackInputs,
189                                   boolean pollForCompletion,
190                                   int timeoutMinutes,
191                                   String environment,
192                                   Map <String, Object> files) throws MsoException {
193         return this.createStack (cloudSiteId,
194                                  tenantId,
195                                  stackName,
196                                  heatTemplate,
197                                  stackInputs,
198                                  pollForCompletion,
199                                  timeoutMinutes,
200                                  environment,
201                                  files,
202                                  null,
203                                  true);
204     }
205
206     // This method has environment, files, heatfiles
207     public StackInfo createStack (String cloudSiteId,
208                                   String tenantId,
209                                   String stackName,
210                                   String heatTemplate,
211                                   Map <String, ? extends Object> stackInputs,
212                                   boolean pollForCompletion,
213                                   int timeoutMinutes,
214                                   String environment,
215                                   Map <String, Object> files,
216                                   Map <String, Object> heatFiles) throws MsoException {
217         return this.createStack (cloudSiteId,
218                                  tenantId,
219                                  stackName,
220                                  heatTemplate,
221                                  stackInputs,
222                                  pollForCompletion,
223                                  timeoutMinutes,
224                                  environment,
225                                  files,
226                                  heatFiles,
227                                  true);
228     }
229
230     /**
231      * Create a new Stack in the specified cloud location and tenant. The Heat template
232      * and parameter map are passed in as arguments, along with the cloud access credentials.
233      * It is expected that parameters have been validated and contain at minimum the required
234      * parameters for the given template with no extra (undefined) parameters..
235      *
236      * The Stack name supplied by the caller must be unique in the scope of this tenant.
237      * However, it should also be globally unique, as it will be the identifier for the
238      * resource going forward in Inventory. This latter is managed by the higher levels
239      * invoking this function.
240      *
241      * The caller may choose to let this function poll Openstack for completion of the
242      * stack creation, or may handle polling itself via separate calls to query the status.
243      * In either case, a StackInfo object will be returned containing the current status.
244      * When polling is enabled, a status of CREATED is expected. When not polling, a
245      * status of BUILDING is expected.
246      *
247      * An error will be thrown if the requested Stack already exists in the specified
248      * Tenant and Cloud.
249      *
250      * For 1510 - add "environment", "files" (nested templates), and "heatFiles" (get_files) as
251      * parameters for createStack. If environment is non-null, it will be added to the stack.
252      * The nested templates and get_file entries both end up being added to the "files" on the
253      * stack. We must combine them before we add them to the stack if they're both non-null.
254      *
255      * @param cloudSiteId The cloud (may be a region) in which to create the stack.
256      * @param tenantId The Openstack ID of the tenant in which to create the Stack
257      * @param stackName The name of the stack to create
258      * @param stackTemplate The Heat template
259      * @param stackInputs A map of key/value inputs
260      * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
261      * @param environment An optional yaml-format string to specify environmental parameters
262      * @param files a Map<String, Object> that lists the child template IDs (file is the string, object is an int of
263      *        Template id)
264      * @param heatFiles a Map<String, Object> that lists the get_file entries (fileName, fileBody)
265      * @param backout Donot delete stack on create Failure - defaulted to True
266      * @return A StackInfo object
267      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
268      */
269
270     @SuppressWarnings("unchecked")
271     public StackInfo createStack (String cloudSiteId,
272                                   String tenantId,
273                                   String stackName,
274                                   String heatTemplate,
275                                   Map <String, ? extends Object> stackInputs,
276                                   boolean pollForCompletion,
277                                   int timeoutMinutes,
278                                   String environment,
279                                   Map <String, Object> files,
280                                   Map <String, Object> heatFiles,
281                                   boolean backout) throws MsoException {
282         // Create local variables checking to see if we have an environment, nested, get_files
283         // Could later add some checks to see if it's valid.
284         boolean haveEnvtVariable = true;
285         if (environment == null || "".equalsIgnoreCase (environment.trim ())) {
286             haveEnvtVariable = false;
287             LOGGER.debug ("createStack called with no environment variable");
288         } else {
289             LOGGER.debug ("createStack called with an environment variable: " + environment);
290         }
291
292         boolean haveFiles = true;
293         if (files == null || files.isEmpty ()) {
294             haveFiles = false;
295             LOGGER.debug ("createStack called with no files / child template ids");
296         } else {
297             LOGGER.debug ("createStack called with " + files.size () + " files / child template ids");
298         }
299
300         boolean haveHeatFiles = true;
301         if (heatFiles == null || heatFiles.isEmpty ()) {
302             haveHeatFiles = false;
303             LOGGER.debug ("createStack called with no heatFiles");
304         } else {
305             LOGGER.debug ("createStack called with " + heatFiles.size () + " heatFiles");
306         }
307
308         // Obtain the cloud site information where we will create the stack
309         CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId);
310         if (cloudSite == null) {
311             throw new MsoCloudSiteNotFound (cloudSiteId);
312         }
313         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
314         // This could throw MsoTenantNotFound or MsoOpenstackException (both propagated)
315         Heat heatClient = getHeatClient (cloudSite, tenantId);
316
317         LOGGER.debug ("Ready to Create Stack (" + heatTemplate + ") with input params: " + stackInputs);
318
319         // Build up the stack to create
320         // Disable auto-rollback, because error reason is lost. Always rollback in the code.
321         CreateStackParam stack = new CreateStackParam ();
322         stack.setStackName (stackName);
323         stack.setTimeoutMinutes (timeoutMinutes);
324         stack.setParameters ((Map <String, Object>) stackInputs);
325         stack.setTemplate (heatTemplate);
326         stack.setDisableRollback (true);
327         // TJM New for PO Adapter - add envt variable
328         if (haveEnvtVariable) {
329             LOGGER.debug ("Found an environment variable - value: " + environment);
330             stack.setEnvironment (environment);
331         }
332         // Now handle nested templates or get_files - have to combine if we have both
333         // as they're both treated as "files:" on the stack.
334         if (haveFiles && haveHeatFiles) {
335             // Let's do this here - not in the bean
336             LOGGER.debug ("Found files AND heatFiles - combine and add!");
337             Map <String, Object> combinedFiles = new HashMap <String, Object> ();
338             for (String keyString : files.keySet ()) {
339                 combinedFiles.put (keyString, files.get (keyString));
340             }
341             for (String keyString : heatFiles.keySet ()) {
342                 combinedFiles.put (keyString, heatFiles.get (keyString));
343             }
344             stack.setFiles (combinedFiles);
345         } else {
346             // Handle if we only have one or neither:
347             if (haveFiles) {
348                 LOGGER.debug ("Found files - adding to stack");
349                 stack.setFiles (files);
350             }
351             if (haveHeatFiles) {
352                 LOGGER.debug ("Found heatFiles - adding to stack");
353                 // the setFiles was modified to handle adding the entries
354                 stack.setFiles (heatFiles);
355             }
356         }
357
358         Stack heatStack = null;
359         try {
360             // Execute the actual Openstack command to create the Heat stack
361             OpenStackRequest <Stack> request = heatClient.getStacks ().create (stack);
362             // Begin X-Auth-User
363             // Obtain an MSO token for the tenant
364             CloudIdentity cloudIdentity = cloudSite.getIdentityService ();
365             // cloudIdentity.getMsoId(), cloudIdentity.getMsoPass()
366             //req
367             request.header ("X-Auth-User", cloudIdentity.getMsoId ());
368             request.header ("X-Auth-Key", cloudIdentity.getMsoPass ());
369             LOGGER.debug ("headers added, about to executeAndRecordOpenstackRequest");
370             LOGGER.debug(this.requestToStringBuilder(stack).toString());
371             // END - try to fix X-Auth-User
372             heatStack = executeAndRecordOpenstackRequest (request, msoProps);
373         } catch (OpenStackResponseException e) {
374             // Since this came on the 'Create Stack' command, nothing was changed
375             // in the cloud. Return the error as an exception.
376             if (e.getStatus () == 409) {
377                 // Stack already exists. Return a specific error for this case
378                 MsoStackAlreadyExists me = new MsoStackAlreadyExists (stackName, tenantId, cloudSiteId);
379                 me.addContext (CREATE_STACK);
380                 throw me;
381             } else {
382                 // Convert the OpenStackResponseException to an MsoOpenstackException
383                 LOGGER.debug("ERROR STATUS = " + e.getStatus() + ",\n" + e.getMessage() + "\n" + e.getLocalizedMessage());
384                 throw heatExceptionToMsoException (e, CREATE_STACK);
385             }
386         } catch (OpenStackConnectException e) {
387             // Error connecting to Openstack instance. Convert to an MsoException
388             throw heatExceptionToMsoException (e, CREATE_STACK);
389         } catch (RuntimeException e) {
390             // Catch-all
391             throw runtimeExceptionToMsoException (e, CREATE_STACK);
392         }
393
394         // Subsequent access by the canonical name "<stack name>/<stack-id>".
395         // Otherwise, simple query by name returns a 302 redirect.
396         // NOTE: This is specific to the v1 Orchestration API.
397         String canonicalName = stackName + "/" + heatStack.getId ();
398
399         // If client has requested a final response, poll for stack completion
400         if (pollForCompletion) {
401             // Set a time limit on overall polling.
402             // Use the resource (template) timeout for Openstack (expressed in minutes)
403             // and add one poll interval to give Openstack a chance to fail on its own.
404             int createPollInterval = msoProps.getIntProperty (createPollIntervalProp, createPollIntervalDefault);
405             int pollTimeout = (timeoutMinutes * 60) + createPollInterval;
406             // New 1610 - poll on delete if we rollback - use same values for now
407             int deletePollInterval = createPollInterval;
408             int deletePollTimeout = pollTimeout;
409             boolean createTimedOut = false;
410             StringBuilder stackErrorStatusReason = new StringBuilder("");
411             LOGGER.debug("createPollInterval=" + createPollInterval + ", pollTimeout=" + pollTimeout);
412
413             while (true) {
414                 try {
415                     heatStack = queryHeatStack (heatClient, canonicalName);
416                     LOGGER.debug (heatStack.getStackStatus () + " (" + canonicalName + ")");
417                     try {
418                         LOGGER.debug("Current stack " + this.getOutputsAsStringBuilder(heatStack).toString());
419                     } catch (Exception e) {
420                         LOGGER.debug("an error occurred trying to print out the current outputs of the stack");
421                     }
422
423                     if ("CREATE_IN_PROGRESS".equals (heatStack.getStackStatus ())) {
424                         // Stack creation is still running.
425                         // Sleep and try again unless timeout has been reached
426                         if (pollTimeout <= 0) {
427                             // Note that this should not occur, since there is a timeout specified
428                             // in the Openstack call.
429                             LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName, heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Create stack timeout");
430                             createTimedOut = true;
431                             break;
432                         }
433                         try {
434                             Thread.sleep (createPollInterval * 1000L);
435                         } catch (InterruptedException e) {
436                             LOGGER.debug ("Thread interrupted while sleeping", e);
437                         }
438
439                         pollTimeout -= createPollInterval;
440                                 LOGGER.debug("pollTimeout remaining: " + pollTimeout);
441                     } else {
442                         //save off the status & reason msg before we attempt delete
443                         stackErrorStatusReason.append("Stack error (" + heatStack.getStackStatus() + "): " + heatStack.getStackStatusReason());
444                         break;
445                     }
446                 } catch (MsoException me) {
447                         // Cannot query the stack status. Something is wrong.
448                         // Try to roll back the stack
449                         if (!backout)
450                         {
451                                 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in Create Stack, stack deletion suppressed");
452                         }
453                         else
454                         {
455                                 try {
456                                         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");
457                                         OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
458                                         executeAndRecordOpenstackRequest (request, msoProps);
459                                         // this may be a waste of time - if we just got an exception trying to query the stack - we'll just
460                                         // get another one, n'est-ce pas?
461                                         boolean deleted = false;
462                                         while (!deleted) {
463                                                 try {
464                                                         heatStack = queryHeatStack(heatClient, canonicalName);
465                                                         if (heatStack != null) {
466                                                         LOGGER.debug(heatStack.getStackStatus());
467                                                         if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
468                                                                 if (deletePollTimeout <= 0) {
469                                                                         LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
470                                                                                         heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError,
471                                                                                         "Rollback: DELETE stack timeout");
472                                                                         break;
473                                                                 } else {
474                                                                         try {
475                                                                                 Thread.sleep(deletePollInterval * 1000L);
476                                                                         } catch (InterruptedException ie) {
477                                                                                 LOGGER.debug("Thread interrupted while sleeping", ie);
478                                                                         }
479                                                                         deletePollTimeout -= deletePollInterval;
480                                                                 }
481                                                         } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
482                                                                 LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
483                                                                 deleted = true;
484                                                                 continue;
485                                                         } else {
486                                                                 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
487                                                                 break;
488                                                         }
489                                                 } else {
490                                                         // assume if we can't find it - it's deleted
491                                                         LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
492                                                         deleted = true;
493                                                         continue;
494                                                         }
495
496                                                 } catch (Exception e3) {
497                                                         // Just log this one. We will report the original exception.
498                                                         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");
499
500                                                 }
501                                         }
502                                 } catch (Exception e2) {
503                                         // Just log this one. We will report the original exception.
504                                         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");
505                                 }
506                         }
507
508                     // Propagate the original exception from Stack Query.
509                     me.addContext (CREATE_STACK);
510                     throw me;
511                 }
512             }
513
514             if (!"CREATE_COMPLETE".equals (heatStack.getStackStatus ())) {
515                 LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack error:  Polling complete with non-success status: "
516                               + heatStack.getStackStatus () + ", " + heatStack.getStackStatusReason (), "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error");
517
518                 // Rollback the stack creation, since it is in an indeterminate state.
519                 if (!backout)
520                 {
521                         LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error, stack deletion suppressed");
522                 }
523                 else
524                 {
525                         try {
526                                 LOGGER.debug("Create Stack errored - attempting to DELETE stack: " + canonicalName);
527                                 LOGGER.debug("deletePollInterval=" + deletePollInterval + ", deletePollTimeout=" + deletePollTimeout);
528                                 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
529                                 executeAndRecordOpenstackRequest (request, msoProps);
530                                 boolean deleted = false;
531                                 while (!deleted) {
532                                         try {
533                                                 heatStack = queryHeatStack(heatClient, canonicalName);
534                                                 if (heatStack != null) {
535                                                         LOGGER.debug(heatStack.getStackStatus() + " (" + canonicalName + ")");
536                                                         if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
537                                                                 if (deletePollTimeout <= 0) {
538                                                                         LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
539                                                                                         heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError,
540                                                                                         "Rollback: DELETE stack timeout");
541                                                                         break;
542                                                                 } else {
543                                                                         try {
544                                                                                 Thread.sleep(deletePollInterval * 1000L);
545                                                                         } catch (InterruptedException ie) {
546                                                                                 LOGGER.debug("Thread interrupted while sleeping", ie);
547                                                                         }
548                                                                         deletePollTimeout -= deletePollInterval;
549                                                                         LOGGER.debug("deletePollTimeout remaining: " + deletePollTimeout);
550                                                                 }
551                                                         } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
552                                                                 LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
553                                                                 deleted = true;
554                                                                 continue;
555                                                         } else if ("DELETE_FAILED".equals(heatStack.getStackStatus())) {
556                                                                 // Warn about this (?) - but still throw the original exception
557                                                                 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion FAILED", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error, stack deletion FAILED");
558                                                                 LOGGER.debug("Stack deletion FAILED on a rollback of a create - " + canonicalName + ", status=" + heatStack.getStackStatus() + ", reason=" + heatStack.getStackStatusReason());
559                                                                 break;
560                                                         } else {
561                                                                 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
562                                                                 break;
563                                                         }
564                                                 } else {
565                                                         // assume if we can't find it - it's deleted
566                                                         LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
567                                                         deleted = true;
568                                                         continue;
569                                                 }
570
571                                         } catch (MsoException me2) {
572                                                 // We got an exception on the delete - don't throw this exception - throw the original - just log.
573                                                 LOGGER.debug("Exception thrown trying to delete " + canonicalName + " on a create->rollback: " + me2.getContextMessage());
574                                                 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, then stack deletion FAILED - exception thrown", "", "", MsoLogger.ErrorCode.BusinessProcesssError, me2.getContextMessage());
575                                         }
576
577                                 } // end while !deleted
578                                 StringBuilder errorContextMessage = null;
579                                 if (createTimedOut) {
580                                         errorContextMessage = new StringBuilder("Stack Creation Timeout");
581                                 } else {
582                                         errorContextMessage  = stackErrorStatusReason;
583                                 }
584                                 if (deleted) {
585                                         errorContextMessage.append(" - stack successfully deleted");
586                                 } else {
587                                         errorContextMessage.append(" - encountered an error trying to delete the stack");
588                                 }
589 //                              MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
590  //                             me.addContext(CREATE_STACK);
591   //                            alarmLogger.sendAlarm(HEAT_ERROR, MsoAlarmLogger.CRITICAL, me.getContextMessage());
592    //                           throw me;
593                         } catch (Exception e2) {
594                                 // shouldn't happen - but handle
595                                 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");
596                         }
597                 }
598                 MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
599                 me.addContext(CREATE_STACK);
600                 alarmLogger.sendAlarm(HEAT_ERROR, MsoAlarmLogger.CRITICAL, me.getContextMessage());
601                 throw me;
602             }
603
604         } else {
605             // Get initial status, since it will have been null after the create.
606             heatStack = queryHeatStack (heatClient, canonicalName);
607             LOGGER.debug (heatStack.getStackStatus ());
608         }
609
610         return new StackInfo (heatStack);
611     }
612
613     /**
614      * Query for a single stack (by Name) in a tenant. This call will always return a
615      * StackInfo object. If the stack does not exist, an "empty" StackInfo will be
616      * returned - containing only the stack name and a status of NOTFOUND.
617      *
618      * @param tenantId The Openstack ID of the tenant in which to query
619      * @param cloudSiteId The cloud identifier (may be a region) in which to query
620      * @param stackName The name of the stack to query (may be simple or canonical)
621      * @return A StackInfo object
622      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
623      */
624     public StackInfo queryStack (String cloudSiteId, String tenantId, String stackName) throws MsoException {
625         LOGGER.debug ("Query HEAT stack: " + stackName + " in tenant " + tenantId);
626
627         // Obtain the cloud site information where we will create the stack
628         CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId);
629         if (cloudSite == null) {
630             throw new MsoCloudSiteNotFound (cloudSiteId);
631         }
632
633         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
634         Heat heatClient = null;
635         try {
636             heatClient = getHeatClient (cloudSite, tenantId);
637         } catch (MsoTenantNotFound e) {
638             // Tenant doesn't exist, so stack doesn't either
639             LOGGER.debug ("Tenant with id " + tenantId + "not found.", e);
640             return new StackInfo (stackName, HeatStatus.NOTFOUND, null, null);
641         } catch (MsoException me) {
642             // Got an Openstack error. Propagate it
643             LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Exception on Token request: " + me, "Openstack", "", MsoLogger.ErrorCode.AvailabilityError, "Connection Exception");
644             me.addContext ("QueryStack");
645             throw me;
646         }
647
648         // Query the Stack.
649         // An MsoException will propagate transparently to the caller.
650         Stack heatStack = queryHeatStack (heatClient, stackName);
651
652         if (heatStack == null) {
653             // Stack does not exist. Return a StackInfo with status NOTFOUND
654             StackInfo stackInfo = new StackInfo (stackName, HeatStatus.NOTFOUND, null, null);
655             return stackInfo;
656         }
657
658         return new StackInfo (heatStack);
659     }
660
661     /**
662      * Delete a stack (by Name/ID) in a tenant. If the stack is not found, it will be
663      * considered a successful deletion. The return value is a StackInfo object which
664      * contains the current stack status.
665      *
666      * The client may choose to let the adapter poll Openstack for completion of the
667      * stack deletion, or may handle polling itself via separate query calls. In either
668      * case, a StackInfo object will be returned. When polling is enabled, a final
669      * status of NOTFOUND is expected. When not polling, a status of DELETING is expected.
670      *
671      * There is no rollback from a successful stack deletion. A deletion failure will
672      * also result in an undefined stack state - the components may or may not have been
673      * all or partially deleted, so the resulting stack must be considered invalid.
674      *
675      * @param tenantId The Openstack ID of the tenant in which to perform the delete
676      * @param cloudSiteId The cloud identifier (may be a region) from which to delete the stack.
677      * @param stackName The name/id of the stack to delete. May be simple or canonical
678      * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
679      * @return A StackInfo object
680      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
681      * @throws MsoCloudSiteNotFound
682      */
683     public StackInfo deleteStack (String tenantId,
684                                   String cloudSiteId,
685                                   String stackName,
686                                   boolean pollForCompletion) throws MsoException {
687         // Obtain the cloud site information where we will create the stack
688         CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId);
689         if (cloudSite == null) {
690             throw new MsoCloudSiteNotFound (cloudSiteId);
691         }
692
693         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
694         Heat heatClient = null;
695         try {
696             heatClient = getHeatClient (cloudSite, tenantId);
697         } catch (MsoTenantNotFound e) {
698             // Tenant doesn't exist, so stack doesn't either
699             LOGGER.debug ("Tenant with id " + tenantId + "not found.", e);
700             return new StackInfo (stackName, HeatStatus.NOTFOUND, null, null);
701         } catch (MsoException me) {
702             // Got an Openstack error. Propagate it
703             LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack", "Openstack Exception on Token request: " + me, "Openstack", "", MsoLogger.ErrorCode.AvailabilityError, "Connection Exception");
704             me.addContext (DELETE_STACK);
705             throw me;
706         }
707
708         // OK if stack not found, perform a query first
709         Stack heatStack = queryHeatStack (heatClient, stackName);
710         if (heatStack == null || "DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
711             // Not found. Return a StackInfo with status NOTFOUND
712             return new StackInfo (stackName, HeatStatus.NOTFOUND, null, null);
713         }
714
715         // Delete the stack.
716
717         // Use canonical name "<stack name>/<stack-id>" to delete.
718         // Otherwise, deletion by name returns a 302 redirect.
719         // NOTE: This is specific to the v1 Orchestration API.
720         String canonicalName = heatStack.getStackName () + "/" + heatStack.getId ();
721
722         try {
723             OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
724             executeAndRecordOpenstackRequest (request, msoProps);
725         } catch (OpenStackResponseException e) {
726             if (e.getStatus () == 404) {
727                 // Not found. We are OK with this. Return a StackInfo with status NOTFOUND
728                 return new StackInfo (stackName, HeatStatus.NOTFOUND, null, null);
729             } else {
730                 // Convert the OpenStackResponseException to an MsoOpenstackException
731                 throw heatExceptionToMsoException (e, DELETE_STACK);
732             }
733         } catch (OpenStackConnectException e) {
734             // Error connecting to Openstack instance. Convert to an MsoException
735             throw heatExceptionToMsoException (e, DELETE_STACK);
736         } catch (RuntimeException e) {
737             // Catch-all
738             throw runtimeExceptionToMsoException (e, DELETE_STACK);
739         }
740
741         // Requery the stack for current status.
742         // It will probably still exist with "DELETE_IN_PROGRESS" status.
743         heatStack = queryHeatStack (heatClient, canonicalName);
744
745         if (pollForCompletion) {
746             // Set a timeout on polling
747             int pollInterval = msoProps.getIntProperty (deletePollIntervalProp, deletePollIntervalDefault);
748             int pollTimeout = msoProps.getIntProperty (deletePollTimeoutProp, deletePollTimeoutDefault);
749
750             // When querying by canonical name, Openstack returns DELETE_COMPLETE status
751             // instead of "404" (which would result from query by stack name).
752             while (heatStack != null && !"DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
753                 LOGGER.debug ("Stack status: " + heatStack.getStackStatus ());
754
755                 if ("DELETE_FAILED".equals (heatStack.getStackStatus ())) {
756                     // Throw a 'special case' of MsoOpenstackException to report the Heat status
757                     String error = "Stack delete error (" + heatStack.getStackStatus ()
758                                    + "): "
759                                    + heatStack.getStackStatusReason ();
760                     MsoOpenstackException me = new MsoOpenstackException (0, "", error);
761                     me.addContext (DELETE_STACK);
762
763                     // Alarm this condition, stack deletion failed
764                     alarmLogger.sendAlarm (HEAT_ERROR, MsoAlarmLogger.CRITICAL, me.getContextMessage ());
765
766                     throw me;
767                 }
768
769                 if (pollTimeout <= 0) {
770                     LOGGER.error (MessageEnum.RA_DELETE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName, heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Delete Stack Timeout");
771
772                     // Throw a 'special case' of MsoOpenstackException to report the Heat status
773                     MsoOpenstackException me = new MsoOpenstackException (0, "", "Stack Deletion Timeout");
774                     me.addContext (DELETE_STACK);
775
776                     // Alarm this condition, stack deletion failed
777                     alarmLogger.sendAlarm (HEAT_ERROR, MsoAlarmLogger.CRITICAL, me.getContextMessage ());
778
779                     throw me;
780                 }
781
782                 try {
783                     Thread.sleep (pollInterval * 1000L);
784                 } catch (InterruptedException e) {
785                     LOGGER.debug ("Thread interrupted while sleeping", e);
786                 }
787
788                 pollTimeout -= pollInterval;
789
790                 heatStack = queryHeatStack (heatClient, canonicalName);
791             }
792
793             // The stack is gone when this point is reached
794             return new StackInfo (stackName, HeatStatus.NOTFOUND, null, null);
795         }
796
797         // Return the current status (if not polling, the delete may still be in progress)
798         StackInfo stackInfo = new StackInfo (heatStack);
799         stackInfo.setName (stackName);
800
801         return stackInfo;
802     }
803
804     /**
805      * Query for all stacks in a tenant site. This call will return a List of StackInfo
806      * objects, one for each deployed stack.
807      *
808      * Note that this is limited to a single site. To ensure that a tenant is truly
809      * empty would require looping across all tenant endpoints.
810      *
811      * @param tenantId The Openstack ID of the tenant to query
812      * @param cloudSiteId The cloud identifier (may be a region) in which to query.
813      * @return A List of StackInfo objects
814      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
815      * @throws MsoCloudSiteNotFound
816      */
817     public List <StackInfo> queryAllStacks (String tenantId, String cloudSiteId) throws MsoException {
818         // Obtain the cloud site information where we will create the stack
819         CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId);
820         if (cloudSite == null) {
821             throw new MsoCloudSiteNotFound (cloudSiteId);
822         }
823
824         // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
825         Heat heatClient = getHeatClient (cloudSite, tenantId);
826
827         try {
828             OpenStackRequest <Stacks> request = heatClient.getStacks ().list ();
829             Stacks stacks = executeAndRecordOpenstackRequest (request, msoProps);
830
831             List <StackInfo> stackList = new ArrayList <StackInfo> ();
832
833             // Not sure if returns an empty list or null if no stacks exist
834             if (stacks != null) {
835                 for (Stack stack : stacks) {
836                     stackList.add (new StackInfo (stack));
837                 }
838             }
839
840             return stackList;
841         } catch (OpenStackResponseException e) {
842             if (e.getStatus () == 404) {
843                 // Not sure if this can happen, but return an empty list
844                 LOGGER.debug ("queryAllStacks - stack not found: ");
845                 return new ArrayList <StackInfo> ();
846             } else {
847                 // Convert the OpenStackResponseException to an MsoOpenstackException
848                 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
849             }
850         } catch (OpenStackConnectException e) {
851             // Error connecting to Openstack instance. Convert to an MsoException
852             throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
853         } catch (RuntimeException e) {
854             // Catch-all
855             throw runtimeExceptionToMsoException (e, QUERY_ALL_STACKS);
856         }
857     }
858
859     /**
860      * Validate parameters to be passed to Heat template. This method performs
861      * three functions:
862      * 1. Apply default values to parameters which have them defined
863      * 2. Report any required parameters that are missing. This will generate an
864      * exception in the caller, since stack create/update operations would fail.
865      * 3. Report and remove any extraneous parameters. This will allow clients to
866      * pass supersets of parameters and not get errors.
867      *
868      * These functions depend on the HeatTemplate definition from the MSO Catalog DB,
869      * along with the input parameter Map. The output is an updated parameter map.
870      * If the parameters are invalid for the template, an IllegalArgumentException
871      * is thrown.
872      */
873     public Map <String, Object> validateStackParams (Map <String, Object> inputParams,
874                                                      HeatTemplate heatTemplate) throws IllegalArgumentException {
875         // Check that required parameters have been supplied for this template type
876         String missingParams = null;
877         List <String> paramList = new ArrayList <String> ();
878
879         // TODO: Enhance DB to support defaults for Heat Template parameters
880
881         for (HeatTemplateParam parm : heatTemplate.getParameters ()) {
882             if (parm.isRequired () && !inputParams.containsKey (parm.getParamName ())) {
883                 if (missingParams == null) {
884                     missingParams = parm.getParamName ();
885                 } else {
886                     missingParams += "," + parm.getParamName ();
887                 }
888             }
889             paramList.add (parm.getParamName ());
890         }
891         if (missingParams != null) {
892             // Problem - missing one or more required parameters
893             String error = "Missing Required inputs for HEAT Template: " + missingParams;
894             LOGGER.error (MessageEnum.RA_MISSING_PARAM, missingParams + " for HEAT Template", "", "", MsoLogger.ErrorCode.SchemaError, "Missing Required inputs for HEAT Template: " + missingParams);
895             throw new IllegalArgumentException (error);
896         }
897
898         // Remove any extraneous parameters (don't throw an error)
899         Map <String, Object> updatedParams = new HashMap <String, Object> ();
900         List <String> extraParams = new ArrayList <String> ();
901         for (String key : inputParams.keySet ()) {
902             if (!paramList.contains (key)) {
903                 // This is not a valid parameter for this template
904                 extraParams.add (key);
905             } else {
906                 updatedParams.put (key, inputParams.get (key));
907             }
908         }
909         if (!extraParams.isEmpty ()) {
910             LOGGER.warn (MessageEnum.RA_GENERAL_WARNING, "Heat Stack (" + heatTemplate.getTemplateName ()
911                          + ") extra input params received: "
912                          + extraParams, "", "", MsoLogger.ErrorCode.DataError, "Heat Stack (" + heatTemplate.getTemplateName () + ") extra input params received: "+ extraParams);
913         }
914
915         return updatedParams;
916     }
917
918     // ---------------------------------------------------------------
919     // PRIVATE FUNCTIONS FOR USE WITHIN THIS CLASS
920
921     /**
922      * Get a Heat client for the Openstack Identity service.
923      * This requires a 'member'-level userId + password, which will be retrieved from
924      * properties based on the specified cloud Id. The tenant in which to operate
925      * must also be provided.
926      * <p>
927      * On successful authentication, the Heat object will be cached for the
928      * tenantID + cloudId so that it can be reused without reauthenticating with
929      * Openstack every time.
930      *
931      * @param tenantName
932      * @param cloudId
933      * @return an authenticated Heat object
934      */
935     public Heat getHeatClient (CloudSite cloudSite, String tenantId) throws MsoException {
936         String cloudId = cloudSite.getId ();
937
938         // Check first in the cache of previously authorized clients
939         String cacheKey = cloudId + ":" + tenantId;
940         if (heatClientCache.containsKey (cacheKey)) {
941             if (!heatClientCache.get (cacheKey).isExpired ()) {
942                 LOGGER.debug ("Using Cached HEAT Client for " + cacheKey);
943                 return heatClientCache.get (cacheKey).getHeatClient ();
944             } else {
945                 // Token is expired. Remove it from cache.
946                 heatClientCache.remove (cacheKey);
947                 LOGGER.debug ("Expired Cached HEAT Client for " + cacheKey);
948             }
949         }
950
951         // Obtain an MSO token for the tenant
952         CloudIdentity cloudIdentity = cloudSite.getIdentityService ();
953         String keystoneUrl = cloudIdentity.getKeystoneUrl (cloudId, msoPropID);
954         Keystone keystoneTenantClient = new Keystone (keystoneUrl);
955         Access access = null;
956         try {
957                 Authentication credentials = cloudIdentity.getAuthentication ();
958
959                 OpenStackRequest <Access> request = keystoneTenantClient.tokens ()
960                        .authenticate (credentials).withTenantId (tenantId);
961
962             access = executeAndRecordOpenstackRequest (request, msoProps);
963         } catch (OpenStackResponseException e) {
964             if (e.getStatus () == 401) {
965                 // Authentication error.
966                 String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId ();
967                 alarmLogger.sendAlarm ("MsoAuthenticationError", MsoAlarmLogger.CRITICAL, error);
968                 throw new MsoAdapterException (error);
969             } else {
970                 throw keystoneErrorToMsoException (e, TOKEN_AUTH);
971             }
972         } catch (OpenStackConnectException e) {
973             // Connection to Openstack failed
974             MsoIOException me = new MsoIOException (e.getMessage (), e);
975             me.addContext (TOKEN_AUTH);
976             throw me;
977         } catch (RuntimeException e) {
978             // Catch-all
979             throw runtimeExceptionToMsoException (e, TOKEN_AUTH);
980         }
981
982         // For DCP/LCP, the region should be the cloudId.
983         String region = cloudSite.getRegionId ();
984         String heatUrl = null;
985         try {
986             heatUrl = KeystoneUtils.findEndpointURL (access.getServiceCatalog (), "orchestration", region, "public");
987         } catch (RuntimeException e) {
988             // This comes back for not found (probably an incorrect region ID)
989             String error = "Orchestration service not found: region=" + region + ",cloud=" + cloudIdentity.getId ();
990             alarmLogger.sendAlarm ("MsoConfigurationError", MsoAlarmLogger.CRITICAL, error);
991             throw new MsoAdapterException (error, e);
992         }
993
994         Heat heatClient = new Heat (heatUrl);
995         heatClient.token (access.getToken ().getId ());
996
997         heatClientCache.put (cacheKey,
998                              new HeatCacheEntry (heatUrl,
999                                                  access.getToken ().getId (),
1000                                                  access.getToken ().getExpires ()));
1001         LOGGER.debug ("Caching HEAT Client for " + cacheKey);
1002
1003         return heatClient;
1004     }
1005
1006     /**
1007      * Forcibly expire a HEAT client from the cache. This call is for use by
1008      * the KeystoneClient in case where a tenant is deleted. In that case,
1009      * all cached credentials must be purged so that fresh authentication is
1010      * done if a similarly named tenant is re-created.
1011      * <p>
1012      * Note: This is probably only applicable to dev/test environments where
1013      * the same Tenant Name is repeatedly used for creation/deletion.
1014      * <p>
1015      *
1016      * @param tenantName
1017      * @param cloudId
1018      */
1019     public static void expireHeatClient (String tenantId, String cloudId) {
1020         String cacheKey = cloudId + ":" + tenantId;
1021         if (heatClientCache.containsKey (cacheKey)) {
1022             heatClientCache.remove (cacheKey);
1023             LOGGER.debug ("Deleted Cached HEAT Client for " + cacheKey);
1024         }
1025     }
1026
1027     /*
1028      * Query for a Heat Stack. This function is needed in several places, so
1029      * a common method is useful. This method takes an authenticated Heat Client
1030      * (which internally identifies the cloud & tenant to search), and returns
1031      * a Stack object if found, Null if not found, or an MsoOpenstackException
1032      * if the Openstack API call fails.
1033      *
1034      * The stack name may be a simple name or a canonical name ("{name}/{id}").
1035      * When simple name is used, Openstack always returns a 302 redirect which
1036      * results in a 2nd request (to the canonical name). Note that query by
1037      * canonical name for a deleted stack returns a Stack object with status
1038      * "DELETE_COMPLETE" while query by simple name for a deleted stack returns
1039      * HTTP 404.
1040      *
1041      * @param heatClient an authenticated Heat client
1042      *
1043      * @param stackName the stack name to query
1044      *
1045      * @return a Stack object that describes the current stack or null if the
1046      * requested stack doesn't exist.
1047      *
1048      * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
1049      */
1050     protected Stack queryHeatStack (Heat heatClient, String stackName) throws MsoException {
1051         if (stackName == null) {
1052             return null;
1053         }
1054         try {
1055             OpenStackRequest <Stack> request = heatClient.getStacks ().byName (stackName);
1056             return executeAndRecordOpenstackRequest (request, msoProps);
1057         } catch (OpenStackResponseException e) {
1058             if (e.getStatus () == 404) {
1059                 LOGGER.debug ("queryHeatStack - stack not found: " + stackName);
1060                 return null;
1061             } else {
1062                 // Convert the OpenStackResponseException to an MsoOpenstackException
1063                 throw heatExceptionToMsoException (e, "QueryStack");
1064             }
1065         } catch (OpenStackConnectException e) {
1066             // Connection to Openstack failed
1067             throw heatExceptionToMsoException (e, "QueryAllStack");
1068         }
1069     }
1070
1071     /*
1072      * An entry in the Heat Client Cache. It saves the Heat client object
1073      * along with the token expiration. After this interval, this cache
1074      * item will no longer be used.
1075      */
1076     private static class HeatCacheEntry implements Serializable {
1077
1078         private static final long serialVersionUID = 1L;
1079
1080         private String heatUrl;
1081         private String token;
1082         private Calendar expires;
1083
1084         public HeatCacheEntry (String heatUrl, String token, Calendar expires) {
1085             this.heatUrl = heatUrl;
1086             this.token = token;
1087             this.expires = expires;
1088         }
1089
1090         public Heat getHeatClient () {
1091             Heat heatClient = new Heat (heatUrl);
1092             heatClient.token (token);
1093             return heatClient;
1094         }
1095
1096         public boolean isExpired () {
1097             if (expires == null) {
1098                 return true;
1099             }
1100
1101             return System.currentTimeMillis() > expires.getTimeInMillis();
1102         }
1103     }
1104
1105     /**
1106      * Clean up the Heat client cache to remove expired entries.
1107      */
1108     public static void heatCacheCleanup () {
1109         for (String cacheKey : heatClientCache.keySet ()) {
1110             if (heatClientCache.get (cacheKey).isExpired ()) {
1111                 heatClientCache.remove (cacheKey);
1112                 LOGGER.debug ("Cleaned Up Cached Heat Client for " + cacheKey);
1113             }
1114         }
1115     }
1116
1117     /**
1118      * Reset the Heat client cache.
1119      * This may be useful if cached credentials get out of sync.
1120      */
1121     public static void heatCacheReset () {
1122         heatClientCache = new HashMap <String, HeatCacheEntry> ();
1123     }
1124
1125         public Map<String, Object> queryStackForOutputs(String cloudSiteId,
1126                         String tenantId, String stackName) throws MsoException {
1127                 LOGGER.debug("MsoHeatUtils.queryStackForOutputs)");
1128                 StackInfo heatStack = this.queryStack(cloudSiteId, tenantId, stackName);
1129                 if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
1130                         return null;
1131                 }
1132                 Map<String, Object> outputs = heatStack.getOutputs();
1133                 return outputs;
1134         }
1135
1136         public void queryAndCopyOutputsToInputs(String cloudSiteId,
1137                         String tenantId, String stackName, Map<String, String> inputs,
1138                         boolean overWrite) throws MsoException {
1139                 LOGGER.debug("MsoHeatUtils.queryAndCopyOutputsToInputs");
1140                 Map<String, Object> outputs = this.queryStackForOutputs(cloudSiteId,
1141                                 tenantId, stackName);
1142                 this.copyStringOutputsToInputs(inputs, outputs, overWrite);
1143                 return;
1144         }
1145
1146         public void copyStringOutputsToInputs(Map<String, String> inputs,
1147                         Map<String, Object> otherStackOutputs, boolean overWrite) {
1148                 if (inputs == null || otherStackOutputs == null)
1149                         return;
1150                 for (String key : otherStackOutputs.keySet()) {
1151                         if (!inputs.containsKey(key)) {
1152                                 Object obj = otherStackOutputs.get(key);
1153                                 if (obj instanceof String) {
1154                                         inputs.put(key, (String) otherStackOutputs.get(key));
1155                                 } else if (obj instanceof JsonNode ){
1156                                         // This is a bit of mess - but I think it's the least impacting
1157                                         // let's convert it BACK to a string - then it will get converted back later
1158                                         try {
1159                                                 String str = this.convertNode((JsonNode) obj);
1160                                                 inputs.put(key, str);
1161                                         } catch (Exception e) {
1162                                                 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for "+ key);
1163                                                 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1164                                         }
1165                                 } else if (obj instanceof java.util.LinkedHashMap) {
1166                                         LOGGER.debug("LinkedHashMap - this is showing up as a LinkedHashMap instead of JsonNode");
1167                                         try {
1168                                                 String str = JSON_MAPPER.writeValueAsString(obj);
1169                                                 inputs.put(key, str);
1170                                         } catch (Exception e) {
1171                                                 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for "+ key);
1172                                         }
1173                                 } else {
1174                                         // just try to cast it - could be an integer or some such
1175                                         try {
1176                                                 String str = (String) obj;
1177                                                 inputs.put(key, str);
1178                                         } catch (Exception e) {
1179                                                 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for "+ key);
1180                                                 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1181                                         }
1182                                 }
1183                         }
1184                 }
1185                 return;
1186         }
1187         public StringBuilder requestToStringBuilder(CreateStackParam stack) {
1188                 StringBuilder sb = new StringBuilder();
1189                 sb.append("Stack:\n");
1190                 sb.append("\tStackName: " + stack.getStackName());
1191                 sb.append("\tTemplateUrl: " + stack.getTemplateUrl());
1192                 sb.append("\tTemplate: " + stack.getTemplate());
1193                 sb.append("\tEnvironment: " + stack.getEnvironment());
1194                 sb.append("\tTimeout: " + stack.getTimeoutMinutes());
1195                 sb.append("\tParameters:\n");
1196                 Map<String, Object> params = stack.getParameters();
1197                 if (params == null || params.size() < 1) {
1198                         sb.append("\tNONE");
1199                 } else {
1200                         for (String key : params.keySet()) {
1201                                 if (params.get(key) instanceof String) {
1202                                         sb.append("\t" + key + "=" + (String) params.get(key));
1203                                 } else if (params.get(key) instanceof JsonNode) {
1204                                         String jsonStringOut = this.convertNode((JsonNode)params.get(key));
1205                                         sb.append("\t" + key + "=" + jsonStringOut);
1206                                 } else {
1207                                         sb.append("\t" + key + "= [some non-string/non-json]");
1208                                 }
1209                         }
1210                 }
1211                 return sb;
1212         }
1213
1214         private String convertNode(final JsonNode node) {
1215                 try {
1216                         final Object obj = JSON_MAPPER.treeToValue(node, Object.class);
1217                         final String json = JSON_MAPPER.writeValueAsString(obj);
1218                         return json;
1219                 } catch (JsonParseException jpe) {
1220                         LOGGER.debug("Error converting json to string " + jpe.getMessage());
1221                 } catch (Exception e) {
1222                         LOGGER.debug("Error converting json to string " + e.getMessage());
1223                 }
1224                 return "[Error converting json to string]";
1225         }
1226
1227
1228     private StringBuilder getOutputsAsStringBuilder(Stack heatStack) {
1229         // This should only be used as a utility to print out the stack outputs
1230         // to the log
1231         StringBuilder sb = new StringBuilder("");
1232         if (heatStack == null) {
1233             sb.append("(heatStack is null)");
1234             return sb;
1235         }
1236         List<Output> outputList = heatStack.getOutputs();
1237         if (outputList == null || outputList.isEmpty()) {
1238             sb.append("(outputs is empty)");
1239             return sb;
1240         }
1241         Map<String, Object> outputs = new HashMap<String,Object>();
1242         for (Output outputItem : outputList) {
1243             outputs.put(outputItem.getOutputKey(), outputItem.getOutputValue());
1244         }
1245         int counter = 0;
1246         sb.append("OUTPUTS:");
1247         for (String key : outputs.keySet()) {
1248             sb.append("outputs[" + counter++ + "]: " + key + "=");
1249             Object obj = outputs.get(key);
1250             if (obj instanceof String) {
1251                 sb.append((String)obj);
1252             } else if (obj instanceof JsonNode) {
1253                 sb.append(this.convertNode((JsonNode)obj));
1254             } else if (obj instanceof java.util.LinkedHashMap) {
1255                 try {
1256                     String str = JSON_MAPPER.writeValueAsString(obj);
1257                     sb.append(str);
1258                 } catch (Exception e) {
1259                     sb.append("(a LinkedHashMap value that would not convert nicely)");
1260                 }
1261             } else {
1262                 String str = "";
1263                 try {
1264                     str = (String) obj;
1265                 } catch (Exception e) {
1266                     str = "(a value unable to be cast as a String)";
1267                 }
1268                 sb.append(str);
1269             }
1270             sb.append("; ");
1271         }
1272         sb.append("[END]");
1273         return sb;
1274     }
1275 }