re base code
[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.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;
16
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 @Component("forwardingPathValidator")
22 public class ForwardingPathValidator {
23
24     @Autowired
25     protected ToscaOperationFacade toscaOperationFacade;
26
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;
31
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,
36                     serviceId, isUpdate);
37             if (forwardingPathResponseEither.isRight()) {
38                 return forwardingPathResponseEither;
39             }
40         }
41         return Either.left(Boolean.TRUE);
42     }
43
44     private Either<Boolean, ResponseFormat> validateForwardingPath(ForwardingPathDataDefinition path,
45                                                                    String serviceId, boolean isUpdate) {
46         ResponseFormatManager responseFormatManager = getResponseFormatManager();
47
48         Either<Boolean, ResponseFormat> errorResponseName = validateName(path,
49                 responseFormatManager, serviceId, isUpdate);
50         if (errorResponseName != null)
51             return errorResponseName;
52
53         Either<Boolean, ResponseFormat> protocolErrorResponse = validateProtocol(path, responseFormatManager);
54         if (protocolErrorResponse != null)
55             return protocolErrorResponse;
56
57         Either<Boolean, ResponseFormat> portNumberResponse = validateDestinationPortNumber(path, responseFormatManager);
58         if (portNumberResponse != null)
59             return portNumberResponse;
60
61         return Either.left(true);
62     }
63
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);
73         }
74         return null;
75     }
76
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);
84         }
85         return null;
86     }
87
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;
95
96         Either<Boolean, ResponseFormat> pathLengthResponse = validatePathNameLength(responseFormatManager, pathName);
97         if (pathLengthResponse != null)
98             return pathLengthResponse;
99
100         Either<Boolean, ResponseFormat> isPathNameUniqueResponse = validatePathIfUnique(dataDefinition, serviceId, isUpdate, responseFormatManager );
101         if(isPathNameUniqueResponse.isRight()) {
102             return Either.right(isPathNameUniqueResponse.right().value());
103         }
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);
109         }
110         return null;
111     }
112
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);
119         }
120         return null;
121     }
122
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);
128         }
129         return null;
130     }
131
132
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));
142         }
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()) );
146
147         if (isUpdate){
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;
152                 }
153
154                 if(entry.getKey().equals(dataDefinition.getUniqueId()) && !pathNames.values().contains(dataDefinition.getName())){
155                     isPathNameUnique = true;
156                 }
157             }
158         }
159         else
160         if (!pathNames.values().contains(dataDefinition.getName())){
161             isPathNameUnique = true;
162         }
163
164         return Either.left(isPathNameUnique);
165     }
166
167     protected ResponseFormatManager getResponseFormatManager() {
168         return ResponseFormatManager.getInstance();
169     }
170
171
172 }