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.MessageEnum;
72 import org.onap.so.logger.MsoLogger;
73 import org.onap.so.openstack.beans.HeatStatus;
74 import org.onap.so.openstack.beans.StackInfo;
75 import org.onap.so.openstack.exceptions.MsoAdapterException;
76 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
77 import org.onap.so.openstack.exceptions.MsoException;
78 import org.onap.so.openstack.exceptions.MsoIOException;
79 import org.onap.so.openstack.exceptions.MsoOpenstackException;
80 import org.onap.so.openstack.exceptions.MsoStackAlreadyExists;
81 import org.onap.so.openstack.exceptions.MsoTenantNotFound;
82 import org.onap.so.openstack.mappers.StackInfoMapper;
83 import org.onap.so.utils.CryptoUtils;
84 import org.slf4j.Logger;
85 import org.slf4j.LoggerFactory;
86 import org.springframework.beans.factory.annotation.Autowired;
87 import org.springframework.context.annotation.Primary;
88 import org.springframework.core.env.Environment;
89 import org.springframework.stereotype.Component;
91 import com.fasterxml.jackson.core.JsonProcessingException;
92 import com.fasterxml.jackson.core.type.TypeReference;
93 import com.fasterxml.jackson.databind.JsonNode;
94 import com.fasterxml.jackson.databind.ObjectMapper;
95 import com.woorea.openstack.base.client.OpenStackConnectException;
96 import com.woorea.openstack.base.client.OpenStackRequest;
97 import com.woorea.openstack.base.client.OpenStackResponseException;
98 import com.woorea.openstack.heat.Heat;
99 import com.woorea.openstack.heat.model.CreateStackParam;
100 import com.woorea.openstack.heat.model.Resources;
101 import com.woorea.openstack.heat.model.Stack;
102 import com.woorea.openstack.heat.model.Stack.Output;
103 import com.woorea.openstack.heat.model.Stacks;
104 import com.woorea.openstack.keystone.Keystone;
105 import com.woorea.openstack.keystone.model.Access;
106 import com.woorea.openstack.keystone.model.Authentication;
107 import com.woorea.openstack.keystone.utils.KeystoneUtils;
111 public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{
113 private static final String TOKEN_AUTH = "TokenAuth";
115 private static final String QUERY_ALL_STACKS = "QueryAllStacks";
117 private static final String DELETE_STACK = "DeleteStack";
119 protected static final String HEAT_ERROR = "HeatError";
121 protected static final String CREATE_STACK = "CreateStack";
123 // Fetch cloud configuration each time (may be cached in CloudConfig class)
125 protected CloudConfig cloudConfig;
128 private Environment environment;
131 private AuthenticationMethodFactory authenticationMethodFactory;
134 private MsoTenantUtilsFactory tenantUtilsFactory;
137 private KeystoneV3Authentication keystoneV3Authentication;
139 private static final Logger logger = LoggerFactory.getLogger(MsoHeatUtils.class);
141 // Properties names and variables (with default values)
142 protected String createPollIntervalProp = "org.onap.so.adapters.po.pollInterval";
143 private String deletePollIntervalProp = "org.onap.so.adapters.po.pollInterval";
144 private String deletePollTimeoutProp = "org.onap.so.adapters.po.pollTimeout";
146 protected static final String createPollIntervalDefault = "15";
147 private static final String deletePollIntervalDefault = "15";
149 private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
152 * keep this old method signature here to maintain backwards compatibility. keep others as well.
153 * this method does not include environment, files, or heatFiles
155 public StackInfo createStack (String cloudSiteId,
159 Map <String, ?> stackInputs,
160 boolean pollForCompletion,
161 int timeoutMinutes) throws MsoException {
162 // Just call the new method with the environment & files variable set to null
163 return this.createStack (cloudSiteId,
176 // This method has environment, but not files or heatFiles
177 public StackInfo createStack (String cloudSiteId,
181 Map <String, ?> stackInputs,
182 boolean pollForCompletion,
184 String environment) throws MsoException {
185 // Just call the new method with the files/heatFiles variables set to null
186 return this.createStack (cloudSiteId,
199 // This method has environment and files, but not heatFiles.
200 public StackInfo createStack (String cloudSiteId,
204 Map <String, ?> stackInputs,
205 boolean pollForCompletion,
208 Map <String, Object> files) throws MsoException {
209 return this.createStack (cloudSiteId,
222 // This method has environment, files, heatfiles
223 public StackInfo createStack (String cloudSiteId,
227 Map <String, ?> stackInputs,
228 boolean pollForCompletion,
231 Map <String, Object> files,
232 Map <String, Object> heatFiles) throws MsoException {
233 return this.createStack (cloudSiteId,
247 * Create a new Stack in the specified cloud location and tenant. The Heat template
248 * and parameter map are passed in as arguments, along with the cloud access credentials.
249 * It is expected that parameters have been validated and contain at minimum the required
250 * parameters for the given template with no extra (undefined) parameters..
252 * The Stack name supplied by the caller must be unique in the scope of this tenant.
253 * However, it should also be globally unique, as it will be the identifier for the
254 * resource going forward in Inventory. This latter is managed by the higher levels
255 * invoking this function.
257 * The caller may choose to let this function poll Openstack for completion of the
258 * stack creation, or may handle polling itself via separate calls to query the status.
259 * In either case, a StackInfo object will be returned containing the current status.
260 * When polling is enabled, a status of CREATED is expected. When not polling, a
261 * status of BUILDING is expected.
263 * An error will be thrown if the requested Stack already exists in the specified
266 * For 1510 - add "environment", "files" (nested templates), and "heatFiles" (get_files) as
267 * parameters for createStack. If environment is non-null, it will be added to the stack.
268 * The nested templates and get_file entries both end up being added to the "files" on the
269 * stack. We must combine them before we add them to the stack if they're both non-null.
271 * @param cloudSiteId The cloud (may be a region) in which to create the stack.
272 * @param tenantId The Openstack ID of the tenant in which to create the Stack
273 * @param stackName The name of the stack to create
274 * @param heatTemplate The Heat template
275 * @param stackInputs A map of key/value inputs
276 * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
277 * @param environment An optional yaml-format string to specify environmental parameters
278 * @param files a Map<String, Object> that lists the child template IDs (file is the string, object is an int of
280 * @param heatFiles a Map<String, Object> that lists the get_file entries (fileName, fileBody)
281 * @param backout Donot delete stack on create Failure - defaulted to True
282 * @return A StackInfo object
283 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
286 @SuppressWarnings("unchecked")
287 public StackInfo createStack (String cloudSiteId,
291 Map <String, ?> stackInputs,
292 boolean pollForCompletion,
295 Map <String, Object> files,
296 Map <String, Object> heatFiles,
297 boolean backout) throws MsoException {
299 // Take out the multicloud inputs, if present.
300 for (String key : MsoMulticloudUtils.MULTICLOUD_INPUTS) {
301 if (stackInputs.containsKey(key)) {
302 stackInputs.remove(key);
303 if (stackInputs.isEmpty()) {
309 CreateStackParam stack = createStackParam(stackName, heatTemplate, stackInputs, timeoutMinutes, environment, files, heatFiles);
311 // Obtain the cloud site information where we will create the stack
312 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
313 () -> new MsoCloudSiteNotFound(cloudSiteId));
314 logger.debug("Found: {}", cloudSite);
315 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
316 // This could throw MsoTenantNotFound or MsoOpenstackException (both propagated)
317 Heat heatClient = getHeatClient (cloudSite, tenantId);
318 logger.debug("Found: {}", heatClient);
320 logger.debug ("Ready to Create Stack ({}) with input params: {}", heatTemplate, stackInputs);
322 Stack heatStack = null;
324 OpenStackRequest <Stack> request = heatClient.getStacks ().create (stack);
325 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
326 request.header ("X-Auth-User", cloudIdentity.getMsoId ());
327 request.header ("X-Auth-Key", CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass ()));
328 heatStack = executeAndRecordOpenstackRequest (request);
329 } catch (OpenStackResponseException e) {
330 if (e.getStatus () == 409) {
331 MsoStackAlreadyExists me = new MsoStackAlreadyExists (stackName, tenantId, cloudSiteId);
332 me.addContext (CREATE_STACK);
335 logger.debug("ERROR STATUS = {},\n{}\n{}", e.getStatus(), e.getMessage(), e.getLocalizedMessage());
336 throw heatExceptionToMsoException (e, CREATE_STACK);
338 } catch (OpenStackConnectException e) {
339 throw heatExceptionToMsoException (e, CREATE_STACK);
340 } catch (RuntimeException e) {
341 throw runtimeExceptionToMsoException (e, CREATE_STACK);
344 // Subsequent access by the canonical name "<stack name>/<stack-id>".
345 // Otherwise, simple query by name returns a 302 redirect.
346 // NOTE: This is specific to the v1 Orchestration API.
347 String canonicalName = stackName + "/" + heatStack.getId ();
349 if (pollForCompletion) {
350 heatStack = pollStackForCompletion(cloudSiteId, tenantId, stackName, timeoutMinutes, backout, heatClient,
351 heatStack, canonicalName);
353 // Get initial status, since it will have been null after the create.
354 heatStack = queryHeatStack (heatClient, canonicalName);
355 logger.debug (heatStack.getStackStatus ());
357 return new StackInfoMapper(heatStack).map();
360 private Stack pollStackForCompletion(String cloudSiteId, String tenantId, String stackName, int timeoutMinutes,
361 boolean backout, Heat heatClient, Stack heatStack, String canonicalName)
362 throws MsoException, MsoOpenstackException {
363 int createPollInterval = Integer.parseInt(this.environment.getProperty(createPollIntervalProp, createPollIntervalDefault));
364 int pollTimeout = (timeoutMinutes * 60) + createPollInterval;
365 int deletePollInterval = createPollInterval;
366 int deletePollTimeout = pollTimeout;
367 boolean createTimedOut = false;
368 StringBuilder stackErrorStatusReason = new StringBuilder("");
369 logger.debug("createPollInterval={}, pollTimeout={}", createPollInterval, pollTimeout);
373 heatStack = queryHeatStack (heatClient, canonicalName);
374 logger.debug("{} ({})", heatStack.getStackStatus(), canonicalName);
376 logger.debug("Current stack {}", this.getOutputsAsStringBuilder(heatStack).toString());
377 } catch (Exception e) {
378 logger.debug("an error occurred trying to print out the current outputs of the stack", e);
381 if ("CREATE_IN_PROGRESS".equals (heatStack.getStackStatus ())) {
382 if (pollTimeout <= 0) {
383 logger.error("{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Create stack timeout",
384 MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
385 heatStack.getStackStatus(), MsoLogger.ErrorCode.AvailabilityError.getValue());
386 createTimedOut = true;
389 sleep(createPollInterval * 1000L);
390 pollTimeout -= createPollInterval;
391 logger.debug("pollTimeout remaining: {}", pollTimeout);
393 stackErrorStatusReason.append("Stack error (" + heatStack.getStackStatus() + "): " + heatStack.getStackStatusReason());
396 } catch (MsoException me) {
397 // Cannot query the stack status. Something is wrong.
398 // Try to roll back the stack
401 logger.warn("{} Exception in Create Stack, stack deletion suppressed {}",
402 MessageEnum.RA_CREATE_STACK_ERR, MsoLogger.ErrorCode.BusinessProcesssError.getValue());
408 "Create Stack error - unable to query for stack status - attempting to delete stack: {}"
409 + " - This will likely fail and/or we won't be able to query to see if delete worked",
411 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
412 executeAndRecordOpenstackRequest (request);
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) {
422 "{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Rollback: DELETE stack timeout",
423 MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
424 heatStack.getStackStatus(), MsoLogger.ErrorCode.AvailabilityError.getValue());
427 sleep(deletePollInterval * 1000L);
428 deletePollTimeout -= deletePollInterval;
430 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
431 logger.debug("DELETE_COMPLETE for {}", canonicalName);
435 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
439 // assume if we can't find it - it's deleted
440 logger.debug("heatStack returned null - assume the stack {} has been deleted", canonicalName);
445 } catch (Exception e3) {
446 // Just log this one. We will report the original exception.
447 logger.error("{} Create Stack: Nested exception rolling back stack: {} ",
448 MessageEnum.RA_CREATE_STACK_ERR, MsoLogger.ErrorCode.BusinessProcesssError.getValue(), e3);
451 } catch (Exception e2) {
452 // Just log this one. We will report the original exception.
453 logger.error("{} Create Stack: Nested exception rolling back stack: {} ",
454 MessageEnum.RA_CREATE_STACK_ERR, MsoLogger.ErrorCode.BusinessProcesssError.getValue(), e2);
458 // Propagate the original exception from Stack Query.
459 me.addContext (CREATE_STACK);
464 if (!"CREATE_COMPLETE".equals (heatStack.getStackStatus ())) {
465 logger.error("{} Create Stack error: Polling complete with non-success status: {}, {} {} ",
466 MessageEnum.RA_CREATE_STACK_ERR, heatStack.getStackStatus(), heatStack.getStackStatusReason(),
467 MsoLogger.ErrorCode.BusinessProcesssError.getValue());
469 // Rollback the stack creation, since it is in an indeterminate state.
473 "{} Create Stack errored, stack deletion suppressed {} Create Stack error, stack deletion suppressed",
474 MessageEnum.RA_CREATE_STACK_ERR, MsoLogger.ErrorCode.BusinessProcesssError.getValue());
479 logger.debug("Create Stack errored - attempting to DELETE stack: {}", canonicalName);
480 logger.debug("deletePollInterval={}, deletePollTimeout={}", deletePollInterval, deletePollTimeout);
481 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
482 executeAndRecordOpenstackRequest (request);
483 boolean deleted = false;
486 heatStack = queryHeatStack(heatClient, canonicalName);
487 if (heatStack != null) {
488 logger.debug("{} ({})", heatStack.getStackStatus(), canonicalName);
489 if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
490 if (deletePollTimeout <= 0) {
492 "{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Rollback: DELETE stack timeout",
493 MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
494 heatStack.getStackStatus(), MsoLogger.ErrorCode.AvailabilityError.getValue());
497 sleep(deletePollInterval * 1000L);
498 deletePollTimeout -= deletePollInterval;
499 logger.debug("deletePollTimeout remaining: {}", deletePollTimeout);
501 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
502 logger.debug("DELETE_COMPLETE for {}", canonicalName);
505 } else if ("DELETE_FAILED".equals(heatStack.getStackStatus())) {
506 // Warn about this (?) - but still throw the original exception
508 "{} Create Stack errored, stack deletion FAILED {} Create Stack error, stack deletion FAILED",
509 MessageEnum.RA_CREATE_STACK_ERR, MsoLogger.ErrorCode.BusinessProcesssError.getValue());
510 logger.debug("Stack deletion FAILED on a rollback of a create - {}, status={}, reason={}",
511 canonicalName, heatStack.getStackStatus(), heatStack.getStackStatusReason());
514 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
518 // assume if we can't find it - it's deleted
519 logger.debug("heatStack returned null - assume the stack {} has been deleted", canonicalName);
524 } catch (MsoException me2) {
525 // We got an exception on the delete - don't throw this exception - throw the original - just log.
526 logger.debug("Exception thrown trying to delete {} on a create->rollback: {} ", canonicalName,
527 me2.getContextMessage(), me2);
528 logger.warn("{} Create Stack errored, then stack deletion FAILED - exception thrown {} {}",
529 MessageEnum.RA_CREATE_STACK_ERR, MsoLogger.ErrorCode.BusinessProcesssError.getValue(),
530 me2.getContextMessage());
533 } // end while !deleted
534 StringBuilder errorContextMessage;
535 if (createTimedOut) {
536 errorContextMessage = new StringBuilder("Stack Creation Timeout");
538 errorContextMessage = stackErrorStatusReason;
541 errorContextMessage.append(" - stack successfully deleted");
543 errorContextMessage.append(" - encountered an error trying to delete the stack");
545 } catch (Exception e2) {
546 // shouldn't happen - but handle
547 logger.error("{} Create Stack: Nested exception rolling back stack: {} ", MessageEnum.RA_CREATE_STACK_ERR,
548 MsoLogger.ErrorCode.BusinessProcesssError.getValue(), e2);
551 MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
552 me.addContext(CREATE_STACK);
559 * Query for a single stack (by Name) in a tenant. This call will always return a
560 * StackInfo object. If the stack does not exist, an "empty" StackInfo will be
561 * returned - containing only the stack name and a status of NOTFOUND.
563 * @param tenantId The Openstack ID of the tenant in which to query
564 * @param cloudSiteId The cloud identifier (may be a region) in which to query
565 * @param stackName The name of the stack to query (may be simple or canonical)
566 * @return A StackInfo object
567 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
569 public StackInfo queryStack (String cloudSiteId, String tenantId, String stackName) throws MsoException {
570 logger.debug ("Query HEAT stack: {} in tenant {}", stackName, tenantId);
572 // Obtain the cloud site information where we will create the stack
573 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
574 () -> new MsoCloudSiteNotFound(cloudSiteId));
575 logger.debug("Found: {}", cloudSite.toString());
577 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
578 Heat heatClient = null;
580 heatClient = getHeatClient (cloudSite, tenantId);
581 if (heatClient != null) {
582 logger.debug("Found: {}", heatClient.toString());
584 } catch (MsoTenantNotFound e) {
585 // Tenant doesn't exist, so stack doesn't either
586 logger.debug ("Tenant with id " + tenantId + "not found.", e);
587 return new StackInfo (stackName, HeatStatus.NOTFOUND);
588 } catch (MsoException me) {
589 // Got an Openstack error. Propagate it
590 logger.error("{} {} Openstack Exception on Token request: ", MessageEnum.RA_CONNECTION_EXCEPTION,
591 MsoLogger.ErrorCode.AvailabilityError.getValue(), me);
592 me.addContext ("QueryStack");
597 // An MsoException will propagate transparently to the caller.
598 Stack heatStack = queryHeatStack (heatClient, stackName);
600 if (heatStack == null) {
601 // Stack does not exist. Return a StackInfo with status NOTFOUND
602 return new StackInfo (stackName, HeatStatus.NOTFOUND);
605 return new StackInfoMapper(heatStack).map();
609 * Delete a stack (by Name/ID) in a tenant. If the stack is not found, it will be
610 * considered a successful deletion. The return value is a StackInfo object which
611 * contains the current stack status.
613 * The client may choose to let the adapter poll Openstack for completion of the
614 * stack deletion, or may handle polling itself via separate query calls. In either
615 * case, a StackInfo object will be returned. When polling is enabled, a final
616 * status of NOTFOUND is expected. When not polling, a status of DELETING is expected.
618 * There is no rollback from a successful stack deletion. A deletion failure will
619 * also result in an undefined stack state - the components may or may not have been
620 * all or partially deleted, so the resulting stack must be considered invalid.
622 * @param tenantId The Openstack ID of the tenant in which to perform the delete
623 * @param cloudSiteId The cloud identifier (may be a region) from which to delete the stack.
624 * @param stackName The name/id of the stack to delete. May be simple or canonical
625 * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
626 * @return A StackInfo object
627 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
628 * @throws MsoCloudSiteNotFound
630 public StackInfo deleteStack (String tenantId,
633 boolean pollForCompletion) throws MsoException {
634 // Obtain the cloud site information where we will create the stack
635 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
636 () -> new MsoCloudSiteNotFound(cloudSiteId));
637 logger.debug("Found: {}", cloudSite.toString());
639 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
640 Heat heatClient = null;
642 heatClient = getHeatClient (cloudSite, tenantId);
643 if (heatClient != null) {
644 logger.debug("Found: {}", heatClient.toString());
646 } catch (MsoTenantNotFound e) {
647 // Tenant doesn't exist, so stack doesn't either
648 logger.debug ("Tenant with id " + tenantId + "not found.", e);
649 return new StackInfo (stackName, HeatStatus.NOTFOUND);
650 } catch (MsoException me) {
651 // Got an Openstack error. Propagate it
652 logger.error("{} {} Openstack Exception on Token request: ", MessageEnum.RA_CONNECTION_EXCEPTION,
653 MsoLogger.ErrorCode.AvailabilityError.getValue(), me);
654 me.addContext (DELETE_STACK);
658 // OK if stack not found, perform a query first
659 Stack heatStack = queryHeatStack (heatClient, stackName);
660 if (heatStack == null || "DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
661 // Not found. Return a StackInfo with status NOTFOUND
662 return new StackInfo (stackName, HeatStatus.NOTFOUND);
667 // Use canonical name "<stack name>/<stack-id>" to delete.
668 // Otherwise, deletion by name returns a 302 redirect.
669 // NOTE: This is specific to the v1 Orchestration API.
670 String canonicalName = heatStack.getStackName () + "/" + heatStack.getId ();
673 OpenStackRequest <Void> request = null;
674 if(null != heatClient) {
675 request = heatClient.getStacks ().deleteByName (canonicalName);
678 logger.debug ("Heat Client is NULL" );
681 executeAndRecordOpenstackRequest (request);
682 } catch (OpenStackResponseException e) {
683 if (e.getStatus () == 404) {
684 // Not found. We are OK with this. Return a StackInfo with status NOTFOUND
685 return new StackInfo (stackName, HeatStatus.NOTFOUND);
687 // Convert the OpenStackResponseException to an MsoOpenstackException
688 throw heatExceptionToMsoException (e, DELETE_STACK);
690 } catch (OpenStackConnectException e) {
691 // Error connecting to Openstack instance. Convert to an MsoException
692 throw heatExceptionToMsoException (e, DELETE_STACK);
693 } catch (RuntimeException e) {
695 throw runtimeExceptionToMsoException (e, DELETE_STACK);
698 // Requery the stack for current status.
699 // It will probably still exist with "DELETE_IN_PROGRESS" status.
700 heatStack = queryHeatStack (heatClient, canonicalName);
702 if (pollForCompletion) {
703 // Set a timeout on polling
705 int pollInterval = Integer.parseInt(this.environment.getProperty(deletePollIntervalProp, "" + deletePollIntervalDefault));
706 int pollTimeout = Integer.parseInt(this.environment.getProperty(deletePollTimeoutProp, "" + deletePollIntervalDefault));
708 // When querying by canonical name, Openstack returns DELETE_COMPLETE status
709 // instead of "404" (which would result from query by stack name).
710 while (heatStack != null && !"DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
711 logger.debug ("Stack status: {}", heatStack.getStackStatus ());
713 if ("DELETE_FAILED".equals (heatStack.getStackStatus ())) {
714 // Throw a 'special case' of MsoOpenstackException to report the Heat status
715 String error = "Stack delete error (" + heatStack.getStackStatus ()
717 + heatStack.getStackStatusReason ();
718 MsoOpenstackException me = new MsoOpenstackException (0, "", error);
719 me.addContext (DELETE_STACK);
721 // Alarm this condition, stack deletion failed
727 if (pollTimeout <= 0) {
728 logger.error("{} Cloud site: {} Tenant: {} Stack: {} Stack status: {} {} Delete Stack Timeout",
729 MessageEnum.RA_DELETE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
730 heatStack.getStackStatus(), MsoLogger.ErrorCode.AvailabilityError.getValue());
732 // Throw a 'special case' of MsoOpenstackException to report the Heat status
733 MsoOpenstackException me = new MsoOpenstackException (0, "", "Stack Deletion Timeout");
734 me.addContext (DELETE_STACK);
736 // Alarm this condition, stack deletion failed
742 sleep(pollInterval * 1000L);
744 pollTimeout -= pollInterval;
745 logger.debug("pollTimeout remaining: {}", pollTimeout);
747 heatStack = queryHeatStack (heatClient, canonicalName);
750 // The stack is gone when this point is reached
751 return new StackInfo (stackName, HeatStatus.NOTFOUND);
754 // Return the current status (if not polling, the delete may still be in progress)
755 StackInfo stackInfo = new StackInfoMapper(heatStack).map();
756 stackInfo.setName (stackName);
762 * Query for all stacks in a tenant site. This call will return a List of StackInfo
763 * objects, one for each deployed stack.
765 * Note that this is limited to a single site. To ensure that a tenant is truly
766 * empty would require looping across all tenant endpoints.
768 * @param tenantId The Openstack ID of the tenant to query
769 * @param cloudSiteId The cloud identifier (may be a region) in which to query.
770 * @return A List of StackInfo objects
771 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
772 * @throws MsoCloudSiteNotFound
774 public List <StackInfo> queryAllStacks (String tenantId, String cloudSiteId) throws MsoException {
775 // Obtain the cloud site information where we will create the stack
776 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
777 () -> new MsoCloudSiteNotFound(cloudSiteId));
778 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
779 Heat heatClient = getHeatClient (cloudSite, tenantId);
782 OpenStackRequest <Stacks> request = heatClient.getStacks ().list ();
783 Stacks stacks = executeAndRecordOpenstackRequest (request);
785 List <StackInfo> stackList = new ArrayList <> ();
787 // Not sure if returns an empty list or null if no stacks exist
788 if (stacks != null) {
789 for (Stack stack : stacks) {
790 stackList.add (new StackInfoMapper(stack).map());
795 } catch (OpenStackResponseException e) {
796 if (e.getStatus () == 404) {
797 // Not sure if this can happen, but return an empty list
798 logger.debug ("queryAllStacks - stack not found: ");
799 return new ArrayList <> ();
801 // Convert the OpenStackResponseException to an MsoOpenstackException
802 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
804 } catch (OpenStackConnectException e) {
805 // Error connecting to Openstack instance. Convert to an MsoException
806 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
807 } catch (RuntimeException e) {
809 throw runtimeExceptionToMsoException (e, QUERY_ALL_STACKS);
814 * Validate parameters to be passed to Heat template. This method performs
816 * 1. Apply default values to parameters which have them defined
817 * 2. Report any required parameters that are missing. This will generate an
818 * exception in the caller, since stack create/update operations would fail.
819 * 3. Report and remove any extraneous parameters. This will allow clients to
820 * pass supersets of parameters and not get errors.
822 * These functions depend on the HeatTemplate definition from the MSO Catalog DB,
823 * along with the input parameter Map. The output is an updated parameter map.
824 * If the parameters are invalid for the template, an IllegalArgumentException
827 public Map <String, Object> validateStackParams (Map <String, Object> inputParams,
828 HeatTemplate heatTemplate) {
829 // Check that required parameters have been supplied for this template type
830 StringBuilder missingParams = null;
831 List <String> paramList = new ArrayList <> ();
833 // TODO: Enhance DB to support defaults for Heat Template parameters
835 for (HeatTemplateParam parm : heatTemplate.getParameters ()) {
836 if (parm.isRequired () && !inputParams.containsKey (parm.getParamName ())) {
837 if (missingParams == null) {
838 missingParams = new StringBuilder(parm.getParamName());
840 missingParams.append("," + parm.getParamName());
843 paramList.add (parm.getParamName ());
845 if (missingParams != null) {
846 // Problem - missing one or more required parameters
847 String error = "Missing Required inputs for HEAT Template: " + missingParams;
848 logger.error("{} for HEAT Template {} Missing Required inputs for HEAT Template: {}",
849 MessageEnum.RA_MISSING_PARAM, MsoLogger.ErrorCode.SchemaError.getValue(), missingParams);
850 throw new IllegalArgumentException (error);
853 // Remove any extraneous parameters (don't throw an error)
854 Map <String, Object> updatedParams = new HashMap <> ();
855 List <String> extraParams = new ArrayList <> ();
857 for (Entry<String, Object> entry : inputParams.entrySet()) {
858 if (!paramList.contains(entry.getKey())) {
859 // This is not a valid parameter for this template
860 extraParams.add(entry.getKey());
862 updatedParams.put(entry.getKey(), entry.getValue());
866 if (!extraParams.isEmpty ()) {
867 logger.warn("{} Heat Stack ({}) extra input params received: {} {}", MessageEnum.RA_GENERAL_WARNING,
868 heatTemplate.getTemplateName(), extraParams, MsoLogger.ErrorCode.DataError.getValue());
871 return updatedParams;
874 // ---------------------------------------------------------------
875 // PRIVATE FUNCTIONS FOR USE WITHIN THIS CLASS
878 * Get a Heat client for the Openstack Identity service.
879 * This requires a 'member'-level userId + password, which will be retrieved from
880 * properties based on the specified cloud Id. The tenant in which to operate
881 * must also be provided.
883 * On successful authentication, the Heat object will be cached for the
884 * tenantID + cloudId so that it can be reused without reauthenticating with
885 * Openstack every time.
887 * @return an authenticated Heat object
889 public Heat getHeatClient (CloudSite cloudSite, String tenantId) throws MsoException {
890 String cloudId = cloudSite.getId();
891 // For DCP/LCP, the region should be the cloudId.
892 String region = cloudSite.getRegionId ();
894 // Obtain an MSO token for the tenant
895 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
896 logger.debug("Found: {}", cloudIdentity.toString());
897 MsoTenantUtils tenantUtils = tenantUtilsFactory.getTenantUtilsByServerType(cloudIdentity.getIdentityServerType());
898 String keystoneUrl = tenantUtils.getKeystoneUrl(cloudId, cloudIdentity);
899 logger.debug("keystoneUrl={}", keystoneUrl);
900 String heatUrl = null;
901 String tokenId = null;
902 Calendar expiration = null;
904 if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
905 Keystone keystoneTenantClient = new Keystone (keystoneUrl);
906 Access access = null;
908 Authentication credentials = authenticationMethodFactory.getAuthenticationFor(cloudIdentity);
910 OpenStackRequest <Access> request = keystoneTenantClient.tokens ()
911 .authenticate (credentials).withTenantId (tenantId);
913 access = executeAndRecordOpenstackRequest (request);
916 // Isolate trying to printout the region IDs
918 logger.debug("access={}", access.toString());
919 for (Access.Service service : access.getServiceCatalog()) {
920 List<Access.Service.Endpoint> endpoints = service.getEndpoints();
921 for (Access.Service.Endpoint endpoint : endpoints) {
922 logger.debug("AIC returned region={}", endpoint.getRegion());
925 } catch (Exception e) {
926 logger.debug("Encountered an error trying to printout Access object returned from AIC. {}",
929 heatUrl = KeystoneUtils.findEndpointURL (access.getServiceCatalog (), "orchestration", region, "public");
930 logger.debug("heatUrl={}, region={}", heatUrl, region);
931 } catch (RuntimeException e) {
932 // This comes back for not found (probably an incorrect region ID)
933 String error = "AIC did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
934 throw new MsoAdapterException (error, e);
936 tokenId = access.getToken ().getId ();
937 expiration = access.getToken ().getExpires ();
938 } else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
940 KeystoneAuthHolder holder = keystoneV3Authentication.getToken(cloudSite, tenantId, "orchestration");
941 tokenId = holder.getId();
942 expiration = holder.getexpiration();
943 heatUrl = holder.getServiceUrl();
944 } catch (ServiceEndpointNotFoundException e) {
945 // This comes back for not found (probably an incorrect region ID)
946 String error = "cloud did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
947 throw new MsoAdapterException (error, e);
950 } catch (OpenStackResponseException e) {
951 if (e.getStatus () == 401) {
952 // Authentication error.
953 String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId ();
955 throw new MsoAdapterException (error);
957 throw keystoneErrorToMsoException (e, TOKEN_AUTH);
959 } catch (OpenStackConnectException e) {
960 // Connection to Openstack failed
961 MsoIOException me = new MsoIOException (e.getMessage (), e);
962 me.addContext (TOKEN_AUTH);
964 } catch (RuntimeException e) {
966 throw runtimeExceptionToMsoException (e, TOKEN_AUTH);
968 Heat heatClient = new Heat (heatUrl);
969 heatClient.token (tokenId);
974 * Query for a Heat Stack. This function is needed in several places, so
975 * a common method is useful. This method takes an authenticated Heat Client
976 * (which internally identifies the cloud & tenant to search), and returns
977 * a Stack object if found, Null if not found, or an MsoOpenstackException
978 * if the Openstack API call fails.
980 * The stack name may be a simple name or a canonical name ("{name}/{id}").
981 * When simple name is used, Openstack always returns a 302 redirect which
982 * results in a 2nd request (to the canonical name). Note that query by
983 * canonical name for a deleted stack returns a Stack object with status
984 * "DELETE_COMPLETE" while query by simple name for a deleted stack returns
987 * @param heatClient an authenticated Heat client
989 * @param stackName the stack name to query
991 * @return a Stack object that describes the current stack or null if the
992 * requested stack doesn't exist.
994 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
996 protected Stack queryHeatStack (Heat heatClient, String stackName) throws MsoException {
997 if (stackName == null) {
1001 OpenStackRequest <Stack> request = heatClient.getStacks ().byName (stackName);
1002 return executeAndRecordOpenstackRequest (request);
1003 } catch (OpenStackResponseException e) {
1004 logger.error("Error in Query Stack", e);
1005 if (e.getStatus () == 404) {
1006 logger.debug ("queryHeatStack - stack not found: {}", stackName);
1009 // Convert the OpenStackResponseException to an MsoOpenstackException
1010 throw heatExceptionToMsoException (e, "QueryStack");
1012 } catch (OpenStackConnectException e) {
1013 // Connection to Openstack failed
1014 throw heatExceptionToMsoException (e, "QueryAllStack");
1019 public Map<String, Object> queryStackForOutputs(String cloudSiteId,
1020 String tenantId, String stackName) throws MsoException {
1021 logger.debug("MsoHeatUtils.queryStackForOutputs)");
1022 StackInfo heatStack = this.queryStack(cloudSiteId, tenantId, stackName);
1023 if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
1026 return heatStack.getOutputs();
1029 public void copyStringOutputsToInputs(Map<String, Object> inputs,
1030 Map<String, Object> otherStackOutputs, boolean overWrite) {
1031 if (inputs == null || otherStackOutputs == null)
1033 for (String key : otherStackOutputs.keySet()) {
1034 if (!inputs.containsKey(key)) {
1035 Object obj = otherStackOutputs.get(key);
1036 if (obj instanceof String) {
1037 inputs.put(key, (String) otherStackOutputs.get(key));
1038 } else if (obj instanceof JsonNode ){
1039 // This is a bit of mess - but I think it's the least impacting
1040 // let's convert it BACK to a string - then it will get converted back later
1042 String str = this.convertNode((JsonNode) obj);
1043 inputs.put(key, str);
1044 } catch (Exception e) {
1045 logger.debug("DANGER WILL ROBINSON: unable to convert value for JsonNode {} ", key, e);
1046 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1048 } else if (obj instanceof java.util.LinkedHashMap) {
1049 logger.debug("LinkedHashMap - this is showing up as a LinkedHashMap instead of JsonNode");
1051 String str = JSON_MAPPER.writeValueAsString(obj);
1052 inputs.put(key, str);
1053 } catch (Exception e) {
1054 logger.debug("DANGER WILL ROBINSON: unable to convert value for LinkedHashMap {} ", key, e);
1056 } else if (obj instanceof Integer) {
1058 String str = "" + obj;
1059 inputs.put(key, str);
1060 } catch (Exception e) {
1061 logger.debug("DANGER WILL ROBINSON: unable to convert value for Integer {} ", key, e);
1065 String str = obj.toString();
1066 inputs.put(key, str);
1067 } catch (Exception e) {
1068 logger.debug("DANGER WILL ROBINSON: unable to convert value for Other {} ({}) ", key, e.getMessage(), e);
1069 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1076 public StringBuilder requestToStringBuilder(CreateStackParam stack) {
1077 StringBuilder sb = new StringBuilder();
1078 sb.append("Stack:\n");
1079 sb.append("\tStackName: " + stack.getStackName());
1080 sb.append("\tTemplateUrl: " + stack.getTemplateUrl());
1081 sb.append("\tTemplate: " + stack.getTemplate());
1082 sb.append("\tEnvironment: " + stack.getEnvironment());
1083 sb.append("\tTimeout: " + stack.getTimeoutMinutes());
1084 sb.append("\tParameters:\n");
1085 Map<String, Object> params = stack.getParameters();
1086 if (params == null || params.size() < 1) {
1087 sb.append("\nNONE");
1089 for (String key : params.keySet()) {
1090 if (params.get(key) instanceof String) {
1091 sb.append("\n").append(key).append("=").append((String) params.get(key));
1092 } else if (params.get(key) instanceof JsonNode) {
1093 String jsonStringOut = this.convertNode((JsonNode)params.get(key));
1094 sb.append("\n").append(key).append("=").append(jsonStringOut);
1095 } else if (params.get(key) instanceof Integer) {
1096 String integerOut = "" + params.get(key);
1097 sb.append("\n").append(key).append("=").append(integerOut);
1101 String str = params.get(key).toString();
1102 sb.append("\n").append(key).append("=").append(str);
1103 } catch (Exception e) {
1104 logger.debug("Exception :", e);
1112 private String convertNode(final JsonNode node) {
1114 final Object obj = JSON_MAPPER.treeToValue(node, Object.class);
1115 final String json = JSON_MAPPER.writeValueAsString(obj);
1117 } catch (Exception e) {
1118 logger.debug("Error converting json to string {} ", e.getMessage(), e);
1120 return "[Error converting json to string]";
1124 protected StringBuilder getOutputsAsStringBuilder(Stack heatStack) {
1125 // This should only be used as a utility to print out the stack outputs
1127 StringBuilder sb = new StringBuilder("");
1128 if (heatStack == null) {
1129 sb.append("(heatStack is null)");
1132 List<Output> outputList = heatStack.getOutputs();
1133 if (outputList == null || outputList.isEmpty()) {
1134 sb.append("(outputs is empty)");
1137 Map<String, Object> outputs = new HashMap<>();
1138 for (Output outputItem : outputList) {
1139 outputs.put(outputItem.getOutputKey(), outputItem.getOutputValue());
1142 sb.append("OUTPUTS:\n");
1143 for (String key : outputs.keySet()) {
1144 sb.append("outputs[").append(counter++).append("]: ").append(key).append("=");
1145 Object obj = outputs.get(key);
1146 if (obj instanceof String) {
1147 sb.append((String) obj).append(" (a string)");
1148 } else if (obj instanceof JsonNode) {
1149 sb.append(this.convertNode((JsonNode) obj)).append(" (a JsonNode)");
1150 } else if (obj instanceof java.util.LinkedHashMap) {
1152 String str = JSON_MAPPER.writeValueAsString(obj);
1153 sb.append(str).append(" (a java.util.LinkedHashMap)");
1154 } catch (Exception e) {
1155 logger.debug("Exception :", e);
1156 sb.append("(a LinkedHashMap value that would not convert nicely)");
1158 } else if (obj instanceof Integer) {
1161 str = obj.toString() + " (an Integer)\n";
1162 } catch (Exception e) {
1163 logger.debug("Exception :", e);
1164 str = "(an Integer unable to call .toString() on)";
1167 } else if (obj instanceof ArrayList) {
1170 str = obj.toString() + " (an ArrayList)";
1171 } catch (Exception e) {
1172 logger.debug("Exception :", e);
1173 str = "(an ArrayList unable to call .toString() on?)";
1176 } else if (obj instanceof Boolean) {
1179 str = obj.toString() + " (a Boolean)";
1180 } catch (Exception e) {
1181 logger.debug("Exception :", e);
1182 str = "(an Boolean unable to call .toString() on?)";
1189 str = obj.toString() + " (unknown Object type)";
1190 } catch (Exception e) {
1191 logger.debug("Exception :", e);
1192 str = "(a value unable to call .toString() on?)";
1203 public void copyBaseOutputsToInputs(Map<String, Object> inputs,
1204 Map<String, Object> otherStackOutputs, List<String> paramNames, Map<String, String> aliases) {
1205 if (inputs == null || otherStackOutputs == null)
1207 for (String key : otherStackOutputs.keySet()) {
1208 if (paramNames != null) {
1209 if (!paramNames.contains(key) && !aliases.containsKey(key)) {
1210 logger.debug("\tParameter {} is NOT defined to be in the template - do not copy to inputs", key);
1213 if (aliases.containsKey(key)) {
1214 logger.debug("Found an alias! Will move {} to {}", key, aliases.get(key));
1215 Object obj = otherStackOutputs.get(key);
1216 key = aliases.get(key);
1217 otherStackOutputs.put(key, obj);
1220 if (!inputs.containsKey(key)) {
1221 Object obj = otherStackOutputs.get(key);
1222 logger.debug("\t**Adding {} to inputs (.toString()={}", key, obj.toString());
1223 if (obj instanceof String) {
1224 logger.debug("\t\t**A String");
1225 inputs.put(key, obj);
1226 } else if (obj instanceof Integer) {
1227 logger.debug("\t\t**An Integer");
1228 inputs.put(key, obj);
1229 } else if (obj instanceof JsonNode) {
1230 logger.debug("\t\t**A JsonNode");
1231 inputs.put(key, obj);
1232 } else if (obj instanceof Boolean) {
1233 logger.debug("\t\t**A Boolean");
1234 inputs.put(key, obj);
1235 } else if (obj instanceof java.util.LinkedHashMap) {
1236 logger.debug("\t\t**A java.util.LinkedHashMap **");
1237 inputs.put(key, obj);
1238 } else if (obj instanceof java.util.ArrayList) {
1239 logger.debug("\t\t**An ArrayList");
1240 inputs.put(key, obj);
1242 logger.debug("\t\t**UNKNOWN OBJECT TYPE");
1243 inputs.put(key, obj);
1246 logger.debug("key={} is already in the inputs - will not overwrite", key);
1252 public List<String> convertCdlToArrayList(String cdl) {
1253 String cdl2 = cdl.trim();
1255 if (cdl2.startsWith("[") && cdl2.endsWith("]")) {
1256 cdl3 = cdl2.substring(1, cdl2.lastIndexOf("]"));
1260 return new ArrayList<>(Arrays.asList(cdl3.split(",")));
1264 * New with 1707 - this method will convert all the String *values* of the inputs
1265 * to their "actual" object type (based on the param type: in the db - which comes from the template):
1266 * (heat variable type) -> java Object type
1269 * json -> marshal object to json
1270 * comma_delimited_list -> ArrayList
1271 * boolean -> Boolean
1272 * if any of the conversions should fail, we will default to adding it to the inputs
1273 * as a string - see if Openstack can handle it.
1274 * Also, will remove any params that are extra.
1275 * Any aliases will be converted to their appropriate name (anyone use this feature?)
1276 * @param inputs - the Map<String, String> of the inputs received on the request
1277 * @param template the HeatTemplate object - this is so we can also verify if the param is valid for this template
1278 * @return HashMap<String, Object> of the inputs, cleaned and converted
1280 public Map<String, Object> convertInputMap(Map<String, Object> inputs, HeatTemplate template) {
1281 HashMap<String, Object> newInputs = new HashMap<>();
1282 HashMap<String, HeatTemplateParam> params = new HashMap<>();
1283 HashMap<String, HeatTemplateParam> paramAliases = new HashMap<>();
1285 if (inputs == null) {
1286 logger.debug("convertInputMap - inputs is null - nothing to do here");
1287 return new HashMap<>();
1290 logger.debug("convertInputMap in MsoHeatUtils called, with {} inputs, and template {}", inputs.size(),
1291 template.getArtifactUuid());
1293 logger.debug(template.toString());
1294 Set<HeatTemplateParam> paramSet = template.getParameters();
1295 logger.debug("paramSet has {} entries", paramSet.size());
1296 } catch (Exception e) {
1297 logger.debug("Exception occurred in convertInputMap:" + e.getMessage(), e);
1300 for (HeatTemplateParam htp : template.getParameters()) {
1301 logger.debug("Adding {}", htp.getParamName());
1302 params.put(htp.getParamName(), htp);
1303 if (htp.getParamAlias() != null && !"".equals(htp.getParamAlias())) {
1304 logger.debug("\tFound ALIAS {} -> {}", htp.getParamName(), htp.getParamAlias());
1305 paramAliases.put(htp.getParamAlias(), htp);
1308 logger.debug("Now iterate through the inputs...");
1309 for (String key : inputs.keySet()) {
1310 logger.debug("key={}", key);
1311 boolean alias = false;
1312 String realName = null;
1313 if (!params.containsKey(key)) {
1314 logger.debug("{} is not a parameter in the template! - check for an alias", key);
1315 // add check here for an alias
1316 if (!paramAliases.containsKey(key)) {
1317 logger.debug("The parameter {} is in the inputs, but it's not a parameter for this template - omit", key);
1321 realName = paramAliases.get(key).getParamName();
1322 logger.debug("FOUND AN ALIAS! Will use {} in lieu of give key/alias {}", realName, key);
1325 String type = params.get(key).getParamType();
1326 if (type == null || "".equals(type)) {
1327 logger.debug("**PARAM_TYPE is null/empty for {}, will default to string", key);
1330 logger.debug("Parameter: {} is of type {}", key, type);
1331 if ("string".equalsIgnoreCase(type)) {
1333 String str = inputs.get(key) != null ? inputs.get(key).toString() : null;
1335 newInputs.put(realName, str);
1337 newInputs.put(key, str);
1338 } else if ("number".equalsIgnoreCase(type)) {
1339 String integerString = inputs.get(key) != null ? inputs.get(key).toString() : null;
1340 Integer anInteger = null;
1342 anInteger = Integer.parseInt(integerString);
1343 } catch (Exception e) {
1344 logger.debug("Unable to convert {} to an integer!!", integerString, e);
1347 if (anInteger != null) {
1349 newInputs.put(realName, anInteger);
1351 newInputs.put(key, anInteger);
1355 newInputs.put(realName, integerString);
1357 newInputs.put(key, integerString);
1359 } else if ("json".equalsIgnoreCase(type)) {
1360 Object jsonObj = inputs.get(key);
1363 if (jsonObj instanceof String) {
1364 json = JSON_MAPPER.readTree(jsonObj.toString());
1366 //will already marshal to json without intervention
1369 } catch (IOException e) {
1370 logger.error("failed to map to json, directly converting to string instead", e);
1371 json = jsonObj.toString();
1374 newInputs.put(realName, json);
1376 newInputs.put(key, json);
1377 } else if ("comma_delimited_list".equalsIgnoreCase(type)) {
1378 String commaSeparated = inputs.get(key) != null ? inputs.get(key).toString() : null;
1380 List<String> anArrayList = this.convertCdlToArrayList(commaSeparated);
1382 newInputs.put(realName, anArrayList);
1384 newInputs.put(key, anArrayList);
1385 } catch (Exception e) {
1386 logger.debug("Unable to convert {} to an ArrayList!!", commaSeparated, e);
1388 newInputs.put(realName, commaSeparated);
1390 newInputs.put(key, commaSeparated);
1392 } else if ("boolean".equalsIgnoreCase(type)) {
1393 String booleanString = inputs.get(key) != null ? inputs.get(key).toString() : null;
1394 Boolean aBool = Boolean.valueOf(booleanString);
1396 newInputs.put(realName, aBool);
1398 newInputs.put(key, aBool);
1400 // it's null or something undefined - just add it back as a String
1401 String str = inputs.get(key).toString();
1403 newInputs.put(realName, str);
1405 newInputs.put(key, str);
1412 * This helpful method added for Valet
1414 public String getCloudSiteKeystoneUrl(String cloudSiteId) throws MsoCloudSiteNotFound {
1415 String keystone_url = null;
1417 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1418 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
1419 keystone_url = cloudIdentity.getIdentityUrl();
1420 } catch (Exception e) {
1421 throw new MsoCloudSiteNotFound(cloudSiteId);
1423 if (keystone_url == null || keystone_url.isEmpty()) {
1424 throw new MsoCloudSiteNotFound(cloudSiteId);
1426 return keystone_url;
1430 * Create a string suitable for being dumped to a debug log that creates a
1431 * pseudo-JSON request dumping what's being sent to Openstack API in the create or update request
1434 private String printStackRequest(String tenantId,
1435 Map<String, Object> heatFiles,
1436 Map<String, Object> nestedTemplates,
1438 Map<String, Object> inputs,
1439 String vfModuleName,
1443 String cloudSiteId) {
1444 StringBuilder sb = new StringBuilder();
1445 sb.append("CREATE STACK REQUEST (formatted for readability)\n");
1446 sb.append("tenant=" + tenantId + ", cloud=" + cloudSiteId);
1448 sb.append(" \"stack_name\": \"" + vfModuleName + "\",\n");
1449 sb.append(" \"disable_rollback\": " + backout + ",\n");
1450 sb.append(" \"timeout_mins\": " + timeoutMinutes + ",\n");
1451 sb.append(" \"template\": {\n");
1452 sb.append(template);
1454 sb.append(" \"environment\": {\n");
1455 if (environment == null)
1456 sb.append("<none>");
1458 sb.append(environment);
1460 sb.append(" \"files\": {\n");
1461 int filesCounter = 0;
1462 if (heatFiles != null) {
1463 for (String key : heatFiles.keySet()) {
1465 if (filesCounter > 1) {
1468 sb.append(" \"" + key + "\": {\n");
1469 sb.append(heatFiles.get(key).toString() + "\n }");
1472 if (nestedTemplates != null) {
1473 for (String key : nestedTemplates.keySet()) {
1475 if (filesCounter > 1) {
1478 sb.append(" \"" + key + "\": {\n");
1479 sb.append(nestedTemplates.get(key).toString() + "\n }");
1482 sb.append("\n },\n");
1483 sb.append(" \"parameters\": {\n");
1484 int paramCounter = 0;
1485 for (String name : inputs.keySet()) {
1487 if (paramCounter > 1) {
1490 Object o = inputs.get(name);
1491 if (o instanceof java.lang.String) {
1492 sb.append(" \"" + name + "\": \"" + inputs.get(name).toString() + "\"");
1493 } else if (o instanceof Integer) {
1494 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1495 } else if (o instanceof ArrayList) {
1496 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1497 } else if (o instanceof Boolean) {
1498 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1500 sb.append(" \"" + name + "\": " + "\"(there was an issue trying to dump this value...)\"" );
1503 sb.append("\n }\n}\n");
1505 return sb.toString();
1508 /*******************************************************************************
1510 * Methods (and associated utilities) to implement the VduPlugin interface
1512 *******************************************************************************/
1515 * VduPlugin interface for instantiate function.
1517 * Translate the VduPlugin parameters to the corresponding 'createStack' parameters,
1518 * and then invoke the existing function.
1521 public VduInstance instantiateVdu (
1522 CloudInfo cloudInfo,
1523 String instanceName,
1524 Map<String,Object> inputs,
1525 VduModelInfo vduModel,
1526 boolean rollbackOnFailure)
1529 String cloudSiteId = cloudInfo.getCloudSiteId();
1530 String tenantId = cloudInfo.getTenantId();
1532 // Translate the VDU ModelInformation structure to that which is needed for
1533 // creating the Heat stack. Loop through the artifacts, looking specifically
1534 // for MAIN_TEMPLATE and ENVIRONMENT. Any other artifact will
1535 // be attached as a FILE.
1536 String heatTemplate = null;
1537 Map<String,Object> nestedTemplates = new HashMap<>();
1538 Map<String,Object> files = new HashMap<>();
1539 String heatEnvironment = null;
1541 for (VduArtifact vduArtifact: vduModel.getArtifacts()) {
1542 if (vduArtifact.getType() == ArtifactType.MAIN_TEMPLATE) {
1543 heatTemplate = new String(vduArtifact.getContent());
1545 else if (vduArtifact.getType() == ArtifactType.NESTED_TEMPLATE) {
1546 nestedTemplates.put(vduArtifact.getName(), new String(vduArtifact.getContent()));
1548 else if (vduArtifact.getType() == ArtifactType.ENVIRONMENT) {
1549 heatEnvironment = new String(vduArtifact.getContent());
1554 StackInfo stackInfo = createStack (cloudSiteId,
1559 true, // poll for completion
1560 vduModel.getTimeoutMinutes(),
1566 // Populate a vduInstance from the StackInfo
1567 return stackInfoToVduInstance(stackInfo);
1569 catch (Exception e) {
1570 throw new VduException ("MsoHeatUtils (instantiateVDU): createStack Exception", e);
1576 * VduPlugin interface for query function.
1579 public VduInstance queryVdu (CloudInfo cloudInfo, String instanceId)
1582 String cloudSiteId = cloudInfo.getCloudSiteId();
1583 String tenantId = cloudInfo.getTenantId();
1586 // Query the Cloudify Deployment object and populate a VduInstance
1587 StackInfo stackInfo = queryStack (cloudSiteId, tenantId, instanceId);
1589 return stackInfoToVduInstance(stackInfo);
1591 catch (Exception e) {
1592 throw new VduException ("MsoHeatUtile (queryVdu): queryStack Exception ", e);
1598 * VduPlugin interface for delete function.
1601 public VduInstance deleteVdu (CloudInfo cloudInfo, String instanceId, int timeoutMinutes)
1604 String cloudSiteId = cloudInfo.getCloudSiteId();
1605 String tenantId = cloudInfo.getTenantId();
1608 // Delete the Heat stack
1609 StackInfo stackInfo = deleteStack (tenantId, cloudSiteId, instanceId, true);
1611 // Populate a VduInstance based on the deleted Cloudify Deployment object
1612 VduInstance vduInstance = stackInfoToVduInstance(stackInfo);
1614 // Override return state to DELETED (HeatUtils sets to NOTFOUND)
1615 vduInstance.getStatus().setState(VduStateType.DELETED);
1619 catch (Exception e) {
1620 throw new VduException ("Delete VDU Exception", e);
1626 * VduPlugin interface for update function.
1628 * Update is currently not supported in the MsoHeatUtils implementation of VduPlugin.
1629 * Just return a VduException.
1633 public VduInstance updateVdu (
1634 CloudInfo cloudInfo,
1636 Map<String,Object> inputs,
1637 VduModelInfo vduModel,
1638 boolean rollbackOnFailure)
1641 throw new VduException ("MsoHeatUtils: updateVdu interface not supported");
1646 * Convert the local DeploymentInfo object (Cloudify-specific) to a generic VduInstance object
1648 protected VduInstance stackInfoToVduInstance (StackInfo stackInfo)
1650 VduInstance vduInstance = new VduInstance();
1652 // The full canonical name as the instance UUID
1653 vduInstance.setVduInstanceId(stackInfo.getCanonicalName());
1654 vduInstance.setVduInstanceName(stackInfo.getName());
1656 // Copy inputs and outputs
1657 vduInstance.setInputs(stackInfo.getParameters());
1658 vduInstance.setOutputs(stackInfo.getOutputs());
1660 // Translate the status elements
1661 vduInstance.setStatus(stackStatusToVduStatus (stackInfo));
1666 private VduStatus stackStatusToVduStatus (StackInfo stackInfo)
1668 VduStatus vduStatus = new VduStatus();
1670 // Map the status fields to more generic VduStatus.
1671 // There are lots of HeatStatus values, so this is a bit long...
1672 HeatStatus heatStatus = stackInfo.getStatus();
1673 String statusMessage = stackInfo.getStatusMessage();
1675 if (heatStatus == HeatStatus.INIT || heatStatus == HeatStatus.BUILDING) {
1676 vduStatus.setState(VduStateType.INSTANTIATING);
1677 vduStatus.setLastAction((new PluginAction ("create", "in_progress", statusMessage)));
1679 else if (heatStatus == HeatStatus.NOTFOUND) {
1680 vduStatus.setState(VduStateType.NOTFOUND);
1682 else if (heatStatus == HeatStatus.CREATED) {
1683 vduStatus.setState(VduStateType.INSTANTIATED);
1684 vduStatus.setLastAction((new PluginAction ("create", "complete", statusMessage)));
1686 else if (heatStatus == HeatStatus.UPDATED) {
1687 vduStatus.setState(VduStateType.INSTANTIATED);
1688 vduStatus.setLastAction((new PluginAction ("update", "complete", statusMessage)));
1690 else if (heatStatus == HeatStatus.UPDATING) {
1691 vduStatus.setState(VduStateType.UPDATING);
1692 vduStatus.setLastAction((new PluginAction ("update", "in_progress", statusMessage)));
1694 else if (heatStatus == HeatStatus.DELETING) {
1695 vduStatus.setState(VduStateType.DELETING);
1696 vduStatus.setLastAction((new PluginAction ("delete", "in_progress", statusMessage)));
1698 else if (heatStatus == HeatStatus.FAILED) {
1699 vduStatus.setState(VduStateType.FAILED);
1700 vduStatus.setErrorMessage(stackInfo.getStatusMessage());
1702 vduStatus.setState(VduStateType.UNKNOWN);
1708 public Resources queryStackResources(String cloudSiteId, String tenantId, String stackName) throws MsoException {
1709 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1710 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1711 Heat heatClient = getHeatClient(cloudSite, tenantId);
1712 OpenStackRequest<Resources> request = heatClient.getResources().listResources(stackName);
1713 return executeAndRecordOpenstackRequest(request);
1716 public <R> R executeHeatClientRequest(String url, String cloudSiteId, String tenantId, Class<R> returnType) throws MsoException {
1717 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1718 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1719 Heat heatClient = getHeatClient(cloudSite, tenantId);
1720 OpenStackRequest<R> request = heatClient.get(url, returnType);
1721 return executeAndRecordOpenstackRequest(request);
1724 protected void sleep(long time) {
1727 } catch (InterruptedException e) {
1728 logger.debug ("Thread interrupted while sleeping", e);
1729 Thread.currentThread().interrupt();