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