2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.design.dbervices;
25 import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION;
26 import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION_LEVEL;
27 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_CONTENTS;
28 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_NAME;
29 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_TYPE;
30 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_VERSOIN;
31 import static org.onap.appc.design.services.util.DesignServiceConstants.GETARTIFACT;
32 import static org.onap.appc.design.services.util.DesignServiceConstants.GETAPPCTIMESTAMPUTC;
33 import static org.onap.appc.design.services.util.DesignServiceConstants.GETDESIGNS;
34 import static org.onap.appc.design.services.util.DesignServiceConstants.GETSTATUS;
35 import static org.onap.appc.design.services.util.DesignServiceConstants.PROTOCOL;
36 import static org.onap.appc.design.services.util.DesignServiceConstants.SETINCART;
37 import static org.onap.appc.design.services.util.DesignServiceConstants.SETPROTOCOLREFERENCE;
38 import static org.onap.appc.design.services.util.DesignServiceConstants.SETSTATUS;
39 import static org.onap.appc.design.services.util.DesignServiceConstants.STATUS;
40 import static org.onap.appc.design.services.util.DesignServiceConstants.UPLOADARTIFACT;
41 import static org.onap.appc.design.services.util.DesignServiceConstants.USER_ID;
42 import static org.onap.appc.design.services.util.DesignServiceConstants.VNF_TYPE;
44 import com.att.eelf.configuration.EELFLogger;
45 import com.att.eelf.configuration.EELFManager;
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import java.io.IOException;
50 import org.onap.appc.design.services.util.DesignServiceConstants;
52 public class RequestValidator {
54 private static final EELFLogger log = EELFManager.getInstance().getLogger(RequestValidator.class);
56 private RequestValidator() {
59 public static void validate(String action, String payload) throws RequestValidationException, IOException {
60 log.info("validate: action:" + action );
61 log.info("validate: payload:" + payload);
62 ObjectMapper objectMapper = new ObjectMapper();
63 JsonNode payloadObject = objectMapper.readTree(payload);
64 log.info("payloadObject:" + payloadObject.get(ARTIFACT_CONTENTS));
66 String errorString= null;
69 errorString = resolveGetDesignsErrorString(payloadObject);
71 case GETAPPCTIMESTAMPUTC:
72 log.info("validate: No payload validation needed for Timestamp.");
75 errorString = resolveGetArtifactErrorString(payloadObject);
78 errorString = resolveGetStatusErrorString(payloadObject);
81 errorString = resolveSetStatusErrorString(payloadObject);
84 errorString = resolveUploadArtifactErrorString(payloadObject);
86 case DesignServiceConstants.UPLOADADMINARTIFACT:
87 errorString = resolveUploadArtifactErrorString(payloadObject);
89 case DesignServiceConstants.RETRIEVEVNFPERMISSIONS:
90 errorString = resolveRetrieveVnfPermissionsErrorString(payloadObject);
92 case SETPROTOCOLREFERENCE:
94 errorString = resolveErrorString(payloadObject);
96 case DesignServiceConstants.CHECKVNF:
97 errorString = resolveCheckVNFErrorString(payloadObject);
99 case DesignServiceConstants.SAVEVNFPERMISSIONS:
100 errorString = resolveSaveUserPermissionErrorString(payloadObject);
103 throw new RequestValidationException(" Action " + action + " not found while processing request ");
105 checkForErrorString(errorString);
108 private static String resolveSaveUserPermissionErrorString(JsonNode payloadObject) {
109 if (nullOrEmpty(payloadObject, VNF_TYPE))
114 private static String resolveCheckVNFErrorString(JsonNode payloadObject) {
115 if (nullOrEmpty(payloadObject, VNF_TYPE))
120 private static void checkForErrorString(String errorString) throws RequestValidationException {
121 if (errorString != null) {
122 throw new RequestValidationException(" Missing input parameter :-" + errorString + " -:");
126 private static String resolveGetDesignsErrorString(JsonNode payloadObject) {
127 return nullOrEmpty(payloadObject, USER_ID) ? USER_ID : null;
130 private static String resolveErrorString(JsonNode payloadObject) {
131 if (nullOrEmpty(payloadObject, ACTION)) {
133 } else if (nullOrEmpty(payloadObject, ACTION_LEVEL)) {
135 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
137 } else if (nullOrEmpty(payloadObject, PROTOCOL)) {
143 private static String resolveUploadArtifactErrorString(JsonNode payloadObject) {
144 if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
145 return ARTIFACT_NAME;
146 } else if (doesNotContainReference(payloadObject)) {
148 } else if (nullOrEmpty(payloadObject, ARTIFACT_VERSOIN)) {
149 return ARTIFACT_VERSOIN;
150 } else if (payloadObject.get(ARTIFACT_CONTENTS) == null) {
151 return ARTIFACT_CONTENTS;
152 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
153 return ARTIFACT_TYPE;
154 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
160 private static boolean doesNotContainReference(JsonNode payloadObject) {
162 !payloadObject.get(ARTIFACT_NAME).textValue().contains("reference")
163 && nullOrEmpty(payloadObject, ARTIFACT_NAME);
166 private static String resolveSetStatusErrorString(JsonNode payloadObject) {
167 if (nullOrEmpty(payloadObject, USER_ID)) {
169 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
171 } else if (nullOrEmpty(payloadObject, ACTION)) {
173 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
174 return ARTIFACT_TYPE;
175 } else if (nullOrEmpty(payloadObject, STATUS)) {
181 private static String resolveGetStatusErrorString(JsonNode payloadObject) {
182 if (nullOrEmpty(payloadObject, USER_ID)) {
184 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
190 private static String resolveGetArtifactErrorString(JsonNode payloadObject) {
191 if (nullOrEmpty(payloadObject, VNF_TYPE)) {
193 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
194 return ARTIFACT_TYPE;
195 } else if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
196 return ARTIFACT_NAME;
201 private static boolean nullOrEmpty(JsonNode payloadObject, String param) {
202 JsonNode payload = payloadObject.get(param);
203 return payload == null || payload.textValue().trim().isEmpty();
206 private static String resolveRetrieveVnfPermissionsErrorString(JsonNode payloadObject) {
207 if (nullOrEmpty(payloadObject, VNF_TYPE)) {