2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.path;
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.components.impl.exceptions.ByActionStatusComponentException;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition;
29 import org.openecomp.sdc.be.model.ComponentParametersView;
30 import org.openecomp.sdc.be.model.Service;
31 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
37 import java.util.Collection;
38 import java.util.HashMap;
41 @Component("forwardingPathValidator")
42 public class ForwardingPathValidator {
45 protected ToscaOperationFacade toscaOperationFacade;
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;
52 public void validateForwardingPaths(Collection<ForwardingPathDataDefinition> paths,
53 String serviceId, boolean isUpdate) {
54 for (ForwardingPathDataDefinition path : paths) {
55 validateForwardingPath(path, serviceId, isUpdate);
59 private void validateForwardingPath(ForwardingPathDataDefinition path, String serviceId, boolean isUpdate) {
60 ResponseFormatManager responseFormatManager = getResponseFormatManager();
61 validateName(path, responseFormatManager, serviceId, isUpdate);
62 validateProtocol(path);
63 validateDestinationPortNumber(path);
66 private void validateDestinationPortNumber(ForwardingPathDataDefinition dataDefinition) {
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 throw new ByActionStatusComponentException(ActionStatus
72 .FORWARDING_PATH_DESTINATION_PORT_MAXIMUM_LENGTH, dataDefinition.getDestinationPortNumber());
76 private void validateProtocol(ForwardingPathDataDefinition dataDefinition) {
77 if (dataDefinition.getProtocol() != null && dataDefinition.getProtocol().length() > PROTOCOL_LENGTH) {
78 logger.debug("Forwarding path protocol {} too long, , maximum allowed 200 characters ", dataDefinition.getProtocol());
79 throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_PROTOCOL_MAXIMUM_LENGTH, dataDefinition.getProtocol());
83 private void validateName(ForwardingPathDataDefinition dataDefinition,
84 ResponseFormatManager responseFormatManager,
85 String serviceId, boolean isUpdate) {
86 String pathName = dataDefinition.getName();
87 validatePathNameIfEmpty(responseFormatManager, pathName);
89 validatePathNameLength(responseFormatManager, pathName);
91 Boolean isPathNameUniqueResponse = validatePathIfUnique(dataDefinition, serviceId, isUpdate, responseFormatManager );
92 if (!isPathNameUniqueResponse) {
93 logger.debug("Forwarding path name {} already in use ", dataDefinition.getName());
94 throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_NAME_ALREADY_IN_USE, dataDefinition.getName());
98 private void validatePathNameLength(ResponseFormatManager responseFormatManager, String pathName) {
99 if (pathName.length() > PATH_NAME_LENGTH) {
100 logger.debug("Forwarding path name {} too long, , maximum allowed 200 characters ", pathName);
101 throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_NAME_MAXIMUM_LENGTH, pathName);
105 private void validatePathNameIfEmpty(ResponseFormatManager responseFormatManager, String pathName) {
106 if (StringUtils.isEmpty(pathName)) {
107 logger.debug("Forwarding Path Name can't be empty");
108 throw new ByActionStatusComponentException(ActionStatus.FORWARDING_PATH_NAME_EMPTY);
113 private Boolean validatePathIfUnique(ForwardingPathDataDefinition dataDefinition, String serviceId,
114 boolean isUpdate, ResponseFormatManager responseFormatManager) {
115 boolean isPathNameUnique = false;
116 ComponentParametersView filter = new ComponentParametersView(true);
117 filter.setIgnoreForwardingPath(false);
118 Either<Service, StorageOperationStatus> forwardingPathOrigin = toscaOperationFacade
119 .getToscaElement(serviceId, filter);
120 if (forwardingPathOrigin.isRight()){
121 throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
123 Collection<ForwardingPathDataDefinition> allPaths = forwardingPathOrigin.left().value().getForwardingPaths().values();
124 Map<String, String> pathNames = new HashMap<>();
125 allPaths.forEach( path -> pathNames.put(path.getUniqueId(), path.getName()) );
128 for(Map.Entry<String, String> entry : pathNames.entrySet()){
129 if (entry.getKey().equals(dataDefinition.getUniqueId()) && entry.getValue().
130 equals(dataDefinition.getName())) {
131 isPathNameUnique = true;
134 if(entry.getKey().equals(dataDefinition.getUniqueId()) && !pathNames.values().contains(dataDefinition.getName())){
135 isPathNameUnique = true;
140 if (!pathNames.values().contains(dataDefinition.getName())){
141 isPathNameUnique = true;
144 return isPathNameUnique;
147 protected ResponseFormatManager getResponseFormatManager() {
148 return ResponseFormatManager.getInstance();