tvaultapi/src/main/java/com/tmobile/cso/vault/api/service/SSLCertificateService.java
private ResponseEntity<String> createCertificateMetadataAndPolicies(SSLCertificateOnboardRequest sslCertificateRequest,
			UserDetails userDetails, int containerId, String nclmAccessToken) {
		// Policy Creation
		boolean isPoliciesCreated = false;
		CertResponse enrollResponse = new CertResponse();
		ResponseEntity<String> permissionResponse;
		try {
			String metadataJson = populateMetadataForSSLOnboard(sslCertificateRequest, containerId,
					nclmAccessToken);
			if (metadataJson == null) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST)
						.body("{\"errors\":[\"Certificate onboard failed.\"]}");
			}
			SSLCertificateRequest sslCertRequest = new SSLCertificateRequest();
			BeanUtils.copyProperties(sslCertRequest, sslCertificateRequest);

			boolean sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson,
					tokenUtils.getSelfServiceToken());
			log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
					.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
					.put(LogMessage.ACTION,
							String.format(" [%s] - Metadata creation status - [%s] for metadatajson - [%s]",
									sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus,
									metadataJson))
					.build()));
			
			//add certificate name into application metadata list
            boolean sslApplicationMetaDataSaveStatus = certificateMetadataForApplicationDetails(metadataJson, tokenUtils.getSelfServiceToken(),"create");
            log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
					.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
					.put(LogMessage.ACTION,
							String.format(" [%s] - Applicatio Metadata creation status - [%s] for metadatajson - [%s]",
									sslCertificateRequest.getCertificateName(), sslApplicationMetaDataSaveStatus,
									metadataJson))
					.build()));

			if (sslMetaDataCreationStatus) {
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, String.format(" [%s] - Metadata Creation - Completed ",
								sslCertificateRequest.getCertificateName()))
						.build()));
				isPoliciesCreated = createPolicies(sslCertRequest, tokenUtils.getSelfServiceToken());
			}else {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION,
								String.format(" ERROR [%s] - Onboard failed. Metadata creation failed. metaDataStatus[%s]",
										sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus))
						.build()));
			}

			if (isPoliciesCreated) {
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, String.format(" [%s] - Policycreation - Completed",
								sslCertificateRequest.getCertificateName()))
						.build()));
			} else {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION,
								String.format(" ERROR [%s] - Onboard failed. Policy creation failed . policyStatus[%s]",
										sslCertificateRequest.getCertificateName(), isPoliciesCreated))
						.build()));
			}

			// Send failed certificate response in case of any issues in Policy/Meta data
			// creation
			if ((!sslMetaDataCreationStatus) || (!isPoliciesCreated)) {
				enrollResponse.setResponse("Metadatacreation failed");
				enrollResponse.setSuccess(Boolean.FALSE);
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, String.format(
								"ERROR [%s] - Onboard failed. Metadata creation failed - metaDataStatus[%s] - policyStatus[%s]",
								sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus,
								isPoliciesCreated))
						.build()));
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
						.body(ERRORS + enrollResponse.getResponse() + "\"]}");
			} else {
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, "Onboard SSL certificate")
						.put(LogMessage.MESSAGE, "Sudo Policy Creation Started  ")
						.put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
				permissionResponse = addSudoPermissionToCertificateOwner(sslCertRequest, userDetails,
						enrollResponse, isPoliciesCreated, sslMetaDataCreationStatus, tokenUtils.getSelfServiceToken(),
						SSLCertificateConstants.ONBOARD);
				//Delete metadata and permissions if add sudo permission failed
				deleteMeataDataIfOwnerPermissionFailed(sslCertificateRequest, permissionResponse);

				return permissionResponse;
			}
		} catch (Exception e) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body(ERRORS + enrollResponse.getResponse() + "\"]}");
		}
	}
/**
     * Method to create the metadata and policy for a certificate
     * @param sslCertificateRequest
     * @param userDetails
     * @param containerId
     * @return
     */
