2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.controller;
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import javax.json.JsonArray;
30 import javax.json.JsonObject;
32 import org.openecomp.policy.admin.PolicyManagerServlet;
33 import org.openecomp.policy.rest.adapter.ClosedLoopPMBody;
34 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
35 import org.openecomp.policy.rest.jpa.PolicyEntity;
37 import com.fasterxml.jackson.databind.ObjectMapper;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
46 public class CreateClosedLoopPMController{
48 protected PolicyRestAdapter policyAdapter = null;
50 public void prePopulateClosedLoopPMPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
51 if (policyAdapter.getPolicyData() instanceof PolicyType) {
52 Object policyData = policyAdapter.getPolicyData();
53 PolicyType policy = (PolicyType) policyData;
54 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
55 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("PM_") +3);
56 policyAdapter.setPolicyName(policyNameValue);
57 String description = "";
59 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
61 description = policy.getDescription();
63 policyAdapter.setPolicyDescription(description);
64 // Get the target data under policy.
65 TargetType target = policy.getTarget();
67 // Under target we have AnyOFType
68 List<AnyOfType> anyOfList = target.getAnyOf();
69 if (anyOfList != null) {
70 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
71 while (iterAnyOf.hasNext()) {
72 AnyOfType anyOf = iterAnyOf.next();
73 // Under AnyOFType we have AllOFType
74 List<AllOfType> allOfList = anyOf.getAllOf();
75 if (allOfList != null) {
76 Iterator<AllOfType> iterAllOf = allOfList.iterator();
77 while (iterAllOf.hasNext()) {
78 AllOfType allOf = iterAllOf.next();
79 // Under AllOFType we have Match
80 List<MatchType> matchList = allOf.getMatch();
81 if (matchList != null) {
83 Iterator<MatchType> iterMatch = matchList.iterator();
84 while (matchList.size()>1 && iterMatch.hasNext()) {
85 MatchType match = iterMatch.next();
87 // Under the match we have attributevalue and
88 // attributeDesignator. So,finally down to the actual attribute.
90 AttributeValueType attributeValue = match.getAttributeValue();
91 String value = (String) attributeValue.getContent().get(0);
93 // First match in the target is EcompName, so set that value.
95 policyAdapter.setEcompName(value);
99 policyAdapter.setRiskType(value);
103 policyAdapter.setRiskLevel(value);
107 policyAdapter.setGuard(value);
110 if (index == 4 && !value.contains("NA")){
111 String newDate = convertDate(value, true);
112 policyAdapter.setTtlDate(newDate);
115 LinkedHashMap<String, String> serviceTypePolicyName1 = new LinkedHashMap<>();
116 String key = "serviceTypePolicyName";
117 serviceTypePolicyName1.put(key, value);
118 policyAdapter.setServiceTypePolicyName(serviceTypePolicyName1);
119 LinkedHashMap<String, String> vertica = new LinkedHashMap<>();
120 vertica.put("verticaMetrics", getVertica(value));
121 policyAdapter.setVerticaMetrics(vertica);
122 LinkedHashMap<String, String> desc = new LinkedHashMap<>();
123 desc.put("policyDescription", getDescription(value));
124 policyAdapter.setDescription(desc);
125 LinkedHashMap<String, Object> attributes = new LinkedHashMap<>();
126 attributes.put("attributes", getAttributes(value));
127 policyAdapter.setAttributeFields(attributes);
137 readClosedLoopJSONFile(policyAdapter, entity);
141 private String convertDate(String dateTTL, boolean portalType) {
142 String formateDate = null;
147 parts = dateTTL.split("-");
148 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
150 date = dateTTL.split("T");
151 parts = date[0].split("-");
152 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
157 protected void readClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
158 ObjectMapper mapper = new ObjectMapper();
160 ClosedLoopPMBody closedLoopBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopPMBody.class);
161 policyAdapter.setJsonBodyData(closedLoopBody);
162 } catch (IOException e) {
167 //get vertica metrics data from the table
168 private String getVertica(String policyName){
169 String verticas = null;
170 JsonArray data = PolicyManagerServlet.getPolicyNames();
171 for(int i=0 ; i< data.size(); i++){
172 if(policyName.equals(data.getJsonObject(i).getJsonString("serviceTypePolicyName").getString())){
173 verticas = data.getJsonObject(i).getJsonString("verticaMetrics").getString();
180 //get policy description from the table
181 private String getDescription(String policyName){
182 String description = null;
183 JsonArray data = PolicyManagerServlet.getPolicyNames();
184 for(int i=0 ; i< data.size(); i++){
185 if(policyName.equals(data.getJsonObject(i).getJsonString("serviceTypePolicyName").getString())){
186 description = data.getJsonObject(i).getJsonString("policyDescription").getString();
194 private JsonObject getAttributes(String policyName){
195 JsonObject attributes = null;
196 JsonArray data = PolicyManagerServlet.getPolicyNames();
197 for(int i=0 ; i< data.size(); i++){
198 if(policyName.equals(data.getJsonObject(i).getJsonString("serviceTypePolicyName").getString())){
199 attributes = data.getJsonObject(i).getJsonObject("attributes");