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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.design.dbervices;
26 import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION;
27 import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION_LEVEL;
28 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_CONTENTS;
29 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_NAME;
30 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_TYPE;
31 import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_VERSOIN;
32 import static org.onap.appc.design.services.util.DesignServiceConstants.GETARTIFACT;
33 import static org.onap.appc.design.services.util.DesignServiceConstants.GETAPPCTIMESTAMPUTC;
34 import static org.onap.appc.design.services.util.DesignServiceConstants.GETDESIGNS;
35 import static org.onap.appc.design.services.util.DesignServiceConstants.GETSTATUS;
36 import static org.onap.appc.design.services.util.DesignServiceConstants.PROTOCOL;
37 import static org.onap.appc.design.services.util.DesignServiceConstants.SETINCART;
38 import static org.onap.appc.design.services.util.DesignServiceConstants.SETPROTOCOLREFERENCE;
39 import static org.onap.appc.design.services.util.DesignServiceConstants.SETSTATUS;
40 import static org.onap.appc.design.services.util.DesignServiceConstants.STATUS;
41 import static org.onap.appc.design.services.util.DesignServiceConstants.UPLOADARTIFACT;
42 import static org.onap.appc.design.services.util.DesignServiceConstants.USER_ID;
43 import static org.onap.appc.design.services.util.DesignServiceConstants.VNF_TYPE;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import com.fasterxml.jackson.databind.JsonNode;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49 import java.io.IOException;
51 public class RequestValidator {
53 private static final EELFLogger log = EELFManager.getInstance().getLogger(RequestValidator.class);
55 private RequestValidator() {
58 public static void validate(String action, String payload) throws RequestValidationException, IOException {
59 log.info("validate: action:" + action );
60 log.info("validate: payload:" + payload);
61 ObjectMapper objectMapper = new ObjectMapper();
62 JsonNode payloadObject = objectMapper.readTree(payload);
63 log.info("payloadObject:" + payloadObject.get(ARTIFACT_CONTENTS));
65 String errorString= null;
68 errorString = resolveGetDesignsErrorString(payloadObject);
70 case GETAPPCTIMESTAMPUTC:
71 log.info("validate: No payload validation needed for Timestamp.");
74 errorString = resolveGetArtifactErrorString(payloadObject);
77 errorString = resolveGetStatusErrorString(payloadObject);
80 errorString = resolveSetStatusErrorString(payloadObject);
83 errorString = resolveUploadArtifactErrorString(payloadObject);
85 case SETPROTOCOLREFERENCE:
87 errorString = resolveErrorString(payloadObject);
90 throw new RequestValidationException(" Action " + action + " not found while processing request ");
92 checkForErrorString(errorString);
95 private static void checkForErrorString(String errorString) throws RequestValidationException {
96 if (errorString != null) {
97 throw new RequestValidationException(" Missing input parameter :-" + errorString + " -:");
101 private static String resolveGetDesignsErrorString(JsonNode payloadObject) {
102 return nullOrEmpty(payloadObject, USER_ID) ? USER_ID : null;
105 private static String resolveErrorString(JsonNode payloadObject) {
106 if (nullOrEmpty(payloadObject, ACTION)) {
108 } else if (nullOrEmpty(payloadObject, ACTION_LEVEL)) {
110 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
112 } else if (nullOrEmpty(payloadObject, PROTOCOL)) {
118 private static String resolveUploadArtifactErrorString(JsonNode payloadObject) {
119 if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
120 return ARTIFACT_NAME;
121 } else if (doesNotContainReference(payloadObject)) {
123 } else if (nullOrEmpty(payloadObject, ARTIFACT_VERSOIN)) {
124 return ARTIFACT_VERSOIN;
125 } else if (payloadObject.get(ARTIFACT_CONTENTS) == null) {
126 return ARTIFACT_CONTENTS;
127 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
128 return ARTIFACT_TYPE;
129 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
135 private static boolean doesNotContainReference(JsonNode payloadObject) {
137 !payloadObject.get(ARTIFACT_NAME).textValue().contains("reference")
138 && nullOrEmpty(payloadObject, ARTIFACT_NAME);
141 private static String resolveSetStatusErrorString(JsonNode payloadObject) {
142 if (nullOrEmpty(payloadObject, USER_ID)) {
144 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
146 } else if (nullOrEmpty(payloadObject, ACTION)) {
148 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
149 return ARTIFACT_TYPE;
150 } else if (nullOrEmpty(payloadObject, STATUS)) {
156 private static String resolveGetStatusErrorString(JsonNode payloadObject) {
157 if (nullOrEmpty(payloadObject, USER_ID)) {
159 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
165 private static String resolveGetArtifactErrorString(JsonNode payloadObject) {
166 if (nullOrEmpty(payloadObject, VNF_TYPE)) {
168 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
169 return ARTIFACT_TYPE;
170 } else if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
171 return ARTIFACT_NAME;
176 private static boolean nullOrEmpty(JsonNode payloadObject, String param) {
177 return payloadObject.get(param) == null || payloadObject
178 .get(param).textValue().isEmpty();