[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateClosedLoopFaultController.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.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.rest.adapter.ClosedLoopFaultBody;
35 import org.onap.policy.rest.adapter.ClosedLoopFaultTriggerUISignatures;
36 import org.onap.policy.rest.adapter.ClosedLoopSignatures;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import org.onap.policy.rest.dao.CommonClassDao;
39 import org.onap.policy.rest.jpa.OnapName;
40 import org.onap.policy.rest.jpa.PolicyEntity;
41 import org.onap.policy.rest.jpa.VarbindDictionary;
42 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46
47 import com.fasterxml.jackson.databind.DeserializationFeature;
48 import com.fasterxml.jackson.databind.JsonNode;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50 import com.fasterxml.jackson.databind.ObjectWriter;
51
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
59
60 @Controller
61 @RequestMapping("/")
62 public class CreateClosedLoopFaultController extends RestrictedBaseController{
63
64         private static final Logger policyLogger        = FlexLogger.getLogger(CreateClosedLoopFaultController.class);
65         
66         protected PolicyRestAdapter policyAdapter = null;
67         
68         
69         private static CommonClassDao commonclassdao;
70         
71         @Autowired
72         private CreateClosedLoopFaultController(CommonClassDao commonclassdao){
73                 CreateClosedLoopFaultController.commonclassdao = commonclassdao;
74         }
75         
76         public CreateClosedLoopFaultController(){}
77         
78         public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root){
79                 try{
80                         ObjectMapper mapper = new ObjectMapper();
81                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
82                         TrapDatas trapDatas = mapper.readValue(root.get("trapData").toString(), TrapDatas.class);
83                         TrapDatas faultDatas = mapper.readValue(root.get("faultData").toString(), TrapDatas.class);
84                         ClosedLoopGridJSONData policyJsonData = mapper.readValue(root.get("policyData").get("policy").toString(), ClosedLoopGridJSONData.class);
85                         ClosedLoopFaultBody jsonBody = mapper.readValue(root.get("policyData").get("policy").get("jsonBodyData").toString(), ClosedLoopFaultBody.class);
86
87                         ArrayList<Object> trapSignatureDatas = new ArrayList<>();
88                         if(trapDatas.getTrap1() != null){
89                                 trapSignatureDatas.add(trapDatas);
90                         }
91                         ArrayList<Object> faultSignatureDatas = new ArrayList<>();
92                         if(faultDatas.getTrap1() != null){
93                                 faultSignatureDatas.add(faultDatas);
94                         }
95
96                         String resultBody = "";
97                         if(!policyJsonData.getConnecttriggerSignatures().isEmpty()){
98                                 resultBody = resultBody + "(";
99                                 for(int i = policyJsonData.getConnecttriggerSignatures().size()-1; i>=0 ; i--){
100                                         String connectBody = connectTriggerSignature(i, policyJsonData.getConnecttriggerSignatures(), trapSignatureDatas.get(0));
101                                         resultBody = resultBody  + connectBody;
102                                 }
103                                 resultBody = resultBody + ")";
104                         }else{
105                                 if(!trapSignatureDatas.isEmpty()){
106                                         resultBody = callTrap("nill", trapSignatureDatas.get(0));
107                                 }
108                         }
109                         ClosedLoopSignatures triggerSignatures = new ClosedLoopSignatures();
110                         triggerSignatures.setSignatures(resultBody);
111                         if(policyData.getClearTimeOut() != null){
112                                 triggerSignatures.setTimeWindow(Integer.parseInt(policyData.getClearTimeOut()));        
113                                 triggerSignatures.setTrapMaxAge(Integer.parseInt(policyData.getTrapMaxAge()));
114                                 ClosedLoopFaultTriggerUISignatures uiTriggerSignatures = new ClosedLoopFaultTriggerUISignatures();
115                                 if(!trapSignatureDatas.isEmpty()){
116                                         uiTriggerSignatures.setSignatures(getUITriggerSignature("Trap", trapSignatureDatas.get(0)));
117                                         if(!policyJsonData.getConnecttriggerSignatures().isEmpty()){
118                                                 uiTriggerSignatures.setConnectSignatures(getUIConnectTraps(policyJsonData.getConnecttriggerSignatures()));
119                                         }                               
120                                 }                       
121                                 jsonBody.setTriggerSignaturesUsedForUI(uiTriggerSignatures);
122                                 jsonBody.setTriggerTimeWindowUsedForUI(Integer.parseInt(policyData.getClearTimeOut()));
123                                 jsonBody.setTrapMaxAgeUsedForUI(Integer.parseInt(policyData.getTrapMaxAge()));
124                         }
125
126                         jsonBody.setTriggerSignatures(triggerSignatures);
127                         String faultBody = "";
128                         if(!policyJsonData.getConnectVerificationSignatures().isEmpty()){
129                                 faultBody = faultBody + "(";
130                                 for(int i = policyJsonData.getConnectVerificationSignatures().size()-1; i>=0 ; i--){
131                                         String connectBody = connectTriggerSignature(i, policyJsonData.getConnectVerificationSignatures(), faultSignatureDatas.get(0));
132                                         faultBody = faultBody  + connectBody;
133                                 }
134                                 faultBody = faultBody + ")";
135                         }else{
136                                 if(!faultSignatureDatas.isEmpty()){
137                                         faultBody = callTrap("nill", faultSignatureDatas.get(0));
138                                 }
139                         }
140                         ClosedLoopSignatures faultSignatures = new ClosedLoopSignatures();
141                         faultSignatures.setSignatures(faultBody);
142                         if(policyData.getVerificationclearTimeOut() != null){
143                                 faultSignatures.setTimeWindow(Integer.parseInt(policyData.getVerificationclearTimeOut()));
144                                 ClosedLoopFaultTriggerUISignatures uifaultSignatures = new ClosedLoopFaultTriggerUISignatures();
145                                 if(!faultSignatureDatas.isEmpty()){
146                                         uifaultSignatures.setSignatures(getUITriggerSignature("Fault", faultSignatureDatas.get(0)));
147                                         if(!policyJsonData.getConnectVerificationSignatures().isEmpty()){
148                                                 uifaultSignatures.setConnectSignatures(getUIConnectTraps(policyJsonData.getConnectVerificationSignatures()));
149                                         }               
150                                 }
151
152                                 jsonBody.setVerificationSignaturesUsedForUI(uifaultSignatures);
153                                 jsonBody.setVerfificationTimeWindowUsedForUI(Integer.parseInt(policyData.getVerificationclearTimeOut()));
154                         }               
155                         jsonBody.setVerificationSignatures(faultSignatures);
156                         ObjectWriter om = new ObjectMapper().writer();
157                         String json = om.writeValueAsString(jsonBody);
158                         policyData.setJsonBody(json);
159
160                 }catch(Exception e){
161                         policyLogger.error("Exception Occured while setting data to Adapter" , e);
162                 }
163                 return policyData;
164         }
165         
166
167         @SuppressWarnings("unchecked")
168         private String connectTriggerSignature(int index, ArrayList<Object> triggerSignatures, Object object) {
169                 String resultBody = "";
170                 Map<String, String> connectTraps = (Map<String, String>) triggerSignatures.get(index);
171                 try{
172                         String notBox = "";
173                         if(connectTraps.keySet().contains("notBox")){
174                                 notBox = connectTraps.get("notBox");
175                         }
176                         resultBody = resultBody + "(" + notBox;
177                 }catch(NullPointerException e){
178                         policyLogger.info("General error" , e);
179                         resultBody = resultBody + "(";
180                 }
181                 String connectTrap1 = connectTraps.get("connectTrap1");
182                 if(connectTrap1.startsWith("Trap") || connectTrap1.startsWith("Fault")){
183                         String trapBody = callTrap(connectTrap1, object);
184                         if(trapBody!=null){
185                                 resultBody = resultBody + trapBody;
186                         }
187                 }else if(connectTrap1.startsWith("C")){
188                         for(int i=0; i<= triggerSignatures.size(); i++){
189                                 Map<String,String> triggerSignature = (Map<String, String>) triggerSignatures.get(i);
190                                 if(triggerSignature.get("id").equals(connectTrap1)){
191                                         resultBody = resultBody + "(";
192                                         String connectBody = connectTriggerSignature(i, triggerSignatures, object);
193                                         resultBody = resultBody + connectBody + ")";
194                                 }else{
195                                         i++;
196                                 }
197                         }
198                 }
199                 try{
200                         String trapCount1 = connectTraps.get("trapCount1");
201                         resultBody = resultBody + ", Time = " + trapCount1 + ")";
202                 }catch(NullPointerException e){
203                         policyLogger.info("General error" , e);
204                 }
205                 try{
206                         String operatorBox = connectTraps.get("operatorBox");
207                         resultBody = resultBody + operatorBox +"(";
208                 }catch (NullPointerException e){
209                         policyLogger.info("General error" , e);
210                 }
211                 try{
212                         String connectTrap2 = connectTraps.get("connectTrap2");
213                         if(connectTrap2.startsWith("Trap") || connectTrap2.startsWith("Fault")){
214                                 String trapBody = callTrap(connectTrap2, object);
215                                 if(trapBody!=null){
216                                         resultBody = resultBody + trapBody;
217                                 }
218                         }else if(connectTrap2.startsWith("C")){
219                                 for(int i=0; i<= triggerSignatures.size(); i++){
220                                         Map<String,String> triggerSignature = (Map<String, String>) triggerSignatures.get(i);
221                                         if(triggerSignature.get("id").equals(connectTrap2)){
222                                                 resultBody = resultBody + "(";
223                                                 String connectBody = connectTriggerSignature(i, triggerSignatures, object);
224                                                 resultBody = resultBody + connectBody + ")";
225                                         }else{
226                                                 i++;
227                                         }
228                                 }
229                         }
230                 }catch(NullPointerException e){
231                         policyLogger.info("General error" , e);
232                 }
233                 try{
234                         String trapCount2 = connectTraps.get("trapCount2");
235                         resultBody = resultBody + ", Time = " + trapCount2 + ")";
236                 }catch(NullPointerException e){
237                         policyLogger.info("General error" , e);
238                 }
239                 return resultBody;
240         }
241
242         
243         private String callTrap(String trap, Object object) {
244                 String signatureBody = "";
245                 TrapDatas trapDatas = (TrapDatas) object;
246                 ArrayList<Object> attributeList = new ArrayList<>();
247                 // Read the Trap 
248                 if(!trap.equals("nill")){
249                         try{
250                                 if(trap.startsWith("Trap")){
251                                         if(trap.equals("Trap1")){
252                                                  attributeList = trapDatas.getTrap1();
253                                         }else if(trap.equals("Trap2")){
254                                                 attributeList = trapDatas.getTrap2();
255                                         }else if(trap.equals("Trap3")){
256                                                 attributeList = trapDatas.getTrap3();
257                                         }else if(trap.equals("Trap4")){
258                                                 attributeList = trapDatas.getTrap4();
259                                         }else if(trap.equals("Trap5")){
260                                                 attributeList = trapDatas.getTrap5();
261                                         }else if(trap.equals("Trap6")){
262                                                 attributeList = trapDatas.getTrap6();
263                                         }
264                                 }else{
265                                         if(trap.startsWith("Fault")){
266                                                 if(trap.equals("Fault1")){
267                                                         attributeList = trapDatas.getTrap1();
268                                                 }else if(trap.equals("Fault2")){
269                                                         attributeList = trapDatas.getTrap2();
270                                                 }else if(trap.equals("Fault3")){
271                                                         attributeList = trapDatas.getTrap3();
272                                                 }else if(trap.equals("Fault4")){
273                                                         attributeList = trapDatas.getTrap4();
274                                                 }else if(trap.equals("Fault5")){
275                                                         attributeList = trapDatas.getTrap5();
276                                                 }else if(trap.equals("Fault6")){
277                                                         attributeList = trapDatas.getTrap6();
278                                                 }       
279                                         }
280                                 }
281                         } catch(Exception e){
282                                 return "(" + trap + ")";
283                         }
284                 }else{
285                         if(trapDatas.getTrap1()!=null){
286                                 attributeList = trapDatas.getTrap1();
287                         }else{
288                                 return "";
289                         }
290                 }
291                 signatureBody = signatureBody + "(" + readAttributes(attributeList, attributeList.size()-1) + ")";
292                 return signatureBody;
293         }
294         
295         @SuppressWarnings("unchecked")
296         private String readAttributes(ArrayList<Object> object, int index) {
297                 String attributes = "";
298                 Map<String, String> trapSignatures = (Map<String, String>) object.get(index);
299                 // Read the Elements. 
300                 Object notBox = "";
301                 if(trapSignatures.keySet().contains("notBox")){
302                         notBox = trapSignatures.get("notBox");
303                 }
304                 if(notBox!=null){
305                         attributes = attributes + notBox.toString();
306                 }
307                 Object trapName1 = trapSignatures.get("trigger1");
308                 if(trapName1!=null){
309                         String attrib = trapName1.toString();
310                         if(attrib.startsWith("A")){
311                                 try{
312                                         int iy = Integer.parseInt(attrib.substring(1))-1;
313                                         attributes = attributes + "(" + readAttributes(object, iy) + ")";
314                                 }catch(NumberFormatException e){
315                                         try {
316                                                 attrib  = getVarbindOID(attrib);
317                                                 attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8")+ ")";
318                                         } catch (UnsupportedEncodingException e1) {
319                                                 //logger.error("Caused Exception while Encoding Varbind Dictionary Values"+e1);
320                                         }
321                                 }
322                         }else{
323                                 try {
324                                         attrib  = getVarbindOID(attrib);
325                                         attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8")+ ")";
326                                 } catch (UnsupportedEncodingException e) {
327                                         //logger.error("Caused Exception while Encoding Varbind Dictionary Values"+e);
328                                 }
329                         }
330                 }else{
331                         return "";
332                 }
333                 Object comboBox = trapSignatures.get("operatorBox");
334                 if(comboBox!=null){
335                         attributes = attributes + comboBox.toString();
336                 }else{
337                         return attributes;
338                 }
339                 Object trapName2 = trapSignatures.get("trigger2"); 
340                 if(trapName2!=null){
341                         String attrib = trapName2.toString();
342                         if(attrib.startsWith("A")){
343                                 try{
344                                         int iy = Integer.parseInt(attrib.substring(1))-1;
345                                         attributes = attributes + "(" + readAttributes(object, iy) + ")";
346                                 }catch(NumberFormatException e){
347                                         try {
348                                                 attrib  = getVarbindOID(attrib);
349                                                 attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8") + ")";
350                                         } catch (UnsupportedEncodingException e1) {
351                                                 //logger.error("Caused Exception while Encoding Varbind Dictionary Values"+e1);
352                                         }
353                                 }
354                         }else{
355                                 try {
356                                         attrib  = getVarbindOID(attrib);
357                                         attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8") + ")";
358                                 } catch (UnsupportedEncodingException e) {
359                                         //logger.error("Caused Exception while Encoding Varbind Dictionary Values"+e);
360                                 }
361                         }
362                 }
363                 return attributes;
364         }
365         
366         private String getVarbindOID(String attrib) {
367                 VarbindDictionary varbindId = null;
368                 try{
369                          varbindId = (VarbindDictionary) commonclassdao.getEntityItem(VarbindDictionary.class, "varbindName", attrib);
370                          return varbindId.getVarbindOID();
371                 }catch(Exception e){
372                         return attrib;
373                 }
374         }
375
376         
377         //connect traps data set to JSON Body as String
378                 @SuppressWarnings({ "unchecked", "rawtypes" })
379                 private String getUIConnectTraps(ArrayList<Object> connectTrapSignatures) {
380                         String resultBody = "";
381                         String connectMainBody = "";
382                         for(int j = 0; j < connectTrapSignatures.size(); j++){
383                                 Map<String, String> connectTraps = (Map<String, String>)connectTrapSignatures.get(j);
384                                 String connectBody = "";
385                                 Object object = connectTraps;
386                                 if(object instanceof LinkedHashMap<?, ?>){
387                                         String notBox = "";
388                                         String connectTrap1 = "";
389                                         String trapCount1 = "";
390                                         String operatorBox = "";
391                                         String connectTrap2 = "";
392                                         String trapCount2 = "";
393                                         if(((LinkedHashMap) object).keySet().contains("notBox")){
394                                                 notBox = ((LinkedHashMap) object).get("notBox").toString();
395                                         }
396                                         if(((LinkedHashMap) object).get("connectTrap1") != null){
397                                                 connectTrap1 = ((LinkedHashMap) object).get("connectTrap1").toString();
398                                         }
399                                         if(((LinkedHashMap) object).get("trapCount1") != null){
400                                                 trapCount1 = ((LinkedHashMap) object).get("trapCount1").toString();
401                                         }
402                                         if(((LinkedHashMap) object).get("operatorBox") != null){
403                                                 operatorBox = ((LinkedHashMap) object).get("operatorBox").toString();
404                                         }
405                                         if(((LinkedHashMap) object).get("connectTrap2") != null){
406                                                 connectTrap2 = ((LinkedHashMap) object).get("connectTrap2").toString();
407                                         }
408                                         if(((LinkedHashMap) object).get("trapCount2") != null){
409                                                 trapCount2 = ((LinkedHashMap) object).get("trapCount2").toString();
410                                         }
411                                         connectBody = notBox + "@!" + connectTrap1 + "@!" + trapCount1 + "@!" + operatorBox + "@!" + connectTrap2 + "@!" + trapCount2 + "#!?!"; 
412                                 }
413                                 resultBody = resultBody + connectBody;
414                         }
415                         connectMainBody = connectMainBody + resultBody;
416                         return connectMainBody;
417                 }
418
419                         
420
421                         // get Trigger signature from JSON body
422                         @SuppressWarnings({ "rawtypes", "unchecked" })
423                         private String getUITriggerSignature(String trap, Object object2) {
424                                 String triggerBody = "";
425                                 TrapDatas trapDatas = (TrapDatas) object2;
426                                 ArrayList<Object> attributeList = new ArrayList<>();
427                                 // Read the Trap 
428                                 if(trap.startsWith("Trap")){
429                                         if(trapDatas.getTrap1()!= null){
430                                                 attributeList.add(trapDatas.getTrap1());
431                                         }
432                                         if(trapDatas.getTrap2()!= null){
433                                                 attributeList.add(trapDatas.getTrap2());
434                                         }
435                                         if(trapDatas.getTrap3()!= null){
436                                                 attributeList.add(trapDatas.getTrap3());
437                                         }
438                                         if(trapDatas.getTrap4()!= null){
439                                                 attributeList.add(trapDatas.getTrap4());
440                                         }
441                                         if(trapDatas.getTrap5()!= null){
442                                                 attributeList.add(trapDatas.getTrap5());
443                                         }
444                                         if(trapDatas.getTrap6()!= null){
445                                                 attributeList.add(trapDatas.getTrap6());
446                                         }
447                                 }else{
448                                         if(trap.startsWith("Fault")){
449                                                 if(trapDatas.getTrap1()!= null){
450                                                         attributeList.add(trapDatas.getTrap1());
451                                                 }
452                                                 if(trapDatas.getTrap2()!= null){
453                                                         attributeList.add(trapDatas.getTrap2());
454                                                 }
455                                                 if(trapDatas.getTrap3()!= null){
456                                                         attributeList.add(trapDatas.getTrap3());
457                                                 }
458                                                 if(trapDatas.getTrap4()!= null){
459                                                         attributeList.add(trapDatas.getTrap4());
460                                                 }
461                                                 if(trapDatas.getTrap5()!= null){
462                                                         attributeList.add(trapDatas.getTrap5());
463                                                 }
464                                                 if(trapDatas.getTrap6()!= null){
465                                                         attributeList.add(trapDatas.getTrap6());
466                                                 }                               
467                                         }       
468                                 }
469
470                                 for(int j = 0; j < attributeList.size(); j++){
471                                         String signatureBody = "";
472                                         ArrayList<Object> connectTraps = (ArrayList<Object>) attributeList.get(j);
473                                         for(int i =0 ; i < connectTraps.size(); i++){
474                                                 String connectBody = "";
475                                                 Object object = connectTraps.get(i);
476                                                 if(object instanceof LinkedHashMap<?, ?>){
477                                                         String notBox = "";
478                                                         String trigger1 = "";
479                                                         String operatorBox = "";
480                                                         String trigger2 = "";
481                                                         if(((LinkedHashMap) object).keySet().contains("notBox")){
482                                                                 notBox = ((LinkedHashMap) object).get("notBox").toString();
483                                                         }
484                                                         if(((LinkedHashMap) object).get("trigger1") != null){
485                                                                 trigger1 = ((LinkedHashMap) object).get("trigger1").toString();
486                                                         }
487                                                         if(((LinkedHashMap) object).get("operatorBox") != null){
488                                                                 operatorBox = ((LinkedHashMap) object).get("operatorBox").toString();
489                                                         }
490                                                         if(((LinkedHashMap) object).get("trigger2") != null){
491                                                                 trigger2 = ((LinkedHashMap) object).get("trigger2").toString();
492                                                         }
493                                                         connectBody = notBox + "@!" + trigger1 + "@!" + operatorBox + "@!" + trigger2 + "#!"; 
494                                                 }
495                                                 signatureBody = signatureBody + connectBody;
496                                         }
497                                         triggerBody = triggerBody + signatureBody + "?!";
498                                 }
499                                 
500                                 return triggerBody;             
501                         }
502
503         public  void prePopulateClosedLoopFaultPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
504                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
505                         Object policyData = policyAdapter.getPolicyData();
506                         PolicyType policy = (PolicyType) policyData;
507                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
508                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("Fault_") +6);
509                         policyAdapter.setPolicyName(policyNameValue);
510                         String description = "";
511                         try{
512                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
513                         }catch(Exception e){
514                                 description = policy.getDescription();
515                         }
516                         policyAdapter.setPolicyDescription(description);
517                         // Get the target data under policy.
518                         TargetType target = policy.getTarget();
519                         if (target != null) {
520                                 // Under target we have AnyOFType
521                                 List<AnyOfType> anyOfList = target.getAnyOf();
522                                 if (anyOfList != null) {
523                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
524                                         while (iterAnyOf.hasNext()) {
525                                                 AnyOfType anyOf = iterAnyOf.next();
526                                                 // Under AnyOFType we have AllOFType
527                                                 List<AllOfType> allOfList = anyOf.getAllOf();
528                                                 if (allOfList != null) {
529                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
530                                                         while (iterAllOf.hasNext()) {
531                                                                 AllOfType allOf = iterAllOf.next();
532                                                                 // Under AllOFType we have Match
533                                                                 List<MatchType> matchList = allOf.getMatch();
534                                                                 if (matchList != null) {
535                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
536                                                                         while (iterMatch.hasNext()) {
537                                                                                 MatchType match = iterMatch.next();
538                                                                                 //
539                                                                                 // Under the match we have attribute value and
540                                                                                 // attributeDesignator. So,finally down to the actual attribute.
541                                                                                 //
542                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
543                                                                                 String value = (String) attributeValue.getContent().get(0);
544                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
545                                                                                 String attributeId = designator.getAttributeId();
546                                                                                 
547                                                                                 // First match in the target is OnapName, so set that value.
548                                                                                 if (attributeId.equals("ONAPName")) {
549                                                                                         policyAdapter.setOnapName(value);
550                                                                                         OnapName onapName = new OnapName();
551                                                                                         onapName.setOnapName(value);
552                                                                                         policyAdapter.setOnapNameField(onapName);
553                                                                                 }
554                                                                                 if (attributeId.equals("RiskType")){
555                                                                                         policyAdapter.setRiskType(value);
556                                                                                 }
557                                                                                 if (attributeId.equals("RiskLevel")){
558                                                                                         policyAdapter.setRiskLevel(value);
559                                                                                 }
560                                                                                 if (attributeId.equals("guard")){
561                                                                                         policyAdapter.setGuard(value);
562                                                                                 }
563                                                                                 if (attributeId.equals("TTLDate") && !value.contains("NA")){
564                                                                                         String newDate = convertDate(value, true);
565                                                                                         policyAdapter.setTtlDate(newDate);
566                                                                                 }
567                                                                         }
568                                                                 }
569                                                         }
570                                                 }
571                                         }
572                                 }
573                         }
574                         readClosedLoopJSONFile(policyAdapter, entity);
575                 }       
576
577         }
578
579         private String convertDate(String dateTTL, boolean portalType) {
580                 String formateDate = null;
581                 String[] date;
582                 String[] parts;
583
584                 if (portalType){
585                         parts = dateTTL.split("-");
586                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
587                 } else {
588                         date  = dateTTL.split("T");
589                         parts = date[0].split("-");
590                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
591                 }
592                 return formateDate;
593         }
594
595         private String readClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
596                 ObjectMapper mapper = new ObjectMapper();
597                 try {
598                         ClosedLoopFaultBody closedLoopBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopFaultBody.class);
599                         if(closedLoopBody.getClosedLoopPolicyStatus().equalsIgnoreCase("ACTIVE")){
600                                 closedLoopBody.setClosedLoopPolicyStatus("Active");
601                         }else{
602                                 closedLoopBody.setClosedLoopPolicyStatus("InActive");
603                         }
604                         policyAdapter.setJsonBodyData(closedLoopBody);
605                         if(closedLoopBody.getTrapMaxAgeUsedForUI() != null){
606                                 policyAdapter.setTrapMaxAge(closedLoopBody.getTrapMaxAgeUsedForUI().toString());
607                         }
608                         if(closedLoopBody.getTriggerTimeWindowUsedForUI() != null){
609                                 policyAdapter.setClearTimeOut(closedLoopBody.getTriggerTimeWindowUsedForUI().toString());
610                         }
611                         if(closedLoopBody.getVerfificationTimeWindowUsedForUI() != null){
612                                 policyAdapter.setVerificationclearTimeOut(closedLoopBody.getVerfificationTimeWindowUsedForUI().toString());
613                         }
614
615                 } catch (Exception e) {
616                         policyLogger.error("Exception Occured"+e);
617                 }
618
619                 return null;    
620         }
621
622 }
623
624 class TrapDatas{
625         private ArrayList<Object> trap1;
626         private ArrayList<Object> trap2;
627         private ArrayList<Object> trap3;
628         private ArrayList<Object> trap4;
629         private ArrayList<Object> trap5;
630         private ArrayList<Object> trap6;
631         public ArrayList<Object> getTrap1() {
632                 return trap1;
633         }
634         public void setTrap1(ArrayList<Object> trap1) {
635                 this.trap1 = trap1;
636         }
637         public ArrayList<Object> getTrap2() {
638                 return trap2;
639         }
640         public void setTrap2(ArrayList<Object> trap2) {
641                 this.trap2 = trap2;
642         }
643         public ArrayList<Object> getTrap3() {
644                 return trap3;
645         }
646         public void setTrap3(ArrayList<Object> trap3) {
647                 this.trap3 = trap3;
648         }
649         public ArrayList<Object> getTrap4() {
650                 return trap4;
651         }
652         public void setTrap4(ArrayList<Object> trap4) {
653                 this.trap4 = trap4;
654         }
655         public ArrayList<Object> getTrap5() {
656                 return trap5;
657         }
658         public void setTrap5(ArrayList<Object> trap5) {
659                 this.trap5 = trap5;
660         }
661         public ArrayList<Object> getTrap6() {
662                 return trap6;
663         }
664         public void setTrap6(ArrayList<Object> trap6) {
665                 this.trap6 = trap6;
666         }
667 }
668
669 class ClosedLoopGridJSONData{
670
671         private String clearTimeOut;
672         private String trapMaxAge;
673         private String verificationclearTimeOut;
674         private ArrayList<Object> connecttriggerSignatures;
675         private ArrayList<Object> connectVerificationSignatures;
676
677         public String getClearTimeOut() {
678                 return clearTimeOut;
679         }
680         public void setClearTimeOut(String clearTimeOut) {
681                 this.clearTimeOut = clearTimeOut;
682         }
683         public String getTrapMaxAge() {
684                 return trapMaxAge;
685         }
686         public void setTrapMaxAge(String trapMaxAge) {
687                 this.trapMaxAge = trapMaxAge;
688         }
689         public String getVerificationclearTimeOut() {
690                 return verificationclearTimeOut;
691         }
692         public void setVerificationclearTimeOut(String verificationclearTimeOut) {
693                 this.verificationclearTimeOut = verificationclearTimeOut;
694         }
695         
696
697         public ArrayList<Object> getConnecttriggerSignatures() {
698                 return connecttriggerSignatures;
699         }
700         public void setConnecttriggerSignatures(ArrayList<Object> connecttriggerSignatures) {
701                 this.connecttriggerSignatures = connecttriggerSignatures;
702         }
703         public ArrayList<Object> getConnectVerificationSignatures() {
704                 return connectVerificationSignatures;
705         }
706         public void setConnectVerificationSignatures(ArrayList<Object> connectVerificationSignatures) {
707                 this.connectVerificationSignatures = connectVerificationSignatures;
708         }
709 }