Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / main / java / org / onap / appc / requesthandler / impl / LocalRequestValidatorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.requesthandler.impl;
26
27 import com.att.eelf.i18n.EELFResourceManager;
28 import org.onap.appc.domainmodel.lcm.ActionIdentifiers;
29 import org.onap.appc.domainmodel.lcm.RequestContext;
30 import org.onap.appc.domainmodel.lcm.RuntimeContext;
31 import org.onap.appc.exceptions.InvalidInputException;
32 import org.onap.appc.i18n.Msg;
33 import org.onap.appc.logging.LoggingConstants;
34 import org.onap.appc.logging.LoggingUtils;
35 import org.onap.appc.requesthandler.exceptions.DuplicateRequestException;
36 import org.onap.appc.requesthandler.exceptions.LCMOperationsDisabledException;
37 import org.onap.appc.requesthandler.exceptions.RequestExpiredException;
38 import org.onap.appc.util.JsonUtil;
39
40 import java.io.IOException;
41 import java.util.Map;
42
43 /**
44  * * This class validates the LCM-Requests that don't need communication with the VNF.
45  */
46 public class LocalRequestValidatorImpl extends AbstractRequestValidatorImpl {
47     @Override
48     public void validateRequest(RuntimeContext runtimeContext) throws LCMOperationsDisabledException,
49         DuplicateRequestException, RequestExpiredException, InvalidInputException {
50
51         if (!lcmStateManager.isLCMOperationEnabled()) {
52             LoggingUtils.logErrorMessage(
53                 LoggingConstants.TargetNames.REQUEST_VALIDATOR,
54                 EELFResourceManager.format(Msg.LCM_OPERATIONS_DISABLED),
55                 this.getClass().getCanonicalName());
56             throw new LCMOperationsDisabledException("APPC LCM operations have been administratively disabled");
57         }
58
59         validateInput(runtimeContext);
60     }
61
62     @Override
63     public void validateInput(RuntimeContext runtimeContext) throws DuplicateRequestException,
64         RequestExpiredException, InvalidInputException {
65         RequestContext requestContext = runtimeContext.getRequestContext();
66         super.validateInput(runtimeContext);
67         switch (requestContext.getAction()) {
68             case ActionStatus:
69                 validateActionStatusPayload(requestContext.getPayload());
70                 validateActionStatusActionIdentifiers(requestContext.getActionIdentifiers());
71                 break;
72             default:
73                 logger.warn(String.format("Action %s not supported!", requestContext.getAction()));
74         }
75     }
76
77     private void validateActionStatusPayload(String payload) throws InvalidInputException {
78         Map<String, String> map;
79         try {
80             map = JsonUtil.convertJsonStringToFlatMap(payload);
81         } catch (IOException e) {
82             logger.error(String.format("Error encountered when converting JSON payload '%s' to map", payload), e);
83             throw new InvalidInputException("Search criteria cannot be determined from Payload due to format issue");
84         }
85         if ((map == null) || !map.containsKey("request-id")) {
86             throw new InvalidInputException("request-id is absent in the Payload");
87         }
88     }
89
90     private void validateActionStatusActionIdentifiers(ActionIdentifiers identifiers) throws InvalidInputException {
91         if (identifiers == null || identifiers.getVnfId() == null) {
92             throw new InvalidInputException("VNF-Id is absent in Action Identifiers");
93         }
94     }
95 }