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