2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.so.openstack.utils;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Calendar;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.Optional;
35 import org.onap.so.adapters.vdu.CloudInfo;
36 import org.onap.so.adapters.vdu.PluginAction;
37 import org.onap.so.adapters.vdu.VduArtifact;
38 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
39 import org.onap.so.adapters.vdu.VduException;
40 import org.onap.so.adapters.vdu.VduInstance;
41 import org.onap.so.adapters.vdu.VduModelInfo;
42 import org.onap.so.adapters.vdu.VduPlugin;
43 import org.onap.so.adapters.vdu.VduStateType;
44 import org.onap.so.adapters.vdu.VduStatus;
45 import org.onap.so.cloud.CloudConfig;
46 import org.onap.so.cloud.authentication.AuthenticationMethodFactory;
47 import org.onap.so.cloud.authentication.KeystoneAuthHolder;
48 import org.onap.so.cloud.authentication.KeystoneV3Authentication;
49 import org.onap.so.cloud.authentication.ServiceEndpointNotFoundException;
50 import org.onap.so.db.catalog.beans.CloudIdentity;
51 import org.onap.so.db.catalog.beans.CloudSite;
52 import org.onap.so.db.catalog.beans.HeatTemplate;
53 import org.onap.so.db.catalog.beans.HeatTemplateParam;
54 import org.onap.so.db.catalog.beans.ServerType;
55 import org.onap.so.logger.MessageEnum;
57 import org.onap.so.logger.MsoLogger;
58 import org.onap.so.openstack.beans.HeatCacheEntry;
59 import org.onap.so.openstack.beans.HeatStatus;
60 import org.onap.so.openstack.beans.StackInfo;
61 import org.onap.so.openstack.exceptions.MsoAdapterException;
62 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
63 import org.onap.so.openstack.exceptions.MsoException;
64 import org.onap.so.openstack.exceptions.MsoIOException;
65 import org.onap.so.openstack.exceptions.MsoOpenstackException;
66 import org.onap.so.openstack.exceptions.MsoStackAlreadyExists;
67 import org.onap.so.openstack.exceptions.MsoTenantNotFound;
68 import org.onap.so.openstack.mappers.StackInfoMapper;
69 import org.onap.so.utils.CryptoUtils;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.context.annotation.Primary;
72 import org.springframework.core.env.Environment;
73 import org.springframework.stereotype.Component;
75 import com.fasterxml.jackson.core.type.TypeReference;
76 import com.fasterxml.jackson.databind.JsonNode;
77 import com.fasterxml.jackson.databind.ObjectMapper;
78 import com.woorea.openstack.base.client.OpenStackConnectException;
79 import com.woorea.openstack.base.client.OpenStackRequest;
80 import com.woorea.openstack.base.client.OpenStackResponseException;
81 import com.woorea.openstack.heat.Heat;
82 import com.woorea.openstack.heat.model.CreateStackParam;
83 import com.woorea.openstack.heat.model.Resources;
84 import com.woorea.openstack.heat.model.Stack;
85 import com.woorea.openstack.heat.model.Stack.Output;
86 import com.woorea.openstack.heat.model.Stacks;
87 import com.woorea.openstack.keystone.Keystone;
88 import com.woorea.openstack.keystone.model.Access;
89 import com.woorea.openstack.keystone.model.Authentication;
90 import com.woorea.openstack.keystone.utils.KeystoneUtils;
94 public class MsoHeatUtils extends MsoCommonUtils implements VduPlugin{
96 private static final String TOKEN_AUTH = "TokenAuth";
98 private static final String QUERY_ALL_STACKS = "QueryAllStacks";
100 private static final String DELETE_STACK = "DeleteStack";
102 protected static final String HEAT_ERROR = "HeatError";
104 protected static final String CREATE_STACK = "CreateStack";
106 // Fetch cloud configuration each time (may be cached in CloudConfig class)
108 protected CloudConfig cloudConfig;
111 private Environment environment;
114 private AuthenticationMethodFactory authenticationMethodFactory;
117 private MsoTenantUtilsFactory tenantUtilsFactory;
120 private KeystoneV3Authentication keystoneV3Authentication;
122 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, MsoHeatUtils.class);
124 // Properties names and variables (with default values)
125 protected String createPollIntervalProp = "org.onap.so.adapters.po.pollInterval";
126 private String deletePollIntervalProp = "org.onap.so.adapters.po.pollInterval";
127 private String deletePollTimeoutProp = "org.onap.so.adapters.po.pollTimeout";
129 protected static final String createPollIntervalDefault = "15";
130 private static final String deletePollIntervalDefault = "15";
132 private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
135 * keep this old method signature here to maintain backwards compatibility. keep others as well.
136 * this method does not include environment, files, or heatFiles
138 public StackInfo createStack (String cloudSiteId,
142 Map <String, ?> stackInputs,
143 boolean pollForCompletion,
144 int timeoutMinutes) throws MsoException {
145 // Just call the new method with the environment & files variable set to null
146 return this.createStack (cloudSiteId,
159 // This method has environment, but not files or heatFiles
160 public StackInfo createStack (String cloudSiteId,
164 Map <String, ?> stackInputs,
165 boolean pollForCompletion,
167 String environment) throws MsoException {
168 // Just call the new method with the files/heatFiles variables set to null
169 return this.createStack (cloudSiteId,
182 // This method has environment and files, but not heatFiles.
183 public StackInfo createStack (String cloudSiteId,
187 Map <String, ?> stackInputs,
188 boolean pollForCompletion,
191 Map <String, Object> files) throws MsoException {
192 return this.createStack (cloudSiteId,
205 // This method has environment, files, heatfiles
206 public StackInfo createStack (String cloudSiteId,
210 Map <String, ?> stackInputs,
211 boolean pollForCompletion,
214 Map <String, Object> files,
215 Map <String, Object> heatFiles) throws MsoException {
216 return this.createStack (cloudSiteId,
230 * Create a new Stack in the specified cloud location and tenant. The Heat template
231 * and parameter map are passed in as arguments, along with the cloud access credentials.
232 * It is expected that parameters have been validated and contain at minimum the required
233 * parameters for the given template with no extra (undefined) parameters..
235 * The Stack name supplied by the caller must be unique in the scope of this tenant.
236 * However, it should also be globally unique, as it will be the identifier for the
237 * resource going forward in Inventory. This latter is managed by the higher levels
238 * invoking this function.
240 * The caller may choose to let this function poll Openstack for completion of the
241 * stack creation, or may handle polling itself via separate calls to query the status.
242 * In either case, a StackInfo object will be returned containing the current status.
243 * When polling is enabled, a status of CREATED is expected. When not polling, a
244 * status of BUILDING is expected.
246 * An error will be thrown if the requested Stack already exists in the specified
249 * For 1510 - add "environment", "files" (nested templates), and "heatFiles" (get_files) as
250 * parameters for createStack. If environment is non-null, it will be added to the stack.
251 * The nested templates and get_file entries both end up being added to the "files" on the
252 * stack. We must combine them before we add them to the stack if they're both non-null.
254 * @param cloudSiteId The cloud (may be a region) in which to create the stack.
255 * @param tenantId The Openstack ID of the tenant in which to create the Stack
256 * @param stackName The name of the stack to create
257 * @param heatTemplate The Heat template
258 * @param stackInputs A map of key/value inputs
259 * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
260 * @param environment An optional yaml-format string to specify environmental parameters
261 * @param files a Map<String, Object> that lists the child template IDs (file is the string, object is an int of
263 * @param heatFiles a Map<String, Object> that lists the get_file entries (fileName, fileBody)
264 * @param backout Donot delete stack on create Failure - defaulted to True
265 * @return A StackInfo object
266 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
269 @SuppressWarnings("unchecked")
270 public StackInfo createStack (String cloudSiteId,
274 Map <String, ?> stackInputs,
275 boolean pollForCompletion,
278 Map <String, Object> files,
279 Map <String, Object> heatFiles,
280 boolean backout) throws MsoException {
282 // Take out the multicloud inputs, if present.
283 for (String key : MsoMulticloudUtils.MULTICLOUD_INPUTS) {
284 if (stackInputs.containsKey(key)) {
285 stackInputs.remove(key);
286 if (stackInputs.isEmpty()) {
292 CreateStackParam stack = createStackParam(stackName, heatTemplate, stackInputs, timeoutMinutes, environment, files, heatFiles);
294 // Obtain the cloud site information where we will create the stack
295 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
296 () -> new MsoCloudSiteNotFound(cloudSiteId));
297 LOGGER.debug("Found: " + cloudSite.toString());
298 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
299 // This could throw MsoTenantNotFound or MsoOpenstackException (both propagated)
300 Heat heatClient = getHeatClient (cloudSite, tenantId);
301 if (heatClient != null) {
302 LOGGER.debug("Found: " + heatClient.toString());
305 LOGGER.debug ("Ready to Create Stack (" + heatTemplate + ") with input params: " + stackInputs);
307 Stack heatStack = null;
309 OpenStackRequest <Stack> request = heatClient.getStacks ().create (stack);
310 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
311 request.header ("X-Auth-User", cloudIdentity.getMsoId ());
312 request.header ("X-Auth-Key", CryptoUtils.decryptCloudConfigPassword(cloudIdentity.getMsoPass ()));
313 heatStack = executeAndRecordOpenstackRequest (request);
314 } catch (OpenStackResponseException e) {
315 if (e.getStatus () == 409) {
316 MsoStackAlreadyExists me = new MsoStackAlreadyExists (stackName, tenantId, cloudSiteId);
317 me.addContext (CREATE_STACK);
320 LOGGER.debug("ERROR STATUS = " + e.getStatus() + ",\n" + e.getMessage() + "\n" + e.getLocalizedMessage());
321 throw heatExceptionToMsoException (e, CREATE_STACK);
323 } catch (OpenStackConnectException e) {
324 throw heatExceptionToMsoException (e, CREATE_STACK);
325 } catch (RuntimeException e) {
326 throw runtimeExceptionToMsoException (e, CREATE_STACK);
329 // Subsequent access by the canonical name "<stack name>/<stack-id>".
330 // Otherwise, simple query by name returns a 302 redirect.
331 // NOTE: This is specific to the v1 Orchestration API.
332 String canonicalName = stackName + "/" + heatStack.getId ();
334 if (pollForCompletion) {
335 heatStack = pollStackForCompletion(cloudSiteId, tenantId, stackName, timeoutMinutes, backout, heatClient,
336 heatStack, canonicalName);
338 // Get initial status, since it will have been null after the create.
339 heatStack = queryHeatStack (heatClient, canonicalName);
340 LOGGER.debug (heatStack.getStackStatus ());
342 return new StackInfoMapper(heatStack).map();
345 private Stack pollStackForCompletion(String cloudSiteId, String tenantId, String stackName, int timeoutMinutes,
346 boolean backout, Heat heatClient, Stack heatStack, String canonicalName)
347 throws MsoException, MsoOpenstackException {
348 int createPollInterval = Integer.parseInt(this.environment.getProperty(createPollIntervalProp, createPollIntervalDefault));
349 int pollTimeout = (timeoutMinutes * 60) + createPollInterval;
350 int deletePollInterval = createPollInterval;
351 int deletePollTimeout = pollTimeout;
352 boolean createTimedOut = false;
353 StringBuilder stackErrorStatusReason = new StringBuilder("");
354 LOGGER.debug("createPollInterval=" + createPollInterval + ", pollTimeout=" + pollTimeout);
358 heatStack = queryHeatStack (heatClient, canonicalName);
359 LOGGER.debug (heatStack.getStackStatus () + " (" + canonicalName + ")");
361 LOGGER.debug("Current stack " + this.getOutputsAsStringBuilder(heatStack).toString());
362 } catch (Exception e) {
363 LOGGER.debug("an error occurred trying to print out the current outputs of the stack", e);
366 if ("CREATE_IN_PROGRESS".equals (heatStack.getStackStatus ())) {
367 if (pollTimeout <= 0) {
368 LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName, heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Create stack timeout");
369 createTimedOut = true;
372 sleep(createPollInterval * 1000L);
373 pollTimeout -= createPollInterval;
374 LOGGER.debug("pollTimeout remaining: " + pollTimeout);
376 stackErrorStatusReason.append("Stack error (" + heatStack.getStackStatus() + "): " + heatStack.getStackStatusReason());
379 } catch (MsoException me) {
380 // Cannot query the stack status. Something is wrong.
381 // Try to roll back the stack
384 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in Create Stack, stack deletion suppressed");
389 LOGGER.debug("Create Stack error - unable to query for stack status - attempting to delete stack: " + canonicalName + " - This will likely fail and/or we won't be able to query to see if delete worked");
390 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
391 executeAndRecordOpenstackRequest (request);
392 boolean deleted = false;
395 heatStack = queryHeatStack(heatClient, canonicalName);
396 if (heatStack != null) {
397 LOGGER.debug(heatStack.getStackStatus());
398 if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
399 if (deletePollTimeout <= 0) {
400 LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
401 heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError,
402 "Rollback: DELETE stack timeout");
405 sleep(deletePollInterval * 1000L);
406 deletePollTimeout -= deletePollInterval;
408 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
409 LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
413 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
417 // assume if we can't find it - it's deleted
418 LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
423 } catch (Exception e3) {
424 // Just log this one. We will report the original exception.
425 LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack: Nested exception rolling back stack: " + e3, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack: Nested exception rolling back stack on error on query");
429 } catch (Exception e2) {
430 // Just log this one. We will report the original exception.
431 LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack: Nested exception rolling back stack: " + e2, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack: Nested exception rolling back stack");
435 // Propagate the original exception from Stack Query.
436 me.addContext (CREATE_STACK);
441 if (!"CREATE_COMPLETE".equals (heatStack.getStackStatus ())) {
442 LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack error: Polling complete with non-success status: "
443 + heatStack.getStackStatus () + ", " + heatStack.getStackStatusReason (), "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error");
445 // Rollback the stack creation, since it is in an indeterminate state.
448 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion suppressed", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error, stack deletion suppressed");
453 LOGGER.debug("Create Stack errored - attempting to DELETE stack: " + canonicalName);
454 LOGGER.debug("deletePollInterval=" + deletePollInterval + ", deletePollTimeout=" + deletePollTimeout);
455 OpenStackRequest <Void> request = heatClient.getStacks ().deleteByName (canonicalName);
456 executeAndRecordOpenstackRequest (request);
457 boolean deleted = false;
460 heatStack = queryHeatStack(heatClient, canonicalName);
461 if (heatStack != null) {
462 LOGGER.debug(heatStack.getStackStatus() + " (" + canonicalName + ")");
463 if ("DELETE_IN_PROGRESS".equals(heatStack.getStackStatus())) {
464 if (deletePollTimeout <= 0) {
465 LOGGER.error (MessageEnum.RA_CREATE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName,
466 heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError,
467 "Rollback: DELETE stack timeout");
470 sleep(deletePollInterval * 1000L);
471 deletePollTimeout -= deletePollInterval;
472 LOGGER.debug("deletePollTimeout remaining: " + deletePollTimeout);
474 } else if ("DELETE_COMPLETE".equals(heatStack.getStackStatus())){
475 LOGGER.debug("DELETE_COMPLETE for " + canonicalName);
478 } else if ("DELETE_FAILED".equals(heatStack.getStackStatus())) {
479 // Warn about this (?) - but still throw the original exception
480 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, stack deletion FAILED", "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Create Stack error, stack deletion FAILED");
481 LOGGER.debug("Stack deletion FAILED on a rollback of a create - " + canonicalName + ", status=" + heatStack.getStackStatus() + ", reason=" + heatStack.getStackStatusReason());
484 //got a status other than DELETE_IN_PROGRESS or DELETE_COMPLETE - so break and evaluate
488 // assume if we can't find it - it's deleted
489 LOGGER.debug("heatStack returned null - assume the stack " + canonicalName + " has been deleted");
494 } catch (MsoException me2) {
495 // We got an exception on the delete - don't throw this exception - throw the original - just log.
496 LOGGER.debug("Exception thrown trying to delete " + canonicalName + " on a create->rollback: " + me2.getContextMessage(), me2);
497 LOGGER.warn(MessageEnum.RA_CREATE_STACK_ERR, "Create Stack errored, then stack deletion FAILED - exception thrown", "", "", MsoLogger.ErrorCode.BusinessProcesssError, me2.getContextMessage());
500 } // end while !deleted
501 StringBuilder errorContextMessage;
502 if (createTimedOut) {
503 errorContextMessage = new StringBuilder("Stack Creation Timeout");
505 errorContextMessage = stackErrorStatusReason;
508 errorContextMessage.append(" - stack successfully deleted");
510 errorContextMessage.append(" - encountered an error trying to delete the stack");
512 } catch (Exception e2) {
513 // shouldn't happen - but handle
514 LOGGER.error (MessageEnum.RA_CREATE_STACK_ERR, "Create Stack: Nested exception rolling back stack: " + e2, "", "", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in Create Stack: rolling back stack");
517 MsoOpenstackException me = new MsoOpenstackException(0, "", stackErrorStatusReason.toString());
518 me.addContext(CREATE_STACK);
525 * Query for a single stack (by Name) in a tenant. This call will always return a
526 * StackInfo object. If the stack does not exist, an "empty" StackInfo will be
527 * returned - containing only the stack name and a status of NOTFOUND.
529 * @param tenantId The Openstack ID of the tenant in which to query
530 * @param cloudSiteId The cloud identifier (may be a region) in which to query
531 * @param stackName The name of the stack to query (may be simple or canonical)
532 * @return A StackInfo object
533 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
535 public StackInfo queryStack (String cloudSiteId, String tenantId, String stackName) throws MsoException {
536 LOGGER.debug ("Query HEAT stack: " + stackName + " in tenant " + tenantId);
538 // Obtain the cloud site information where we will create the stack
539 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
540 () -> new MsoCloudSiteNotFound(cloudSiteId));
541 LOGGER.debug("Found: " + cloudSite.toString());
543 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
544 Heat heatClient = null;
546 heatClient = getHeatClient (cloudSite, tenantId);
547 if (heatClient != null) {
548 LOGGER.debug("Found: " + heatClient.toString());
550 } catch (MsoTenantNotFound e) {
551 // Tenant doesn't exist, so stack doesn't either
552 LOGGER.debug ("Tenant with id " + tenantId + "not found.", e);
553 return new StackInfo (stackName, HeatStatus.NOTFOUND);
554 } catch (MsoException me) {
555 // Got an Openstack error. Propagate it
556 LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Exception on Token request: " + me, "Openstack", "", MsoLogger.ErrorCode.AvailabilityError, "Connection Exception");
557 me.addContext ("QueryStack");
562 // An MsoException will propagate transparently to the caller.
563 Stack heatStack = queryHeatStack (heatClient, stackName);
565 if (heatStack == null) {
566 // Stack does not exist. Return a StackInfo with status NOTFOUND
567 return new StackInfo (stackName, HeatStatus.NOTFOUND);
570 return new StackInfoMapper(heatStack).map();
574 * Delete a stack (by Name/ID) in a tenant. If the stack is not found, it will be
575 * considered a successful deletion. The return value is a StackInfo object which
576 * contains the current stack status.
578 * The client may choose to let the adapter poll Openstack for completion of the
579 * stack deletion, or may handle polling itself via separate query calls. In either
580 * case, a StackInfo object will be returned. When polling is enabled, a final
581 * status of NOTFOUND is expected. When not polling, a status of DELETING is expected.
583 * There is no rollback from a successful stack deletion. A deletion failure will
584 * also result in an undefined stack state - the components may or may not have been
585 * all or partially deleted, so the resulting stack must be considered invalid.
587 * @param tenantId The Openstack ID of the tenant in which to perform the delete
588 * @param cloudSiteId The cloud identifier (may be a region) from which to delete the stack.
589 * @param stackName The name/id of the stack to delete. May be simple or canonical
590 * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
591 * @return A StackInfo object
592 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
593 * @throws MsoCloudSiteNotFound
595 public StackInfo deleteStack (String tenantId,
598 boolean pollForCompletion) throws MsoException {
599 // Obtain the cloud site information where we will create the stack
600 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
601 () -> new MsoCloudSiteNotFound(cloudSiteId));
602 LOGGER.debug("Found: " + cloudSite.toString());
604 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
605 Heat heatClient = null;
607 heatClient = getHeatClient (cloudSite, tenantId);
608 if (heatClient != null) {
609 LOGGER.debug("Found: " + heatClient.toString());
611 } catch (MsoTenantNotFound e) {
612 // Tenant doesn't exist, so stack doesn't either
613 LOGGER.debug ("Tenant with id " + tenantId + "not found.", e);
614 return new StackInfo (stackName, HeatStatus.NOTFOUND);
615 } catch (MsoException me) {
616 // Got an Openstack error. Propagate it
617 LOGGER.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack", "Openstack Exception on Token request: " + me, "Openstack", "", MsoLogger.ErrorCode.AvailabilityError, "Connection Exception");
618 me.addContext (DELETE_STACK);
622 // OK if stack not found, perform a query first
623 Stack heatStack = queryHeatStack (heatClient, stackName);
624 if (heatStack == null || "DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
625 // Not found. Return a StackInfo with status NOTFOUND
626 return new StackInfo (stackName, HeatStatus.NOTFOUND);
631 // Use canonical name "<stack name>/<stack-id>" to delete.
632 // Otherwise, deletion by name returns a 302 redirect.
633 // NOTE: This is specific to the v1 Orchestration API.
634 String canonicalName = heatStack.getStackName () + "/" + heatStack.getId ();
637 OpenStackRequest <Void> request = null;
638 if(null != heatClient) {
639 request = heatClient.getStacks ().deleteByName (canonicalName);
642 LOGGER.debug ("Heat Client is NULL" );
645 executeAndRecordOpenstackRequest (request);
646 } catch (OpenStackResponseException e) {
647 if (e.getStatus () == 404) {
648 // Not found. We are OK with this. Return a StackInfo with status NOTFOUND
649 return new StackInfo (stackName, HeatStatus.NOTFOUND);
651 // Convert the OpenStackResponseException to an MsoOpenstackException
652 throw heatExceptionToMsoException (e, DELETE_STACK);
654 } catch (OpenStackConnectException e) {
655 // Error connecting to Openstack instance. Convert to an MsoException
656 throw heatExceptionToMsoException (e, DELETE_STACK);
657 } catch (RuntimeException e) {
659 throw runtimeExceptionToMsoException (e, DELETE_STACK);
662 // Requery the stack for current status.
663 // It will probably still exist with "DELETE_IN_PROGRESS" status.
664 heatStack = queryHeatStack (heatClient, canonicalName);
666 if (pollForCompletion) {
667 // Set a timeout on polling
669 int pollInterval = Integer.parseInt(this.environment.getProperty(deletePollIntervalProp, "" + deletePollIntervalDefault));
670 int pollTimeout = Integer.parseInt(this.environment.getProperty(deletePollTimeoutProp, "" + deletePollIntervalDefault));
672 // When querying by canonical name, Openstack returns DELETE_COMPLETE status
673 // instead of "404" (which would result from query by stack name).
674 while (heatStack != null && !"DELETE_COMPLETE".equals (heatStack.getStackStatus ())) {
675 LOGGER.debug ("Stack status: " + heatStack.getStackStatus ());
677 if ("DELETE_FAILED".equals (heatStack.getStackStatus ())) {
678 // Throw a 'special case' of MsoOpenstackException to report the Heat status
679 String error = "Stack delete error (" + heatStack.getStackStatus ()
681 + heatStack.getStackStatusReason ();
682 MsoOpenstackException me = new MsoOpenstackException (0, "", error);
683 me.addContext (DELETE_STACK);
685 // Alarm this condition, stack deletion failed
691 if (pollTimeout <= 0) {
692 LOGGER.error (MessageEnum.RA_DELETE_STACK_TIMEOUT, cloudSiteId, tenantId, stackName, heatStack.getStackStatus (), "", "", MsoLogger.ErrorCode.AvailabilityError, "Delete Stack Timeout");
694 // Throw a 'special case' of MsoOpenstackException to report the Heat status
695 MsoOpenstackException me = new MsoOpenstackException (0, "", "Stack Deletion Timeout");
696 me.addContext (DELETE_STACK);
698 // Alarm this condition, stack deletion failed
704 sleep(pollInterval * 1000L);
706 pollTimeout -= pollInterval;
707 LOGGER.debug("pollTimeout remaining: " + pollTimeout);
709 heatStack = queryHeatStack (heatClient, canonicalName);
712 // The stack is gone when this point is reached
713 return new StackInfo (stackName, HeatStatus.NOTFOUND);
716 // Return the current status (if not polling, the delete may still be in progress)
717 StackInfo stackInfo = new StackInfoMapper(heatStack).map();
718 stackInfo.setName (stackName);
724 * Query for all stacks in a tenant site. This call will return a List of StackInfo
725 * objects, one for each deployed stack.
727 * Note that this is limited to a single site. To ensure that a tenant is truly
728 * empty would require looping across all tenant endpoints.
730 * @param tenantId The Openstack ID of the tenant to query
731 * @param cloudSiteId The cloud identifier (may be a region) in which to query.
732 * @return A List of StackInfo objects
733 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
734 * @throws MsoCloudSiteNotFound
736 public List <StackInfo> queryAllStacks (String tenantId, String cloudSiteId) throws MsoException {
737 // Obtain the cloud site information where we will create the stack
738 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(
739 () -> new MsoCloudSiteNotFound(cloudSiteId));
740 // Get a Heat client. They are cached between calls (keyed by tenantId:cloudId)
741 Heat heatClient = getHeatClient (cloudSite, tenantId);
744 OpenStackRequest <Stacks> request = heatClient.getStacks ().list ();
745 Stacks stacks = executeAndRecordOpenstackRequest (request);
747 List <StackInfo> stackList = new ArrayList <> ();
749 // Not sure if returns an empty list or null if no stacks exist
750 if (stacks != null) {
751 for (Stack stack : stacks) {
752 stackList.add (new StackInfoMapper(stack).map());
757 } catch (OpenStackResponseException e) {
758 if (e.getStatus () == 404) {
759 // Not sure if this can happen, but return an empty list
760 LOGGER.debug ("queryAllStacks - stack not found: ");
761 return new ArrayList <> ();
763 // Convert the OpenStackResponseException to an MsoOpenstackException
764 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
766 } catch (OpenStackConnectException e) {
767 // Error connecting to Openstack instance. Convert to an MsoException
768 throw heatExceptionToMsoException (e, QUERY_ALL_STACKS);
769 } catch (RuntimeException e) {
771 throw runtimeExceptionToMsoException (e, QUERY_ALL_STACKS);
776 * Validate parameters to be passed to Heat template. This method performs
778 * 1. Apply default values to parameters which have them defined
779 * 2. Report any required parameters that are missing. This will generate an
780 * exception in the caller, since stack create/update operations would fail.
781 * 3. Report and remove any extraneous parameters. This will allow clients to
782 * pass supersets of parameters and not get errors.
784 * These functions depend on the HeatTemplate definition from the MSO Catalog DB,
785 * along with the input parameter Map. The output is an updated parameter map.
786 * If the parameters are invalid for the template, an IllegalArgumentException
789 public Map <String, Object> validateStackParams (Map <String, Object> inputParams,
790 HeatTemplate heatTemplate) {
791 // Check that required parameters have been supplied for this template type
792 StringBuilder missingParams = null;
793 List <String> paramList = new ArrayList <> ();
795 // TODO: Enhance DB to support defaults for Heat Template parameters
797 for (HeatTemplateParam parm : heatTemplate.getParameters ()) {
798 if (parm.isRequired () && !inputParams.containsKey (parm.getParamName ())) {
799 if (missingParams == null) {
800 missingParams = new StringBuilder(parm.getParamName());
802 missingParams.append("," + parm.getParamName());
805 paramList.add (parm.getParamName ());
807 if (missingParams != null) {
808 // Problem - missing one or more required parameters
809 String error = "Missing Required inputs for HEAT Template: " + missingParams;
810 LOGGER.error (MessageEnum.RA_MISSING_PARAM, missingParams + " for HEAT Template", "", "", MsoLogger.ErrorCode.SchemaError, "Missing Required inputs for HEAT Template: " + missingParams);
811 throw new IllegalArgumentException (error);
814 // Remove any extraneous parameters (don't throw an error)
815 Map <String, Object> updatedParams = new HashMap <> ();
816 List <String> extraParams = new ArrayList <> ();
818 for (Entry<String, Object> entry : inputParams.entrySet()) {
819 if (!paramList.contains(entry.getKey())) {
820 // This is not a valid parameter for this template
821 extraParams.add(entry.getKey());
823 updatedParams.put(entry.getKey(), entry.getValue());
827 if (!extraParams.isEmpty ()) {
828 LOGGER.warn (MessageEnum.RA_GENERAL_WARNING, "Heat Stack (" + heatTemplate.getTemplateName ()
829 + ") extra input params received: "
830 + extraParams, "", "", MsoLogger.ErrorCode.DataError, "Heat Stack (" + heatTemplate.getTemplateName () + ") extra input params received: "+ extraParams);
833 return updatedParams;
836 // ---------------------------------------------------------------
837 // PRIVATE FUNCTIONS FOR USE WITHIN THIS CLASS
840 * Get a Heat client for the Openstack Identity service.
841 * This requires a 'member'-level userId + password, which will be retrieved from
842 * properties based on the specified cloud Id. The tenant in which to operate
843 * must also be provided.
845 * On successful authentication, the Heat object will be cached for the
846 * tenantID + cloudId so that it can be reused without reauthenticating with
847 * Openstack every time.
849 * @return an authenticated Heat object
851 public Heat getHeatClient (CloudSite cloudSite, String tenantId) throws MsoException {
852 String cloudId = cloudSite.getId();
853 // For DCP/LCP, the region should be the cloudId.
854 String region = cloudSite.getRegionId ();
856 // Obtain an MSO token for the tenant
857 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
858 LOGGER.debug("Found: " + cloudIdentity.toString());
859 MsoTenantUtils tenantUtils = tenantUtilsFactory.getTenantUtilsByServerType(cloudIdentity.getIdentityServerType());
860 String keystoneUrl = tenantUtils.getKeystoneUrl(cloudId, cloudIdentity);
861 LOGGER.debug("keystoneUrl=" + keystoneUrl);
862 String heatUrl = null;
863 String tokenId = null;
864 Calendar expiration = null;
866 if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
867 Keystone keystoneTenantClient = new Keystone (keystoneUrl);
868 Access access = null;
870 Authentication credentials = authenticationMethodFactory.getAuthenticationFor(cloudIdentity);
872 OpenStackRequest <Access> request = keystoneTenantClient.tokens ()
873 .authenticate (credentials).withTenantId (tenantId);
875 access = executeAndRecordOpenstackRequest (request);
878 // Isolate trying to printout the region IDs
880 LOGGER.debug("access=" + access.toString());
881 for (Access.Service service : access.getServiceCatalog()) {
882 List<Access.Service.Endpoint> endpoints = service.getEndpoints();
883 for (Access.Service.Endpoint endpoint : endpoints) {
884 LOGGER.debug("AIC returned region=" + endpoint.getRegion());
887 } catch (Exception e) {
888 LOGGER.debug("Encountered an error trying to printout Access object returned from AIC. " + e.getMessage());
890 heatUrl = KeystoneUtils.findEndpointURL (access.getServiceCatalog (), "orchestration", region, "public");
891 LOGGER.debug("heatUrl=" + heatUrl + ", region=" + region);
892 } catch (RuntimeException e) {
893 // This comes back for not found (probably an incorrect region ID)
894 String error = "AIC did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
895 throw new MsoAdapterException (error, e);
897 tokenId = access.getToken ().getId ();
898 expiration = access.getToken ().getExpires ();
899 } else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
901 KeystoneAuthHolder holder = keystoneV3Authentication.getToken(cloudSite, tenantId, "orchestration");
902 tokenId = holder.getId();
903 expiration = holder.getexpiration();
904 heatUrl = holder.getServiceUrl();
905 } catch (ServiceEndpointNotFoundException e) {
906 // This comes back for not found (probably an incorrect region ID)
907 String error = "cloud did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
908 throw new MsoAdapterException (error, e);
911 } catch (OpenStackResponseException e) {
912 if (e.getStatus () == 401) {
913 // Authentication error.
914 String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId ();
916 throw new MsoAdapterException (error);
918 throw keystoneErrorToMsoException (e, TOKEN_AUTH);
920 } catch (OpenStackConnectException e) {
921 // Connection to Openstack failed
922 MsoIOException me = new MsoIOException (e.getMessage (), e);
923 me.addContext (TOKEN_AUTH);
925 } catch (RuntimeException e) {
927 throw runtimeExceptionToMsoException (e, TOKEN_AUTH);
929 Heat heatClient = new Heat (heatUrl);
930 heatClient.token (tokenId);
935 * Query for a Heat Stack. This function is needed in several places, so
936 * a common method is useful. This method takes an authenticated Heat Client
937 * (which internally identifies the cloud & tenant to search), and returns
938 * a Stack object if found, Null if not found, or an MsoOpenstackException
939 * if the Openstack API call fails.
941 * The stack name may be a simple name or a canonical name ("{name}/{id}").
942 * When simple name is used, Openstack always returns a 302 redirect which
943 * results in a 2nd request (to the canonical name). Note that query by
944 * canonical name for a deleted stack returns a Stack object with status
945 * "DELETE_COMPLETE" while query by simple name for a deleted stack returns
948 * @param heatClient an authenticated Heat client
950 * @param stackName the stack name to query
952 * @return a Stack object that describes the current stack or null if the
953 * requested stack doesn't exist.
955 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
957 protected Stack queryHeatStack (Heat heatClient, String stackName) throws MsoException {
958 if (stackName == null) {
962 OpenStackRequest <Stack> request = heatClient.getStacks ().byName (stackName);
963 return executeAndRecordOpenstackRequest (request);
964 } catch (OpenStackResponseException e) {
965 if (e.getStatus () == 404) {
966 LOGGER.debug ("queryHeatStack - stack not found: " + stackName);
969 // Convert the OpenStackResponseException to an MsoOpenstackException
970 throw heatExceptionToMsoException (e, "QueryStack");
972 } catch (OpenStackConnectException e) {
973 // Connection to Openstack failed
974 throw heatExceptionToMsoException (e, "QueryAllStack");
979 public Map<String, Object> queryStackForOutputs(String cloudSiteId,
980 String tenantId, String stackName) throws MsoException {
981 LOGGER.debug("MsoHeatUtils.queryStackForOutputs)");
982 StackInfo heatStack = this.queryStack(cloudSiteId, tenantId, stackName);
983 if (heatStack == null || heatStack.getStatus() == HeatStatus.NOTFOUND) {
986 return heatStack.getOutputs();
989 public void copyStringOutputsToInputs(Map<String, String> inputs,
990 Map<String, Object> otherStackOutputs, boolean overWrite) {
991 if (inputs == null || otherStackOutputs == null)
993 for (String key : otherStackOutputs.keySet()) {
994 if (!inputs.containsKey(key)) {
995 Object obj = otherStackOutputs.get(key);
996 if (obj instanceof String) {
997 inputs.put(key, (String) otherStackOutputs.get(key));
998 } else if (obj instanceof JsonNode ){
999 // This is a bit of mess - but I think it's the least impacting
1000 // let's convert it BACK to a string - then it will get converted back later
1002 String str = this.convertNode((JsonNode) obj);
1003 inputs.put(key, str);
1004 } catch (Exception e) {
1005 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for JsonNode "+ key, e);
1006 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1008 } else if (obj instanceof java.util.LinkedHashMap) {
1009 LOGGER.debug("LinkedHashMap - this is showing up as a LinkedHashMap instead of JsonNode");
1011 String str = JSON_MAPPER.writeValueAsString(obj);
1012 inputs.put(key, str);
1013 } catch (Exception e) {
1014 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for LinkedHashMap "+ key, e);
1016 } else if (obj instanceof Integer) {
1018 String str = "" + obj;
1019 inputs.put(key, str);
1020 } catch (Exception e) {
1021 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for Integer "+ key, e);
1025 String str = obj.toString();
1026 inputs.put(key, str);
1027 } catch (Exception e) {
1028 LOGGER.debug("DANGER WILL ROBINSON: unable to convert value for Other "+ key +" (" + e.getMessage() + ")", e);
1029 //effect here is this value will not have been copied to the inputs - and therefore will error out downstream
1036 public StringBuilder requestToStringBuilder(CreateStackParam stack) {
1037 StringBuilder sb = new StringBuilder();
1038 sb.append("Stack:\n");
1039 sb.append("\tStackName: " + stack.getStackName());
1040 sb.append("\tTemplateUrl: " + stack.getTemplateUrl());
1041 sb.append("\tTemplate: " + stack.getTemplate());
1042 sb.append("\tEnvironment: " + stack.getEnvironment());
1043 sb.append("\tTimeout: " + stack.getTimeoutMinutes());
1044 sb.append("\tParameters:\n");
1045 Map<String, Object> params = stack.getParameters();
1046 if (params == null || params.size() < 1) {
1047 sb.append("\nNONE");
1049 for (String key : params.keySet()) {
1050 if (params.get(key) instanceof String) {
1051 sb.append("\n").append(key).append("=").append((String) params.get(key));
1052 } else if (params.get(key) instanceof JsonNode) {
1053 String jsonStringOut = this.convertNode((JsonNode)params.get(key));
1054 sb.append("\n").append(key).append("=").append(jsonStringOut);
1055 } else if (params.get(key) instanceof Integer) {
1056 String integerOut = "" + params.get(key);
1057 sb.append("\n").append(key).append("=").append(integerOut);
1061 String str = params.get(key).toString();
1062 sb.append("\n").append(key).append("=").append(str);
1063 } catch (Exception e) {
1064 LOGGER.debug("Exception :",e);
1072 private String convertNode(final JsonNode node) {
1074 final Object obj = JSON_MAPPER.treeToValue(node, Object.class);
1075 final String json = JSON_MAPPER.writeValueAsString(obj);
1077 } catch (Exception e) {
1078 LOGGER.debug("Error converting json to string " + e.getMessage(), e);
1080 return "[Error converting json to string]";
1084 protected StringBuilder getOutputsAsStringBuilder(Stack heatStack) {
1085 // This should only be used as a utility to print out the stack outputs
1087 StringBuilder sb = new StringBuilder("");
1088 if (heatStack == null) {
1089 sb.append("(heatStack is null)");
1092 List<Output> outputList = heatStack.getOutputs();
1093 if (outputList == null || outputList.isEmpty()) {
1094 sb.append("(outputs is empty)");
1097 Map<String, Object> outputs = new HashMap<>();
1098 for (Output outputItem : outputList) {
1099 outputs.put(outputItem.getOutputKey(), outputItem.getOutputValue());
1102 sb.append("OUTPUTS:\n");
1103 for (String key : outputs.keySet()) {
1104 sb.append("outputs[").append(counter++).append("]: ").append(key).append("=");
1105 Object obj = outputs.get(key);
1106 if (obj instanceof String) {
1107 sb.append((String) obj).append(" (a string)");
1108 } else if (obj instanceof JsonNode) {
1109 sb.append(this.convertNode((JsonNode) obj)).append(" (a JsonNode)");
1110 } else if (obj instanceof java.util.LinkedHashMap) {
1112 String str = JSON_MAPPER.writeValueAsString(obj);
1113 sb.append(str).append(" (a java.util.LinkedHashMap)");
1114 } catch (Exception e) {
1115 LOGGER.debug("Exception :",e);
1116 sb.append("(a LinkedHashMap value that would not convert nicely)");
1118 } else if (obj instanceof Integer) {
1121 str = obj.toString() + " (an Integer)\n";
1122 } catch (Exception e) {
1123 LOGGER.debug("Exception :",e);
1124 str = "(an Integer unable to call .toString() on)";
1127 } else if (obj instanceof ArrayList) {
1130 str = obj.toString() + " (an ArrayList)";
1131 } catch (Exception e) {
1132 LOGGER.debug("Exception :",e);
1133 str = "(an ArrayList unable to call .toString() on?)";
1136 } else if (obj instanceof Boolean) {
1139 str = obj.toString() + " (a Boolean)";
1140 } catch (Exception e) {
1141 LOGGER.debug("Exception :",e);
1142 str = "(an Boolean unable to call .toString() on?)";
1149 str = obj.toString() + " (unknown Object type)";
1150 } catch (Exception e) {
1151 LOGGER.debug("Exception :",e);
1152 str = "(a value unable to call .toString() on?)";
1163 public void copyBaseOutputsToInputs(Map<String, Object> inputs,
1164 Map<String, Object> otherStackOutputs, List<String> paramNames, Map<String, String> aliases) {
1165 if (inputs == null || otherStackOutputs == null)
1167 for (String key : otherStackOutputs.keySet()) {
1168 if (paramNames != null) {
1169 if (!paramNames.contains(key) && !aliases.containsKey(key)) {
1170 LOGGER.debug("\tParameter " + key + " is NOT defined to be in the template - do not copy to inputs");
1173 if (aliases.containsKey(key)) {
1174 LOGGER.debug("Found an alias! Will move " + key + " to " + aliases.get(key));
1175 Object obj = otherStackOutputs.get(key);
1176 key = aliases.get(key);
1177 otherStackOutputs.put(key, obj);
1180 if (!inputs.containsKey(key)) {
1181 Object obj = otherStackOutputs.get(key);
1182 LOGGER.debug("\t**Adding " + key + " to inputs (.toString()=" + obj.toString());
1183 if (obj instanceof String) {
1184 LOGGER.debug("\t\t**A String");
1185 inputs.put(key, obj);
1186 } else if (obj instanceof Integer) {
1187 LOGGER.debug("\t\t**An Integer");
1188 inputs.put(key, obj);
1189 } else if (obj instanceof JsonNode) {
1190 LOGGER.debug("\t\t**A JsonNode");
1191 inputs.put(key, obj);
1192 } else if (obj instanceof Boolean) {
1193 LOGGER.debug("\t\t**A Boolean");
1194 inputs.put(key, obj);
1195 } else if (obj instanceof java.util.LinkedHashMap) {
1196 LOGGER.debug("\t\t**A java.util.LinkedHashMap **");
1197 inputs.put(key, obj);
1198 } else if (obj instanceof java.util.ArrayList) {
1199 LOGGER.debug("\t\t**An ArrayList");
1200 inputs.put(key, obj);
1202 LOGGER.debug("\t\t**UNKNOWN OBJECT TYPE");
1203 inputs.put(key, obj);
1206 LOGGER.debug("key=" + key + " is already in the inputs - will not overwrite");
1212 public List<String> convertCdlToArrayList(String cdl) {
1213 String cdl2 = cdl.trim();
1215 if (cdl2.startsWith("[") && cdl2.endsWith("]")) {
1216 cdl3 = cdl2.substring(1, cdl2.lastIndexOf("]"));
1220 return new ArrayList<>(Arrays.asList(cdl3.split(",")));
1224 * New with 1707 - this method will convert all the String *values* of the inputs
1225 * to their "actual" object type (based on the param type: in the db - which comes from the template):
1226 * (heat variable type) -> java Object type
1229 * json -> JsonNode XXX Removed with MSO-1475 / 1802
1230 * comma_delimited_list -> ArrayList
1231 * boolean -> Boolean
1232 * if any of the conversions should fail, we will default to adding it to the inputs
1233 * as a string - see if Openstack can handle it.
1234 * Also, will remove any params that are extra.
1235 * Any aliases will be converted to their appropriate name (anyone use this feature?)
1236 * @param inputs - the Map<String, String> of the inputs received on the request
1237 * @param template the HeatTemplate object - this is so we can also verify if the param is valid for this template
1238 * @return HashMap<String, Object> of the inputs, cleaned and converted
1240 public Map<String, Object> convertInputMap(Map<String, String> inputs, HeatTemplate template) {
1241 HashMap<String, Object> newInputs = new HashMap<>();
1242 HashMap<String, HeatTemplateParam> params = new HashMap<>();
1243 HashMap<String, HeatTemplateParam> paramAliases = new HashMap<>();
1245 if (inputs == null) {
1246 LOGGER.debug("convertInputMap - inputs is null - nothing to do here");
1247 return new HashMap<>();
1250 LOGGER.debug("convertInputMap in MsoHeatUtils called, with " + inputs.size() + " inputs, and template " + template.getArtifactUuid());
1252 LOGGER.debug(template.toString());
1253 Set<HeatTemplateParam> paramSet = template.getParameters();
1254 LOGGER.debug("paramSet has " + paramSet.size() + " entries");
1255 } catch (Exception e) {
1256 LOGGER.debug("Exception occurred in convertInputMap:" + e.getMessage(), e);
1259 for (HeatTemplateParam htp : template.getParameters()) {
1260 LOGGER.debug("Adding " + htp.getParamName());
1261 params.put(htp.getParamName(), htp);
1262 if (htp.getParamAlias() != null && !"".equals(htp.getParamAlias())) {
1263 LOGGER.debug("\tFound ALIAS " + htp.getParamName() + "->" + htp.getParamAlias());
1264 paramAliases.put(htp.getParamAlias(), htp);
1267 LOGGER.debug("Now iterate through the inputs...");
1268 for (String key : inputs.keySet()) {
1269 LOGGER.debug("key=" + key);
1270 boolean alias = false;
1271 String realName = null;
1272 if (!params.containsKey(key)) {
1273 LOGGER.debug(key + " is not a parameter in the template! - check for an alias");
1274 // add check here for an alias
1275 if (!paramAliases.containsKey(key)) {
1276 LOGGER.debug("The parameter " + key + " is in the inputs, but it's not a parameter for this template - omit");
1280 realName = paramAliases.get(key).getParamName();
1281 LOGGER.debug("FOUND AN ALIAS! Will use " + realName + " in lieu of give key/alias " + key);
1284 String type = params.get(key).getParamType();
1285 if (type == null || "".equals(type)) {
1286 LOGGER.debug("**PARAM_TYPE is null/empty for " + key + ", will default to string");
1289 LOGGER.debug("Parameter: " + key + " is of type " + type);
1290 if ("string".equalsIgnoreCase(type)) {
1292 String str = inputs.get(key);
1294 newInputs.put(realName, str);
1296 newInputs.put(key, str);
1297 } else if ("number".equalsIgnoreCase(type)) {
1298 String integerString = inputs.get(key);
1299 Integer anInteger = null;
1301 anInteger = Integer.parseInt(integerString);
1302 } catch (Exception e) {
1303 LOGGER.debug("Unable to convert " + integerString + " to an integer!!", e);
1306 if (anInteger != null) {
1308 newInputs.put(realName, anInteger);
1310 newInputs.put(key, anInteger);
1314 newInputs.put(realName, integerString);
1316 newInputs.put(key, integerString);
1318 } else if ("json".equalsIgnoreCase(type)) {
1319 // MSO-1475 - Leave this as a string now
1320 String jsonString = inputs.get(key);
1321 LOGGER.debug("Skipping conversion to jsonNode...");
1323 newInputs.put(realName, jsonString);
1325 newInputs.put(key, jsonString);
1327 } else if ("comma_delimited_list".equalsIgnoreCase(type)) {
1328 String commaSeparated = inputs.get(key);
1330 List<String> anArrayList = this.convertCdlToArrayList(commaSeparated);
1332 newInputs.put(realName, anArrayList);
1334 newInputs.put(key, anArrayList);
1335 } catch (Exception e) {
1336 LOGGER.debug("Unable to convert " + commaSeparated + " to an ArrayList!!", e);
1338 newInputs.put(realName, commaSeparated);
1340 newInputs.put(key, commaSeparated);
1342 } else if ("boolean".equalsIgnoreCase(type)) {
1343 String booleanString = inputs.get(key);
1344 Boolean aBool = Boolean.valueOf(booleanString);
1346 newInputs.put(realName, aBool);
1348 newInputs.put(key, aBool);
1350 // it's null or something undefined - just add it back as a String
1351 String str = inputs.get(key);
1353 newInputs.put(realName, str);
1355 newInputs.put(key, str);
1362 * This helpful method added for Valet
1364 public String getCloudSiteKeystoneUrl(String cloudSiteId) throws MsoCloudSiteNotFound {
1365 String keystone_url = null;
1367 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1368 CloudIdentity cloudIdentity = cloudSite.getIdentityService();
1369 keystone_url = cloudIdentity.getIdentityUrl();
1370 } catch (Exception e) {
1371 throw new MsoCloudSiteNotFound(cloudSiteId);
1373 if (keystone_url == null || keystone_url.isEmpty()) {
1374 throw new MsoCloudSiteNotFound(cloudSiteId);
1376 return keystone_url;
1380 * Create a string suitable for being dumped to a debug log that creates a
1381 * pseudo-JSON request dumping what's being sent to Openstack API in the create or update request
1384 private String printStackRequest(String tenantId,
1385 Map<String, Object> heatFiles,
1386 Map<String, Object> nestedTemplates,
1388 Map<String, Object> inputs,
1389 String vfModuleName,
1393 String cloudSiteId) {
1394 StringBuilder sb = new StringBuilder();
1395 sb.append("CREATE STACK REQUEST (formatted for readability)\n");
1396 sb.append("tenant=" + tenantId + ", cloud=" + cloudSiteId);
1398 sb.append(" \"stack_name\": \"" + vfModuleName + "\",\n");
1399 sb.append(" \"disable_rollback\": " + backout + ",\n");
1400 sb.append(" \"timeout_mins\": " + timeoutMinutes + ",\n");
1401 sb.append(" \"template\": {\n");
1402 sb.append(template);
1404 sb.append(" \"environment\": {\n");
1405 if (environment == null)
1406 sb.append("<none>");
1408 sb.append(environment);
1410 sb.append(" \"files\": {\n");
1411 int filesCounter = 0;
1412 if (heatFiles != null) {
1413 for (String key : heatFiles.keySet()) {
1415 if (filesCounter > 1) {
1418 sb.append(" \"" + key + "\": {\n");
1419 sb.append(heatFiles.get(key).toString() + "\n }");
1422 if (nestedTemplates != null) {
1423 for (String key : nestedTemplates.keySet()) {
1425 if (filesCounter > 1) {
1428 sb.append(" \"" + key + "\": {\n");
1429 sb.append(nestedTemplates.get(key).toString() + "\n }");
1432 sb.append("\n },\n");
1433 sb.append(" \"parameters\": {\n");
1434 int paramCounter = 0;
1435 for (String name : inputs.keySet()) {
1437 if (paramCounter > 1) {
1440 Object o = inputs.get(name);
1441 if (o instanceof java.lang.String) {
1442 sb.append(" \"" + name + "\": \"" + inputs.get(name).toString() + "\"");
1443 } else if (o instanceof Integer) {
1444 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1445 } else if (o instanceof ArrayList) {
1446 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1447 } else if (o instanceof Boolean) {
1448 sb.append(" \"" + name + "\": " + inputs.get(name).toString() );
1450 sb.append(" \"" + name + "\": " + "\"(there was an issue trying to dump this value...)\"" );
1453 sb.append("\n }\n}\n");
1455 return sb.toString();
1458 /*******************************************************************************
1460 * Methods (and associated utilities) to implement the VduPlugin interface
1462 *******************************************************************************/
1465 * VduPlugin interface for instantiate function.
1467 * Translate the VduPlugin parameters to the corresponding 'createStack' parameters,
1468 * and then invoke the existing function.
1471 public VduInstance instantiateVdu (
1472 CloudInfo cloudInfo,
1473 String instanceName,
1474 Map<String,Object> inputs,
1475 VduModelInfo vduModel,
1476 boolean rollbackOnFailure)
1479 String cloudSiteId = cloudInfo.getCloudSiteId();
1480 String tenantId = cloudInfo.getTenantId();
1482 // Translate the VDU ModelInformation structure to that which is needed for
1483 // creating the Heat stack. Loop through the artifacts, looking specifically
1484 // for MAIN_TEMPLATE and ENVIRONMENT. Any other artifact will
1485 // be attached as a FILE.
1486 String heatTemplate = null;
1487 Map<String,Object> nestedTemplates = new HashMap<>();
1488 Map<String,Object> files = new HashMap<>();
1489 String heatEnvironment = null;
1491 for (VduArtifact vduArtifact: vduModel.getArtifacts()) {
1492 if (vduArtifact.getType() == ArtifactType.MAIN_TEMPLATE) {
1493 heatTemplate = new String(vduArtifact.getContent());
1495 else if (vduArtifact.getType() == ArtifactType.NESTED_TEMPLATE) {
1496 nestedTemplates.put(vduArtifact.getName(), new String(vduArtifact.getContent()));
1498 else if (vduArtifact.getType() == ArtifactType.ENVIRONMENT) {
1499 heatEnvironment = new String(vduArtifact.getContent());
1504 StackInfo stackInfo = createStack (cloudSiteId,
1509 true, // poll for completion
1510 vduModel.getTimeoutMinutes(),
1516 // Populate a vduInstance from the StackInfo
1517 return stackInfoToVduInstance(stackInfo);
1519 catch (Exception e) {
1520 throw new VduException ("MsoHeatUtils (instantiateVDU): createStack Exception", e);
1526 * VduPlugin interface for query function.
1529 public VduInstance queryVdu (CloudInfo cloudInfo, String instanceId)
1532 String cloudSiteId = cloudInfo.getCloudSiteId();
1533 String tenantId = cloudInfo.getTenantId();
1536 // Query the Cloudify Deployment object and populate a VduInstance
1537 StackInfo stackInfo = queryStack (cloudSiteId, tenantId, instanceId);
1539 return stackInfoToVduInstance(stackInfo);
1541 catch (Exception e) {
1542 throw new VduException ("MsoHeatUtile (queryVdu): queryStack Exception ", e);
1548 * VduPlugin interface for delete function.
1551 public VduInstance deleteVdu (CloudInfo cloudInfo, String instanceId, int timeoutMinutes)
1554 String cloudSiteId = cloudInfo.getCloudSiteId();
1555 String tenantId = cloudInfo.getTenantId();
1558 // Delete the Heat stack
1559 StackInfo stackInfo = deleteStack (tenantId, cloudSiteId, instanceId, true);
1561 // Populate a VduInstance based on the deleted Cloudify Deployment object
1562 VduInstance vduInstance = stackInfoToVduInstance(stackInfo);
1564 // Override return state to DELETED (HeatUtils sets to NOTFOUND)
1565 vduInstance.getStatus().setState(VduStateType.DELETED);
1569 catch (Exception e) {
1570 throw new VduException ("Delete VDU Exception", e);
1576 * VduPlugin interface for update function.
1578 * Update is currently not supported in the MsoHeatUtils implementation of VduPlugin.
1579 * Just return a VduException.
1583 public VduInstance updateVdu (
1584 CloudInfo cloudInfo,
1586 Map<String,Object> inputs,
1587 VduModelInfo vduModel,
1588 boolean rollbackOnFailure)
1591 throw new VduException ("MsoHeatUtils: updateVdu interface not supported");
1596 * Convert the local DeploymentInfo object (Cloudify-specific) to a generic VduInstance object
1598 protected VduInstance stackInfoToVduInstance (StackInfo stackInfo)
1600 VduInstance vduInstance = new VduInstance();
1602 // The full canonical name as the instance UUID
1603 vduInstance.setVduInstanceId(stackInfo.getCanonicalName());
1604 vduInstance.setVduInstanceName(stackInfo.getName());
1606 // Copy inputs and outputs
1607 vduInstance.setInputs(stackInfo.getParameters());
1608 vduInstance.setOutputs(stackInfo.getOutputs());
1610 // Translate the status elements
1611 vduInstance.setStatus(stackStatusToVduStatus (stackInfo));
1616 private VduStatus stackStatusToVduStatus (StackInfo stackInfo)
1618 VduStatus vduStatus = new VduStatus();
1620 // Map the status fields to more generic VduStatus.
1621 // There are lots of HeatStatus values, so this is a bit long...
1622 HeatStatus heatStatus = stackInfo.getStatus();
1623 String statusMessage = stackInfo.getStatusMessage();
1625 if (heatStatus == HeatStatus.INIT || heatStatus == HeatStatus.BUILDING) {
1626 vduStatus.setState(VduStateType.INSTANTIATING);
1627 vduStatus.setLastAction((new PluginAction ("create", "in_progress", statusMessage)));
1629 else if (heatStatus == HeatStatus.NOTFOUND) {
1630 vduStatus.setState(VduStateType.NOTFOUND);
1632 else if (heatStatus == HeatStatus.CREATED) {
1633 vduStatus.setState(VduStateType.INSTANTIATED);
1634 vduStatus.setLastAction((new PluginAction ("create", "complete", statusMessage)));
1636 else if (heatStatus == HeatStatus.UPDATED) {
1637 vduStatus.setState(VduStateType.INSTANTIATED);
1638 vduStatus.setLastAction((new PluginAction ("update", "complete", statusMessage)));
1640 else if (heatStatus == HeatStatus.UPDATING) {
1641 vduStatus.setState(VduStateType.UPDATING);
1642 vduStatus.setLastAction((new PluginAction ("update", "in_progress", statusMessage)));
1644 else if (heatStatus == HeatStatus.DELETING) {
1645 vduStatus.setState(VduStateType.DELETING);
1646 vduStatus.setLastAction((new PluginAction ("delete", "in_progress", statusMessage)));
1648 else if (heatStatus == HeatStatus.FAILED) {
1649 vduStatus.setState(VduStateType.FAILED);
1650 vduStatus.setErrorMessage(stackInfo.getStatusMessage());
1652 vduStatus.setState(VduStateType.UNKNOWN);
1658 public Resources queryStackResources(String cloudSiteId, String tenantId, String stackName) throws MsoException {
1659 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1660 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1661 Heat heatClient = getHeatClient(cloudSite, tenantId);
1662 OpenStackRequest<Resources> request = heatClient.getResources().listResources(stackName);
1663 return executeAndRecordOpenstackRequest(request);
1666 public <R> R executeHeatClientRequest(String url, String cloudSiteId, String tenantId, Class<R> returnType) throws MsoException {
1667 CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId)
1668 .orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
1669 Heat heatClient = getHeatClient(cloudSite, tenantId);
1670 OpenStackRequest<R> request = heatClient.get(url, returnType);
1671 return executeAndRecordOpenstackRequest(request);
1674 protected void sleep(long time) {
1677 } catch (InterruptedException e) {
1678 LOGGER.debug ("Thread interrupted while sleeping", e);
1679 Thread.currentThread().interrupt();