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