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.validationpolicy;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.DeserializationFeature;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.appc.domainmodel.lcm.VNFOperation;
32 import org.onap.appc.validationpolicy.executors.ActionInProgressRuleExecutor;
33 import org.onap.appc.validationpolicy.executors.RuleExecutor;
34 import org.onap.appc.validationpolicy.objects.Policy;
35 import org.onap.appc.validationpolicy.objects.PolicyNames;
36 import org.onap.appc.validationpolicy.objects.Rule;
37 import org.onap.appc.validationpolicy.objects.ValidationJSON;
38 import org.onap.appc.validationpolicy.rules.RuleFactory;
39 import org.onap.ccsdk.sli.core.dblib.DbLibService;
41 import javax.sql.rowset.CachedRowSet;
42 import java.sql.SQLException;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.List;
49 import java.util.stream.Collectors;
52 * Reads the request validation policy on start-up and provides
53 * accessors for rule executors
55 public class RequestValidationPolicy {
57 private DbLibService dbLibService;
59 private RuleExecutor actionInProgressRuleExecutor;
61 private final EELFLogger logger = EELFManager.getInstance().getLogger(RequestValidationPolicy.class);
63 public void setDbLibService(DbLibService dbLibService) {
64 this.dbLibService = dbLibService;
67 public void initialize(){
69 String jsonContent = getPolicyJson();
70 if (jsonContent == null) return;
72 ObjectMapper objectMapper = new ObjectMapper();
73 objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
74 ValidationJSON validationJSON = objectMapper.readValue(jsonContent, ValidationJSON.class);
75 List<Policy> policyList = validationJSON.getPolicies();
77 .filter(policy -> PolicyNames.ActionInProgress.name().equals(policy.getPolicyName()))
79 Rule[] ruleDTOs = policy.getRules();
80 Map<String, org.onap.appc.validationpolicy.rules.Rule> rules = new HashMap<>();
81 for(Rule ruleDTO : ruleDTOs) {
82 String action = ruleDTO.getActionReceived();
83 String validationRule = ruleDTO.getValidationRule();
84 Set<VNFOperation> inclusionSet = null;
85 Set<VNFOperation> exclusionSet = null;
86 if (ruleDTO.getInclusionList() != null && !ruleDTO.getInclusionList().isEmpty()) {
87 inclusionSet = ruleDTO.getInclusionList().stream()
88 .map(VNFOperation::findByString).filter(operation -> operation!=null)
89 .collect(Collectors.toSet());
91 if (ruleDTO.getExclusionList() != null && !ruleDTO.getExclusionList().isEmpty()) {
92 exclusionSet = ruleDTO.getExclusionList().stream()
93 .map(VNFOperation::findByString).filter(operation -> operation!=null)
94 .collect(Collectors.toSet());
96 org.onap.appc.validationpolicy.rules.Rule rule = RuleFactory
97 .createRule(validationRule, inclusionSet, exclusionSet);
98 rules.put(action, rule);
100 actionInProgressRuleExecutor = new ActionInProgressRuleExecutor(Collections.unmodifiableMap(rules));
102 } catch (Exception e) {
103 logger.error("Error reading request validation policies",e);
107 protected String getPolicyJson() {
108 String schema = "sdnctl";
109 String query = "SELECT MAX(INTERNAL_VERSION),ARTIFACT_CONTENT " +
110 "FROM ASDC_ARTIFACTS " +
111 "WHERE ARTIFACT_NAME = ? " +
112 "GROUP BY ARTIFACT_NAME";
113 ArrayList<String> arguments = new ArrayList<>();
114 arguments.add("request_validation_policy");
115 String jsonContent =null;
117 CachedRowSet rowSet = dbLibService.getData(query,arguments,schema);
119 jsonContent = rowSet.getString("ARTIFACT_CONTENT");
121 if(logger.isDebugEnabled()){
122 logger.debug("request validation policy = " + jsonContent);
124 if(StringUtils.isBlank(jsonContent)){
125 logger.warn("request validation policy not found in app-c database");
128 catch(SQLException e){
129 logger.error("Error accessing database",e);
130 throw new RuntimeException(e);
135 public RuleExecutor getInProgressRuleExecutor(){
136 if(actionInProgressRuleExecutor ==null){
137 throw new RuntimeException("Rule executor not available, initialization of RequestValidationPolicy failed");
139 return actionInProgressRuleExecutor;