Policy 1707 commit to LF
[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.rest.adapter.ClosedLoopPMBody;
34 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
35 import org.openecomp.policy.rest.jpa.PolicyEntity;
36
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
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;
45
46 public class CreateClosedLoopPMController{
47
48         protected PolicyRestAdapter policyAdapter = null;
49
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 = "";
58                         try{
59                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
60                         }catch(Exception e){
61                                 description = policy.getDescription();
62                         }
63                         policyAdapter.setPolicyDescription(description);
64                         // Get the target data under policy.
65                         TargetType target = policy.getTarget();
66                         if (target != null) {
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) {
82                                                                         int index = 0;
83                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
84                                                                         while (matchList.size()>1 && iterMatch.hasNext()) {
85                                                                                 MatchType match = iterMatch.next();
86                                                                                 //
87                                                                                 // Under the match we have attributevalue and
88                                                                                 // attributeDesignator. So,finally down to the actual attribute.
89                                                                                 //
90                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
91                                                                                 String value = (String) attributeValue.getContent().get(0);
92
93                                                                                 // First match in the target is EcompName, so set that value.
94                                                                                 if (index == 0) {
95                                                                                         policyAdapter.setEcompName(value);
96                                                                                 }
97
98                                                                                 if (index ==  1){
99                                                                                         policyAdapter.setRiskType(value);
100                                                                                 }
101
102                                                                                 if (index ==  2){
103                                                                                         policyAdapter.setRiskLevel(value);
104                                                                                 }
105
106                                                                                 if (index ==  3){
107                                                                                         policyAdapter.setGuard(value);
108                                                                                 }
109
110                                                                                 if (index == 4 && !value.contains("NA")){
111                                                                                         String newDate = convertDate(value, true);
112                                                                                         policyAdapter.setTtlDate(newDate);
113                                                                                 }
114                                                                                 if (index == 5){
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);
128                                                                                 }
129                                                                                 index++;
130                                                                         }
131                                                                 }
132                                                         }
133                                                 }
134                                         }
135                                 }
136                         }
137                         readClosedLoopJSONFile(policyAdapter, entity);
138                 }               
139         }
140
141         private String convertDate(String dateTTL, boolean portalType) {
142                 String formateDate = null;
143                 String[] date;
144                 String[] parts;
145
146                 if (portalType){
147                         parts = dateTTL.split("-");
148                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
149                 } else {
150                         date  = dateTTL.split("T");
151                         parts = date[0].split("-");
152                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
153                 }
154                 return formateDate;
155         }
156
157         protected void readClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
158                 ObjectMapper mapper = new ObjectMapper();
159                 try {
160                         ClosedLoopPMBody closedLoopBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopPMBody.class);
161                         policyAdapter.setJsonBodyData(closedLoopBody);
162                 } catch (IOException e) {
163                         e.printStackTrace();
164                 }       
165         }
166         
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();
174                                 return verticas;
175                         }
176                 }
177                 return verticas;
178         }
179
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();
187                                 return description;
188                         }
189                 }
190                 return description;
191         }
192
193         //get Attributes
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");
200                                 return attributes;
201                         }
202                 }
203                 return attributes;
204         }
205
206 }