aboutsummaryrefslogtreecommitdiffstats
path: root/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/RequestValidator.java
blob: f98aae46f39f425766d28ecde58a11149be4b4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.design.dbervices;

import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION;
import static org.onap.appc.design.services.util.DesignServiceConstants.ACTION_LEVEL;
import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_CONTENTS;
import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_NAME;
import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_TYPE;
import static org.onap.appc.design.services.util.DesignServiceConstants.ARTIFACT_VERSOIN;
import static org.onap.appc.design.services.util.DesignServiceConstants.GETARTIFACT;
import static org.onap.appc.design.services.util.DesignServiceConstants.GETAPPCTIMESTAMPUTC;
import static org.onap.appc.design.services.util.DesignServiceConstants.GETDESIGNS;
import static org.onap.appc.design.services.util.DesignServiceConstants.GETSTATUS;
import static org.onap.appc.design.services.util.DesignServiceConstants.PROTOCOL;
import static org.onap.appc.design.services.util.DesignServiceConstants.SETINCART;
import static org.onap.appc.design.services.util.DesignServiceConstants.SETPROTOCOLREFERENCE;
import static org.onap.appc.design.services.util.DesignServiceConstants.SETSTATUS;
import static org.onap.appc.design.services.util.DesignServiceConstants.STATUS;
import static org.onap.appc.design.services.util.DesignServiceConstants.UPLOADARTIFACT;
import static org.onap.appc.design.services.util.DesignServiceConstants.USER_ID;
import static org.onap.appc.design.services.util.DesignServiceConstants.VNF_TYPE;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

import org.onap.appc.design.services.util.DesignServiceConstants;

public class RequestValidator {

    private static final EELFLogger log = EELFManager.getInstance().getLogger(RequestValidator.class);

    private RequestValidator() {
    }

    public static void validate(String action, String payload) throws RequestValidationException, IOException {
        log.info("validate: action:"  +  action );
        log.info("validate: payload:" + payload);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode payloadObject = objectMapper.readTree(payload);
        log.info("payloadObject:" + payloadObject.get(ARTIFACT_CONTENTS));

        String errorString= null;
        switch (action) {
            case GETDESIGNS:
                errorString = resolveGetDesignsErrorString(payloadObject);
                break;
            case GETAPPCTIMESTAMPUTC:
                log.info("validate: No payload validation needed for Timestamp.");
                break;
            case GETARTIFACT:
                errorString = resolveGetArtifactErrorString(payloadObject);
                break;
            case GETSTATUS:
                errorString = resolveGetStatusErrorString(payloadObject);
                break;
            case SETSTATUS:
                errorString = resolveSetStatusErrorString(payloadObject);
                break;
            case UPLOADARTIFACT:
                errorString = resolveUploadArtifactErrorString(payloadObject);
                break;
            case DesignServiceConstants.UPLOADADMINARTIFACT:
                errorString = resolveUploadArtifactErrorString(payloadObject);
                break;
            case SETPROTOCOLREFERENCE:
            case SETINCART:
                errorString = resolveErrorString(payloadObject);
                break;
            case DesignServiceConstants.CHECKVNF:
                errorString = resolveCheckVNFErrorString(payloadObject);
                break;
            default:
                throw new RequestValidationException(" Action " + action + " not found while processing request ");
        }
        checkForErrorString(errorString);
    }

    private static String resolveCheckVNFErrorString(JsonNode payloadObject) {
        if (nullOrEmpty(payloadObject, VNF_TYPE)) 
            return VNF_TYPE;
        return null;
    }

    private static void checkForErrorString(String errorString) throws RequestValidationException {
        if (errorString != null) {
            throw new RequestValidationException(" Missing input parameter :-" + errorString + " -:");
        }
    }

    private static String resolveGetDesignsErrorString(JsonNode payloadObject) {
        return nullOrEmpty(payloadObject, USER_ID) ? USER_ID : null;
    }

    private static String resolveErrorString(JsonNode payloadObject) {
        if (nullOrEmpty(payloadObject, ACTION)) {
            return ACTION;
        } else if (nullOrEmpty(payloadObject, ACTION_LEVEL)) {
            return ACTION_LEVEL;
        } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
            return VNF_TYPE;
        } else if (nullOrEmpty(payloadObject, PROTOCOL)) {
            return PROTOCOL;
        }
        return null;
    }

    private static String resolveUploadArtifactErrorString(JsonNode payloadObject) {
        if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
            return ARTIFACT_NAME;
        } else if (doesNotContainReference(payloadObject)) {
            return ACTION;
        } else if (nullOrEmpty(payloadObject, ARTIFACT_VERSOIN)) {
            return ARTIFACT_VERSOIN;
        } else if (payloadObject.get(ARTIFACT_CONTENTS) == null) {
            return ARTIFACT_CONTENTS;
        } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
            return ARTIFACT_TYPE;
        } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
            return VNF_TYPE;
        }
        return null;
    }

    private static boolean doesNotContainReference(JsonNode payloadObject) {
        return
            !payloadObject.get(ARTIFACT_NAME).textValue().contains("reference")
                && nullOrEmpty(payloadObject, ARTIFACT_NAME);
    }

    private static String resolveSetStatusErrorString(JsonNode payloadObject) {
        if (nullOrEmpty(payloadObject, USER_ID)) {
            return USER_ID;
        } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
            return VNF_TYPE;
        } else if (nullOrEmpty(payloadObject, ACTION)) {
            return ACTION;
        } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
            return ARTIFACT_TYPE;
        } else if (nullOrEmpty(payloadObject, STATUS)) {
            return STATUS;
        }
        return null;
    }

    private static String resolveGetStatusErrorString(JsonNode payloadObject) {
        if (nullOrEmpty(payloadObject, USER_ID)) {
            return USER_ID;
        } else if (nullOrEmpty(payloadObject, VNF_TYPE)) {
            return VNF_TYPE;
        }
        return null;
    }

    private static String resolveGetArtifactErrorString(JsonNode payloadObject) {
        if (nullOrEmpty(payloadObject, VNF_TYPE)) {
            return VNF_TYPE;
        } else if (nullOrEmpty(payloadObject, ARTIFACT_TYPE)) {
            return ARTIFACT_TYPE;
        } else if (nullOrEmpty(payloadObject, ARTIFACT_NAME)) {
            return ARTIFACT_NAME;
        }
        return null;
    }

    private static boolean nullOrEmpty(JsonNode payloadObject, String param) {
        JsonNode payload = payloadObject.get(param);
        return payload == null || payload.textValue().trim().isEmpty();
    }

}