import fj.data.Either;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
.asList(TOSCA_SIMPLE_YAML_PREFIX + "1_0_0", TOSCA_SIMPLE_YAML_PREFIX + "1_1_0", "tosca_simple_profile_for_nfv_1_0_0",
TOSCA_SIMPLE_YAML_PREFIX + "1_0", TOSCA_SIMPLE_YAML_PREFIX + "1_1", TOSCA_SIMPLE_YAML_PREFIX + "1_2", TOSCA_SIMPLE_YAML_PREFIX + "1_3");
private static final List<String> TOSCA_YML_CSAR_VALID_SUFFIX = Arrays.asList(".yml", ".yaml", ".csar", ".meta");
+ private static final String INVALID_JSON_WAS_RECEIVED = "Invalid json was received.";
+ private static final String AUDIT_BEFORE_SENDING_RESPONSE = "audit before sending response";
+ private static final String VALIDATE_USER_ROLE = "validate user role";
+ private static final String USER_IS_NOT_IN_APPROPRIATE_ROLE_TO_PERFORM_ACTION = "user is not in appropriate role to perform action";
protected final ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
protected ServletUtils servletUtils;
protected ResourceImportManager resourceImportManager;
protected ServiceImportManager serviceImportManager;
- public AbstractValidationsServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL,
- ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager) {
- super(userBusinessLogic, componentsUtils);
+ protected AbstractValidationsServlet(ComponentInstanceBusinessLogic componentInstanceBL,
+ ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager) {
+ super(componentsUtils);
this.servletUtils = servletUtils;
this.resourceImportManager = resourceImportManager;
this.componentInstanceBusinessLogic = componentInstanceBL;
}
protected void validateUserRole(Wrapper<Response> errorResponseWrapper, User user) {
- log.debug("validate user role");
+ log.debug(VALIDATE_USER_ROLE);
if (!user.getRole().equals(Role.ADMIN.name()) && !user.getRole().equals(Role.DESIGNER.name())) {
- log.info("user is not in appropriate role to perform action");
+ log.info(USER_IS_NOT_IN_APPROPRIATE_ROLE_TO_PERFORM_ACTION);
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
- log.debug("audit before sending response");
+ log.debug(AUDIT_BEFORE_SENDING_RESPONSE);
getComponentsUtils().auditResource(responseFormat, user, "", AuditingActionEnum.IMPORT_RESOURCE);
Response response = buildErrorResponse(responseFormat);
errorResponseWrapper.setInnerElement(response);
try (InputStream fileInputStream = new FileInputStream(file)) {
byte[] data = new byte[(int) file.length()];
if (fileInputStream.read(data) == -1) {
- log.info("Invalid json was received.");
+ log.info(INVALID_JSON_WAS_RECEIVED);
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
Response errorResp = buildErrorResponse(responseFormat);
responseWrapper.setInnerElement(errorResp);
}
protected void validateUserRole(Wrapper<Response> errorResponseWrapper, User user, ResourceAuthorityTypeEnum resourceAuthority) {
- log.debug("validate user role");
+ log.debug(VALIDATE_USER_ROLE);
if (resourceAuthority == ResourceAuthorityTypeEnum.NORMATIVE_TYPE_BE) {
if (!user.getRole().equals(Role.ADMIN.name())) {
- log.info("user is not in appropriate role to perform action");
+ log.info(USER_IS_NOT_IN_APPROPRIATE_ROLE_TO_PERFORM_ACTION);
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
- log.debug("audit before sending response");
+ log.debug(AUDIT_BEFORE_SENDING_RESPONSE);
getComponentsUtils().auditResource(responseFormat, user, "", AuditingActionEnum.IMPORT_RESOURCE);
Response response = buildErrorResponse(responseFormat);
errorResponseWrapper.setInnerElement(response);
if (resourceInfoObject == null) {
isValid = false;
} else {
+ resourceInfoObject.setNormative(!resourceAuthorityEnum.isUserTypeResource());
if (!resourceAuthorityEnum.isBackEndImport()) {
isValid = resourceInfoObject.getPayloadName() != null && !resourceInfoObject.getPayloadName().isEmpty();
//only resource name is checked
isValid = false;
}
if (!isValid) {
- log.info("Invalid json was received.");
+ log.info(INVALID_JSON_WAS_RECEIVED);
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
getComponentsUtils().auditResource(responseFormat, user, "", AuditingActionEnum.IMPORT_RESOURCE);
Response errorResp = buildErrorResponse(responseFormat);
return getServletUtils().getGson();
}
+ @Override
public ComponentsUtils getComponentsUtils() {
return getServletUtils().getComponentsUtils();
}
private void validateToscaTemplatePayloadName(Wrapper<Response> responseWrapper, UploadResourceInfo uploadResourceInfo, User user) {
String toscaTemplatePayloadName = uploadResourceInfo.getPayloadName();
- boolean isValidSuffix = isToscaTemplatePayloadNameValid(responseWrapper, toscaTemplatePayloadName);
+ boolean isValidSuffix = isToscaTemplatePayloadNameValid(toscaTemplatePayloadName);
if (!isValidSuffix) {
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_TOSCA_FILE_EXTENSION);
Response errorResponse = buildErrorResponse(responseFormat);
}
}
- private boolean isToscaTemplatePayloadNameValid(Wrapper<Response> responseWrapper, String toscaTemplatePayloadName) {
+ private boolean isToscaTemplatePayloadNameValid(String toscaTemplatePayloadName) {
boolean isValidSuffix = false;
if (toscaTemplatePayloadName != null && !toscaTemplatePayloadName.isEmpty()) {
for (String validSuffix : TOSCA_YML_CSAR_VALID_SUFFIX) {
importedResourceStatus = importResourceFromUICsar(resourceInfoObject, user, resourceUniqueId);
} else if (!authority.isUserTypeResource()) {
log.debug("import normative type resource");
- createOrUpdateResponse = resourceImportManager.importNormativeResource(yamlAsString, resourceInfoObject, user, createNewVersion, true);
+ createOrUpdateResponse =
+ resourceImportManager.importNormativeResource(yamlAsString, resourceInfoObject, user, createNewVersion, true, false);
} else {
log.debug("import user resource (not normative type)");
createOrUpdateResponse = resourceImportManager.importUserDefinedResource(yamlAsString, resourceInfoObject, user, false);
private void getAndValidateComponentCsarYaml(Map<String, byte[]> csarUIPayload, Component component, User user, String csarUUID) {
Either<ImmutablePair<String, String>, ResponseFormat> getToscaYamlRes = CsarValidationUtils
- .getToscaYaml(csarUIPayload, csarUUID, getComponentsUtils());
+ .getToscaYaml(csarUIPayload, csarUUID, getComponentsUtils(), null);
if (getToscaYamlRes.isRight()) {
ResponseFormat responseFormat = getToscaYamlRes.right().value();
log.debug("Error when try to get csar toscayamlFile with csar ID {}, error: {}", csarUUID, responseFormat);
validateUserRole(responseWrapper, userWrapper.getInnerElement(), serviceAuthorityEnum);
}
if (responseWrapper.isEmpty()) {
- validateAndFillServiceJson(responseWrapper, uploadServiceInfoWrapper, userWrapper.getInnerElement(), serviceAuthorityEnum,
+ validateAndFillServiceJson(responseWrapper, uploadServiceInfoWrapper, serviceAuthorityEnum,
serviceInfoJsonString);
}
if (responseWrapper.isEmpty()) {
- validateToscaTemplatePayloadName(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), userWrapper.getInnerElement());
+ validateToscaTemplatePayloadName(responseWrapper, uploadServiceInfoWrapper.getInnerElement());
}
}
protected void validateUserRole(Wrapper<Response> errorResponseWrapper, User user, ServiceAuthorityTypeEnum serviceAuthority) {
- log.debug("validate user role");
+ log.debug(VALIDATE_USER_ROLE);
if (serviceAuthority == ServiceAuthorityTypeEnum.NORMATIVE_TYPE_BE) {
if (!user.getRole().equals(Role.ADMIN.name())) {
- log.info("user is not in appropriate role to perform action");
+ log.info(USER_IS_NOT_IN_APPROPRIATE_ROLE_TO_PERFORM_ACTION);
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.RESTRICTED_OPERATION);
- log.debug("audit before sending response");
+ log.debug(AUDIT_BEFORE_SENDING_RESPONSE);
Response response = buildErrorResponse(responseFormat);
errorResponseWrapper.setInnerElement(response);
}
}
}
- protected void validateAndFillServiceJson(Wrapper<Response> responseWrapper, Wrapper<UploadServiceInfo> uploadServiceInfoWrapper, User user,
+ protected void validateAndFillServiceJson(Wrapper<Response> responseWrapper, Wrapper<UploadServiceInfo> uploadServiceInfoWrapper,
ServiceAuthorityTypeEnum serviceAuthorityEnum, String serviceInfo) {
boolean isValid;
try {
isValid = false;
}
if (!isValid) {
- log.info("Invalid json was received.");
+ log.info(INVALID_JSON_WAS_RECEIVED);
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
Response errorResp = buildErrorResponse(responseFormat);
responseWrapper.setInnerElement(errorResp);
}
}
- protected void validateToscaTemplatePayloadName(Wrapper<Response> responseWrapper, UploadServiceInfo uploadServiceInfo, User user) {
+ protected void validateToscaTemplatePayloadName(Wrapper<Response> responseWrapper, UploadServiceInfo uploadServiceInfo) {
String toscaTemplatePayloadName = uploadServiceInfo.getPayloadName();
- boolean isValidSuffix = isToscaTemplatePayloadNameValid(responseWrapper, toscaTemplatePayloadName);
+ boolean isValidSuffix = isToscaTemplatePayloadNameValid(toscaTemplatePayloadName);
if (!isValidSuffix) {
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_TOSCA_FILE_EXTENSION);
Response errorResponse = buildErrorResponse(responseFormat);
}
protected void specificServiceAuthorityValidations(Wrapper<Response> responseWrapper, Wrapper<UploadServiceInfo> uploadServiceInfoWrapper,
- Wrapper<String> yamlStringWrapper, User user, HttpServletRequest request,
- String serviceInfoJsonString, ServiceAuthorityTypeEnum serviceAuthorityEnum)
- throws FileNotFoundException {
+ Wrapper<String> yamlStringWrapper, HttpServletRequest request,
+ String serviceInfoJsonString, ServiceAuthorityTypeEnum serviceAuthorityEnum) {
if (responseWrapper.isEmpty()) {
// UI Only Validation
if (!serviceAuthorityEnum.isBackEndImport()) {
- importUIValidations(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), user, request, serviceInfoJsonString);
+ importUIValidations(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), request, serviceInfoJsonString);
}
// User Defined Type Services
- if (serviceAuthorityEnum.isUserTypeService() && !CsarValidationUtils
- .isCsarPayloadName(uploadServiceInfoWrapper.getInnerElement().getPayloadName())) {
- if (responseWrapper.isEmpty()) {
- validatePayloadNameSpace(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), user, yamlStringWrapper.getInnerElement());
- }
+ if (serviceAuthorityEnum.isUserTypeService()
+ && !CsarValidationUtils.isCsarPayloadName(uploadServiceInfoWrapper.getInnerElement().getPayloadName())
+ && responseWrapper.isEmpty()) {
+ validatePayloadNameSpace(responseWrapper, uploadServiceInfoWrapper.getInnerElement(), yamlStringWrapper.getInnerElement());
}
}
}
- protected void importUIValidations(Wrapper<Response> responseWrapper, UploadServiceInfo serviceInfo, User user, HttpServletRequest request,
+ protected void importUIValidations(Wrapper<Response> responseWrapper, UploadServiceInfo serviceInfo, HttpServletRequest request,
String serviceInfoJsonString) {
if (responseWrapper.isEmpty()) {
- validateMD5(responseWrapper, user, serviceInfo, request, serviceInfoJsonString);
+ validateMD5(responseWrapper, request, serviceInfoJsonString);
}
if (responseWrapper.isEmpty() && request != null && request.getMethod() != null && request.getMethod().equals("POST")) {
- validateServiceDoesNotExist(responseWrapper, user, serviceInfo.getName());
+ validateServiceDoesNotExist(responseWrapper, serviceInfo.getName());
}
}
- protected void validatePayloadNameSpace(Wrapper<Response> responseWrapper, UploadServiceInfo serviceInfo, User user, String toscaPayload) {
+ protected void validatePayloadNameSpace(Wrapper<Response> responseWrapper, UploadServiceInfo serviceInfo, String toscaPayload) {
boolean isValid;
String nameSpace = "";
Map<String, Object> mappedToscaTemplate = (Map<String, Object>) new Yaml().load(toscaPayload);
}
}
- protected void validateMD5(Wrapper<Response> responseWrapper, User user, UploadServiceInfo serviceInfo, HttpServletRequest request,
+ protected void validateMD5(Wrapper<Response> responseWrapper, HttpServletRequest request,
String serviceInfoJsonString) {
boolean isValid;
String recievedMD5 = request.getHeader(Constants.MD5_HEADER);
}
}
- protected void validateServiceDoesNotExist(Wrapper<Response> responseWrapper, User user, String serviceName) {
+ protected void validateServiceDoesNotExist(Wrapper<Response> responseWrapper, String serviceName) {
if (serviceImportManager.isServiceExist(serviceName)) {
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.SERVICE_ALREADY_EXISTS);
Response errorResponse = buildErrorResponse(responseFormat);
}
}
- protected void handleImportService(Wrapper<Response> responseWrapper, User user, UploadServiceInfo serviceInfoObject, String yamlAsString,
- ServiceAuthorityTypeEnum authority, boolean createNewVersion, String serviceUniqueId) throws ZipException {
+ protected void handleImportService(Wrapper<Response> responseWrapper, User user, UploadServiceInfo serviceInfoObject) {
Response response = null;
- Object representation = null;
ImmutablePair<Service, ActionStatus> importedServiceStatus = null;
if (CsarValidationUtils.isCsarPayloadName(serviceInfoObject.getPayloadName())) {
log.debug("import service from csar");
- importedServiceStatus = importServiceFromUICsar(serviceInfoObject, user, serviceUniqueId);
+ importedServiceStatus = importServiceFromUICsar(serviceInfoObject, user);
}
if (importedServiceStatus != null) {
+ Object representation = null;
try {
representation = RepresentationUtils.toRepresentation(importedServiceStatus.left);
} catch (IOException e) {
responseWrapper.setInnerElement(response);
}
- private ImmutablePair<Service, ActionStatus> importServiceFromUICsar(UploadServiceInfo serviceInfoObject, User user, String serviceUniqueId)
- throws ZipException {
- Service newService;
- ImmutablePair<Service, ActionStatus> result = null;
- ActionStatus actionStatus;
+ private ImmutablePair<Service, ActionStatus> importServiceFromUICsar(UploadServiceInfo serviceInfoObject, User user) {
Service service = new Service();
String payloadName = serviceInfoObject.getPayloadName();
fillServiceFromServiceInfoObject(service, serviceInfoObject);
Map<String, byte[]> csarUIPayloadRes = getCsarFromPayload(serviceInfoObject);
getAndValidateCsarYaml(csarUIPayloadRes, service, user, payloadName);
- newService = serviceImportManager.getServiceImportBusinessLogic()
+ final Service newService = serviceImportManager.getServiceImportBusinessLogic()
.createService(service, AuditingActionEnum.CREATE_SERVICE, user, csarUIPayloadRes, payloadName);
- actionStatus = ActionStatus.CREATED;
- return new ImmutablePair<>(newService, actionStatus);
+ return new ImmutablePair<>(newService, ActionStatus.CREATED);
}
private void fillServiceFromServiceInfoObject(Service service, UploadServiceInfo serviceInfoObject) {
fillArtifacts(service, serviceInfoObject);
}
- private Map<String, byte[]> getCsarFromPayload(UploadServiceInfo innerElement) throws ZipException {
+ private Map<String, byte[]> getCsarFromPayload(UploadServiceInfo innerElement) {
String csarUUID = innerElement.getPayloadName();
String payloadData = innerElement.getPayloadData();
return getComponentCsarFromPayload(csarUUID, payloadData);
log.debug("enter fillServicePayloadDataFromFile");
byte[] data = new byte[(int) file.length()];
if (fileInputStream.read(data) == -1) {
- log.info("Invalid json was received.");
+ log.info(INVALID_JSON_WAS_RECEIVED);
ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
Response errorResp = buildErrorResponse(responseFormat);
responseWrapper.setInnerElement(errorResp);