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.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 public class RequestValidator {
52 private static final EELFLogger log = EELFManager.getInstance().getLogger(RequestValidator.class);
54 private RequestValidator() {
57 public static void validate(String action, String payload) throws RequestValidationException, IOException {
58 log.info("payload" + payload);
59 ObjectMapper objectMapper = new ObjectMapper();
60 JsonNode payloadObject = objectMapper.readTree(payload);
61 log.info("payloadObject" + payloadObject.get(ARTIFACT_CONTENTS));
66 errorString = resolveGetDesignsErrorString(payloadObject);
69 errorString = resolveGetArtifactErrorString(payloadObject);
72 errorString = resolveGetStatusErrorString(payloadObject);
75 errorString = resolveSetStatusErrorString(payloadObject);
78 errorString = resolveUploadArtifactErrorString(payloadObject);
80 case SETPROTOCOLREFERENCE:
82 errorString = resolveErrorString(payloadObject);
85 throw new RequestValidationException(" Action " + action + " not found while processing request ");
87 checkForErrorString(errorString);
90 private static void checkForErrorString(String errorString) throws RequestValidationException {
91 if (errorString != null) {
92 throw new RequestValidationException(" Missing input parameter :-" + errorString + " -:");
96 private static String resolveGetDesignsErrorString(JsonNode payloadObject) {
97 return nullOrEmpty(payloadObject, USER_ID) ? USER_ID : null;
100 private static String resolveErrorString(JsonNode payloadObject) {
101 if (nullOrEmpty(payloadObject, ACTION)) {
103 } else if (nullOrEmpty(payloadObject, ACTION_LEVEL)) {
105 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
107 } else if (nullOrEmpty(payloadObject, PROTOCOL)) {
113 private static String resolveUploadArtifactErrorString(JsonNode payloadObject) {
114 if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
115 return ARTIFACT_NAME;
116 } else if (doesNotContainReference(payloadObject)) {
118 } else if (nullOrEmpty(payloadObject, ARTIFACT_VERSOIN)) {
119 return ARTIFACT_VERSOIN;
120 } else if (payloadObject.get(ARTIFACT_CONTENTS) == null) {
121 return ARTIFACT_CONTENTS;
122 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
123 return ARTIFACT_TYPE;
124 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
130 private static boolean doesNotContainReference(JsonNode payloadObject) {
132 !payloadObject.get(ARTIFACT_NAME).textValue().contains("reference")
133 && nullOrEmpty(payloadObject, ARTIFACT_NAME);
136 private static String resolveSetStatusErrorString(JsonNode payloadObject) {
137 if (nullOrEmpty(payloadObject, USER_ID)) {
139 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
141 } else if (nullOrEmpty(payloadObject, ACTION)) {
143 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
144 return ARTIFACT_TYPE;
145 } else if (nullOrEmpty(payloadObject, STATUS)) {
151 private static String resolveGetStatusErrorString(JsonNode payloadObject) {
152 if (nullOrEmpty(payloadObject, USER_ID)) {
154 } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
160 private static String resolveGetArtifactErrorString(JsonNode payloadObject) {
161 if (nullOrEmpty(payloadObject, VNF_TYPE)) {
163 } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
164 return ARTIFACT_TYPE;
165 } else if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
166 return ARTIFACT_NAME;
171 private static boolean nullOrEmpty(JsonNode payloadObject, String param) {
172 return payloadObject.get(param) == null || payloadObject
173 .get(param).textValue().isEmpty();