2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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 com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.IOException;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Objects;
31 import javax.json.JsonArray;
32 import javax.json.JsonObject;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
41 import org.onap.policy.admin.PolicyManagerServlet;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.rest.adapter.ClosedLoopPMBody;
45 import org.onap.policy.rest.adapter.PolicyRestAdapter;
46 import org.onap.policy.rest.jpa.PolicyEntity;
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 =
86 policyAdapter.getPolicyName().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 =
102 mapper.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).forEach(allOfList ->
114 // Extract nonNull list of MatchType objs from each AllOFType obj
115 allOfList.stream().map(AllOfType::getMatch).filter(Objects::nonNull)
116 .forEach(matchList -> matchList.forEach(match -> {
117 // Under the match we have attribute value and
118 // attributeDesignator. So,finally down to the actual attribute.
119 AttributeValueType attributeValue = match.getAttributeValue();
120 String value = (String) attributeValue.getContent().get(0);
121 AttributeDesignatorType designator = match.getAttributeDesignator();
122 String attributeId = designator.getAttributeId();
123 // First match in the target is OnapName, so set that value.
124 if ("ONAPName".equals(attributeId)) {
125 policyAdapter.setOnapName(value);
126 } else if ("RiskType".equals(attributeId)) {
127 policyAdapter.setRiskType(value);
128 } else if ("RiskLevel".equals(attributeId)) {
129 policyAdapter.setRiskLevel(value);
130 } else if ("guard".equals(attributeId)) {
131 policyAdapter.setGuard(value);
132 } else if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
133 PolicyController controller = new PolicyController();
134 String newDate = controller.convertDate(value);
135 policyAdapter.setTtlDate(newDate);
136 } else if ("ServiceType".equals(attributeId)) {
137 LinkedHashMap<String, String> serviceTypePolicyName1 = new LinkedHashMap<>();
138 serviceTypePolicyName1.put(KEY_SERVICE_TYPE_POLICY_NAME, value);
139 policyAdapter.setServiceTypePolicyName(serviceTypePolicyName1);
140 LinkedHashMap<String, String> vertica = new LinkedHashMap<>();
141 vertica.put("verticaMetrics", getVertica(value));
142 policyAdapter.setVerticaMetrics(vertica);
143 LinkedHashMap<String, String> desc = new LinkedHashMap<>();
144 desc.put("policyDescription", getDescription(value));
145 policyAdapter.setDescription(desc);
146 LinkedHashMap<String, Object> attributes = new LinkedHashMap<>();
147 attributes.put("attributes", getAttributes(value));
148 policyAdapter.setAttributeFields(attributes);
153 // get vertica metrics data from the table
154 private String getVertica(String policyName) {
155 JsonArray data = PolicyManagerServlet.getPolicyNames();
156 for (int i = 0; i < data.size(); i++) {
157 if (policyName.equals(data.getJsonObject(i).getJsonString(KEY_SERVICE_TYPE_POLICY_NAME).getString())) {
158 return data.getJsonObject(i).getJsonString("verticaMetrics").getString();
164 // get policy description from the table
165 private String getDescription(String policyName) {
166 JsonArray data = PolicyManagerServlet.getPolicyNames();
167 for (int i = 0; i < data.size(); i++) {
168 if (policyName.equals(data.getJsonObject(i).getJsonString(KEY_SERVICE_TYPE_POLICY_NAME).getString())) {
169 return data.getJsonObject(i).getJsonString("policyDescription").getString();
176 private JsonObject getAttributes(String policyName) {
177 JsonArray data = PolicyManagerServlet.getPolicyNames();
178 for (int i = 0; i < data.size(); i++) {
179 if (policyName.equals(data.getJsonObject(i).getJsonString(KEY_SERVICE_TYPE_POLICY_NAME).getString())) {
180 return data.getJsonObject(i).getJsonObject("attributes");