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