Update vulnerable package dependencies
[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 package org.openecomp.sdc.be.components.path;
21
22 import fj.data.Either;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.apache.commons.lang3.StringUtils;
27 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
28 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
31 import org.openecomp.sdc.be.model.ComponentParametersView;
32 import org.openecomp.sdc.be.model.Service;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
34 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 @Component("forwardingPathValidator")
40 public class ForwardingPathValidator {
41
42     @Autowired
43     protected ToscaOperationFacade toscaOperationFacade;
44     private static final Logger logger = Logger.getLogger(ForwardingPathValidator.class);
45     private static final int PATH_NAME_LENGTH = 200;
46     private static final int PROTOCOL_LENGTH = 200;
47     private static final int DESTINATION_PORT_LENGTH = 200;
48
49     public void validateForwardingPaths(Collection<ForwardingPathDataDefinition> paths, String serviceId, boolean isUpdate) {
50         for (ForwardingPathDataDefinition path : paths) {
51             validateForwardingPath(path, serviceId, isUpdate);
52         }
53     }
54
55     private void validateForwardingPath(ForwardingPathDataDefinition path, String serviceId, boolean isUpdate) {
56         ResponseFormatManager responseFormatManager = getResponseFormatManager();
57         validateName(path, responseFormatManager, serviceId, isUpdate);
58         validateProtocol(path);
59         validateDestinationPortNumber(path);
60     }
61
62     private void validateDestinationPortNumber(ForwardingPathDataDefinition dataDefinition) {
63         if (dataDefinition.getDestinationPortNumber() != null && dataDefinition.getDestinationPortNumber().length() > DESTINATION_PORT_LENGTH) {
64             logger
65                 .debug("Forwarding path destination port {} too long, , maximum allowed 200 characters ", dataDefinition.getDestinationPortNumber());
66             throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_DESTINATION_PORT_MAXIMUM_LENGTH,
67                 dataDefinition.getDestinationPortNumber());
68         }
69     }
70
71     private void validateProtocol(ForwardingPathDataDefinition dataDefinition) {
72         if (dataDefinition.getProtocol() != null && dataDefinition.getProtocol().length() > PROTOCOL_LENGTH) {
73             logger.debug("Forwarding path protocol {} too long, , maximum allowed 200 characters ", dataDefinition.getProtocol());
74             throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_PROTOCOL_MAXIMUM_LENGTH, dataDefinition.getProtocol());
75         }
76     }
77
78     private void validateName(ForwardingPathDataDefinition dataDefinition, ResponseFormatManager responseFormatManager, String serviceId,
79                               boolean isUpdate) {
80         String pathName = dataDefinition.getName();
81         validatePathNameIfEmpty(responseFormatManager, pathName);
82         validatePathNameLength(responseFormatManager, pathName);
83         Boolean isPathNameUniqueResponse = validatePathIfUnique(dataDefinition, serviceId, isUpdate, responseFormatManager);
84         if (!isPathNameUniqueResponse) {
85             logger.debug("Forwarding path name {} already in use ", dataDefinition.getName());
86             throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_NAME_ALREADY_IN_USE, dataDefinition.getName());
87         }
88     }
89
90     private void validatePathNameLength(ResponseFormatManager responseFormatManager, String pathName) {
91         if (pathName.length() > PATH_NAME_LENGTH) {
92             logger.debug("Forwarding path name  {} too long, , maximum allowed 200 characters ", pathName);
93             throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_NAME_MAXIMUM_LENGTH, pathName);
94         }
95     }
96
97     private void validatePathNameIfEmpty(ResponseFormatManager responseFormatManager, String pathName) {
98         if (StringUtils.isEmpty(pathName)) {
99             logger.debug("Forwarding Path Name can't be empty");
100             throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_NAME_EMPTY);
101         }
102     }
103
104     private Boolean validatePathIfUnique(ForwardingPathDataDefinition dataDefinition, String serviceId, boolean isUpdate,
105                                          ResponseFormatManager responseFormatManager) {
106         boolean isPathNameUnique = false;
107         ComponentParametersView filter = new ComponentParametersView(true);
108         filter.setIgnoreServicePath(false);
109         Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade.getToscaElement(serviceId, filter);
110         if (forwardingPathOrigin.isRight()) {
111             throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
112         }
113         Collection<ForwardingPathDataDefinition> allPaths = forwardingPathOrigin.left().value().getForwardingPaths().values();
114         Map<String, String> pathNames = new HashMap<>();
115         allPaths.forEach(path -> pathNames.put(path.getUniqueId(), path.getName()));
116         if (isUpdate) {
117             for (Map.Entry<String, String> entry : pathNames.entrySet()) {
118                 if (entry.getKey().equals(dataDefinition.getUniqueId()) && entry.getValue().equals(dataDefinition.getName())) {
119                     isPathNameUnique = true;
120                 }
121                 if (entry.getKey().equals(dataDefinition.getUniqueId()) && !pathNames.values().contains(dataDefinition.getName())) {
122                     isPathNameUnique = true;
123                 }
124             }
125         } else if (!pathNames.values().contains(dataDefinition.getName())) {
126             isPathNameUnique = true;
127         }
128         return isPathNameUnique;
129     }
130
131     protected ResponseFormatManager getResponseFormatManager() {
132         return ResponseFormatManager.getInstance();
133     }
134 }