Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / path / ForwardingPathValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.path;
22
23 import fj.data.Either;
24 import org.apache.commons.lang.StringUtils;
25 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
28 import org.openecomp.sdc.be.model.ComponentParametersView;
29 import org.openecomp.sdc.be.model.Service;
30 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
31 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.openecomp.sdc.exception.ResponseFormat;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.Map;
40
41 @Component("forwardingPathValidator")
42 public class ForwardingPathValidator {
43
44     @Autowired
45     protected ToscaOperationFacade toscaOperationFacade;
46
47     private static final Logger logger = Logger.getLogger(ForwardingPathValidator.class);
48     private static final int  PATH_NAME_LENGTH = 200;
49     private static final int  PROTOCOL_LENGTH = 200;
50     private static final int  DESTINATION_PORT_LENGTH = 200;
51
52     public Either<Boolean, ResponseFormat> validateForwardingPaths(Collection<ForwardingPathDataDefinition> paths,
53                                                                    String serviceId, boolean isUpdate) {
54         for (ForwardingPathDataDefinition path : paths) {
55             Either<Boolean, ResponseFormat> forwardingPathResponseEither = validateForwardingPath(path,
56                     serviceId, isUpdate);
57             if (forwardingPathResponseEither.isRight()) {
58                 return forwardingPathResponseEither;
59             }
60         }
61         return Either.left(Boolean.TRUE);
62     }
63
64     private Either<Boolean, ResponseFormat> validateForwardingPath(ForwardingPathDataDefinition path,
65                                                                    String serviceId, boolean isUpdate) {
66         ResponseFormatManager responseFormatManager = getResponseFormatManager();
67
68         Either<Boolean, ResponseFormat> errorResponseName = validateName(path,
69                 responseFormatManager, serviceId, isUpdate);
70         if (errorResponseName != null)
71             return errorResponseName;
72
73         Either<Boolean, ResponseFormat> protocolErrorResponse = validateProtocol(path, responseFormatManager);
74         if (protocolErrorResponse != null)
75             return protocolErrorResponse;
76
77         Either<Boolean, ResponseFormat> portNumberResponse = validateDestinationPortNumber(path, responseFormatManager);
78         if (portNumberResponse != null)
79             return portNumberResponse;
80
81         return Either.left(true);
82     }
83
84     private Either<Boolean, ResponseFormat> validateDestinationPortNumber(ForwardingPathDataDefinition dataDefinition,
85                                                                           ResponseFormatManager responseFormatManager) {
86         if (dataDefinition.getDestinationPortNumber() != null &&
87             dataDefinition.getDestinationPortNumber().length() > DESTINATION_PORT_LENGTH ) {
88             logger.debug("Forwarding path destination port {} too long, , maximum allowed 200 characters ",
89                     dataDefinition.getDestinationPortNumber());
90             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
91                     .FORWARDING_PATH_DESTINATION_PORT_MAXIMUM_LENGTH, dataDefinition.getDestinationPortNumber());
92             return Either.right(errorResponse);
93         }
94         return null;
95     }
96
97     private Either<Boolean, ResponseFormat> validateProtocol(ForwardingPathDataDefinition dataDefinition,
98                                                              ResponseFormatManager responseFormatManager) {
99         if (dataDefinition.getProtocol() != null && dataDefinition.getProtocol().length() > PROTOCOL_LENGTH) {
100             logger.debug("Forwarding path protocol {} too long, , maximum allowed 200 characters ", dataDefinition.getProtocol());
101             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
102                     .FORWARDING_PATH_PROTOCOL_MAXIMUM_LENGTH, dataDefinition.getProtocol());
103             return Either.right(errorResponse);
104         }
105         return null;
106     }
107
108     private Either<Boolean, ResponseFormat> validateName(ForwardingPathDataDefinition dataDefinition,
109                                                          ResponseFormatManager responseFormatManager,
110                                                          String serviceId, boolean isUpdate) {
111         String pathName = dataDefinition.getName();
112         Either<Boolean, ResponseFormat> pathEmptyResponse = validatePathNameIfEmpty(responseFormatManager, pathName);
113         if (pathEmptyResponse != null)
114             return pathEmptyResponse;
115
116         Either<Boolean, ResponseFormat> pathLengthResponse = validatePathNameLength(responseFormatManager, pathName);
117         if (pathLengthResponse != null)
118             return pathLengthResponse;
119
120         Either<Boolean, ResponseFormat> isPathNameUniqueResponse = validatePathIfUnique(dataDefinition, serviceId, isUpdate, responseFormatManager );
121         if(isPathNameUniqueResponse.isRight()) {
122             return Either.right(isPathNameUniqueResponse.right().value());
123         }
124         if (!isPathNameUniqueResponse.left().value()) {
125             logger.debug("Forwarding path name {} already in use ", dataDefinition.getName());
126             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
127                     .FORWARDING_PATH_NAME_ALREADY_IN_USE, dataDefinition.getName());
128             return Either.right(errorResponse);
129         }
130         return null;
131     }
132
133     private Either<Boolean, ResponseFormat> validatePathNameLength(ResponseFormatManager responseFormatManager, String pathName) {
134         if (pathName.length() > PATH_NAME_LENGTH) {
135             logger.debug("Forwarding path name  {} too long, , maximum allowed 200 characters ", pathName);
136             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus
137                     .FORWARDING_PATH_NAME_MAXIMUM_LENGTH, pathName);
138             return Either.right(errorResponse);
139         }
140         return null;
141     }
142
143     private Either<Boolean, ResponseFormat> validatePathNameIfEmpty(ResponseFormatManager responseFormatManager, String pathName) {
144         if (StringUtils.isEmpty(pathName)) {
145             logger.debug("Forwarding Path Name can't be empty");
146             ResponseFormat errorResponse = responseFormatManager.getResponseFormat(ActionStatus.FORWARDING_PATH_NAME_EMPTY);
147             return Either.right(errorResponse);
148         }
149         return null;
150     }
151
152
153     private Either<Boolean, ResponseFormat> validatePathIfUnique(ForwardingPathDataDefinition dataDefinition, String serviceId,
154                                                                  boolean isUpdate, ResponseFormatManager responseFormatManager) {
155         boolean isPathNameUnique = false;
156         ComponentParametersView filter = new ComponentParametersView(true);
157         filter.setIgnoreForwardingPath(false);
158         Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade
159                 .getToscaElement(serviceId, filter);
160         if (forwardingPathOrigin.isRight()){
161             return Either.right(responseFormatManager.getResponseFormat(ActionStatus.GENERAL_ERROR));
162         }
163         Collection<ForwardingPathDataDefinition> allPaths = forwardingPathOrigin.left().value().getForwardingPaths().values();
164         Map<String, String> pathNames = new HashMap<>();
165         allPaths.forEach( path -> pathNames.put(path.getUniqueId(), path.getName()) );
166
167         if (isUpdate){
168             for(Map.Entry<String, String> entry : pathNames.entrySet()){
169                 if (entry.getKey().equals(dataDefinition.getUniqueId()) && entry.getValue().
170                         equals(dataDefinition.getName())) {
171                     isPathNameUnique = true;
172                 }
173
174                 if(entry.getKey().equals(dataDefinition.getUniqueId()) && !pathNames.values().contains(dataDefinition.getName())){
175                     isPathNameUnique = true;
176                 }
177             }
178         }
179         else
180         if (!pathNames.values().contains(dataDefinition.getName())){
181             isPathNameUnique = true;
182         }
183
184         return Either.left(isPathNameUnique);
185     }
186
187     protected ResponseFormatManager getResponseFormatManager() {
188         return ResponseFormatManager.getInstance();
189     }
190
191
192 }