Method to create the metadata and policy for a certificate
@param sslCertificateRequest
@param userDetails
@param containerId
@return
Method to create the metadata and policy for a certificate @param sslCertificateRequest @param userDetails @param containerId @return
private ResponseEntity<String> createCertificateMetadataAndPolicies(SSLCertificateOnboardRequest sslCertificateRequest,
			UserDetails userDetails, int containerId, String nclmAccessToken) {
		boolean isPoliciesCreated = false;
		CertResponse enrollResponse = new CertResponse();
		ResponseEntity<String> permissionResponse;
		try {
			String metadataJson = populateMetadataForSSLOnboard(sslCertificateRequest, containerId,
					nclmAccessToken);
			if (metadataJson == null) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST)
						.body("{\"errors\":[\"Certificate onboard failed.\"]}");
			}
			SSLCertificateRequest sslCertRequest = new SSLCertificateRequest();
			BeanUtils.copyProperties(sslCertRequest, sslCertificateRequest);
			boolean sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson,
					tokenUtils.getSelfServiceToken());
			log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
					.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
					.put(LogMessage.ACTION,
							String.format(" [%s] - Metadata creation status - [%s] for metadatajson - [%s]",
									sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus,
									metadataJson))
					.build()));
            boolean sslApplicationMetaDataSaveStatus = certificateMetadataForApplicationDetails(metadataJson, tokenUtils.getSelfServiceToken(),"create");
            log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
					.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
					.put(LogMessage.ACTION,
							String.format(" [%s] - Applicatio Metadata creation status - [%s] for metadatajson - [%s]",
									sslCertificateRequest.getCertificateName(), sslApplicationMetaDataSaveStatus,
									metadataJson))
					.build()));
			if (sslMetaDataCreationStatus) {
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, String.format(" [%s] - Metadata Creation - Completed ",
								sslCertificateRequest.getCertificateName()))
						.build()));
				isPoliciesCreated = createPolicies(sslCertRequest, tokenUtils.getSelfServiceToken());
			}else {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION,
								String.format(" ERROR [%s] - Onboard failed. Metadata creation failed. metaDataStatus[%s]",
										sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus))
						.build()));
			}
			if (isPoliciesCreated) {
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, String.format(" [%s] - Policycreation - Completed",
								sslCertificateRequest.getCertificateName()))
						.build()));
			} else {
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION,
								String.format(" ERROR [%s] - Onboard failed. Policy creation failed . policyStatus[%s]",
										sslCertificateRequest.getCertificateName(), isPoliciesCreated))
						.build()));
			}
			if ((!sslMetaDataCreationStatus) || (!isPoliciesCreated)) {
				enrollResponse.setResponse("Metadatacreation failed");
				enrollResponse.setSuccess(Boolean.FALSE);
				log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, String.format(
								"ERROR [%s] - Onboard failed. Metadata creation failed - metaDataStatus[%s] - policyStatus[%s]",
								sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus,
								isPoliciesCreated))
						.build()));
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
						.body(ERRORS + enrollResponse.getResponse() + "\"]}");
			} else {
				log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder()
						.put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER))
						.put(LogMessage.ACTION, "Onboard SSL certificate")
						.put(LogMessage.MESSAGE, "Sudo Policy Creation Started  ")
						.put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build()));
				permissionResponse = addSudoPermissionToCertificateOwner(sslCertRequest, userDetails,
						enrollResponse, isPoliciesCreated, sslMetaDataCreationStatus, tokenUtils.getSelfServiceToken(),
						SSLCertificateConstants.ONBOARD);
				deleteMeataDataIfOwnerPermissionFailed(sslCertificateRequest, permissionResponse);
				return permissionResponse;
			}
		} catch (Exception e) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body(ERRORS + enrollResponse.getResponse() + "\"]}");
		}
	}
