Fixes for sonar critical issues
[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                             policyLogger.warn("Error during callTrap" , e);
283                                 return "(" + trap + ")";
284                         }
285                 }else{
286                         if(trapDatas.getTrap1()!=null){
287                                 attributeList = trapDatas.getTrap1();
288                         }else{
289                                 return "";
290                         }
291                 }
292                 signatureBody = signatureBody + "(" + readAttributes(attributeList, attributeList.size()-1) + ")";
293                 return signatureBody;
294         }
295         
296         @SuppressWarnings("unchecked")
297         private String readAttributes(ArrayList<Object> object, int index) {
298                 String attributes = "";
299                 Map<String, String> trapSignatures = (Map<String, String>) object.get(index);
300                 // Read the Elements. 
301                 Object notBox = "";
302                 if(trapSignatures.keySet().contains("notBox")){
303                         notBox = trapSignatures.get("notBox");
304                 }
305                 if(notBox!=null){
306                         attributes = attributes + notBox.toString();
307                 }
308                 Object trapName1 = trapSignatures.get("trigger1");
309                 if(trapName1!=null){
310                         String attrib = trapName1.toString();
311                         if(attrib.startsWith("A")){
312                                 try{
313                                         int iy = Integer.parseInt(attrib.substring(1))-1;
314                                         attributes = attributes + "(" + readAttributes(object, iy) + ")";
315                                 }catch(NumberFormatException e){
316                                         try {
317                                                 attrib  = getVarbindOID(attrib);
318                                                 attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8")+ ")";
319                                         } catch (UnsupportedEncodingException e1) {
320                                                 policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values",e1);
321                                         }
322                                 }
323                         }else{
324                                 try {
325                                         attrib  = getVarbindOID(attrib);
326                                         attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8")+ ")";
327                                 } catch (UnsupportedEncodingException e) {
328                                         policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values",e);
329                                 }
330                         }
331                 }else{
332                         return "";
333                 }
334                 Object comboBox = trapSignatures.get("operatorBox");
335                 if(comboBox!=null){
336                         attributes = attributes + comboBox.toString();
337                 }else{
338                         return attributes;
339                 }
340                 Object trapName2 = trapSignatures.get("trigger2"); 
341                 if(trapName2!=null){
342                         String attrib = trapName2.toString();
343                         if(attrib.startsWith("A")){
344                                 try{
345                                         int iy = Integer.parseInt(attrib.substring(1))-1;
346                                         attributes = attributes + "(" + readAttributes(object, iy) + ")";
347                                 }catch(NumberFormatException e){
348                                         try {
349                                                 attrib  = getVarbindOID(attrib);
350                                                 attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8") + ")";
351                                         } catch (UnsupportedEncodingException e1) {
352                                                 policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values",e1);
353                                         }
354                                 }
355                         }else{
356                                 try {
357                                         attrib  = getVarbindOID(attrib);
358                                         attributes = attributes + "("+ URLEncoder.encode(attrib, "UTF-8") + ")";
359                                 } catch (UnsupportedEncodingException e) {
360                                         policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values",e);
361                                 }
362                         }
363                 }
364                 return attributes;
365         }
366         
367         private String getVarbindOID(String attrib) {
368                 VarbindDictionary varbindId = null;
369                 try{
370                          varbindId = (VarbindDictionary) commonclassdao.getEntityItem(VarbindDictionary.class, "varbindName", attrib);
371                          return varbindId.getVarbindOID();
372                 }catch(Exception e){
373                     policyLogger.error("Error during retrieving varbindName " + attrib, e);
374                         return attrib;
375                 }
376         }
377
378         
379         //connect traps data set to JSON Body as String
380                 @SuppressWarnings({ "unchecked", "rawtypes" })
381                 private String getUIConnectTraps(ArrayList<Object> connectTrapSignatures) {
382                         String resultBody = "";
383                         String connectMainBody = "";
384                         for(int j = 0; j < connectTrapSignatures.size(); j++){
385                                 Map<String, String> connectTraps = (Map<String, String>)connectTrapSignatures.get(j);
386                                 String connectBody = "";
387                                 Object object = connectTraps;
388                                 if(object instanceof LinkedHashMap<?, ?>){
389                                         String notBox = "";
390                                         String connectTrap1 = "";
391                                         String trapCount1 = "";
392                                         String operatorBox = "";
393                                         String connectTrap2 = "";
394                                         String trapCount2 = "";
395                                         if(((LinkedHashMap) object).keySet().contains("notBox")){
396                                                 notBox = ((LinkedHashMap) object).get("notBox").toString();
397                                         }
398                                         if(((LinkedHashMap) object).get("connectTrap1") != null){
399                                                 connectTrap1 = ((LinkedHashMap) object).get("connectTrap1").toString();
400                                         }
401                                         if(((LinkedHashMap) object).get("trapCount1") != null){
402                                                 trapCount1 = ((LinkedHashMap) object).get("trapCount1").toString();
403                                         }
404                                         if(((LinkedHashMap) object).get("operatorBox") != null){
405                                                 operatorBox = ((LinkedHashMap) object).get("operatorBox").toString();
406                                         }
407                                         if(((LinkedHashMap) object).get("connectTrap2") != null){
408                                                 connectTrap2 = ((LinkedHashMap) object).get("connectTrap2").toString();
409                                         }
410                                         if(((LinkedHashMap) object).get("trapCount2") != null){
411                                                 trapCount2 = ((LinkedHashMap) object).get("trapCount2").toString();
412                                         }
413                                         connectBody = notBox + "@!" + connectTrap1 + "@!" + trapCount1 + "@!" + operatorBox + "@!" + connectTrap2 + "@!" + trapCount2 + "#!?!"; 
414                                 }
415                                 resultBody = resultBody + connectBody;
416                         }
417                         connectMainBody = connectMainBody + resultBody;
418                         return connectMainBody;
419                 }
420
421                         
422
423                         // get Trigger signature from JSON body
424                         @SuppressWarnings({ "rawtypes", "unchecked" })
425                         private String getUITriggerSignature(String trap, Object object2) {
426                                 String triggerBody = "";
427                                 TrapDatas trapDatas = (TrapDatas) object2;
428                                 ArrayList<Object> attributeList = new ArrayList<>();
429                                 // Read the Trap 
430                                 if(trap.startsWith("Trap")){
431                                         if(trapDatas.getTrap1()!= null){
432                                                 attributeList.add(trapDatas.getTrap1());
433                                         }
434                                         if(trapDatas.getTrap2()!= null){
435                                                 attributeList.add(trapDatas.getTrap2());
436                                         }
437                                         if(trapDatas.getTrap3()!= null){
438                                                 attributeList.add(trapDatas.getTrap3());
439                                         }
440                                         if(trapDatas.getTrap4()!= null){
441                                                 attributeList.add(trapDatas.getTrap4());
442                                         }
443                                         if(trapDatas.getTrap5()!= null){
444                                                 attributeList.add(trapDatas.getTrap5());
445                                         }
446                                         if(trapDatas.getTrap6()!= null){
447                                                 attributeList.add(trapDatas.getTrap6());
448                                         }
449                                 }else{
450                                         if(trap.startsWith("Fault")){
451                                                 if(trapDatas.getTrap1()!= null){
452                                                         attributeList.add(trapDatas.getTrap1());
453                                                 }
454                                                 if(trapDatas.getTrap2()!= null){
455                                                         attributeList.add(trapDatas.getTrap2());
456                                                 }
457                                                 if(trapDatas.getTrap3()!= null){
458                                                         attributeList.add(trapDatas.getTrap3());
459                                                 }
460                                                 if(trapDatas.getTrap4()!= null){
461                                                         attributeList.add(trapDatas.getTrap4());
462                                                 }
463                                                 if(trapDatas.getTrap5()!= null){
464                                                         attributeList.add(trapDatas.getTrap5());
465                                                 }
466                                                 if(trapDatas.getTrap6()!= null){
467                                                         attributeList.add(trapDatas.getTrap6());
468                                                 }                               
469                                         }       
470                                 }
471
472                                 for(int j = 0; j < attributeList.size(); j++){
473                                         String signatureBody = "";
474                                         ArrayList<Object> connectTraps = (ArrayList<Object>) attributeList.get(j);
475                                         for(int i =0 ; i < connectTraps.size(); i++){
476                                                 String connectBody = "";
477                                                 Object object = connectTraps.get(i);
478                                                 if(object instanceof LinkedHashMap<?, ?>){
479                                                         String notBox = "";
480                                                         String trigger1 = "";
481                                                         String operatorBox = "";
482                                                         String trigger2 = "";
483                                                         if(((LinkedHashMap) object).keySet().contains("notBox")){
484                                                                 notBox = ((LinkedHashMap) object).get("notBox").toString();
485                                                         }
486                                                         if(((LinkedHashMap) object).get("trigger1") != null){
487                                                                 trigger1 = ((LinkedHashMap) object).get("trigger1").toString();
488                                                         }
489                                                         if(((LinkedHashMap) object).get("operatorBox") != null){
490                                                                 operatorBox = ((LinkedHashMap) object).get("operatorBox").toString();
491                                                         }
492                                                         if(((LinkedHashMap) object).get("trigger2") != null){
493                                                                 trigger2 = ((LinkedHashMap) object).get("trigger2").toString();
494                                                         }
495                                                         connectBody = notBox + "@!" + trigger1 + "@!" + operatorBox + "@!" + trigger2 + "#!"; 
496                                                 }
497                                                 signatureBody = signatureBody + connectBody;
498                                         }
499                                         triggerBody = triggerBody + signatureBody + "?!";
500                                 }
501                                 
502                                 return triggerBody;             
503                         }
504
505         public  void prePopulateClosedLoopFaultPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
506                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
507                         Object policyData = policyAdapter.getPolicyData();
508                         PolicyType policy = (PolicyType) policyData;
509                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
510                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("Fault_") +6);
511                         policyAdapter.setPolicyName(policyNameValue);
512                         String description = "";
513                         try{
514                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
515                         }catch(Exception e){
516                             policyLogger.error("Error during collecting the description tag info for createClosedLoopFault " + policyNameValue , e);
517                                 description = policy.getDescription();
518                         }
519                         policyAdapter.setPolicyDescription(description);
520                         // Get the target data under policy.
521                         TargetType target = policy.getTarget();
522                         if (target != null) {
523                                 // Under target we have AnyOFType
524                                 List<AnyOfType> anyOfList = target.getAnyOf();
525                                 if (anyOfList != null) {
526                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
527                                         while (iterAnyOf.hasNext()) {
528                                                 AnyOfType anyOf = iterAnyOf.next();
529                                                 // Under AnyOFType we have AllOFType
530                                                 List<AllOfType> allOfList = anyOf.getAllOf();
531                                                 if (allOfList != null) {
532                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
533                                                         while (iterAllOf.hasNext()) {
534                                                                 AllOfType allOf = iterAllOf.next();
535                                                                 // Under AllOFType we have Match
536                                                                 List<MatchType> matchList = allOf.getMatch();
537                                                                 if (matchList != null) {
538                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
539                                                                         while (iterMatch.hasNext()) {
540                                                                                 MatchType match = iterMatch.next();
541                                                                                 //
542                                                                                 // Under the match we have attribute value and
543                                                                                 // attributeDesignator. So,finally down to the actual attribute.
544                                                                                 //
545                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
546                                                                                 String value = (String) attributeValue.getContent().get(0);
547                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
548                                                                                 String attributeId = designator.getAttributeId();
549                                                                                 
550                                                                                 // First match in the target is OnapName, so set that value.
551                                                                                 if (attributeId.equals("ONAPName")) {
552                                                                                         policyAdapter.setOnapName(value);
553                                                                                         OnapName onapName = new OnapName();
554                                                                                         onapName.setOnapName(value);
555                                                                                         policyAdapter.setOnapNameField(onapName);
556                                                                                 }
557                                                                                 if (attributeId.equals("RiskType")){
558                                                                                         policyAdapter.setRiskType(value);
559                                                                                 }
560                                                                                 if (attributeId.equals("RiskLevel")){
561                                                                                         policyAdapter.setRiskLevel(value);
562                                                                                 }
563                                                                                 if (attributeId.equals("guard")){
564                                                                                         policyAdapter.setGuard(value);
565                                                                                 }
566                                                                                 if (attributeId.equals("TTLDate") && !value.contains("NA")){
567                                                                                         String newDate = convertDate(value, true);
568                                                                                         policyAdapter.setTtlDate(newDate);
569                                                                                 }
570                                                                         }
571                                                                 }
572                                                         }
573                                                 }
574                                         }
575                                 }
576                         }
577                         readClosedLoopJSONFile(policyAdapter, entity);
578                 }       
579
580         }
581
582         private String convertDate(String dateTTL, boolean portalType) {
583                 String formateDate = null;
584                 String[] date;
585                 String[] parts;
586
587                 if (portalType){
588                         parts = dateTTL.split("-");
589                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
590                 } else {
591                         date  = dateTTL.split("T");
592                         parts = date[0].split("-");
593                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
594                 }
595                 return formateDate;
596         }
597
598         private String readClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
599                 ObjectMapper mapper = new ObjectMapper();
600                 try {
601                         ClosedLoopFaultBody closedLoopBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopFaultBody.class);
602                         if(closedLoopBody.getClosedLoopPolicyStatus().equalsIgnoreCase("ACTIVE")){
603                                 closedLoopBody.setClosedLoopPolicyStatus("Active");
604                         }else{
605                                 closedLoopBody.setClosedLoopPolicyStatus("InActive");
606                         }
607                         policyAdapter.setJsonBodyData(closedLoopBody);
608                         if(closedLoopBody.getTrapMaxAgeUsedForUI() != null){
609                                 policyAdapter.setTrapMaxAge(closedLoopBody.getTrapMaxAgeUsedForUI().toString());
610                         }
611                         if(closedLoopBody.getTriggerTimeWindowUsedForUI() != null){
612                                 policyAdapter.setClearTimeOut(closedLoopBody.getTriggerTimeWindowUsedForUI().toString());
613                         }
614                         if(closedLoopBody.getVerfificationTimeWindowUsedForUI() != null){
615                                 policyAdapter.setVerificationclearTimeOut(closedLoopBody.getVerfificationTimeWindowUsedForUI().toString());
616                         }
617
618                 } catch (Exception e) {
619                         policyLogger.error("Exception Occured"+e);
620                 }
621
622                 return null;    
623         }
624
625 }
626
627 class TrapDatas{
628         private ArrayList<Object> trap1;
629         private ArrayList<Object> trap2;
630         private ArrayList<Object> trap3;
631         private ArrayList<Object> trap4;
632         private ArrayList<Object> trap5;
633         private ArrayList<Object> trap6;
634         public ArrayList<Object> getTrap1() {
635                 return trap1;
636         }
637         public void setTrap1(ArrayList<Object> trap1) {
638                 this.trap1 = trap1;
639         }
640         public ArrayList<Object> getTrap2() {
641                 return trap2;
642         }
643         public void setTrap2(ArrayList<Object> trap2) {
644                 this.trap2 = trap2;
645         }
646         public ArrayList<Object> getTrap3() {
647                 return trap3;
648         }
649         public void setTrap3(ArrayList<Object> trap3) {
650                 this.trap3 = trap3;
651         }
652         public ArrayList<Object> getTrap4() {
653                 return trap4;
654         }
655         public void setTrap4(ArrayList<Object> trap4) {
656                 this.trap4 = trap4;
657         }
658         public ArrayList<Object> getTrap5() {
659                 return trap5;
660         }
661         public void setTrap5(ArrayList<Object> trap5) {
662                 this.trap5 = trap5;
663         }
664         public ArrayList<Object> getTrap6() {
665                 return trap6;
666         }
667         public void setTrap6(ArrayList<Object> trap6) {
668                 this.trap6 = trap6;
669         }
670 }
671
672 class ClosedLoopGridJSONData{
673
674         private String clearTimeOut;
675         private String trapMaxAge;
676         private String verificationclearTimeOut;
677         private ArrayList<Object> connecttriggerSignatures;
678         private ArrayList<Object> connectVerificationSignatures;
679
680         public String getClearTimeOut() {
681                 return clearTimeOut;
682         }
683         public void setClearTimeOut(String clearTimeOut) {
684                 this.clearTimeOut = clearTimeOut;
685         }
686         public String getTrapMaxAge() {
687                 return trapMaxAge;
688         }
689         public void setTrapMaxAge(String trapMaxAge) {
690                 this.trapMaxAge = trapMaxAge;
691         }
692         public String getVerificationclearTimeOut() {
693                 return verificationclearTimeOut;
694         }
695         public void setVerificationclearTimeOut(String verificationclearTimeOut) {
696                 this.verificationclearTimeOut = verificationclearTimeOut;
697         }
698         
699
700         public ArrayList<Object> getConnecttriggerSignatures() {
701                 return connecttriggerSignatures;
702         }
703         public void setConnecttriggerSignatures(ArrayList<Object> connecttriggerSignatures) {
704                 this.connecttriggerSignatures = connecttriggerSignatures;
705         }
706         public ArrayList<Object> getConnectVerificationSignatures() {
707                 return connectVerificationSignatures;
708         }
709         public void setConnectVerificationSignatures(ArrayList<Object> connectVerificationSignatures) {
710                 this.connectVerificationSignatures = connectVerificationSignatures;
711         }
712 }