2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.so.openstack.utils;
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;
31 import java.util.Map.Entry;
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;
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;
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;
92 public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{
94 private static final String TOKEN_AUTH = "TokenAuth";
96 private static final String QUERY_ALL_STACKS = "QueryAllStacks";
98 private static final String DELETE_STACK = "DeleteStack";
100 protected static final String HEAT_ERROR = "HeatError";
102 protected static final String CREATE_STACK = "CreateStack";
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.
108 // The cache key is "tenantId:cloudId"
109 private static Map <String, HeatCacheEntry> heatClientCache = new HashMap <> ();
111 // Fetch cloud configuration each time (may be cached in CloudConfig class)
113 protected CloudConfig cloudConfig;
116 private Environment environment;
119 private AuthenticationMethodFactory authenticationMethodFactory;
122 private MsoTenantUtilsFactory tenantUtilsFactory;
125 private KeystoneV3Authentication keystoneV3Authentication;
127 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, MsoHeatUtils.class);
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";
134 protected static final String createPollIntervalDefault = "15";
135 private static final String deletePollIntervalDefault = "15";
137 private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
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
143 public StackInfo createStack (String cloudSiteId,
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,
164 // This method has environment, but not files or heatFiles
165 public StackInfo createStack (String cloudSiteId,
169 Map <String, ?> stackInputs,
170 boolean pollForCompletion,
172 String environment) throws MsoException {
173 // Just call the new method with the files/heatFiles variables set to null
174 return this.createStack (cloudSiteId,
187 // This method has environment and files, but not heatFiles.
188 public StackInfo createStack (String cloudSiteId,
192 Map <String, ?> stackInputs,
193 boolean pollForCompletion,
196 Map <String, Object> files) throws MsoException {
197 return this.createStack (cloudSiteId,
210 // This method has environment, files, heatfiles
211 public StackInfo createStack (String cloudSiteId,
215 Map <String, ?> stackInputs,
216 boolean pollForCompletion,
219 Map <String, Object> files,
220 Map <String, Object> heatFiles) throws MsoException {
221 return this.createStack (cloudSiteId,
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..
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.
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.
251 * An error will be thrown if the requested Stack already exists in the specified
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.
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
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.
274 @SuppressWarnings("unchecked")
275 public StackInfo createStack (String cloudSiteId,
279 Map <String, ?> stackInputs,
280 boolean pollForCompletion,
283 Map <String, Object> files,
284 Map <String, Object> heatFiles,
285 boolean backout) throws MsoException {
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()) {
297 CreateStackParam stack = createStackParam(stackName, heatTemplate, stackInputs, timeoutMinutes, environment, files, heatFiles);
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());
310 LOGGER.debug ("Ready to Create Stack (" + heatTemplate + ") with input params: " + stackInputs);
312 Stack heatStack = null;
314 // Execute the actual Openstack command to create the Heat stack
315 OpenStackRequest <Stack> request = heatClient.getStacks ().create (stack);
317 // Obtain an MSO token for the tenant
318 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
319 // cloudIdentity.getMsoId(), cloudIdentity.getMsoPass()
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);
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);
340 } catch (OpenStackConnectException e) {
341 // Error connecting to Openstack instance. Convert to an MsoException
342 throw heatExceptionToMsoException (e, CREATE_STACK);
343 } catch (RuntimeException e) {
345 throw runtimeExceptionToMsoException (e, CREATE_STACK);
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 ();
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
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);
370 heatStack = queryHeatStack (heatClient, canonicalName);
371 LOGGER.debug (heatStack.getStackStatus () + " (" + canonicalName + ")");
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);
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;
389 sleep(createPollInterval * 1000L);
391 pollTimeout -= createPollInterval;
392 LOGGER.debug("pollTimeout remaining: " + pollTimeout);
394 //save off the status & reason msg before we attempt delete
395 stackErrorStatusReason.append("Stack error (" + heatStack.getStackStatus() + "): " + heatStack.getStackStatusReason());
398 } catch (MsoException me) {
399 // Cannot query the stack status. Something is wrong.
400 // Try to roll back the stack
403 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in Create Stack, stack deletion suppressed");
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;
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");
426 sleep(deletePollInterval * 1000L);
427 deletePollTimeout -= deletePollInterval;
429 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
430 LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
434 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
438 // assume if we can't find it - it's deleted
439 LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
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");
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");
456 // Propagate the original exception from Stack Query.
457 me.addContext (CREATE_STACK);
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");
466 // Rollback the stack creation, since it is in an indeterminate state.
469 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error, stack deletion suppressed");
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;
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");
491 sleep(deletePollInterval * 1000L);
492 deletePollTimeout -= deletePollInterval;
493 LOGGER.debug("deletePollTimeout remaining: " + deletePollTimeout);
495 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
496 LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
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());
505 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
509 // assume if we can't find it - it's deleted
510 LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
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());
521 } // end while !deleted
522 StringBuilder errorContextMessage;
523 if (createTimedOut) {
524 errorContextMessage = new StringBuilder("Stack Creation Timeout");
526 errorContextMessage = stackErrorStatusReason;
529 errorContextMessage.append(" - stack successfully deleted");
531 errorContextMessage.append(" - encountered an error trying to delete the stack");
533 // MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
534 // me.addContext(CREATE_STACK);
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");
542 MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
543 me.addContext(CREATE_STACK);
549 // Get initial status, since it will have been null after the create.
550 heatStack = queryHeatStack (heatClient, canonicalName);
551 LOGGER.debug (heatStack.getStackStatus ());
554 return new StackInfoMapper(heatStack).map();
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.
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.
568 public StackInfo queryStack (String cloudSiteId, String tenantId, String stackName) throws MsoException {
569 LOGGER.debug ("Query HEAT stack: " + stackName + " in tenant " + tenantId);
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());
576 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
577 Heat heatClient = null;
579 heatClient = getHeatClient (cloudSite, tenantId);
580 if (heatClient != null) {
581 LOGGER.debug("Found: " + heatClient.toString());
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");
595 // An MsoException will propagate transparently to the caller.
596 Stack heatStack = queryHeatStack (heatClient, stackName);
598 if (heatStack == null) {
599 // Stack does not exist. Return a StackInfo with status NOTFOUND
600 return new StackInfo (stackName, HeatStatus.NOTFOUND);
603 return new StackInfoMapper(heatStack).map();
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.
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.
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.
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
628 public StackInfo deleteStack (String tenantId,
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());
637 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
638 Heat heatClient = null;
640 heatClient = getHeatClient (cloudSite, tenantId);
641 if (heatClient != null) {
642 LOGGER.debug("Found: " + heatClient.toString());
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);
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);
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 ();
670 OpenStackRequest <Void> request = null;
671 if(null != heatClient) {
672 request = heatClient.getStacks ().deleteByName (canonicalName);
675 LOGGER.debug ("Heat Client is NULL" );
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);
684 // Convert the OpenStackResponseException to an MsoOpenstackException
685 throw heatExceptionToMsoException (e, DELETE_STACK);
687 } catch (OpenStackConnectException e) {
688 // Error connecting to Openstack instance. Convert to an MsoException
689 throw heatExceptionToMsoException (e, DELETE_STACK);
690 } catch (RuntimeException e) {
692 throw runtimeExceptionToMsoException (e, DELETE_STACK);
695 // Requery the stack for current status.
696 // It will probably still exist with "DELETE_IN_PROGRESS" status.
697 heatStack = queryHeatStack (heatClient, canonicalName);
699 if (pollForCompletion) {
700 // Set a timeout on polling
702 int pollInterval = Integer.parseInt(this.environment.getProperty(deletePollIntervalProp, "" + deletePollIntervalDefault));
703 int pollTimeout = Integer.parseInt(this.environment.getProperty(deletePollTimeoutProp, "" + deletePollIntervalDefault));
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 ());
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 ()
714 + heatStack.getStackStatusReason ();
715 MsoOpenstackException me = new MsoOpenstackException (0, "", error);
716 me.addContext (DELETE_STACK);
718 // Alarm this condition, stack deletion failed
724 if (pollTimeout <= 0) {
725 LOGGER.error (MessageEnum.RA_DELETE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName, heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Delete Stack Timeout");
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);
731 // Alarm this condition, stack deletion failed
737 sleep(pollInterval * 1000L);
739 pollTimeout -= pollInterval;
740 LOGGER.debug("pollTimeout remaining: " + pollTimeout);
742 heatStack = queryHeatStack (heatClient, canonicalName);
745 // The stack is gone when this point is reached
746 return new StackInfo (stackName, HeatStatus.NOTFOUND);
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);
757 * Query for all stacks in a tenant site. This call will return a List of StackInfo
758 * objects, one for each deployed stack.
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.
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
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);
777 OpenStackRequest <Stacks> request = heatClient.getStacks ().list ();
778 Stacks stacks = executeAndRecordOpenstackRequest (request);
780 List <StackInfo> stackList = new ArrayList <> ();
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());
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 <> ();
796 // Convert the OpenStackResponseException to an MsoOpenstackException
797 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
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) {
804 throw runtimeExceptionToMsoException (e, QUERY_ALL_STACKS);
809 * Validate parameters to be passed to Heat template. This method performs
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.
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
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 <> ();
828 // TODO: Enhance DB to support defaults for Heat Template parameters
830 for (HeatTemplateParam parm : heatTemplate.getParameters ()) {
831 if (parm.isRequired () && !inputParams.containsKey (parm.getParamName ())) {
832 if (missingParams == null) {
833 missingParams = new StringBuilder(parm.getParamName());
835 missingParams.append("," + parm.getParamName());
838 paramList.add (parm.getParamName ());
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);
847 // Remove any extraneous parameters (don't throw an error)
848 Map <String, Object> updatedParams = new HashMap <> ();
849 List <String> extraParams = new ArrayList <> ();
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());
856 updatedParams.put(entry.getKey(), entry.getValue());
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);
866 return updatedParams;
869 // ---------------------------------------------------------------
870 // PRIVATE FUNCTIONS FOR USE WITHIN THIS CLASS
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.
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.
882 * @return an authenticated Heat object
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 ();
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 ();
896 // Token is expired. Remove it from cache.
897 heatClientCache.remove (cacheKey);
898 LOGGER.debug ("Expired Cached HEAT Client for " + cacheKey);
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;
912 if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
913 Keystone keystoneTenantClient = new Keystone (keystoneUrl);
914 Access access = null;
916 Authentication credentials = authenticationMethodFactory.getAuthenticationFor(cloudIdentity);
918 OpenStackRequest <Access> request = keystoneTenantClient.tokens ()
919 .authenticate (credentials).withTenantId (tenantId);
921 access = executeAndRecordOpenstackRequest (request);
924 // Isolate trying to printout the region IDs
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());
933 } catch (Exception e) {
934 LOGGER.debug("Encountered an error trying to printout Access object returned from AIC. " + e.getMessage());
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);
943 tokenId = access.getToken ().getId ();
944 expiration = access.getToken ().getExpires ();
945 } else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
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);
957 } catch (OpenStackResponseException e) {
958 if (e.getStatus () == 401) {
959 // Authentication error.
960 String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId ();
962 throw new MsoAdapterException (error);
964 throw keystoneErrorToMsoException (e, TOKEN_AUTH);
966 } catch (OpenStackConnectException e) {
967 // Connection to Openstack failed
968 MsoIOException me = new MsoIOException (e.getMessage (), e);
969 me.addContext (TOKEN_AUTH);
971 } catch (RuntimeException e) {
973 throw runtimeExceptionToMsoException (e, TOKEN_AUTH);
976 Heat heatClient = new Heat (heatUrl);
977 heatClient.token (tokenId);
979 heatClientCache.put (cacheKey,
980 new HeatCacheEntry (heatUrl,
983 LOGGER.debug ("Caching HEAT Client for " + cacheKey);
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.
994 * Note: This is probably only applicable to dev/test environments where
995 * the same Tenant Name is repeatedly used for creation/deletion.
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);
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.
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
1021 * @param heatClient an authenticated Heat client
1023 * @param stackName the stack name to query
1025 * @return a Stack object that describes the current stack or null if the
1026 * requested stack doesn't exist.
1028 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
1030 protected Stack queryHeatStack (Heat heatClient, String stackName) throws MsoException {
1031 if (stackName == null) {
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);
1042 // Convert the OpenStackResponseException to an MsoOpenstackException
1043 throw heatExceptionToMsoException (e, "QueryStack");
1045 } catch (OpenStackConnectException e) {
1046 // Connection to Openstack failed
1047 throw heatExceptionToMsoException (e, "QueryAllStack");
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) {
1059 return heatStack.getOutputs();
1062 public void copyStringOutputsToInputs(Map<String, String> inputs,
1063 Map<String, Object> otherStackOutputs, boolean overWrite) {
1064 if (inputs == null || otherStackOutputs == null)
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
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
1081 } else if (obj instanceof java.util.LinkedHashMap) {
1082 LOGGER.debug("LinkedHashMap - this is showing up as a LinkedHashMap instead of JsonNode");
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);
1089 } else if (obj instanceof Integer) {
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);
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
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");
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);
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);
1145 private String convertNode(final JsonNode node) {
1147 final Object obj = JSON_MAPPER.treeToValue(node, Object.class);
1148 final String json = JSON_MAPPER.writeValueAsString(obj);
1150 } catch (Exception e) {
1151 LOGGER.debug("Error converting json to string " + e.getMessage(), e);
1153 return "[Error converting json to string]";
1157 protected StringBuilder getOutputsAsStringBuilder(Stack heatStack) {
1158 // This should only be used as a utility to print out the stack outputs
1160 StringBuilder sb = new StringBuilder("");
1161 if (heatStack == null) {
1162 sb.append("(heatStack is null)");
1165 List<Output> outputList = heatStack.getOutputs();
1166 if (outputList == null || outputList.isEmpty()) {
1167 sb.append("(outputs is empty)");
1170 Map<String, Object> outputs = new HashMap<>();
1171 for (Output outputItem : outputList) {
1172 outputs.put(outputItem.getOutputKey(), outputItem.getOutputValue());
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) {
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)");
1191 } else if (obj instanceof Integer) {
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)";
1200 } else if (obj instanceof ArrayList) {
1203 str = obj.toString() + " (an ArrayList)";
1204 } catch (Exception e) {
1205 LOGGER.debug("Exception :",e);
1206 str = "(an ArrayList unable to call .toString() on?)";
1209 } else if (obj instanceof Boolean) {
1212 str = obj.toString() + " (a Boolean)";
1213 } catch (Exception e) {
1214 LOGGER.debug("Exception :",e);
1215 str = "(an Boolean unable to call .toString() on?)";
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?)";
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)
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");
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);
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);
1275 LOGGER.debug("\t\t**UNKNOWN OBJECT TYPE");
1276 inputs.put(key, obj);
1279 LOGGER.debug("key=" + key + " is already in the inputs - will not overwrite");
1285 public List<String> convertCdlToArrayList(String cdl) {
1286 String cdl2 = cdl.trim();
1288 if (cdl2.startsWith("[") && cdl2.endsWith("]")) {
1289 cdl3 = cdl2.substring(1, cdl2.lastIndexOf("]"));
1293 return new ArrayList<>(Arrays.asList(cdl3.split(",")));
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
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
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<>();
1318 if (inputs == null) {
1319 LOGGER.debug("convertInputMap - inputs is null - nothing to do here");
1320 return new HashMap<>();
1323 LOGGER.debug("convertInputMap in MsoHeatUtils called, with " + inputs.size() + " inputs, and template " + template.getArtifactUuid());
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);
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);
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");
1353 realName = paramAliases.get(key).getParamName();
1354 LOGGER.debug("FOUND AN ALIAS! Will use " + realName + " in lieu of give key/alias " + key);
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");
1362 LOGGER.debug("Parameter: " + key + " is of type " + type);
1363 if ("string".equalsIgnoreCase(type)) {
1365 String str = inputs.get(key);
1367 newInputs.put(realName, str);
1369 newInputs.put(key, str);
1370 } else if ("number".equalsIgnoreCase(type)) {
1371 String integerString = inputs.get(key);
1372 Integer anInteger = null;
1374 anInteger = Integer.parseInt(integerString);
1375 } catch (Exception e) {
1376 LOGGER.debug("Unable to convert " + integerString + " to an integer!!", e);
1379 if (anInteger != null) {
1381 newInputs.put(realName, anInteger);
1383 newInputs.put(key, anInteger);
1387 newInputs.put(realName, integerString);
1389 newInputs.put(key, integerString);
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...");
1396 newInputs.put(realName, jsonString);
1398 newInputs.put(key, jsonString);
1400 } else if ("comma_delimited_list".equalsIgnoreCase(type)) {
1401 String commaSeparated = inputs.get(key);
1403 List<String> anArrayList = this.convertCdlToArrayList(commaSeparated);
1405 newInputs.put(realName, anArrayList);
1407 newInputs.put(key, anArrayList);
1408 } catch (Exception e) {
1409 LOGGER.debug("Unable to convert " + commaSeparated + " to an ArrayList!!", e);
1411 newInputs.put(realName, commaSeparated);
1413 newInputs.put(key, commaSeparated);
1415 } else if ("boolean".equalsIgnoreCase(type)) {
1416 String booleanString = inputs.get(key);
1417 Boolean aBool = Boolean.valueOf(booleanString);
1419 newInputs.put(realName, aBool);
1421 newInputs.put(key, aBool);
1423 // it's null or something undefined - just add it back as a String
1424 String str = inputs.get(key);
1426 newInputs.put(realName, str);
1428 newInputs.put(key, str);
1435 * This helpful method added for Valet
1437 public String getCloudSiteKeystoneUrl(String cloudSiteId) throws MsoCloudSiteNotFound {
1438 String keystone_url = null;
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);
1446 if (keystone_url == null || keystone_url.isEmpty()) {
1447 throw new MsoCloudSiteNotFound(cloudSiteId);
1449 return keystone_url;
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
1457 private String printStackRequest(String tenantId,
1458 Map<String, Object> heatFiles,
1459 Map<String, Object> nestedTemplates,
1461 Map<String, Object> inputs,
1462 String vfModuleName,
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);
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);
1477 sb.append(" \"environment\": {\n");
1478 if (environment == null)
1479 sb.append("<none>");
1481 sb.append(environment);
1483 sb.append(" \"files\": {\n");
1484 int filesCounter = 0;
1485 if (heatFiles != null) {
1486 for (String key : heatFiles.keySet()) {
1488 if (filesCounter > 1) {
1491 sb.append(" \"" + key + "\": {\n");
1492 sb.append(heatFiles.get(key).toString() + "\n }");
1495 if (nestedTemplates != null) {
1496 for (String key : nestedTemplates.keySet()) {
1498 if (filesCounter > 1) {
1501 sb.append(" \"" + key + "\": {\n");
1502 sb.append(nestedTemplates.get(key).toString() + "\n }");
1505 sb.append("\n },\n");
1506 sb.append(" \"parameters\": {\n");
1507 int paramCounter = 0;
1508 for (String name : inputs.keySet()) {
1510 if (paramCounter > 1) {
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() );
1523 sb.append(" \"" + name + "\": " + "\"(there was an issue trying to dump this value...)\"" );
1526 sb.append("\n }\n}\n");
1528 return sb.toString();
1531 /*******************************************************************************
1533 * Methods (and associated utilities) to implement the VduPlugin interface
1535 *******************************************************************************/
1538 * VduPlugin interface for instantiate function.
1540 * Translate the VduPlugin parameters to the corresponding 'createStack' parameters,
1541 * and then invoke the existing function.
1544 public VduInstance instantiateVdu (
1545 CloudInfo cloudInfo,
1546 String instanceName,
1547 Map<String,Object> inputs,
1548 VduModelInfo vduModel,
1549 boolean rollbackOnFailure)
1552 String cloudSiteId = cloudInfo.getCloudSiteId();
1553 String tenantId = cloudInfo.getTenantId();
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;
1564 for (VduArtifact vduArtifact: vduModel.getArtifacts()) {
1565 if (vduArtifact.getType() == ArtifactType.MAIN_TEMPLATE) {
1566 heatTemplate = new String(vduArtifact.getContent());
1568 else if (vduArtifact.getType() == ArtifactType.NESTED_TEMPLATE) {
1569 nestedTemplates.put(vduArtifact.getName(), new String(vduArtifact.getContent()));
1571 else if (vduArtifact.getType() == ArtifactType.ENVIRONMENT) {
1572 heatEnvironment = new String(vduArtifact.getContent());
1577 StackInfo stackInfo = createStack (cloudSiteId,
1582 true, // poll for completion
1583 vduModel.getTimeoutMinutes(),
1589 // Populate a vduInstance from the StackInfo
1590 return stackInfoToVduInstance(stackInfo);
1592 catch (Exception e) {
1593 throw new VduException ("MsoHeatUtils (instantiateVDU): createStack Exception", e);
1599 * VduPlugin interface for query function.
1602 public VduInstance queryVdu (CloudInfo cloudInfo, String instanceId)
1605 String cloudSiteId = cloudInfo.getCloudSiteId();
1606 String tenantId = cloudInfo.getTenantId();
1609 // Query the Cloudify Deployment object and populate a VduInstance
1610 StackInfo stackInfo = queryStack (cloudSiteId, tenantId, instanceId);
1612 return stackInfoToVduInstance(stackInfo);
1614 catch (Exception e) {
1615 throw new VduException ("MsoHeatUtile (queryVdu): queryStack Exception ", e);
1621 * VduPlugin interface for delete function.
1624 public VduInstance deleteVdu (CloudInfo cloudInfo, String instanceId, int timeoutMinutes)
1627 String cloudSiteId = cloudInfo.getCloudSiteId();
1628 String tenantId = cloudInfo.getTenantId();
1631 // Delete the Heat stack
1632 StackInfo stackInfo = deleteStack (tenantId, cloudSiteId, instanceId, true);
1634 // Populate a VduInstance based on the deleted Cloudify Deployment object
1635 VduInstance vduInstance = stackInfoToVduInstance(stackInfo);
1637 // Override return state to DELETED (HeatUtils sets to NOTFOUND)
1638 vduInstance.getStatus().setState(VduStateType.DELETED);
1642 catch (Exception e) {
1643 throw new VduException ("Delete VDU Exception", e);
1649 * VduPlugin interface for update function.
1651 * Update is currently not supported in the MsoHeatUtils implementation of VduPlugin.
1652 * Just return a VduException.
1656 public VduInstance updateVdu (
1657 CloudInfo cloudInfo,
1659 Map<String,Object> inputs,
1660 VduModelInfo vduModel,
1661 boolean rollbackOnFailure)
1664 throw new VduException ("MsoHeatUtils: updateVdu interface not supported");
1669 * Convert the local DeploymentInfo object (Cloudify-specific) to a generic VduInstance object
1671 protected VduInstance stackInfoToVduInstance (StackInfo stackInfo)
1673 VduInstance vduInstance = new VduInstance();
1675 // The full canonical name as the instance UUID
1676 vduInstance.setVduInstanceId(stackInfo.getCanonicalName());
1677 vduInstance.setVduInstanceName(stackInfo.getName());
1679 // Copy inputs and outputs
1680 vduInstance.setInputs(stackInfo.getParameters());
1681 vduInstance.setOutputs(stackInfo.getOutputs());
1683 // Translate the status elements
1684 vduInstance.setStatus(stackStatusToVduStatus (stackInfo));
1689 private VduStatus stackStatusToVduStatus (StackInfo stackInfo)
1691 VduStatus vduStatus = new VduStatus();
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();
1698 if (heatStatus == HeatStatus.INIT || heatStatus == HeatStatus.BUILDING) {
1699 vduStatus.setState(VduStateType.INSTANTIATING);
1700 vduStatus.setLastAction((new PluginAction ("create", "in_progress", statusMessage)));
1702 else if (heatStatus == HeatStatus.NOTFOUND) {
1703 vduStatus.setState(VduStateType.NOTFOUND);
1705 else if (heatStatus == HeatStatus.CREATED) {
1706 vduStatus.setState(VduStateType.INSTANTIATED);
1707 vduStatus.setLastAction((new PluginAction ("create", "complete", statusMessage)));
1709 else if (heatStatus == HeatStatus.UPDATED) {
1710 vduStatus.setState(VduStateType.INSTANTIATED);
1711 vduStatus.setLastAction((new PluginAction ("update", "complete", statusMessage)));
1713 else if (heatStatus == HeatStatus.UPDATING) {
1714 vduStatus.setState(VduStateType.UPDATING);
1715 vduStatus.setLastAction((new PluginAction ("update", "in_progress", statusMessage)));
1717 else if (heatStatus == HeatStatus.DELETING) {
1718 vduStatus.setState(VduStateType.DELETING);
1719 vduStatus.setLastAction((new PluginAction ("delete", "in_progress", statusMessage)));
1721 else if (heatStatus == HeatStatus.FAILED) {
1722 vduStatus.setState(VduStateType.FAILED);
1723 vduStatus.setErrorMessage(stackInfo.getStatusMessage());
1725 vduStatus.setState(VduStateType.UNKNOWN);
1731 protected void sleep(long time) {
1734 } catch (InterruptedException e) {
1735 LOGGER.debug ("Thread interrupted while sleeping", e);
1736 Thread.currentThread().interrupt();