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 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.openstack.utils;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.woorea.openstack.base.client.OpenStackConnectException;
29 import com.woorea.openstack.base.client.OpenStackRequest;
30 import com.woorea.openstack.base.client.OpenStackResponseException;
31 import com.woorea.openstack.heat.Heat;
32 import com.woorea.openstack.heat.model.CreateStackParam;
33 import com.woorea.openstack.heat.model.Resources;
34 import com.woorea.openstack.heat.model.Stack;
35 import com.woorea.openstack.heat.model.Stack.Output;
36 import com.woorea.openstack.heat.model.Stacks;
37 import com.woorea.openstack.keystone.Keystone;
38 import com.woorea.openstack.keystone.model.Access;
39 import com.woorea.openstack.keystone.model.Authentication;
40 import com.woorea.openstack.keystone.utils.KeystoneUtils;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Calendar;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.Map.Entry;
51 import org.onap.so.adapters.vdu.CloudInfo;
52 import org.onap.so.adapters.vdu.PluginAction;
53 import org.onap.so.adapters.vdu.VduArtifact;
54 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
55 import org.onap.so.adapters.vdu.VduException;
56 import org.onap.so.adapters.vdu.VduInstance;
57 import org.onap.so.adapters.vdu.VduModelInfo;
58 import org.onap.so.adapters.vdu.VduPlugin;
59 import org.onap.so.adapters.vdu.VduStateType;
60 import org.onap.so.adapters.vdu.VduStatus;
61 import org.onap.so.cloud.CloudConfig;
62 import org.onap.so.cloud.authentication.AuthenticationMethodFactory;
63 import org.onap.so.cloud.authentication.KeystoneAuthHolder;
64 import org.onap.so.cloud.authentication.KeystoneV3Authentication;
65 import org.onap.so.cloud.authentication.ServiceEndpointNotFoundException;
66 import org.onap.so.db.catalog.beans.CloudIdentity;
67 import org.onap.so.db.catalog.beans.CloudSite;
68 import org.onap.so.db.catalog.beans.HeatTemplate;
69 import org.onap.so.db.catalog.beans.HeatTemplateParam;
70 import org.onap.so.db.catalog.beans.ServerType;
71 import org.onap.so.logger.ErrorCode;
72 import org.onap.so.logger.MessageEnum;
73 import org.onap.so.openstack.beans.HeatStatus;
74 import org.onap.so.openstack.beans.StackInfo;
75 import org.onap.so.openstack.exceptions.MsoAdapterException;
76 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
77 import org.onap.so.openstack.exceptions.MsoException;
78 import org.onap.so.openstack.exceptions.MsoIOException;
79 import org.onap.so.openstack.exceptions.MsoOpenstackException;
80 import org.onap.so.openstack.exceptions.MsoStackAlreadyExists;
81 import org.onap.so.openstack.exceptions.MsoTenantNotFound;
82 import org.onap.so.openstack.mappers.StackInfoMapper;
83 import org.onap.so.utils.CryptoUtils;
84 import org.slf4j.Logger;
85 import org.slf4j.LoggerFactory;
86 import org.springframework.beans.factory.annotation.Autowired;
87 import org.springframework.context.annotation.Primary;
88 import org.springframework.core.env.Environment;
89 import org.springframework.stereotype.Component;
93 public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{
95 private static final String TOKEN_AUTH = "TokenAuth";
97 private static final String QUERY_ALL_STACKS = "QueryAllStacks";
99 private static final String DELETE_STACK = "DeleteStack";
101 protected static final String HEAT_ERROR = "HeatError";
103 protected static final String CREATE_STACK = "CreateStack";
105 // Fetch cloud configuration each time (may be cached in CloudConfig class)
107 protected CloudConfig cloudConfig;
110 private Environment environment;
113 private AuthenticationMethodFactory authenticationMethodFactory;
116 private MsoTenantUtilsFactory tenantUtilsFactory;
119 private KeystoneV3Authentication keystoneV3Authentication;
121 private static final Logger logger = LoggerFactory.getLogger(MsoHeatUtils.class);
123 // Properties names and variables (with default values)
124 protected String createPollIntervalProp = "org.onap.so.adapters.po.pollInterval";
125 private String deletePollIntervalProp = "org.onap.so.adapters.po.pollInterval";
126 private String deletePollTimeoutProp = "org.onap.so.adapters.po.pollTimeout";
128 protected static final String createPollIntervalDefault = "15";
129 private static final String deletePollIntervalDefault = "15";
131 private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
134 * keep this old method signature here to maintain backwards compatibility. keep others as well.
135 * this method does not include environment, files, or heatFiles
137 public StackInfo createStack (String cloudSiteId,
141 Map <String, ?> stackInputs,
142 boolean pollForCompletion,
143 int timeoutMinutes) throws MsoException {
144 // Just call the new method with the environment & files variable set to null
145 return this.createStack (cloudSiteId,
158 // This method has environment, but not files or heatFiles
159 public StackInfo createStack (String cloudSiteId,
163 Map <String, ?> stackInputs,
164 boolean pollForCompletion,
166 String environment) throws MsoException {
167 // Just call the new method with the files/heatFiles variables set to null
168 return this.createStack (cloudSiteId,
181 // This method has environment and files, but not heatFiles.
182 public StackInfo createStack (String cloudSiteId,
186 Map <String, ?> stackInputs,
187 boolean pollForCompletion,
190 Map <String, Object> files) throws MsoException {
191 return this.createStack (cloudSiteId,
204 // This method has environment, files, heatfiles
205 public StackInfo createStack (String cloudSiteId,
209 Map <String, ?> stackInputs,
210 boolean pollForCompletion,
213 Map <String, Object> files,
214 Map <String, Object> heatFiles) throws MsoException {
215 return this.createStack (cloudSiteId,
229 * Create a new Stack in the specified cloud location and tenant. The Heat template
230 * and parameter map are passed in as arguments, along with the cloud access credentials.
231 * It is expected that parameters have been validated and contain at minimum the required
232 * parameters for the given template with no extra (undefined) parameters..
234 * The Stack name supplied by the caller must be unique in the scope of this tenant.
235 * However, it should also be globally unique, as it will be the identifier for the
236 * resource going forward in Inventory. This latter is managed by the higher levels
237 * invoking this function.
239 * The caller may choose to let this function poll Openstack for completion of the
240 * stack creation, or may handle polling itself via separate calls to query the status.
241 * In either case, a StackInfo object will be returned containing the current status.
242 * When polling is enabled, a status of CREATED is expected. When not polling, a
243 * status of BUILDING is expected.
245 * An error will be thrown if the requested Stack already exists in the specified
248 * For 1510 - add "environment", "files" (nested templates), and "heatFiles" (get_files) as
249 * parameters for createStack. If environment is non-null, it will be added to the stack.
250 * The nested templates and get_file entries both end up being added to the "files" on the
251 * stack. We must combine them before we add them to the stack if they're both non-null.
253 * @param cloudSiteId The cloud (may be a region) in which to create the stack.
254 * @param tenantId The Openstack ID of the tenant in which to create the Stack
255 * @param stackName The name of the stack to create
256 * @param heatTemplate The Heat template
257 * @param stackInputs A map of key/value inputs
258 * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
259 * @param environment An optional yaml-format string to specify environmental parameters
260 * @param files a Map<String, Object> that lists the child template IDs (file is the string, object is an int of
262 * @param heatFiles a Map<String, Object> that lists the get_file entries (fileName, fileBody)
263 * @param backout Donot delete stack on create Failure - defaulted to True
264 * @return A StackInfo object
265 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
268 @SuppressWarnings("unchecked")
269 public StackInfo createStack (String cloudSiteId,
273 Map <String, ?> stackInputs,
274 boolean pollForCompletion,
277 Map <String, Object> files,
278 Map <String, Object> heatFiles,
279 boolean backout) throws MsoException {
281 // Take out the multicloud inputs, if present.
282 for (String key : MsoMulticloudUtils.MULTICLOUD_INPUTS) {
283 if (stackInputs.containsKey(key)) {
284 stackInputs.remove(key);
285 if (stackInputs.isEmpty()) {
291 CreateStackParam stack = createStackParam(stackName, heatTemplate, stackInputs, timeoutMinutes, environment, files, heatFiles);
293 // Obtain the cloud site information where we will create the stack
294 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
295 () -> new MsoCloudSiteNotFound(cloudSiteId));
296 logger.debug("Found: {}", cloudSite);
297 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
298 // This could throw MsoTenantNotFound or MsoOpenstackException (both propagated)
299 Heat heatClient = getHeatClient (cloudSite, tenantId);
300 logger.debug("Found: {}", heatClient);
302 logger.debug ("Ready to Create Stack ({}) with input params: {}", heatTemplate, stackInputs);
304 Stack heatStack = null;
306 OpenStackRequest <Stack> request = heatClient.getStacks ().create (stack);
307 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
308 request.header ("X-Auth-User", cloudIdentity.getMsoId ());
309 request.header ("X-Auth-Key", CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass ()));
310 heatStack = executeAndRecordOpenstackRequest (request);
311 } catch (OpenStackResponseException e) {
312 if (e.getStatus () == 409) {
313 MsoStackAlreadyExists me = new MsoStackAlreadyExists (stackName, tenantId, cloudSiteId);
314 me.addContext (CREATE_STACK);
317 logger.debug("ERROR STATUS = {},\n{}\n{}", e.getStatus(), e.getMessage(), e.getLocalizedMessage());
318 throw heatExceptionToMsoException (e, CREATE_STACK);
320 } catch (OpenStackConnectException e) {
321 throw heatExceptionToMsoException (e, CREATE_STACK);
322 } catch (RuntimeException e) {
323 throw runtimeExceptionToMsoException (e, CREATE_STACK);
326 // Subsequent access by the canonical name "<stack name>/<stack-id>".
327 // Otherwise, simple query by name returns a 302 redirect.
328 // NOTE: This is specific to the v1 Orchestration API.
329 String canonicalName = stackName + "/" + heatStack.getId ();
331 if (pollForCompletion) {
332 heatStack = pollStackForCompletion(cloudSiteId, tenantId, stackName, timeoutMinutes, backout, heatClient,
333 heatStack, canonicalName);
335 // Get initial status, since it will have been null after the create.
336 heatStack = queryHeatStack (heatClient, canonicalName);
337 logger.debug (heatStack.getStackStatus ());
339 return new StackInfoMapper(heatStack).map();
342 private Stack pollStackForCompletion(String cloudSiteId, String tenantId, String stackName, int timeoutMinutes,
343 boolean backout, Heat heatClient, Stack heatStack, String canonicalName)
344 throws MsoException, MsoOpenstackException {
345 int createPollInterval = Integer.parseInt(this.environment.getProperty(createPollIntervalProp, createPollIntervalDefault));
346 int pollTimeout = (timeoutMinutes * 60) + createPollInterval;
347 int deletePollInterval = createPollInterval;
348 int deletePollTimeout = pollTimeout;
349 boolean createTimedOut = false;
350 StringBuilder stackErrorStatusReason = new StringBuilder("");
351 logger.debug("createPollInterval={}, pollTimeout={}", createPollInterval, pollTimeout);
355 heatStack = queryHeatStack (heatClient, canonicalName);
356 logger.debug("{} ({})", heatStack.getStackStatus(), canonicalName);
358 logger.debug("Current stack {}", this.getOutputsAsStringBuilder(heatStack).toString());
359 } catch (Exception e) {
360 logger.debug("an error occurred trying to print out the current outputs of the stack", e);
363 if ("CREATE_IN_PROGRESS".equals (heatStack.getStackStatus ())) {
364 if (pollTimeout <= 0) {
365 logger.error("{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Create stack timeout",
366 MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
367 heatStack.getStackStatus(), ErrorCode.AvailabilityError.getValue());
368 createTimedOut = true;
371 sleep(createPollInterval * 1000L);
372 pollTimeout -= createPollInterval;
373 logger.debug("pollTimeout remaining: {}", pollTimeout);
375 stackErrorStatusReason.append("Stack error (" + heatStack.getStackStatus() + "): " + heatStack.getStackStatusReason());
378 } catch (MsoException me) {
379 // Cannot query the stack status. Something is wrong.
380 // Try to roll back the stack
383 logger.warn("{} Exception in Create Stack, stack deletion suppressed {}",
384 MessageEnum.RA_CREATE_STACK_ERR, ErrorCode.BusinessProcesssError.getValue());
390 "Create Stack error - unable to query for stack status - attempting to delete stack: {}"
391 + " - This will likely fail and/or we won't be able to query to see if delete worked",
393 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
394 executeAndRecordOpenstackRequest (request);
395 boolean deleted = false;
398 heatStack = queryHeatStack(heatClient, canonicalName);
399 if (heatStack != null) {
400 logger.debug(heatStack.getStackStatus());
401 if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
402 if (deletePollTimeout <= 0) {
404 "{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Rollback: DELETE stack timeout",
405 MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
406 heatStack.getStackStatus(), ErrorCode.AvailabilityError.getValue());
409 sleep(deletePollInterval * 1000L);
410 deletePollTimeout -= deletePollInterval;
412 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
413 logger.debug("DELETE_COMPLETE for {}", canonicalName);
417 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
421 // assume if we can't find it - it's deleted
422 logger.debug("heatStack returned null - assume the stack {} has been deleted", canonicalName);
427 } catch (Exception e3) {
428 // Just log this one. We will report the original exception.
429 logger.error("{} Create Stack: Nested exception rolling back stack: {} ",
430 MessageEnum.RA_CREATE_STACK_ERR, ErrorCode.BusinessProcesssError.getValue(), e3);
433 } catch (Exception e2) {
434 // Just log this one. We will report the original exception.
435 logger.error("{} Create Stack: Nested exception rolling back stack: {} ",
436 MessageEnum.RA_CREATE_STACK_ERR, ErrorCode.BusinessProcesssError.getValue(), e2);
440 // Propagate the original exception from Stack Query.
441 me.addContext (CREATE_STACK);
446 if (!"CREATE_COMPLETE".equals (heatStack.getStackStatus ())) {
447 logger.error("{} Create Stack error: Polling complete with non-success status: {}, {} {} ",
448 MessageEnum.RA_CREATE_STACK_ERR, heatStack.getStackStatus(), heatStack.getStackStatusReason(),
449 ErrorCode.BusinessProcesssError.getValue());
451 // Rollback the stack creation, since it is in an indeterminate state.
455 "{} Create Stack errored, stack deletion suppressed {} Create Stack error, stack deletion suppressed",
456 MessageEnum.RA_CREATE_STACK_ERR, ErrorCode.BusinessProcesssError.getValue());
461 logger.debug("Create Stack errored - attempting to DELETE stack: {}", canonicalName);
462 logger.debug("deletePollInterval={}, deletePollTimeout={}", deletePollInterval, deletePollTimeout);
463 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
464 executeAndRecordOpenstackRequest (request);
465 boolean deleted = false;
468 heatStack = queryHeatStack(heatClient, canonicalName);
469 if (heatStack != null) {
470 logger.debug("{} ({})", heatStack.getStackStatus(), canonicalName);
471 if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
472 if (deletePollTimeout <= 0) {
474 "{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Rollback: DELETE stack timeout",
475 MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
476 heatStack.getStackStatus(), ErrorCode.AvailabilityError.getValue());
479 sleep(deletePollInterval * 1000L);
480 deletePollTimeout -= deletePollInterval;
481 logger.debug("deletePollTimeout remaining: {}", deletePollTimeout);
483 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
484 logger.debug("DELETE_COMPLETE for {}", canonicalName);
487 } else if ("DELETE_FAILED".equals(heatStack.getStackStatus())) {
488 // Warn about this (?) - but still throw the original exception
490 "{} Create Stack errored, stack deletion FAILED {} Create Stack error, stack deletion FAILED",
491 MessageEnum.RA_CREATE_STACK_ERR, ErrorCode.BusinessProcesssError.getValue());
492 logger.debug("Stack deletion FAILED on a rollback of a create - {}, status={}, reason={}",
493 canonicalName, heatStack.getStackStatus(), heatStack.getStackStatusReason());
496 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
500 // assume if we can't find it - it's deleted
501 logger.debug("heatStack returned null - assume the stack {} has been deleted", canonicalName);
506 } catch (MsoException me2) {
507 // We got an exception on the delete - don't throw this exception - throw the original - just log.
508 logger.debug("Exception thrown trying to delete {} on a create->rollback: {} ", canonicalName,
509 me2.getContextMessage(), me2);
510 logger.warn("{} Create Stack errored, then stack deletion FAILED - exception thrown {} {}",
511 MessageEnum.RA_CREATE_STACK_ERR, ErrorCode.BusinessProcesssError.getValue(),
512 me2.getContextMessage());
515 } // end while !deleted
516 StringBuilder errorContextMessage;
517 if (createTimedOut) {
518 errorContextMessage = new StringBuilder("Stack Creation Timeout");
520 errorContextMessage = stackErrorStatusReason;
523 errorContextMessage.append(" - stack successfully deleted");
525 errorContextMessage.append(" - encountered an error trying to delete the stack");
527 } catch (Exception e2) {
528 // shouldn't happen - but handle
529 logger.error("{} Create Stack: Nested exception rolling back stack: {} ", MessageEnum.RA_CREATE_STACK_ERR,
530 ErrorCode.BusinessProcesssError.getValue(), e2);
533 MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
534 me.addContext(CREATE_STACK);
541 * Query for a single stack (by Name) in a tenant. This call will always return a
542 * StackInfo object. If the stack does not exist, an "empty" StackInfo will be
543 * returned - containing only the stack name and a status of NOTFOUND.
545 * @param tenantId The Openstack ID of the tenant in which to query
546 * @param cloudSiteId The cloud identifier (may be a region) in which to query
547 * @param stackName The name of the stack to query (may be simple or canonical)
548 * @return A StackInfo object
549 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
551 public StackInfo queryStack (String cloudSiteId, String tenantId, String stackName) throws MsoException {
552 logger.debug ("Query HEAT stack: {} in tenant {}", stackName, tenantId);
554 // Obtain the cloud site information where we will create the stack
555 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
556 () -> new MsoCloudSiteNotFound(cloudSiteId));
557 logger.debug("Found: {}", cloudSite.toString());
559 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
560 Heat heatClient = null;
562 heatClient = getHeatClient (cloudSite, tenantId);
563 if (heatClient != null) {
564 logger.debug("Found: {}", heatClient.toString());
566 } catch (MsoTenantNotFound e) {
567 // Tenant doesn't exist, so stack doesn't either
568 logger.debug ("Tenant with id " + tenantId + "not found.", e);
569 return new StackInfo (stackName, HeatStatus.NOTFOUND);
570 } catch (MsoException me) {
571 // Got an Openstack error. Propagate it
572 logger.error("{} {} Openstack Exception on Token request: ", MessageEnum.RA_CONNECTION_EXCEPTION,
573 ErrorCode.AvailabilityError.getValue(), me);
574 me.addContext ("QueryStack");
579 // An MsoException will propagate transparently to the caller.
580 Stack heatStack = queryHeatStack (heatClient, stackName);
582 if (heatStack == null) {
583 // Stack does not exist. Return a StackInfo with status NOTFOUND
584 return new StackInfo (stackName, HeatStatus.NOTFOUND);
587 return new StackInfoMapper(heatStack).map();
591 * Delete a stack (by Name/ID) in a tenant. If the stack is not found, it will be
592 * considered a successful deletion. The return value is a StackInfo object which
593 * contains the current stack status.
595 * The client may choose to let the adapter poll Openstack for completion of the
596 * stack deletion, or may handle polling itself via separate query calls. In either
597 * case, a StackInfo object will be returned. When polling is enabled, a final
598 * status of NOTFOUND is expected. When not polling, a status of DELETING is expected.
600 * There is no rollback from a successful stack deletion. A deletion failure will
601 * also result in an undefined stack state - the components may or may not have been
602 * all or partially deleted, so the resulting stack must be considered invalid.
604 * @param tenantId The Openstack ID of the tenant in which to perform the delete
605 * @param cloudSiteId The cloud identifier (may be a region) from which to delete the stack.
606 * @param stackName The name/id of the stack to delete. May be simple or canonical
607 * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
608 * @return A StackInfo object
609 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
610 * @throws MsoCloudSiteNotFound
612 public StackInfo deleteStack (String tenantId,
615 boolean pollForCompletion) throws MsoException {
616 // Obtain the cloud site information where we will create the stack
617 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
618 () -> new MsoCloudSiteNotFound(cloudSiteId));
619 logger.debug("Found: {}", cloudSite.toString());
621 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
622 Heat heatClient = null;
624 heatClient = getHeatClient (cloudSite, tenantId);
625 if (heatClient != null) {
626 logger.debug("Found: {}", heatClient.toString());
628 } catch (MsoTenantNotFound e) {
629 // Tenant doesn't exist, so stack doesn't either
630 logger.debug ("Tenant with id " + tenantId + "not found.", e);
631 return new StackInfo (stackName, HeatStatus.NOTFOUND);
632 } catch (MsoException me) {
633 // Got an Openstack error. Propagate it
634 logger.error("{} {} Openstack Exception on Token request: ", MessageEnum.RA_CONNECTION_EXCEPTION,
635 ErrorCode.AvailabilityError.getValue(), me);
636 me.addContext (DELETE_STACK);
640 // OK if stack not found, perform a query first
641 Stack heatStack = queryHeatStack (heatClient, stackName);
642 if (heatStack == null || "DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
643 // Not found. Return a StackInfo with status NOTFOUND
644 return new StackInfo (stackName, HeatStatus.NOTFOUND);
649 // Use canonical name "<stack name>/<stack-id>" to delete.
650 // Otherwise, deletion by name returns a 302 redirect.
651 // NOTE: This is specific to the v1 Orchestration API.
652 String canonicalName = heatStack.getStackName () + "/" + heatStack.getId ();
655 OpenStackRequest <Void> request = null;
656 if(null != heatClient) {
657 request = heatClient.getStacks ().deleteByName (canonicalName);
660 logger.debug ("Heat Client is NULL" );
663 executeAndRecordOpenstackRequest (request);
664 } catch (OpenStackResponseException e) {
665 if (e.getStatus () == 404) {
666 // Not found. We are OK with this. Return a StackInfo with status NOTFOUND
667 return new StackInfo (stackName, HeatStatus.NOTFOUND);
669 // Convert the OpenStackResponseException to an MsoOpenstackException
670 throw heatExceptionToMsoException (e, DELETE_STACK);
672 } catch (OpenStackConnectException e) {
673 // Error connecting to Openstack instance. Convert to an MsoException
674 throw heatExceptionToMsoException (e, DELETE_STACK);
675 } catch (RuntimeException e) {
677 throw runtimeExceptionToMsoException (e, DELETE_STACK);
680 // Requery the stack for current status.
681 // It will probably still exist with "DELETE_IN_PROGRESS" status.
682 heatStack = queryHeatStack (heatClient, canonicalName);
684 if (pollForCompletion) {
685 // Set a timeout on polling
687 int pollInterval = Integer.parseInt(this.environment.getProperty(deletePollIntervalProp, "" + deletePollIntervalDefault));
688 int pollTimeout = Integer.parseInt(this.environment.getProperty(deletePollTimeoutProp, "" + deletePollIntervalDefault));
690 // When querying by canonical name, Openstack returns DELETE_COMPLETE status
691 // instead of "404" (which would result from query by stack name).
692 while (heatStack != null && !"DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
693 logger.debug ("Stack status: {}", heatStack.getStackStatus ());
695 if ("DELETE_FAILED".equals (heatStack.getStackStatus ())) {
696 // Throw a 'special case' of MsoOpenstackException to report the Heat status
697 String error = "Stack delete error (" + heatStack.getStackStatus ()
699 + heatStack.getStackStatusReason ();
700 MsoOpenstackException me = new MsoOpenstackException (0, "", error);
701 me.addContext (DELETE_STACK);
703 // Alarm this condition, stack deletion failed
709 if (pollTimeout <= 0) {
710 logger.error("{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Delete Stack Timeout",
711 MessageEnum.RA_DELETE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
712 heatStack.getStackStatus(), ErrorCode.AvailabilityError.getValue());
714 // Throw a 'special case' of MsoOpenstackException to report the Heat status
715 MsoOpenstackException me = new MsoOpenstackException (0, "", "Stack Deletion Timeout");
716 me.addContext (DELETE_STACK);
718 // Alarm this condition, stack deletion failed
724 sleep(pollInterval * 1000L);
726 pollTimeout -= pollInterval;
727 logger.debug("pollTimeout remaining: {}", pollTimeout);
729 heatStack = queryHeatStack (heatClient, canonicalName);
732 // The stack is gone when this point is reached
733 return new StackInfo (stackName, HeatStatus.NOTFOUND);
736 // Return the current status (if not polling, the delete may still be in progress)
737 StackInfo stackInfo = new StackInfoMapper(heatStack).map();
738 stackInfo.setName (stackName);
744 * Query for all stacks in a tenant site. This call will return a List of StackInfo
745 * objects, one for each deployed stack.
747 * Note that this is limited to a single site. To ensure that a tenant is truly
748 * empty would require looping across all tenant endpoints.
750 * @param tenantId The Openstack ID of the tenant to query
751 * @param cloudSiteId The cloud identifier (may be a region) in which to query.
752 * @return A List of StackInfo objects
753 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
754 * @throws MsoCloudSiteNotFound
756 public List <StackInfo> queryAllStacks (String tenantId, String cloudSiteId) throws MsoException {
757 // Obtain the cloud site information where we will create the stack
758 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
759 () -> new MsoCloudSiteNotFound(cloudSiteId));
760 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
761 Heat heatClient = getHeatClient (cloudSite, tenantId);
764 OpenStackRequest <Stacks> request = heatClient.getStacks ().list ();
765 Stacks stacks = executeAndRecordOpenstackRequest (request);
767 List <StackInfo> stackList = new ArrayList <> ();
769 // Not sure if returns an empty list or null if no stacks exist
770 if (stacks != null) {
771 for (Stack stack : stacks) {
772 stackList.add (new StackInfoMapper(stack).map());
777 } catch (OpenStackResponseException e) {
778 if (e.getStatus () == 404) {
779 // Not sure if this can happen, but return an empty list
780 logger.debug ("queryAllStacks - stack not found: ");
781 return new ArrayList <> ();
783 // Convert the OpenStackResponseException to an MsoOpenstackException
784 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
786 } catch (OpenStackConnectException e) {
787 // Error connecting to Openstack instance. Convert to an MsoException
788 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
789 } catch (RuntimeException e) {
791 throw runtimeExceptionToMsoException (e, QUERY_ALL_STACKS);
796 * Validate parameters to be passed to Heat template. This method performs
798 * 1. Apply default values to parameters which have them defined
799 * 2. Report any required parameters that are missing. This will generate an
800 * exception in the caller, since stack create/update operations would fail.
801 * 3. Report and remove any extraneous parameters. This will allow clients to
802 * pass supersets of parameters and not get errors.
804 * These functions depend on the HeatTemplate definition from the MSO Catalog DB,
805 * along with the input parameter Map. The output is an updated parameter map.
806 * If the parameters are invalid for the template, an IllegalArgumentException
809 public Map <String, Object> validateStackParams (Map <String, Object> inputParams,
810 HeatTemplate heatTemplate) {
811 // Check that required parameters have been supplied for this template type
812 StringBuilder missingParams = null;
813 List <String> paramList = new ArrayList <> ();
815 // TODO: Enhance DB to support defaults for Heat Template parameters
817 for (HeatTemplateParam parm : heatTemplate.getParameters ()) {
818 if (parm.isRequired () && !inputParams.containsKey (parm.getParamName ())) {
819 if (missingParams == null) {
820 missingParams = new StringBuilder(parm.getParamName());
822 missingParams.append("," + parm.getParamName());
825 paramList.add (parm.getParamName ());
827 if (missingParams != null) {
828 // Problem - missing one or more required parameters
829 String error = "Missing Required inputs for HEAT Template: " + missingParams;
830 logger.error("{} for HEAT Template {} Missing Required inputs for HEAT Template: {}",
831 MessageEnum.RA_MISSING_PARAM, ErrorCode.SchemaError.getValue(), missingParams);
832 throw new IllegalArgumentException (error);
835 // Remove any extraneous parameters (don't throw an error)
836 Map <String, Object> updatedParams = new HashMap <> ();
837 List <String> extraParams = new ArrayList <> ();
839 for (Entry<String, Object> entry : inputParams.entrySet()) {
840 if (!paramList.contains(entry.getKey())) {
841 // This is not a valid parameter for this template
842 extraParams.add(entry.getKey());
844 updatedParams.put(entry.getKey(), entry.getValue());
848 if (!extraParams.isEmpty ()) {
849 logger.warn("{} Heat Stack ({}) extra input params received: {} {}", MessageEnum.RA_GENERAL_WARNING,
850 heatTemplate.getTemplateName(), extraParams, ErrorCode.DataError.getValue());
853 return updatedParams;
856 // ---------------------------------------------------------------
857 // PRIVATE FUNCTIONS FOR USE WITHIN THIS CLASS
860 * Get a Heat client for the Openstack Identity service.
861 * This requires a 'member'-level userId + password, which will be retrieved from
862 * properties based on the specified cloud Id. The tenant in which to operate
863 * must also be provided.
865 * On successful authentication, the Heat object will be cached for the
866 * tenantID + cloudId so that it can be reused without reauthenticating with
867 * Openstack every time.
869 * @return an authenticated Heat object
871 public Heat getHeatClient (CloudSite cloudSite, String tenantId) throws MsoException {
872 String cloudId = cloudSite.getId();
873 // For DCP/LCP, the region should be the cloudId.
874 String region = cloudSite.getRegionId ();
876 // Obtain an MSO token for the tenant
877 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
878 logger.debug("Found: {}", cloudIdentity.toString());
879 MsoTenantUtils tenantUtils = tenantUtilsFactory.getTenantUtilsByServerType(cloudIdentity.getIdentityServerType());
880 String keystoneUrl = tenantUtils.getKeystoneUrl(cloudId, cloudIdentity);
881 logger.debug("keystoneUrl={}", keystoneUrl);
882 String heatUrl = null;
883 String tokenId = null;
884 Calendar expiration = null;
886 if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
887 Keystone keystoneTenantClient = new Keystone (keystoneUrl);
888 Access access = null;
890 Authentication credentials = authenticationMethodFactory.getAuthenticationFor(cloudIdentity);
892 OpenStackRequest <Access> request = keystoneTenantClient.tokens ()
893 .authenticate (credentials).withTenantId (tenantId);
895 access = executeAndRecordOpenstackRequest (request);
898 // Isolate trying to printout the region IDs
900 logger.debug("access={}", access.toString());
901 for (Access.Service service : access.getServiceCatalog()) {
902 List<Access.Service.Endpoint> endpoints = service.getEndpoints();
903 for (Access.Service.Endpoint endpoint : endpoints) {
904 logger.debug("AIC returned region={}", endpoint.getRegion());
907 } catch (Exception e) {
908 logger.debug("Encountered an error trying to printout Access object returned from AIC. {}",
911 heatUrl = KeystoneUtils.findEndpointURL (access.getServiceCatalog (), "orchestration", region, "public");
912 logger.debug("heatUrl={}, region={}", heatUrl, region);
913 } catch (RuntimeException e) {
914 // This comes back for not found (probably an incorrect region ID)
915 String error = "AIC did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
916 throw new MsoAdapterException (error, e);
918 tokenId = access.getToken ().getId ();
919 expiration = access.getToken ().getExpires ();
920 } else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
922 KeystoneAuthHolder holder = keystoneV3Authentication.getToken(cloudSite, tenantId, "orchestration");
923 tokenId = holder.getId();
924 expiration = holder.getexpiration();
925 heatUrl = holder.getServiceUrl();
926 } catch (ServiceEndpointNotFoundException e) {
927 // This comes back for not found (probably an incorrect region ID)
928 String error = "cloud did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
929 throw new MsoAdapterException (error, e);
932 } catch (OpenStackResponseException e) {
933 if (e.getStatus () == 401) {
934 // Authentication error.
935 String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId ();
937 throw new MsoAdapterException (error);
939 throw keystoneErrorToMsoException (e, TOKEN_AUTH);
941 } catch (OpenStackConnectException e) {
942 // Connection to Openstack failed
943 MsoIOException me = new MsoIOException (e.getMessage (), e);
944 me.addContext (TOKEN_AUTH);
946 } catch (RuntimeException e) {
948 throw runtimeExceptionToMsoException (e, TOKEN_AUTH);
950 Heat heatClient = new Heat (heatUrl);
951 heatClient.token (tokenId);
956 * Query for a Heat Stack. This function is needed in several places, so
957 * a common method is useful. This method takes an authenticated Heat Client
958 * (which internally identifies the cloud & tenant to search), and returns
959 * a Stack object if found, Null if not found, or an MsoOpenstackException
960 * if the Openstack API call fails.
962 * The stack name may be a simple name or a canonical name ("{name}/{id}").
963 * When simple name is used, Openstack always returns a 302 redirect which
964 * results in a 2nd request (to the canonical name). Note that query by
965 * canonical name for a deleted stack returns a Stack object with status
966 * "DELETE_COMPLETE" while query by simple name for a deleted stack returns
969 * @param heatClient an authenticated Heat client
971 * @param stackName the stack name to query
973 * @return a Stack object that describes the current stack or null if the
974 * requested stack doesn't exist.
976 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
978 protected Stack queryHeatStack (Heat heatClient, String stackName) throws MsoException {
979 if (stackName == null) {
983 OpenStackRequest <Stack> request = heatClient.getStacks ().byName (stackName);
984 return executeAndRecordOpenstackRequest (request);
985 } catch (OpenStackResponseException e) {
986 logger.error("Error in Query Stack", e);
987 if (e.getStatus () == 404) {
988 logger.debug ("queryHeatStack - stack not found: {}", stackName);
991 // Convert the OpenStackResponseException to an MsoOpenstackException
992 throw heatExceptionToMsoException (e, "QueryStack");
994 } catch (OpenStackConnectException e) {
995 // Connection to Openstack failed
996 throw heatExceptionToMsoException (e, "QueryAllStack");
1001 public Map<String, Object> queryStackForOutputs(String cloudSiteId,
1002 String tenantId, String stackName) throws MsoException {
1003 logger.debug("MsoHeatUtils.queryStackForOutputs)");
1004 StackInfo heatStack = this.queryStack(cloudSiteId, tenantId, stackName);
1005 if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
1008 return heatStack.getOutputs();
1011 public void copyStringOutputsToInputs(Map<String, Object> inputs,
1012 Map<String, Object> otherStackOutputs, boolean overWrite) {
1013 if (inputs == null || otherStackOutputs == null)
1015 for (String key : otherStackOutputs.keySet()) {
1016 if (!inputs.containsKey(key)) {
1017 Object obj = otherStackOutputs.get(key);
1018 if (obj instanceof String) {
1019 inputs.put(key, (String) otherStackOutputs.get(key));
1020 } else if (obj instanceof JsonNode ){
1021 // This is a bit of mess - but I think it's the least impacting
1022 // let's convert it BACK to a string - then it will get converted back later
1024 String str = this.convertNode((JsonNode) obj);
1025 inputs.put(key, str);
1026 } catch (Exception e) {
1027 logger.debug("DANGER WILL ROBINSON: unable to convert value for JsonNode {} ", key, e);
1028 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1030 } else if (obj instanceof java.util.LinkedHashMap) {
1031 logger.debug("LinkedHashMap - this is showing up as a LinkedHashMap instead of JsonNode");
1033 String str = JSON_MAPPER.writeValueAsString(obj);
1034 inputs.put(key, str);
1035 } catch (Exception e) {
1036 logger.debug("DANGER WILL ROBINSON: unable to convert value for LinkedHashMap {} ", key, e);
1038 } else if (obj instanceof Integer) {
1040 String str = "" + obj;
1041 inputs.put(key, str);
1042 } catch (Exception e) {
1043 logger.debug("DANGER WILL ROBINSON: unable to convert value for Integer {} ", key, e);
1047 String str = obj.toString();
1048 inputs.put(key, str);
1049 } catch (Exception e) {
1050 logger.debug("DANGER WILL ROBINSON: unable to convert value for Other {} ({}) ", key, e.getMessage(), e);
1051 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1058 public StringBuilder requestToStringBuilder(CreateStackParam stack) {
1059 StringBuilder sb = new StringBuilder();
1060 sb.append("Stack:\n");
1061 sb.append("\tStackName: " + stack.getStackName());
1062 sb.append("\tTemplateUrl: " + stack.getTemplateUrl());
1063 sb.append("\tTemplate: " + stack.getTemplate());
1064 sb.append("\tEnvironment: " + stack.getEnvironment());
1065 sb.append("\tTimeout: " + stack.getTimeoutMinutes());
1066 sb.append("\tParameters:\n");
1067 Map<String, Object> params = stack.getParameters();
1068 if (params == null || params.size() < 1) {
1069 sb.append("\nNONE");
1071 for (String key : params.keySet()) {
1072 if (params.get(key) instanceof String) {
1073 sb.append("\n").append(key).append("=").append((String) params.get(key));
1074 } else if (params.get(key) instanceof JsonNode) {
1075 String jsonStringOut = this.convertNode((JsonNode)params.get(key));
1076 sb.append("\n").append(key).append("=").append(jsonStringOut);
1077 } else if (params.get(key) instanceof Integer) {
1078 String integerOut = "" + params.get(key);
1079 sb.append("\n").append(key).append("=").append(integerOut);
1083 String str = params.get(key).toString();
1084 sb.append("\n").append(key).append("=").append(str);
1085 } catch (Exception e) {
1086 logger.debug("Exception :", e);
1094 private String convertNode(final JsonNode node) {
1096 final Object obj = JSON_MAPPER.treeToValue(node, Object.class);
1097 final String json = JSON_MAPPER.writeValueAsString(obj);
1099 } catch (Exception e) {
1100 logger.debug("Error converting json to string {} ", e.getMessage(), e);
1102 return "[Error converting json to string]";
1106 protected StringBuilder getOutputsAsStringBuilder(Stack heatStack) {
1107 // This should only be used as a utility to print out the stack outputs
1109 StringBuilder sb = new StringBuilder("");
1110 if (heatStack == null) {
1111 sb.append("(heatStack is null)");
1114 List<Output> outputList = heatStack.getOutputs();
1115 if (outputList == null || outputList.isEmpty()) {
1116 sb.append("(outputs is empty)");
1119 Map<String, Object> outputs = new HashMap<>();
1120 for (Output outputItem : outputList) {
1121 outputs.put(outputItem.getOutputKey(), outputItem.getOutputValue());
1124 sb.append("OUTPUTS:\n");
1125 for (String key : outputs.keySet()) {
1126 sb.append("outputs[").append(counter++).append("]: ").append(key).append("=");
1127 Object obj = outputs.get(key);
1128 if (obj instanceof String) {
1129 sb.append((String) obj).append(" (a string)");
1130 } else if (obj instanceof JsonNode) {
1131 sb.append(this.convertNode((JsonNode) obj)).append(" (a JsonNode)");
1132 } else if (obj instanceof java.util.LinkedHashMap) {
1134 String str = JSON_MAPPER.writeValueAsString(obj);
1135 sb.append(str).append(" (a java.util.LinkedHashMap)");
1136 } catch (Exception e) {
1137 logger.debug("Exception :", e);
1138 sb.append("(a LinkedHashMap value that would not convert nicely)");
1140 } else if (obj instanceof Integer) {
1143 str = obj.toString() + " (an Integer)\n";
1144 } catch (Exception e) {
1145 logger.debug("Exception :", e);
1146 str = "(an Integer unable to call .toString() on)";
1149 } else if (obj instanceof ArrayList) {
1152 str = obj.toString() + " (an ArrayList)";
1153 } catch (Exception e) {
1154 logger.debug("Exception :", e);
1155 str = "(an ArrayList unable to call .toString() on?)";
1158 } else if (obj instanceof Boolean) {
1161 str = obj.toString() + " (a Boolean)";
1162 } catch (Exception e) {
1163 logger.debug("Exception :", e);
1164 str = "(an Boolean unable to call .toString() on?)";
1171 str = obj.toString() + " (unknown Object type)";
1172 } catch (Exception e) {
1173 logger.debug("Exception :", e);
1174 str = "(a value unable to call .toString() on?)";
1185 public void copyBaseOutputsToInputs(Map<String, Object> inputs,
1186 Map<String, Object> otherStackOutputs, List<String> paramNames, Map<String, String> aliases) {
1187 if (inputs == null || otherStackOutputs == null)
1189 for (String key : otherStackOutputs.keySet()) {
1190 if (paramNames != null) {
1191 if (!paramNames.contains(key) && !aliases.containsKey(key)) {
1192 logger.debug("\tParameter {} is NOT defined to be in the template - do not copy to inputs", key);
1195 if (aliases.containsKey(key)) {
1196 logger.debug("Found an alias! Will move {} to {}", key, aliases.get(key));
1197 Object obj = otherStackOutputs.get(key);
1198 key = aliases.get(key);
1199 otherStackOutputs.put(key, obj);
1202 if (!inputs.containsKey(key)) {
1203 Object obj = otherStackOutputs.get(key);
1204 logger.debug("\t**Adding {} to inputs (.toString()={}", key, obj.toString());
1205 if (obj instanceof String) {
1206 logger.debug("\t\t**A String");
1207 inputs.put(key, obj);
1208 } else if (obj instanceof Integer) {
1209 logger.debug("\t\t**An Integer");
1210 inputs.put(key, obj);
1211 } else if (obj instanceof JsonNode) {
1212 logger.debug("\t\t**A JsonNode");
1213 inputs.put(key, obj);
1214 } else if (obj instanceof Boolean) {
1215 logger.debug("\t\t**A Boolean");
1216 inputs.put(key, obj);
1217 } else if (obj instanceof java.util.LinkedHashMap) {
1218 logger.debug("\t\t**A java.util.LinkedHashMap **");
1219 inputs.put(key, obj);
1220 } else if (obj instanceof java.util.ArrayList) {
1221 logger.debug("\t\t**An ArrayList");
1222 inputs.put(key, obj);
1224 logger.debug("\t\t**UNKNOWN OBJECT TYPE");
1225 inputs.put(key, obj);
1228 logger.debug("key={} is already in the inputs - will not overwrite", key);
1234 public List<String> convertCdlToArrayList(String cdl) {
1235 String cdl2 = cdl.trim();
1237 if (cdl2.startsWith("[") && cdl2.endsWith("]")) {
1238 cdl3 = cdl2.substring(1, cdl2.lastIndexOf("]"));
1242 return new ArrayList<>(Arrays.asList(cdl3.split(",")));
1246 * New with 1707 - this method will convert all the String *values* of the inputs
1247 * to their "actual" object type (based on the param type: in the db - which comes from the template):
1248 * (heat variable type) -> java Object type
1251 * json -> marshal object to json
1252 * comma_delimited_list -> ArrayList
1253 * boolean -> Boolean
1254 * if any of the conversions should fail, we will default to adding it to the inputs
1255 * as a string - see if Openstack can handle it.
1256 * Also, will remove any params that are extra.
1257 * Any aliases will be converted to their appropriate name (anyone use this feature?)
1258 * @param inputs - the Map<String, String> of the inputs received on the request
1259 * @param template the HeatTemplate object - this is so we can also verify if the param is valid for this template
1260 * @return HashMap<String, Object> of the inputs, cleaned and converted
1262 public Map<String, Object> convertInputMap(Map<String, Object> inputs, HeatTemplate template) {
1263 HashMap<String, Object> newInputs = new HashMap<>();
1264 HashMap<String, HeatTemplateParam> params = new HashMap<>();
1265 HashMap<String, HeatTemplateParam> paramAliases = new HashMap<>();
1267 if (inputs == null) {
1268 logger.debug("convertInputMap - inputs is null - nothing to do here");
1269 return new HashMap<>();
1272 logger.debug("convertInputMap in MsoHeatUtils called, with {} inputs, and template {}", inputs.size(),
1273 template.getArtifactUuid());
1275 logger.debug(template.toString());
1276 Set<HeatTemplateParam> paramSet = template.getParameters();
1277 logger.debug("paramSet has {} entries", paramSet.size());
1278 } catch (Exception e) {
1279 logger.debug("Exception occurred in convertInputMap:" + e.getMessage(), e);
1282 for (HeatTemplateParam htp : template.getParameters()) {
1283 logger.debug("Adding {}", htp.getParamName());
1284 params.put(htp.getParamName(), htp);
1285 if (htp.getParamAlias() != null && !"".equals(htp.getParamAlias())) {
1286 logger.debug("\tFound ALIAS {} -> {}", htp.getParamName(), htp.getParamAlias());
1287 paramAliases.put(htp.getParamAlias(), htp);
1290 logger.debug("Now iterate through the inputs...");
1291 for (String key : inputs.keySet()) {
1292 logger.debug("key={}", key);
1293 boolean alias = false;
1294 String realName = null;
1295 if (!params.containsKey(key)) {
1296 logger.debug("{} is not a parameter in the template! - check for an alias", key);
1297 // add check here for an alias
1298 if (!paramAliases.containsKey(key)) {
1299 logger.debug("The parameter {} is in the inputs, but it's not a parameter for this template - omit", key);
1303 realName = paramAliases.get(key).getParamName();
1304 logger.debug("FOUND AN ALIAS! Will use {} in lieu of give key/alias {}", realName, key);
1307 String type = params.get(key).getParamType();
1308 if (type == null || "".equals(type)) {
1309 logger.debug("**PARAM_TYPE is null/empty for {}, will default to string", key);
1312 logger.debug("Parameter: {} is of type {}", key, type);
1313 if ("string".equalsIgnoreCase(type)) {
1315 String str = inputs.get(key) != null ? inputs.get(key).toString() : null;
1317 newInputs.put(realName, str);
1319 newInputs.put(key, str);
1320 } else if ("number".equalsIgnoreCase(type)) {
1321 String integerString = inputs.get(key) != null ? inputs.get(key).toString() : null;
1322 Integer anInteger = null;
1324 anInteger = Integer.parseInt(integerString);
1325 } catch (Exception e) {
1326 logger.debug("Unable to convert {} to an integer!!", integerString, e);
1329 if (anInteger != null) {
1331 newInputs.put(realName, anInteger);
1333 newInputs.put(key, anInteger);
1337 newInputs.put(realName, integerString);
1339 newInputs.put(key, integerString);
1341 } else if ("json".equalsIgnoreCase(type)) {
1342 Object jsonObj = inputs.get(key);
1345 if (jsonObj instanceof String) {
1346 json = JSON_MAPPER.readTree(jsonObj.toString());
1348 //will already marshal to json without intervention
1351 } catch (IOException e) {
1352 logger.error("failed to map to json, directly converting to string instead", e);
1353 json = jsonObj.toString();
1356 newInputs.put(realName, json);
1358 newInputs.put(key, json);
1359 } else if ("comma_delimited_list".equalsIgnoreCase(type)) {
1360 String commaSeparated = inputs.get(key) != null ? inputs.get(key).toString() : null;
1362 List<String> anArrayList = this.convertCdlToArrayList(commaSeparated);
1364 newInputs.put(realName, anArrayList);
1366 newInputs.put(key, anArrayList);
1367 } catch (Exception e) {
1368 logger.debug("Unable to convert {} to an ArrayList!!", commaSeparated, e);
1370 newInputs.put(realName, commaSeparated);
1372 newInputs.put(key, commaSeparated);
1374 } else if ("boolean".equalsIgnoreCase(type)) {
1375 String booleanString = inputs.get(key) != null ? inputs.get(key).toString() : null;
1376 Boolean aBool = Boolean.valueOf(booleanString);
1378 newInputs.put(realName, aBool);
1380 newInputs.put(key, aBool);
1382 // it's null or something undefined - just add it back as a String
1383 String str = inputs.get(key).toString();
1385 newInputs.put(realName, str);
1387 newInputs.put(key, str);
1394 * This helpful method added for Valet
1396 public String getCloudSiteKeystoneUrl(String cloudSiteId) throws MsoCloudSiteNotFound {
1397 String keystone_url = null;
1399 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1400 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
1401 keystone_url = cloudIdentity.getIdentityUrl();
1402 } catch (Exception e) {
1403 throw new MsoCloudSiteNotFound(cloudSiteId);
1405 if (keystone_url == null || keystone_url.isEmpty()) {
1406 throw new MsoCloudSiteNotFound(cloudSiteId);
1408 return keystone_url;
1412 * Create a string suitable for being dumped to a debug log that creates a
1413 * pseudo-JSON request dumping what's being sent to Openstack API in the create or update request
1416 private String printStackRequest(String tenantId,
1417 Map<String, Object> heatFiles,
1418 Map<String, Object> nestedTemplates,
1420 Map<String, Object> inputs,
1421 String vfModuleName,
1425 String cloudSiteId) {
1426 StringBuilder sb = new StringBuilder();
1427 sb.append("CREATE STACK REQUEST (formatted for readability)\n");
1428 sb.append("tenant=" + tenantId + ", cloud=" + cloudSiteId);
1430 sb.append(" \"stack_name\": \"" + vfModuleName + "\",\n");
1431 sb.append(" \"disable_rollback\": " + backout + ",\n");
1432 sb.append(" \"timeout_mins\": " + timeoutMinutes + ",\n");
1433 sb.append(" \"template\": {\n");
1434 sb.append(template);
1436 sb.append(" \"environment\": {\n");
1437 if (environment == null)
1438 sb.append("<none>");
1440 sb.append(environment);
1442 sb.append(" \"files\": {\n");
1443 int filesCounter = 0;
1444 if (heatFiles != null) {
1445 for (String key : heatFiles.keySet()) {
1447 if (filesCounter > 1) {
1450 sb.append(" \"" + key + "\": {\n");
1451 sb.append(heatFiles.get(key).toString() + "\n }");
1454 if (nestedTemplates != null) {
1455 for (String key : nestedTemplates.keySet()) {
1457 if (filesCounter > 1) {
1460 sb.append(" \"" + key + "\": {\n");
1461 sb.append(nestedTemplates.get(key).toString() + "\n }");
1464 sb.append("\n },\n");
1465 sb.append(" \"parameters\": {\n");
1466 int paramCounter = 0;
1467 for (String name : inputs.keySet()) {
1469 if (paramCounter > 1) {
1472 Object o = inputs.get(name);
1473 if (o instanceof java.lang.String) {
1474 sb.append(" \"" + name + "\": \"" + inputs.get(name).toString() + "\"");
1475 } else if (o instanceof Integer) {
1476 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1477 } else if (o instanceof ArrayList) {
1478 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1479 } else if (o instanceof Boolean) {
1480 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1482 sb.append(" \"" + name + "\": " + "\"(there was an issue trying to dump this value...)\"" );
1485 sb.append("\n }\n}\n");
1487 return sb.toString();
1490 /*******************************************************************************
1492 * Methods (and associated utilities) to implement the VduPlugin interface
1494 *******************************************************************************/
1497 * VduPlugin interface for instantiate function.
1499 * Translate the VduPlugin parameters to the corresponding 'createStack' parameters,
1500 * and then invoke the existing function.
1503 public VduInstance instantiateVdu (
1504 CloudInfo cloudInfo,
1505 String instanceName,
1506 Map<String,Object> inputs,
1507 VduModelInfo vduModel,
1508 boolean rollbackOnFailure)
1511 String cloudSiteId = cloudInfo.getCloudSiteId();
1512 String tenantId = cloudInfo.getTenantId();
1514 // Translate the VDU ModelInformation structure to that which is needed for
1515 // creating the Heat stack. Loop through the artifacts, looking specifically
1516 // for MAIN_TEMPLATE and ENVIRONMENT. Any other artifact will
1517 // be attached as a FILE.
1518 String heatTemplate = null;
1519 Map<String,Object> nestedTemplates = new HashMap<>();
1520 Map<String,Object> files = new HashMap<>();
1521 String heatEnvironment = null;
1523 for (VduArtifact vduArtifact: vduModel.getArtifacts()) {
1524 if (vduArtifact.getType() == ArtifactType.MAIN_TEMPLATE) {
1525 heatTemplate = new String(vduArtifact.getContent());
1527 else if (vduArtifact.getType() == ArtifactType.NESTED_TEMPLATE) {
1528 nestedTemplates.put(vduArtifact.getName(), new String(vduArtifact.getContent()));
1530 else if (vduArtifact.getType() == ArtifactType.ENVIRONMENT) {
1531 heatEnvironment = new String(vduArtifact.getContent());
1536 StackInfo stackInfo = createStack (cloudSiteId,
1541 true, // poll for completion
1542 vduModel.getTimeoutMinutes(),
1548 // Populate a vduInstance from the StackInfo
1549 return stackInfoToVduInstance(stackInfo);
1551 catch (Exception e) {
1552 throw new VduException ("MsoHeatUtils (instantiateVDU): createStack Exception", e);
1558 * VduPlugin interface for query function.
1561 public VduInstance queryVdu (CloudInfo cloudInfo, String instanceId)
1564 String cloudSiteId = cloudInfo.getCloudSiteId();
1565 String tenantId = cloudInfo.getTenantId();
1568 // Query the Cloudify Deployment object and populate a VduInstance
1569 StackInfo stackInfo = queryStack (cloudSiteId, tenantId, instanceId);
1571 return stackInfoToVduInstance(stackInfo);
1573 catch (Exception e) {
1574 throw new VduException ("MsoHeatUtile (queryVdu): queryStack Exception ", e);
1580 * VduPlugin interface for delete function.
1583 public VduInstance deleteVdu (CloudInfo cloudInfo, String instanceId, int timeoutMinutes)
1586 String cloudSiteId = cloudInfo.getCloudSiteId();
1587 String tenantId = cloudInfo.getTenantId();
1590 // Delete the Heat stack
1591 StackInfo stackInfo = deleteStack (tenantId, cloudSiteId, instanceId, true);
1593 // Populate a VduInstance based on the deleted Cloudify Deployment object
1594 VduInstance vduInstance = stackInfoToVduInstance(stackInfo);
1596 // Override return state to DELETED (HeatUtils sets to NOTFOUND)
1597 vduInstance.getStatus().setState(VduStateType.DELETED);
1601 catch (Exception e) {
1602 throw new VduException ("Delete VDU Exception", e);
1608 * VduPlugin interface for update function.
1610 * Update is currently not supported in the MsoHeatUtils implementation of VduPlugin.
1611 * Just return a VduException.
1615 public VduInstance updateVdu (
1616 CloudInfo cloudInfo,
1618 Map<String,Object> inputs,
1619 VduModelInfo vduModel,
1620 boolean rollbackOnFailure)
1623 throw new VduException ("MsoHeatUtils: updateVdu interface not supported");
1628 * Convert the local DeploymentInfo object (Cloudify-specific) to a generic VduInstance object
1630 protected VduInstance stackInfoToVduInstance (StackInfo stackInfo)
1632 VduInstance vduInstance = new VduInstance();
1634 // The full canonical name as the instance UUID
1635 vduInstance.setVduInstanceId(stackInfo.getCanonicalName());
1636 vduInstance.setVduInstanceName(stackInfo.getName());
1638 // Copy inputs and outputs
1639 vduInstance.setInputs(stackInfo.getParameters());
1640 vduInstance.setOutputs(stackInfo.getOutputs());
1642 // Translate the status elements
1643 vduInstance.setStatus(stackStatusToVduStatus (stackInfo));
1648 private VduStatus stackStatusToVduStatus (StackInfo stackInfo)
1650 VduStatus vduStatus = new VduStatus();
1652 // Map the status fields to more generic VduStatus.
1653 // There are lots of HeatStatus values, so this is a bit long...
1654 HeatStatus heatStatus = stackInfo.getStatus();
1655 String statusMessage = stackInfo.getStatusMessage();
1657 if (heatStatus == HeatStatus.INIT || heatStatus == HeatStatus.BUILDING) {
1658 vduStatus.setState(VduStateType.INSTANTIATING);
1659 vduStatus.setLastAction((new PluginAction ("create", "in_progress", statusMessage)));
1661 else if (heatStatus == HeatStatus.NOTFOUND) {
1662 vduStatus.setState(VduStateType.NOTFOUND);
1664 else if (heatStatus == HeatStatus.CREATED) {
1665 vduStatus.setState(VduStateType.INSTANTIATED);
1666 vduStatus.setLastAction((new PluginAction ("create", "complete", statusMessage)));
1668 else if (heatStatus == HeatStatus.UPDATED) {
1669 vduStatus.setState(VduStateType.INSTANTIATED);
1670 vduStatus.setLastAction((new PluginAction ("update", "complete", statusMessage)));
1672 else if (heatStatus == HeatStatus.UPDATING) {
1673 vduStatus.setState(VduStateType.UPDATING);
1674 vduStatus.setLastAction((new PluginAction ("update", "in_progress", statusMessage)));
1676 else if (heatStatus == HeatStatus.DELETING) {
1677 vduStatus.setState(VduStateType.DELETING);
1678 vduStatus.setLastAction((new PluginAction ("delete", "in_progress", statusMessage)));
1680 else if (heatStatus == HeatStatus.FAILED) {
1681 vduStatus.setState(VduStateType.FAILED);
1682 vduStatus.setErrorMessage(stackInfo.getStatusMessage());
1684 vduStatus.setState(VduStateType.UNKNOWN);
1690 public Resources queryStackResources(String cloudSiteId, String tenantId, String stackName) throws MsoException {
1691 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1692 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1693 Heat heatClient = getHeatClient(cloudSite, tenantId);
1694 OpenStackRequest<Resources> request = heatClient.getResources().listResources(stackName);
1695 return executeAndRecordOpenstackRequest(request);
1698 public <R> R executeHeatClientRequest(String url, String cloudSiteId, String tenantId, Class<R> returnType) throws MsoException {
1699 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1700 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1701 Heat heatClient = getHeatClient(cloudSite, tenantId);
1702 OpenStackRequest<R> request = heatClient.get(url, returnType);
1703 return executeAndRecordOpenstackRequest(request);
1706 protected void sleep(long time) {
1709 } catch (InterruptedException e) {
1710 logger.debug ("Thread interrupted while sleeping", e);
1711 Thread.currentThread().interrupt();