392adf03974650b3d40d7cb4454c659b575e883e
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateClosedLoopPMController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP 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.onap.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.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;
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                                 LOGGER.info("General error" , e);
67                                 description = policy.getDescription();
68                         }
69                         policyAdapter.setPolicyDescription(description);
70                         // Get the target data under policy.
71                         TargetType target = policy.getTarget();
72                         if (target != null) {
73                                 // Under target we have AnyOFType
74                                 List<AnyOfType> anyOfList = target.getAnyOf();
75                                 if (anyOfList != null) {
76                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
77                                         while (iterAnyOf.hasNext()) {
78                                                 AnyOfType anyOf = iterAnyOf.next();
79                                                 // Under AnyOFType we have AllOFType
80                                                 List<AllOfType> allOfList = anyOf.getAllOf();
81                                                 if (allOfList != null) {
82                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
83                                                         while (iterAllOf.hasNext()) {
84                                                                 AllOfType allOf = iterAllOf.next();
85                                                                 // Under AllOFType we have Match
86                                                                 List<MatchType> matchList = allOf.getMatch();
87                                                                 if (matchList != null) {
88                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
89                                                                         while (matchList.size()>1 && iterMatch.hasNext()) {
90                                                                                 MatchType match = iterMatch.next();
91                                                                                 //
92                                                                                 // Under the match we have attribute value and
93                                                                                 // attributeDesignator. So,finally down to the actual attribute.
94                                                                                 //
95                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
96                                                                                 String value = (String) attributeValue.getContent().get(0);
97                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
98                                                                                 String attributeId = designator.getAttributeId();
99
100                                                                                 // First match in the target is OnapName, so set that value.
101                                                                                 if ("ONAPName".equals(attributeId)) {
102                                                                                         policyAdapter.setOnapName(value);
103                                                                                 }
104                                                                                 if ("RiskType".equals(attributeId)){
105                                                                                         policyAdapter.setRiskType(value);
106                                                                                 }
107                                                                                 if ("RiskLevel".equals(attributeId)){
108                                                                                         policyAdapter.setRiskLevel(value);
109                                                                                 }
110                                                                                 if ("guard".equals(attributeId)){
111                                                                                         policyAdapter.setGuard(value);
112                                                                                 }
113                                                                                 if ("TTLDate".equals(attributeId) && !value.contains("NA")){
114                                                                                         PolicyController controller = new PolicyController();
115                                                                                         String newDate = controller.convertDate(value);
116                                                                                         policyAdapter.setTtlDate(newDate);
117                                                                                 }
118                                                                                 if ("ServiceType".equals(attributeId)){
119                                                                                         LinkedHashMap<String, String> serviceTypePolicyName1 = new LinkedHashMap<>();
120                                                                                         String key = "serviceTypePolicyName";
121                                                                                         serviceTypePolicyName1.put(key, value);
122                                                                                         policyAdapter.setServiceTypePolicyName(serviceTypePolicyName1);
123                                                                                         LinkedHashMap<String, String> vertica = new LinkedHashMap<>();
124                                                                                         vertica.put("verticaMetrics", getVertica(value));
125                                                                                         policyAdapter.setVerticaMetrics(vertica);
126                                                                                         LinkedHashMap<String, String> desc = new LinkedHashMap<>();
127                                                                                         desc.put("policyDescription", getDescription(value));
128                                                                                         policyAdapter.setDescription(desc);
129                                                                                         LinkedHashMap<String, Object> attributes = new LinkedHashMap<>();
130                                                                                         attributes.put("attributes", getAttributes(value));
131                                                                                         policyAdapter.setAttributeFields(attributes);
132                                                                                 }
133                                                                         }
134                                                                 }
135                                                         }
136                                                 }
137                                         }
138                                 }
139                         }
140                         readClosedLoopJSONFile(policyAdapter, entity);
141                 }               
142         }
143
144         protected void readClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
145                 ObjectMapper mapper = new ObjectMapper();
146                 try {
147                         ClosedLoopPMBody closedLoopBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopPMBody.class);
148                         policyAdapter.setJsonBodyData(closedLoopBody);
149                 } catch (IOException e) {
150                         LOGGER.error("Exception Occured"+e);
151                 }       
152         }
153         
154         //get vertica metrics data from the table
155         private String getVertica(String policyName){
156                 String verticas = null;
157                 JsonArray data = PolicyManagerServlet.getPolicyNames();
158                 for(int i=0 ; i< data.size(); i++){
159                         if(policyName.equals(data.getJsonObject(i).getJsonString("serviceTypePolicyName").getString())){
160                                 verticas = data.getJsonObject(i).getJsonString("verticaMetrics").getString();
161                                 return verticas;
162                         }
163                 }
164                 return verticas;
165         }
166
167         //get policy description from the table
168         private String getDescription(String policyName){
169                 String description = 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                                 description = data.getJsonObject(i).getJsonString("policyDescription").getString();
174                                 return description;
175                         }
176                 }
177                 return description;
178         }
179
180         //get Attributes
181         private JsonObject getAttributes(String policyName){
182                 JsonObject attributes = 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                                 attributes = data.getJsonObject(i).getJsonObject("attributes");
187                                 return attributes;
188                         }
189                 }
190                 return attributes;
191         }
192
193 }