Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / main / java / org / onap / appc / validationpolicy / RequestValidationPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.validationpolicy;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.databind.DeserializationFeature;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import org.apache.commons.lang.StringUtils;
32 import org.onap.appc.domainmodel.lcm.VNFOperation;
33 import org.onap.appc.validationpolicy.executors.ActionInProgressRuleExecutor;
34 import org.onap.appc.validationpolicy.executors.RuleExecutor;
35 import org.onap.appc.validationpolicy.objects.Policy;
36 import org.onap.appc.validationpolicy.objects.PolicyNames;
37 import org.onap.appc.validationpolicy.objects.Rule;
38 import org.onap.appc.validationpolicy.objects.ValidationJSON;
39 import org.onap.appc.validationpolicy.rules.RuleFactory;
40 import org.onap.ccsdk.sli.core.dblib.DbLibService;
41
42 import javax.sql.rowset.CachedRowSet;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Set;
49 import java.util.stream.Collectors;
50
51 /**
52  * Reads the request validation policy on start-up and provides
53  *  accessors for rule executors
54  */
55 public class RequestValidationPolicy {
56
57     private DbLibService dbLibService;
58
59     private RuleExecutor actionInProgressRuleExecutor;
60
61     private final EELFLogger logger = EELFManager.getInstance().getLogger(RequestValidationPolicy.class);
62
63     public void setDbLibService(DbLibService dbLibService) {
64         this.dbLibService = dbLibService;
65     }
66
67     public void initialize() {
68         try {
69             String jsonContent = getPolicyJson();
70             if (jsonContent == null) return;
71
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();
76             policyList.stream()
77                     .filter(policy -> PolicyNames.ActionInProgress.name().equals(policy.getPolicyName()))
78                     .forEach(policy -> {
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());
90                                 }
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());
95                                 }
96                                 org.onap.appc.validationpolicy.rules.Rule rule = RuleFactory
97                                         .createRule(validationRule, inclusionSet, exclusionSet);
98                                 rules.put(action, rule);
99                             }
100                             actionInProgressRuleExecutor =
101                                     new ActionInProgressRuleExecutor(Collections.unmodifiableMap(rules));
102                             });
103         } catch (Exception e) {
104             logger.error("Error reading request validation policies", e);
105         }
106     }
107
108     protected String getPolicyJson() {
109         String schema = "sdnctl";
110         String query =
111                 "SELECT MAX(INTERNAL_VERSION),ARTIFACT_CONTENT "
112                 + "FROM ASDC_ARTIFACTS "
113                 + "WHERE ARTIFACT_NAME = ? "
114                 + "GROUP BY ARTIFACT_NAME";
115         ArrayList<String> arguments = new ArrayList<>();
116         arguments.add("request_validation_policy");
117         String jsonContent = null;
118         try {
119             CachedRowSet rowSet = dbLibService.getData(query, arguments, schema);
120             if (rowSet != null && rowSet.next()) {
121                 jsonContent = rowSet.getString("ARTIFACT_CONTENT");
122             }
123             if (logger.isDebugEnabled()) {
124                 logger.debug("request validation policy = " + jsonContent);
125             }
126             if (StringUtils.isBlank(jsonContent)) {
127                 logger.warn("request validation policy not found in app-c database");
128             }
129         } catch(Exception e) {
130             logger.debug("Error while accessing database: " + e.getMessage());
131             logger.info("Error connecting to database: " + e.getMessage());
132             logger.error("Error accessing database", e);
133             throw new RuntimeException(e);
134         }
135         logger.info("Got Policy Json");
136         return jsonContent;
137     }
138
139     public RuleExecutor getInProgressRuleExecutor() {
140         if (actionInProgressRuleExecutor == null) {
141             throw new RuntimeException("Rule executor not available, initialization of RequestValidationPolicy failed");
142         }
143         return actionInProgressRuleExecutor;
144     }
145 }