aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-request-handler/appc-request-handler-core/src/main/java/org/onap/appc/requesthandler/impl/LocalRequestValidatorImpl.java
blob: e8cd66b1d8f3b0714474afe895aa0bfcb672be10 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 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.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.requesthandler.impl;

import com.att.eelf.i18n.EELFResourceManager;
import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
import org.onap.appc.domainmodel.lcm.RequestContext;
import org.onap.appc.domainmodel.lcm.RuntimeContext;
import org.onap.appc.exceptions.InvalidInputException;
import org.onap.appc.i18n.Msg;
import org.onap.appc.logging.LoggingConstants;
import org.onap.appc.logging.LoggingUtils;
import org.onap.appc.requesthandler.exceptions.DuplicateRequestException;
import org.onap.appc.requesthandler.exceptions.LCMOperationsDisabledException;
import org.onap.appc.requesthandler.exceptions.RequestExpiredException;
import org.onap.appc.util.JsonUtil;

import java.io.IOException;
import java.util.Map;

/**
 * * This class validates the LCM-Requests that don't need communication with the VNF.
 */
public class LocalRequestValidatorImpl extends AbstractRequestValidatorImpl {
    @Override
    public void validateRequest(RuntimeContext runtimeContext) throws LCMOperationsDisabledException,
        DuplicateRequestException, RequestExpiredException, InvalidInputException {

        if (!lcmStateManager.isLCMOperationEnabled()) {
            LoggingUtils.logErrorMessage(
                LoggingConstants.TargetNames.REQUEST_VALIDATOR,
                EELFResourceManager.format(Msg.LCM_OPERATIONS_DISABLED),
                this.getClass().getCanonicalName());
            throw new LCMOperationsDisabledException("APPC LCM operations have been administratively disabled");
        }

        validateInput(runtimeContext);
    }

    @Override
    public void validateInput(RuntimeContext runtimeContext) throws DuplicateRequestException,
        RequestExpiredException, InvalidInputException {
        RequestContext requestContext = runtimeContext.getRequestContext();
        super.validateInput(runtimeContext);
        switch (requestContext.getAction()) {
            case ActionStatus:
                validateActionStatusPayload(requestContext.getPayload());
                validateActionStatusActionIdentifiers(requestContext.getActionIdentifiers());
                break;
            default:
                logger.warn(String.format("Action %s not supported!", requestContext.getAction()));
        }
    }

    private void validateActionStatusPayload(String payload) throws InvalidInputException {
        Map<String, String> map;
        try {
            map = JsonUtil.convertJsonStringToFlatMap(payload);
        } catch (IOException e) {
            logger.error(String.format("Error encountered when converting JSON payload '%s' to map", payload), e);
            throw new InvalidInputException("Search criteria cannot be determined from Payload due to format issue");
        }
        if ((map == null) || !map.containsKey("request-id")) {
            throw new InvalidInputException("request-id is absent in the Payload");
        }
    }

    private void validateActionStatusActionIdentifiers(ActionIdentifiers identifiers) throws InvalidInputException {
        if (identifiers == null || identifiers.getVnfId() == null) {
            throw new InvalidInputException("VNF-Id is absent in Action Identifiers");
        }
    }
}