2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Bell Canada
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controller;
24 import java.io.IOException;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.Objects;
29 import javax.json.JsonArray;
30 import javax.json.JsonObject;
32 import org.onap.policy.admin.PolicyManagerServlet;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35 import org.onap.policy.rest.adapter.ClosedLoopPMBody;
36 import org.onap.policy.rest.adapter.PolicyRestAdapter;
37 import org.onap.policy.rest.jpa.PolicyEntity;
39 import com.fasterxml.jackson.databind.ObjectMapper;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
48 public class CreateClosedLoopPMController {
50 private static final Logger LOGGER = FlexLogger.getLogger(CreateClosedLoopPMController.class);
51 private static final String KEY_SERVICE_TYPE_POLICY_NAME = "serviceTypePolicyName";
53 protected PolicyRestAdapter policyAdapter = null;
55 public void prePopulateClosedLoopPMPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
56 if (policyAdapter.getPolicyData() instanceof PolicyType) {
57 Object policyData = policyAdapter.getPolicyData();
58 PolicyType policy = (PolicyType) policyData;
60 // Set oldPolicyFileName to PolicyAdapter
61 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
63 // Set policyNameValue and description to PolicyAdapter
64 setPolicyAdapterNameValueAndDescription(policyAdapter, policy);
66 // Set PolicyAdapter JsonBodyData
67 setClosedLoopJSONFile(policyAdapter, entity);
69 // Get the target data under policy.
70 TargetType target = policy.getTarget();
74 // Under target we have AnyOFType
75 List<AnyOfType> anyOfList = target.getAnyOf();
76 if (anyOfList == null) {
79 // Set PolicyAdapter OnapNameField, riskType, riskLevel, guard, ttlDate, ServiceType from match attributes
80 setPolicyAdapterMatchAttributes(policyAdapter, anyOfList);
84 private void setPolicyAdapterNameValueAndDescription(PolicyRestAdapter policyAdapter, PolicyType policy) {
85 String policyNameValue = policyAdapter.getPolicyName()
86 .substring(policyAdapter.getPolicyName().indexOf("PM_") + 3);
87 policyAdapter.setPolicyName(policyNameValue);
90 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
91 } catch (Exception e) {
92 LOGGER.info("General error", e);
93 description = policy.getDescription();
95 policyAdapter.setPolicyDescription(description);
98 private void setClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
99 ObjectMapper mapper = new ObjectMapper();
101 ClosedLoopPMBody closedLoopBody = mapper
102 .readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopPMBody.class);
103 policyAdapter.setJsonBodyData(closedLoopBody);
104 } catch (IOException e) {
105 LOGGER.error("Exception Occured" + e);
109 private void setPolicyAdapterMatchAttributes(final PolicyRestAdapter policyAdapter,
110 final List<AnyOfType> anyOfList) {
112 //Extract nonNull list of AllOfType objs from each AnyOfType obj
113 .map(AnyOfType::getAllOf).filter(Objects::nonNull)
114 .forEach(allOfList ->
115 //Extract nonNull list of MatchType objs from each AllOFType obj
116 allOfList.stream().map(AllOfType::getMatch).filter(Objects::nonNull)
117 .forEach(matchList -> matchList.forEach(match -> {
118 // Under the match we have attribute value and
119 // attributeDesignator. So,finally down to the actual attribute.
120 AttributeValueType attributeValue = match.getAttributeValue();
121 String value = (String) attributeValue.getContent().get(0);
122 AttributeDesignatorType designator = match.getAttributeDesignator();
123 String attributeId = designator.getAttributeId();
124 // First match in the target is OnapName, so set that value.
125 if ("ONAPName".equals(attributeId)) {
126 policyAdapter.setOnapName(value);
127 } else if ("RiskType".equals(attributeId)) {
128 policyAdapter.setRiskType(value);
129 } else if ("RiskLevel".equals(attributeId)) {
130 policyAdapter.setRiskLevel(value);
131 } else if ("guard".equals(attributeId)) {
132 policyAdapter.setGuard(value);
133 } else if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
134 PolicyController controller = new PolicyController();
135 String newDate = controller.convertDate(value);
136 policyAdapter.setTtlDate(newDate);
137 } else if ("ServiceType".equals(attributeId)) {
138 LinkedHashMap<String, String> serviceTypePolicyName1 = new LinkedHashMap<>();
139 serviceTypePolicyName1.put(KEY_SERVICE_TYPE_POLICY_NAME, value);
140 policyAdapter.setServiceTypePolicyName(serviceTypePolicyName1);
141 LinkedHashMap<String, String> vertica = new LinkedHashMap<>();
142 vertica.put("verticaMetrics", getVertica(value));
143 policyAdapter.setVerticaMetrics(vertica);
144 LinkedHashMap<String, String> desc = new LinkedHashMap<>();
145 desc.put("policyDescription", getDescription(value));
146 policyAdapter.setDescription(desc);
147 LinkedHashMap<String, Object> attributes = new LinkedHashMap<>();
148 attributes.put("attributes", getAttributes(value));
149 policyAdapter.setAttributeFields(attributes);
154 //get vertica metrics data from the table
155 private String getVertica(String policyName) {
156 JsonArray data = PolicyManagerServlet.getPolicyNames();
157 for (int i = 0; i < data.size(); i++) {
158 if (policyName.equals(data.getJsonObject(i).getJsonString(KEY_SERVICE_TYPE_POLICY_NAME).getString())) {
159 return data.getJsonObject(i).getJsonString("verticaMetrics").getString();
165 //get policy description from the table
166 private String getDescription(String policyName) {
167 JsonArray data = PolicyManagerServlet.getPolicyNames();
168 for (int i = 0; i < data.size(); i++) {
169 if (policyName.equals(data.getJsonObject(i).getJsonString(KEY_SERVICE_TYPE_POLICY_NAME).getString())) {
170 return data.getJsonObject(i).getJsonString("policyDescription").getString();
177 private JsonObject getAttributes(String policyName) {
178 JsonArray data = PolicyManagerServlet.getPolicyNames();
179 for (int i = 0; i < data.size(); i++) {
180 if (policyName.equals(data.getJsonObject(i).getJsonString(KEY_SERVICE_TYPE_POLICY_NAME).getString())) {
181 return data.getJsonObject(i).getJsonObject("attributes");