private ResponseEntity < String > createCertificateMetadataAndPolicies ( SSLCertificateOnboardRequest sslCertificateRequest , UserDetails userDetails , int containerId , String nclmAccessToken ) { boolean isPoliciesCreated = false ; CertResponse enrollResponse = new CertResponse ( ) ; ResponseEntity < String > permissionResponse ; try { String metadataJson = populateMetadataForSSLOnboard ( sslCertificateRequest , containerId , nclmAccessToken ) ; if ( metadataJson == null ) { return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( "{\"errors\":[\"Certificate onboard failed.\"]}" ) ; } SSLCertificateRequest sslCertRequest = new SSLCertificateRequest ( ) ; BeanUtils . copyProperties ( sslCertRequest , sslCertificateRequest ) ; boolean sslMetaDataCreationStatus = ControllerUtil . createMetadata ( metadataJson , tokenUtils . getSelfServiceToken ( ) ) ; log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , String . format ( " [%s] - Metadata creation status - [%s] for metadatajson - [%s]" , sslCertificateRequest . getCertificateName ( ) , sslMetaDataCreationStatus , metadataJson ) ) . build ( ) ) ) ; boolean sslApplicationMetaDataSaveStatus = certificateMetadataForApplicationDetails ( metadataJson , tokenUtils . getSelfServiceToken ( ) , "create" ) ; log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , String . format ( " [%s] - Applicatio Metadata creation status - [%s] for metadatajson - [%s]" , sslCertificateRequest . getCertificateName ( ) , sslApplicationMetaDataSaveStatus , metadataJson ) ) . build ( ) ) ) ; if ( sslMetaDataCreationStatus ) { log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , String . format ( " [%s] - Metadata Creation - Completed " , sslCertificateRequest . getCertificateName ( ) ) ) . build ( ) ) ) ; isPoliciesCreated = createPolicies ( sslCertRequest , tokenUtils . getSelfServiceToken ( ) ) ; } else { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , String . format ( " ERROR [%s] - Onboard failed. Metadata creation failed. metaDataStatus[%s]" , sslCertificateRequest . getCertificateName ( ) , sslMetaDataCreationStatus ) ) . build ( ) ) ) ; } if ( isPoliciesCreated ) { log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , String . format ( " [%s] - Policycreation - Completed" , sslCertificateRequest . getCertificateName ( ) ) ) . build ( ) ) ) ; } else { log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , String . format ( " ERROR [%s] - Onboard failed. Policy creation failed . policyStatus[%s]" , sslCertificateRequest . getCertificateName ( ) , isPoliciesCreated ) ) . build ( ) ) ) ; } if ( ( ! sslMetaDataCreationStatus ) || ( ! isPoliciesCreated ) ) { enrollResponse . setResponse ( "Metadatacreation failed" ) ; enrollResponse . setSuccess ( Boolean . FALSE ) ; log . error ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , String . format ( "ERROR [%s] - Onboard failed. Metadata creation failed - metaDataStatus[%s] - policyStatus[%s]" , sslCertificateRequest . getCertificateName ( ) , sslMetaDataCreationStatus , isPoliciesCreated ) ) . build ( ) ) ) ; return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( ERRORS + enrollResponse . getResponse ( ) + "\"]}" ) ; } else { log . debug ( JSONUtil . getJSON ( ImmutableMap . < String , String > builder ( ) . put ( LogMessage . USER , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . USER ) ) . put ( LogMessage . ACTION , "Onboard SSL certificate" ) . put ( LogMessage . MESSAGE , "Sudo Policy Creation Started  " ) . put ( LogMessage . APIURL , ThreadLocalContext . getCurrentMap ( ) . get ( LogMessage . APIURL ) ) . build ( ) ) ) ; permissionResponse = addSudoPermissionToCertificateOwner ( sslCertRequest , userDetails , enrollResponse , isPoliciesCreated , sslMetaDataCreationStatus , tokenUtils . getSelfServiceToken ( ) , SSLCertificateConstants . ONBOARD ) ; deleteMeataDataIfOwnerPermissionFailed ( sslCertificateRequest , permissionResponse ) ; return permissionResponse ; } } catch ( Exception e ) { return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( ERRORS + enrollResponse . getResponse ( ) + "\"]}" ) ; } }
Method to create the metadata and policy for a certificate
 @param sslCertificateRequest
 @param userDetails
 @param containerId
 @return
Method to create the metadata and policy for a certificate @param sslCertificateRequest @param userDetails @param containerId @return
// Policy Creation //add certificate name into application metadata list // Send failed certificate response in case of any issues in Policy/Meta data // creation //Delete metadata and permissions if add sudo permission failed