Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / path / ForwardingPathValidator.java
1 package org.openecomp.sdc.be.components.path;
2
3 import fj.data.Either;
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.jsontitan.operations.ToscaOperationFacade;
11 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
12 import org.openecomp.sdc.exception.ResponseFormat;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Component;
17
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 @Component("forwardingPathValidator")
23 public class ForwardingPathValidator {
24
25     @Autowired
26     protected ToscaOperationFacade toscaOperationFacade;
27
28     private static final Logger LOGGER = LoggerFactory.getLogger(ForwardingPathValidator.class);
29     private static final int  PATH_NAME_LENGTH = 200;
30     private static final int  PROTOCOL_LENGTH = 200;
31     private static final int  DESTINATION_PORT_LENGTH = 200;
32
33     public Either<Boolean, ResponseFormat> validateForwardingPaths(Collection<ForwardingPathDataDefinition> paths,
34                                                                    String serviceId, boolean isUpdate) {
35         for (ForwardingPathDataDefinition path : paths) {
36             Either<Boolean, ResponseFormat> forwardingPathResponseEither = validateForwardingPath(path,
37                     serviceId, isUpdate);
38             if (forwardingPathResponseEither.isRight()) {
39                 return forwardingPathResponseEither;
40             }
41         }
42         return Either.left(Boolean.TRUE);
43     }
44
45     private Either<Boolean, ResponseFormat> validateForwardingPath(ForwardingPathDataDefinition path,
46                                                                    String serviceId, boolean isUpdate) {
47         ResponseFormatManager responseFormatManager = getResponseFormatManager();
48
49         Either<Boolean, ResponseFormat> errorResponseName = validateName(path,
50                 responseFormatManager, serviceId, isUpdate);
51         if (errorResponseName != null)
52             return errorResponseName;
53
54         Either<Boolean, ResponseFormat> protocolErrorResponse = validateProtocol(path, responseFormatManager);
55         if (protocolErrorResponse != null)
56             return protocolErrorResponse;
57
58         Either<Boolean, ResponseFormat> portNumberResponse = validateDestinationPortNumber(path, responseFormatManager);
59         if (portNumberResponse != null)
60             return portNumberResponse;
61
62         return Either.left(true);
63     }
64
65     private Either<Boolean, ResponseFormat> validateDestinationPortNumber(ForwardingPathDataDefinition dataDefinition,
66                                                                           ResponseFormatManager responseFormatManager) {
67         if (dataDefinition.getDestinationPortNumber() != null &&
68             dataDefinition.getDestinationPortNumber().length() > DESTINATION_PORT_LENGTH ) {
69             LOGGER.debug("Forwarding path destination port {} too long, , maximum allowed 200 characters ",
70                     dataDefinition.getDestinationPortNumber());
71             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
72                     .FORWARDING_PATH_DESTINATION_PORT_MAXIMUM_LENGTH, dataDefinition.getDestinationPortNumber());
73             return Either.right(errorResponse);
74         }
75         return null;
76     }
77
78     private Either<Boolean, ResponseFormat> validateProtocol(ForwardingPathDataDefinition dataDefinition,
79                                                              ResponseFormatManager responseFormatManager) {
80         if (dataDefinition.getProtocol() != null && dataDefinition.getProtocol().length() > PROTOCOL_LENGTH) {
81             LOGGER.debug("Forwarding path protocol {} too long, , maximum allowed 200 characters ", dataDefinition.getProtocol());
82             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
83                     .FORWARDING_PATH_PROTOCOL_MAXIMUM_LENGTH, dataDefinition.getProtocol());
84             return Either.right(errorResponse);
85         }
86         return null;
87     }
88
89     private Either<Boolean, ResponseFormat> validateName(ForwardingPathDataDefinition dataDefinition,
90                                                          ResponseFormatManager responseFormatManager,
91                                                          String serviceId, boolean isUpdate) {
92         String pathName = dataDefinition.getName();
93         Either<Boolean, ResponseFormat> pathEmptyResponse = validatePathNameIfEmpty(responseFormatManager, pathName);
94         if (pathEmptyResponse != null)
95             return pathEmptyResponse;
96
97         Either<Boolean, ResponseFormat> pathLengthResponse = validatePathNameLength(responseFormatManager, pathName);
98         if (pathLengthResponse != null)
99             return pathLengthResponse;
100
101         Either<Boolean, ResponseFormat> isPathNameUniqueResponse = validatePathIfUnique(dataDefinition, serviceId, isUpdate, responseFormatManager );
102         if(isPathNameUniqueResponse.isRight()) {
103             return Either.right(isPathNameUniqueResponse.right().value());
104         }
105         if (!isPathNameUniqueResponse.left().value()) {
106             LOGGER.debug("Forwarding path name {} already in use ", dataDefinition.getName());
107             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
108                     .FORWARDING_PATH_NAME_ALREADY_IN_USE, dataDefinition.getName());
109             return Either.right(errorResponse);
110         }
111         return null;
112     }
113
114     private Either<Boolean, ResponseFormat> validatePathNameLength(ResponseFormatManager responseFormatManager, String pathName) {
115         if (pathName.length() > PATH_NAME_LENGTH) {
116             LOGGER.debug("Forwarding path name  {} too long, , maximum allowed 200 characters ", pathName);
117             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
118                     .FORWARDING_PATH_NAME_MAXIMUM_LENGTH, pathName);
119             return Either.right(errorResponse);
120         }
121         return null;
122     }
123
124     private Either<Boolean, ResponseFormat> validatePathNameIfEmpty(ResponseFormatManager responseFormatManager, String pathName) {
125         if (StringUtils.isEmpty(pathName)) {
126             LOGGER.debug("Forwarding Path Name can't be empty");
127             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus.FORWARDING_PATH_NAME_EMPTY);
128             return Either.right(errorResponse);
129         }
130         return null;
131     }
132
133
134     private Either<Boolean, ResponseFormat> validatePathIfUnique(ForwardingPathDataDefinition dataDefinition, String serviceId,
135                                                                  boolean isUpdate, ResponseFormatManager responseFormatManager) {
136         boolean isPathNameUnique = false;
137         ComponentParametersView filter = new ComponentParametersView(true);
138         filter.setIgnoreForwardingPath(false);
139         Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade
140                 .getToscaElement(serviceId, filter);
141         if (forwardingPathOrigin.isRight()){
142             return Either.right(responseFormatManager.getResponseFormat(ActionStatus.GENERAL_ERROR));
143         }
144         Collection<ForwardingPathDataDefinition> allPaths = forwardingPathOrigin.left().value().getForwardingPaths().values();
145         Map<String, String> pathNames = new HashMap<>();
146         allPaths.forEach( path -> pathNames.put(path.getUniqueId(), path.getName()) );
147
148         if (isUpdate){
149             for(Map.Entry<String, String> entry : pathNames.entrySet()){
150                 if (entry.getKey().equals(dataDefinition.getUniqueId()) && entry.getValue().
151                         equals(dataDefinition.getName())) {
152                     isPathNameUnique = true;
153                 }
154
155                 if(entry.getKey().equals(dataDefinition.getUniqueId()) && !pathNames.values().contains(dataDefinition.getName())){
156                     isPathNameUnique = true;
157                 }
158             }
159         }
160         else
161         if (!pathNames.values().contains(dataDefinition.getName())){
162             isPathNameUnique = true;
163         }
164
165         return Either.left(isPathNameUnique);
166     }
167
168     protected ResponseFormatManager getResponseFormatManager() {
169         return ResponseFormatManager.getInstance();
170     }
171
172
173 }