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