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