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=========================================================
20 package org.openecomp.sdc.be.components.path;
22 import fj.data.Either;
23 import java.util.Collection;
24 import java.util.HashMap;
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;
39 @Component("forwardingPathValidator")
40 public class ForwardingPathValidator {
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;
49 public void validateForwardingPaths(Collection<ForwardingPathDataDefinition> paths, String serviceId, boolean isUpdate) {
50 for (ForwardingPathDataDefinition path : paths) {
51 validateForwardingPath(path, serviceId, isUpdate);
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);
62 private void validateDestinationPortNumber(ForwardingPathDataDefinition dataDefinition) {
63 if (dataDefinition.getDestinationPortNumber() != null && dataDefinition.getDestinationPortNumber().length() > DESTINATION_PORT_LENGTH) {
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());
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());
78 private void validateName(ForwardingPathDataDefinition dataDefinition, ResponseFormatManager responseFormatManager, String serviceId,
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());
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);
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);
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);
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()));
117 for (Map.Entry<String, String> entry : pathNames.entrySet()) {
118 if (entry.getKey().equals(dataDefinition.getUniqueId()) && entry.getValue().equals(dataDefinition.getName())) {
119 isPathNameUnique = true;
121 if (entry.getKey().equals(dataDefinition.getUniqueId()) && !pathNames.values().contains(dataDefinition.getName())) {
122 isPathNameUnique = true;
125 } else if (!pathNames.values().contains(dataDefinition.getName())) {
126 isPathNameUnique = true;
128 return isPathNameUnique;
131 protected ResponseFormatManager getResponseFormatManager() {
132 return ResponseFormatManager.getInstance();