Policy TestSuite Enabled
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / CreateClosedLoopPMController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.controller;
22
23
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28
29 import javax.json.JsonArray;
30 import javax.json.JsonObject;
31
32 import org.openecomp.policy.admin.PolicyManagerServlet;
33 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import org.openecomp.policy.rest.adapter.ClosedLoopPMBody;
36 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
37 import org.openecomp.policy.rest.jpa.PolicyEntity;
38
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
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.MatchType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
48
49 public class CreateClosedLoopPMController{
50
51         private static final Logger LOGGER      = FlexLogger.getLogger(CreateClosedLoopPMController.class);
52         
53         protected PolicyRestAdapter policyAdapter = null;
54
55         public void prePopulateClosedLoopPMPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
56                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
57                         Object policyData = policyAdapter.getPolicyData();
58                         PolicyType policy = (PolicyType) policyData;
59                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
60                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("PM_") +3);
61                         policyAdapter.setPolicyName(policyNameValue);
62                         String description = "";
63                         try{
64                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
65                         }catch(Exception e){
66                                 description = policy.getDescription();
67                         }
68                         policyAdapter.setPolicyDescription(description);
69                         // Get the target data under policy.
70                         TargetType target = policy.getTarget();
71                         if (target != null) {
72                                 // Under target we have AnyOFType
73                                 List<AnyOfType> anyOfList = target.getAnyOf();
74                                 if (anyOfList != null) {
75                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
76                                         while (iterAnyOf.hasNext()) {
77                                                 AnyOfType anyOf = iterAnyOf.next();
78                                                 // Under AnyOFType we have AllOFType
79                                                 List<AllOfType> allOfList = anyOf.getAllOf();
80                                                 if (allOfList != null) {
81                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
82                                                         while (iterAllOf.hasNext()) {
83                                                                 AllOfType allOf = iterAllOf.next();
84                                                                 // Under AllOFType we have Match
85                                                                 List<MatchType> matchList = allOf.getMatch();
86                                                                 if (matchList != null) {
87                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
88                                                                         while (matchList.size()>1 && iterMatch.hasNext()) {
89                                                                                 MatchType match = iterMatch.next();
90                                                                                 //
91                                                                                 // Under the match we have attribute value and
92                                                                                 // attributeDesignator. So,finally down to the actual attribute.
93                                                                                 //
94                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
95                                                                                 String value = (String) attributeValue.getContent().get(0);
96                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
97                                                                                 String attributeId = designator.getAttributeId();
98
99                                                                                 // First match in the target is EcompName, so set that value.
100                                                                                 if (attributeId.equals("ECOMPName")) {
101                                                                                         policyAdapter.setEcompName(value);
102                                                                                 }
103                                                                                 if (attributeId.equals("RiskType")){
104                                                                                         policyAdapter.setRiskType(value);
105                                                                                 }
106                                                                                 if (attributeId.equals("RiskLevel")){
107                                                                                         policyAdapter.setRiskLevel(value);
108                                                                                 }
109                                                                                 if (attributeId.equals("guard")){
110                                                                                         policyAdapter.setGuard(value);
111                                                                                 }
112                                                                                 if (attributeId.equals("TTLDate") && !value.contains("NA")){
113                                                                                         String newDate = convertDate(value, true);
114                                                                                         policyAdapter.setTtlDate(newDate);
115                                                                                 }
116                                                                                 if (attributeId.equals("ServiceType")){
117                                                                                         LinkedHashMap<String, String> serviceTypePolicyName1 = new LinkedHashMap<>();
118                                                                                         String key = "serviceTypePolicyName";
119                                                                                         serviceTypePolicyName1.put(key, value);
120                                                                                         policyAdapter.setServiceTypePolicyName(serviceTypePolicyName1);
121                                                                                         LinkedHashMap<String, String> vertica = new LinkedHashMap<>();
122                                                                                         vertica.put("verticaMetrics", getVertica(value));
123                                                                                         policyAdapter.setVerticaMetrics(vertica);
124                                                                                         LinkedHashMap<String, String> desc = new LinkedHashMap<>();
125                                                                                         desc.put("policyDescription", getDescription(value));
126                                                                                         policyAdapter.setDescription(desc);
127                                                                                         LinkedHashMap<String, Object> attributes = new LinkedHashMap<>();
128                                                                                         attributes.put("attributes", getAttributes(value));
129                                                                                         policyAdapter.setAttributeFields(attributes);
130                                                                                 }
131                                                                         }
132                                                                 }
133                                                         }
134                                                 }
135                                         }
136                                 }
137                         }
138                         readClosedLoopJSONFile(policyAdapter, entity);
139                 }               
140         }
141
142         private String convertDate(String dateTTL, boolean portalType) {
143                 String formateDate = null;
144                 String[] date;
145                 String[] parts;
146
147                 if (portalType){
148                         parts = dateTTL.split("-");
149                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
150                 } else {
151                         date  = dateTTL.split("T");
152                         parts = date[0].split("-");
153                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
154                 }
155                 return formateDate;
156         }
157
158         protected void readClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
159                 ObjectMapper mapper = new ObjectMapper();
160                 try {
161                         ClosedLoopPMBody closedLoopBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopPMBody.class);
162                         policyAdapter.setJsonBodyData(closedLoopBody);
163                 } catch (IOException e) {
164                         LOGGER.error("Exception Occured"+e);
165                 }       
166         }
167         
168         //get vertica metrics data from the table
169         private String getVertica(String policyName){
170                 String verticas = null;
171                 JsonArray data = PolicyManagerServlet.getPolicyNames();
172                 for(int i=0 ; i< data.size(); i++){
173                         if(policyName.equals(data.getJsonObject(i).getJsonString("serviceTypePolicyName").getString())){
174                                 verticas = data.getJsonObject(i).getJsonString("verticaMetrics").getString();
175                                 return verticas;
176                         }
177                 }
178                 return verticas;
179         }
180
181         //get policy description from the table
182         private String getDescription(String policyName){
183                 String description = null;
184                 JsonArray data = PolicyManagerServlet.getPolicyNames();
185                 for(int i=0 ; i< data.size(); i++){
186                         if(policyName.equals(data.getJsonObject(i).getJsonString("serviceTypePolicyName").getString())){
187                                 description = data.getJsonObject(i).getJsonString("policyDescription").getString();
188                                 return description;
189                         }
190                 }
191                 return description;
192         }
193
194         //get Attributes
195         private JsonObject getAttributes(String policyName){
196                 JsonObject attributes = null;
197                 JsonArray data = PolicyManagerServlet.getPolicyNames();
198                 for(int i=0 ; i< data.size(); i++){
199                         if(policyName.equals(data.getJsonObject(i).getJsonString("serviceTypePolicyName").getString())){
200                                 attributes = data.getJsonObject(i).getJsonObject("attributes");
201                                 return attributes;
202                         }
203                 }
204                 return attributes;
205         }
206
207 }