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