1 package org.openecomp.sdc.be.components.path;
4 import org.apache.commons.lang.StringUtils;
5 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
6 import org.openecomp.sdc.be.dao.api.ActionStatus;
7 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
8 import org.openecomp.sdc.be.model.ComponentParametersView;
9 import org.openecomp.sdc.be.model.Service;
10 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
11 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
12 import org.openecomp.sdc.common.log.wrappers.Logger;
13 import org.openecomp.sdc.exception.ResponseFormat;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Component;
17 import java.util.Collection;
18 import java.util.HashMap;
21 @Component("forwardingPathValidator")
22 public class ForwardingPathValidator {
25 protected ToscaOperationFacade toscaOperationFacade;
27 private static final Logger logger = Logger.getLogger(ForwardingPathValidator.class);
28 private static final int PATH_NAME_LENGTH = 200;
29 private static final int PROTOCOL_LENGTH = 200;
30 private static final int DESTINATION_PORT_LENGTH = 200;
32 public Either<Boolean, ResponseFormat> validateForwardingPaths(Collection<ForwardingPathDataDefinition> paths,
33 String serviceId, boolean isUpdate) {
34 for (ForwardingPathDataDefinition path : paths) {
35 Either<Boolean, ResponseFormat> forwardingPathResponseEither = validateForwardingPath(path,
37 if (forwardingPathResponseEither.isRight()) {
38 return forwardingPathResponseEither;
41 return Either.left(Boolean.TRUE);
44 private Either<Boolean, ResponseFormat> validateForwardingPath(ForwardingPathDataDefinition path,
45 String serviceId, boolean isUpdate) {
46 ResponseFormatManager responseFormatManager = getResponseFormatManager();
48 Either<Boolean, ResponseFormat> errorResponseName = validateName(path,
49 responseFormatManager, serviceId, isUpdate);
50 if (errorResponseName != null)
51 return errorResponseName;
53 Either<Boolean, ResponseFormat> protocolErrorResponse = validateProtocol(path, responseFormatManager);
54 if (protocolErrorResponse != null)
55 return protocolErrorResponse;
57 Either<Boolean, ResponseFormat> portNumberResponse = validateDestinationPortNumber(path, responseFormatManager);
58 if (portNumberResponse != null)
59 return portNumberResponse;
61 return Either.left(true);
64 private Either<Boolean, ResponseFormat> validateDestinationPortNumber(ForwardingPathDataDefinition dataDefinition,
65 ResponseFormatManager responseFormatManager) {
66 if (dataDefinition.getDestinationPortNumber() != null &&
67 dataDefinition.getDestinationPortNumber().length() > DESTINATION_PORT_LENGTH ) {
68 logger.debug("Forwarding path destination port {} too long, , maximum allowed 200 characters ",
69 dataDefinition.getDestinationPortNumber());
70 ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
71 .FORWARDING_PATH_DESTINATION_PORT_MAXIMUM_LENGTH, dataDefinition.getDestinationPortNumber());
72 return Either.right(errorResponse);
77 private Either<Boolean, ResponseFormat> validateProtocol(ForwardingPathDataDefinition dataDefinition,
78 ResponseFormatManager responseFormatManager) {
79 if (dataDefinition.getProtocol() != null && dataDefinition.getProtocol().length() > PROTOCOL_LENGTH) {
80 logger.debug("Forwarding path protocol {} too long, , maximum allowed 200 characters ", dataDefinition.getProtocol());
81 ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
82 .FORWARDING_PATH_PROTOCOL_MAXIMUM_LENGTH, dataDefinition.getProtocol());
83 return Either.right(errorResponse);
88 private Either<Boolean, ResponseFormat> validateName(ForwardingPathDataDefinition dataDefinition,
89 ResponseFormatManager responseFormatManager,
90 String serviceId, boolean isUpdate) {
91 String pathName = dataDefinition.getName();
92 Either<Boolean, ResponseFormat> pathEmptyResponse = validatePathNameIfEmpty(responseFormatManager, pathName);
93 if (pathEmptyResponse != null)
94 return pathEmptyResponse;
96 Either<Boolean, ResponseFormat> pathLengthResponse = validatePathNameLength(responseFormatManager, pathName);
97 if (pathLengthResponse != null)
98 return pathLengthResponse;
100 Either<Boolean, ResponseFormat> isPathNameUniqueResponse = validatePathIfUnique(dataDefinition, serviceId, isUpdate, responseFormatManager );
101 if(isPathNameUniqueResponse.isRight()) {
102 return Either.right(isPathNameUniqueResponse.right().value());
104 if (!isPathNameUniqueResponse.left().value()) {
105 logger.debug("Forwarding path name {} already in use ", dataDefinition.getName());
106 ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
107 .FORWARDING_PATH_NAME_ALREADY_IN_USE, dataDefinition.getName());
108 return Either.right(errorResponse);
113 private Either<Boolean, ResponseFormat> validatePathNameLength(ResponseFormatManager responseFormatManager, String pathName) {
114 if (pathName.length() > PATH_NAME_LENGTH) {
115 logger.debug("Forwarding path name {} too long, , maximum allowed 200 characters ", pathName);
116 ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
117 .FORWARDING_PATH_NAME_MAXIMUM_LENGTH, pathName);
118 return Either.right(errorResponse);
123 private Either<Boolean, ResponseFormat> validatePathNameIfEmpty(ResponseFormatManager responseFormatManager, String pathName) {
124 if (StringUtils.isEmpty(pathName)) {
125 logger.debug("Forwarding Path Name can't be empty");
126 ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus.FORWARDING_PATH_NAME_EMPTY);
127 return Either.right(errorResponse);
133 private Either<Boolean, ResponseFormat> validatePathIfUnique(ForwardingPathDataDefinition dataDefinition, String serviceId,
134 boolean isUpdate, ResponseFormatManager responseFormatManager) {
135 boolean isPathNameUnique = false;
136 ComponentParametersView filter = new ComponentParametersView(true);
137 filter.setIgnoreForwardingPath(false);
138 Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade
139 .getToscaElement(serviceId, filter);
140 if (forwardingPathOrigin.isRight()){
141 return Either.right(responseFormatManager.getResponseFormat(ActionStatus.GENERAL_ERROR));
143 Collection<ForwardingPathDataDefinition> allPaths = forwardingPathOrigin.left().value().getForwardingPaths().values();
144 Map<String, String> pathNames = new HashMap<>();
145 allPaths.forEach( path -> pathNames.put(path.getUniqueId(), path.getName()) );
148 for(Map.Entry<String, String> entry : pathNames.entrySet()){
149 if (entry.getKey().equals(dataDefinition.getUniqueId()) && entry.getValue().
150 equals(dataDefinition.getName())) {
151 isPathNameUnique = true;
154 if(entry.getKey().equals(dataDefinition.getUniqueId()) && !pathNames.values().contains(dataDefinition.getName())){
155 isPathNameUnique = true;
160 if (!pathNames.values().contains(dataDefinition.getName())){
161 isPathNameUnique = true;
164 return Either.left(isPathNameUnique);
167 protected ResponseFormatManager getResponseFormatManager() {
168 return ResponseFormatManager.getInstance();