Format java POLICY-SDK-APP
[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, 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Bell Canada
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controller;
23
24 import com.fasterxml.jackson.databind.DeserializationFeature;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.databind.ObjectWriter;
28
29 import java.io.UnsupportedEncodingException;
30 import java.net.URLEncoder;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.LinkedHashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Objects;
38 import java.util.stream.IntStream;
39
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
46
47 import org.onap.policy.common.logging.flexlogger.FlexLogger;
48 import org.onap.policy.common.logging.flexlogger.Logger;
49 import org.onap.policy.rest.adapter.ClosedLoopFaultBody;
50 import org.onap.policy.rest.adapter.ClosedLoopFaultTrapDatas;
51 import org.onap.policy.rest.adapter.ClosedLoopFaultTriggerUISignatures;
52 import org.onap.policy.rest.adapter.ClosedLoopSignatures;
53 import org.onap.policy.rest.adapter.PolicyRestAdapter;
54 import org.onap.policy.rest.dao.CommonClassDao;
55 import org.onap.policy.rest.jpa.OnapName;
56 import org.onap.policy.rest.jpa.PolicyEntity;
57 import org.onap.policy.rest.jpa.VarbindDictionary;
58 import org.onap.portalsdk.core.controller.RestrictedBaseController;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.stereotype.Controller;
61 import org.springframework.web.bind.annotation.RequestMapping;
62
63 @Controller
64 @RequestMapping("/")
65 public class CreateClosedLoopFaultController extends RestrictedBaseController {
66
67     private static final Logger policyLogger = FlexLogger.getLogger(CreateClosedLoopFaultController.class);
68     private static final String FAULT = "Fault";
69     private static final String TRAP = "Trap";
70     private static final String NOT_BOX = "notBox";
71     private static final String OPERATOR_BOX = "operatorBox";
72     private static final String CONNECT_TRAP_1 = "connectTrap1";
73     private static final String TRAP_COUNT_1 = "trapCount1";
74     private static final String CONNECT_TRAP_2 = "connectTrap2";
75     private static final String TRAP_COUNT_2 = "trapCount2";
76     private static final String TRIGGER_1 = "trigger1";
77     private static final String ENC_UTF_8 = "UTF-8";
78     private static final String TRIGGER_2 = "trigger2";
79
80     protected PolicyRestAdapter policyAdapter = null;
81
82     private static CommonClassDao commonclassdao;
83
84     @Autowired
85     private CreateClosedLoopFaultController(CommonClassDao commonclassdao) {
86         CreateClosedLoopFaultController.commonclassdao = commonclassdao;
87     }
88
89     public CreateClosedLoopFaultController() {
90         // Empty constructor
91     }
92
93     public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
94         try {
95             ObjectMapper mapper = new ObjectMapper();
96             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
97             ClosedLoopFaultTrapDatas trapDatas =
98                     mapper.readValue(root.get("trapData").toString(), ClosedLoopFaultTrapDatas.class);
99             ClosedLoopFaultTrapDatas faultDatas =
100                     mapper.readValue(root.get("faultData").toString(), ClosedLoopFaultTrapDatas.class);
101             ClosedLoopGridJSONData policyJsonData =
102                     mapper.readValue(root.get("policyData").get("policy").toString(), ClosedLoopGridJSONData.class);
103             ClosedLoopFaultBody jsonBody = mapper.readValue(
104                     root.get("policyData").get("policy").get("jsonBodyData").toString(), ClosedLoopFaultBody.class);
105
106             // Build trapSignatureDatas list from faultData
107             List<Object> trapSignatureDatas = new ArrayList<>();
108             if (trapDatas.getTrap1() != null) {
109                 trapSignatureDatas.add(trapDatas);
110             }
111             // Extract resultBody and set jsonBody with trap ClosedLoopSignatures
112             String resultBody = getResultBody(policyJsonData, trapSignatureDatas);
113             ClosedLoopSignatures triggerSignatures = new ClosedLoopSignatures();
114             triggerSignatures.setSignatures(resultBody);
115             if (policyData.getClearTimeOut() != null) {
116                 triggerSignatures.setTimeWindow(Integer.parseInt(policyData.getClearTimeOut()));
117                 triggerSignatures.setTrapMaxAge(Integer.parseInt(policyData.getTrapMaxAge()));
118                 ClosedLoopFaultTriggerUISignatures uiTriggerSignatures = new ClosedLoopFaultTriggerUISignatures();
119                 if (!trapSignatureDatas.isEmpty()) {
120                     uiTriggerSignatures.setSignatures(getUITriggerSignature(TRAP, trapSignatureDatas.get(0)));
121                     if (!policyJsonData.getConnecttriggerSignatures().isEmpty()) {
122                         uiTriggerSignatures
123                                 .setConnectSignatures(getUIConnectTraps(policyJsonData.getConnecttriggerSignatures()));
124                     }
125                 }
126                 jsonBody.setTriggerSignaturesUsedForUI(uiTriggerSignatures);
127                 jsonBody.setTriggerTimeWindowUsedForUI(Integer.parseInt(policyData.getClearTimeOut()));
128                 jsonBody.setTrapMaxAge(Integer.parseInt(policyData.getTrapMaxAge()));
129             }
130
131             jsonBody.setTriggerSignatures(triggerSignatures);
132
133             // Build faultSignatureData list from faultData
134             List<Object> faultSignatureDatas = new ArrayList<>();
135             if (faultDatas.getTrap1() != null) {
136                 faultSignatureDatas.add(faultDatas);
137             }
138             // Extract faultBody and set jsonBody with fault ClosedLoopSignatures
139             String faultBody = getFaultBody(policyJsonData, faultSignatureDatas);
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(
149                                 getUIConnectTraps(policyJsonData.getConnectVerificationSignatures()));
150                     }
151                 }
152                 jsonBody.setVerificationSignaturesUsedForUI(uifaultSignatures);
153                 jsonBody.setVerificationTimeWindowUsedForUI(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     // TODO: Can getResultBody() and getFaultBody() be merged?
167     private String getResultBody(final ClosedLoopGridJSONData policyJsonData, final List<Object> trapSignatureDatas) {
168         StringBuilder resultBody = new StringBuilder();
169         if (!policyJsonData.getConnecttriggerSignatures().isEmpty()) {
170             resultBody.append("(");
171             IntStream.range(0, policyJsonData.getConnecttriggerSignatures().size())
172                     .mapToObj(i -> connectTriggerSignature(i, policyJsonData.getConnecttriggerSignatures(),
173                             trapSignatureDatas.get(0)))
174                     .forEach(resultBody::append);
175             resultBody.append(resultBody).append(")");
176         } else {
177             if (!trapSignatureDatas.isEmpty()) {
178                 resultBody.append(callTrap("nill", trapSignatureDatas.get(0)));
179             }
180         }
181         return resultBody.toString();
182     }
183
184     private String getFaultBody(final ClosedLoopGridJSONData policyJsonData, final List<Object> faultSignatureDatas) {
185         StringBuilder faultBody = new StringBuilder();
186         if (!policyJsonData.getConnectVerificationSignatures().isEmpty()) {
187             faultBody.append("(");
188             IntStream.range(0, policyJsonData.getConnectVerificationSignatures().size())
189                     .mapToObj(i -> connectTriggerSignature(i, policyJsonData.getConnectVerificationSignatures(),
190                             faultSignatureDatas.get(0)))
191                     .forEach(faultBody::append);
192             faultBody.append(")");
193         } else {
194             if (!faultSignatureDatas.isEmpty()) {
195                 faultBody.append(callTrap("nill", faultSignatureDatas.get(0)));
196             }
197         }
198         return faultBody.toString();
199     }
200
201     @SuppressWarnings("unchecked")
202     private String connectTriggerSignature(int index, List<Object> triggerSignatures, Object object) {
203         StringBuilder resultBody = new StringBuilder();
204         Map<String, String> connectTraps = (Map<String, String>) triggerSignatures.get(index);
205         try {
206             String notBox = "";
207             if (connectTraps.keySet().contains(NOT_BOX)) {
208                 notBox = connectTraps.get(NOT_BOX);
209             }
210             resultBody.append("(").append(notBox);
211         } catch (NullPointerException e) {
212             policyLogger.info("General error", e);
213             resultBody.append("(");
214         }
215
216         try {
217             // Append connectTrap1 body to resultBody
218             appendTrapToResultBody(triggerSignatures, object, resultBody, connectTraps, CONNECT_TRAP_1);
219
220             // Update trap1 count to resultBody
221             String trapCount1 = connectTraps.get(TRAP_COUNT_1);
222             resultBody.append(", Time = ").append(trapCount1).append(")");
223
224             // Append connectTrap2 body to resultBody
225             appendTrapToResultBody(triggerSignatures, object, resultBody, connectTraps, CONNECT_TRAP_2);
226
227             // Update operatorBox to resultBody
228             String operatorBox = connectTraps.get(OPERATOR_BOX);
229             resultBody.append(operatorBox).append("(");
230
231             // Update trap2 count to resultBody
232             String trapCount2 = connectTraps.get(TRAP_COUNT_2);
233             resultBody.append(", Time = ").append(trapCount2).append(")");
234         } catch (NullPointerException e) {
235             policyLogger.info("General error", e);
236         }
237         return resultBody.toString();
238     }
239
240     private void appendTrapToResultBody(List<Object> triggerSignatures, Object object, StringBuilder resultBody,
241             Map<String, String> connectTraps, String connectTrapName) {
242         String connectTrap = connectTraps.get(connectTrapName);
243         if (connectTrap.startsWith(TRAP) || connectTrap.startsWith(FAULT)) {
244             String trapBody = callTrap(connectTrap, object);
245             resultBody.append(trapBody);
246         } else if (connectTrap.startsWith("C")) {
247             for (int i = 0; i <= triggerSignatures.size(); i++) {
248                 Map<String, String> triggerSignature = (Map<String, String>) triggerSignatures.get(i);
249                 if (triggerSignature.get("id").equals(connectTrap)) {
250                     resultBody.append("(");
251                     String connectBody = connectTriggerSignature(i, triggerSignatures, object);
252                     resultBody.append(connectBody).append(")");
253                 } else { // FIXME: Is this a bug and can it be removed?
254                     i++;
255                 }
256             }
257         }
258     }
259
260     private String callTrap(String trap, Object object) {
261         String signatureBody = "";
262         ClosedLoopFaultTrapDatas trapDatas = (ClosedLoopFaultTrapDatas) object;
263         List<Object> attributeList = new ArrayList<>();
264         // Read the Trap
265         if (!"nill".equals(trap)) {
266             List<String> trapTypes = new ArrayList<>();
267             if (trap.startsWith(TRAP)) {
268                 trapTypes = Arrays.asList("Trap1", "Trap2", "Trap3", "Trap4", "Trap5", "Trap6");
269             } else if (trap.startsWith(FAULT)) {
270                 trapTypes = Arrays.asList("Fault1", "Fault2", "Fault3", "Fault4", "Fault5", "Fault6");
271             }
272             try {
273                 if (trapTypes.get(0).equals(trap)) {
274                     attributeList = trapDatas.getTrap1();
275                 } else if (trapTypes.get(1).equals(trap)) {
276                     attributeList = trapDatas.getTrap2();
277                 } else if (trapTypes.get(2).equals(trap)) {
278                     attributeList = trapDatas.getTrap3();
279                 } else if (trapTypes.get(3).equals(trap)) {
280                     attributeList = trapDatas.getTrap4();
281                 } else if (trapTypes.get(4).equals(trap)) {
282                     attributeList = trapDatas.getTrap5();
283                 } else if (trapTypes.get(5).equals(trap)) {
284                     attributeList = trapDatas.getTrap6();
285                 }
286             } catch (Exception e) {
287                 policyLogger.warn("Error during callTrap", e);
288                 return "(" + trap + ")";
289             }
290         } else {
291             if (trapDatas.getTrap1() == null) {
292                 return "";
293             }
294             attributeList = trapDatas.getTrap1();
295         }
296         signatureBody = signatureBody + "(" + readAttributes(attributeList, attributeList.size() - 1) + ")";
297         return signatureBody;
298     }
299
300     @SuppressWarnings("unchecked")
301     private String readAttributes(List<Object> object, int index) {
302         String attributes = "";
303         Map<String, String> trapSignatures = (Map<String, String>) object.get(index);
304         // Read the Elements.
305         Object notBox = "";
306         if (trapSignatures.keySet().contains(NOT_BOX)) {
307             notBox = trapSignatures.get(NOT_BOX);
308         }
309         if (notBox != null) {
310             attributes = attributes + notBox.toString();
311         }
312
313         // Get Attributes for trap1 name
314         Object trapName1 = trapSignatures.get(TRIGGER_1);
315         if (trapName1 == null) {
316             return "";
317         }
318         attributes = getTrapAttributesString(object, attributes, trapName1);
319
320         Object comboBox = trapSignatures.get(OPERATOR_BOX);
321         if (comboBox != null) {
322             attributes = attributes + comboBox.toString();
323         } else {
324             return attributes;
325         }
326
327         // Get Attributes for trap1 name
328         Object trapName2 = trapSignatures.get(TRIGGER_2);
329         if (trapName2 != null) {
330             attributes = getTrapAttributesString(object, attributes, trapName2);
331         }
332         return attributes;
333     }
334
335     private String getTrapAttributesString(List<Object> objectList, String attributesStr, Object trapName) {
336         String trap1Attrib = trapName.toString();
337         if (trap1Attrib.startsWith("A")) {
338             try {
339                 int iy = Integer.parseInt(trap1Attrib.substring(1)) - 1;
340                 attributesStr = attributesStr + "(" + readAttributes(objectList, iy) + ")";
341             } catch (NumberFormatException e) {
342                 try {
343                     trap1Attrib = getVarbindOID(trap1Attrib);
344                     attributesStr = attributesStr + "(" + URLEncoder.encode(trap1Attrib, ENC_UTF_8) + ")";
345                 } catch (UnsupportedEncodingException e1) {
346                     policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values", e1);
347                 }
348             }
349         } else {
350             try {
351                 trap1Attrib = getVarbindOID(trap1Attrib);
352                 attributesStr = attributesStr + "(" + URLEncoder.encode(trap1Attrib, ENC_UTF_8) + ")";
353             } catch (UnsupportedEncodingException e) {
354                 policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values", e);
355             }
356         }
357         return attributesStr;
358     }
359
360     private String getVarbindOID(String attrib) {
361         VarbindDictionary varbindId;
362         try {
363             varbindId =
364                     (VarbindDictionary) commonclassdao.getEntityItem(VarbindDictionary.class, "varbindName", attrib);
365             return varbindId.getVarbindOID();
366         } catch (Exception e) {
367             policyLogger.error("Error during retrieving varbindName " + attrib, e);
368             return attrib;
369         }
370     }
371
372     // connect traps data set to JSON Body as String
373     @SuppressWarnings({"unchecked", "rawtypes"})
374     private String getUIConnectTraps(List<Object> connectTrapSignatures) {
375         StringBuilder resultBody = new StringBuilder();
376         String connectMainBody = "";
377         for (Object connectTrapSignature : connectTrapSignatures) {
378             Map<String, String> connectTraps = (Map<String, String>) connectTrapSignature;
379             String connectBody = "";
380             if (connectTraps instanceof LinkedHashMap<?, ?>) {
381                 String notBox = "";
382                 String connectTrap1 = "";
383                 String trapCount1 = "";
384                 String operatorBox = "";
385                 String connectTrap2 = "";
386                 String trapCount2 = "";
387                 if (((LinkedHashMap) connectTraps).keySet().contains(NOT_BOX)) {
388                     notBox = ((LinkedHashMap) connectTraps).get(NOT_BOX).toString();
389                 }
390                 if (((LinkedHashMap) connectTraps).get(CONNECT_TRAP_1) != null) {
391                     connectTrap1 = ((LinkedHashMap) connectTraps).get(CONNECT_TRAP_1).toString();
392                 }
393                 if (((LinkedHashMap) connectTraps).get(TRAP_COUNT_1) != null) {
394                     trapCount1 = ((LinkedHashMap) connectTraps).get(TRAP_COUNT_1).toString();
395                 }
396                 if (((LinkedHashMap) connectTraps).get(OPERATOR_BOX) != null) {
397                     operatorBox = ((LinkedHashMap) connectTraps).get(OPERATOR_BOX).toString();
398                 }
399                 if (((LinkedHashMap) connectTraps).get(CONNECT_TRAP_2) != null) {
400                     connectTrap2 = ((LinkedHashMap) connectTraps).get(CONNECT_TRAP_2).toString();
401                 }
402                 if (((LinkedHashMap) connectTraps).get(TRAP_COUNT_2) != null) {
403                     trapCount2 = ((LinkedHashMap) connectTraps).get(TRAP_COUNT_2).toString();
404                 }
405                 connectBody = notBox + "@!" + connectTrap1 + "@!" + trapCount1 + "@!" + operatorBox + "@!"
406                         + connectTrap2 + "@!" + trapCount2 + "#!?!";
407             }
408             resultBody.append(connectBody);
409         }
410         connectMainBody = connectMainBody + resultBody;
411         return connectMainBody;
412     }
413
414     // get Trigger signature from JSON body
415     private String getUITriggerSignature(String trap, Object object2) {
416         ClosedLoopFaultTrapDatas trapDatas = (ClosedLoopFaultTrapDatas) object2;
417         List<Object> attributeList = new ArrayList<>();
418         // Read the Trap
419         if (trap.startsWith(TRAP) || trap.startsWith(FAULT)) {
420             if (trapDatas.getTrap1() != null) {
421                 attributeList.add(trapDatas.getTrap1());
422             }
423             if (trapDatas.getTrap2() != null) {
424                 attributeList.add(trapDatas.getTrap2());
425             }
426             if (trapDatas.getTrap3() != null) {
427                 attributeList.add(trapDatas.getTrap3());
428             }
429             if (trapDatas.getTrap4() != null) {
430                 attributeList.add(trapDatas.getTrap4());
431             }
432             if (trapDatas.getTrap5() != null) {
433                 attributeList.add(trapDatas.getTrap5());
434             }
435             if (trapDatas.getTrap6() != null) {
436                 attributeList.add(trapDatas.getTrap6());
437             }
438         }
439         return getTriggerBody(attributeList);
440     }
441
442     @SuppressWarnings({"rawtypes", "unchecked"})
443     private String getTriggerBody(final List<Object> attributeList) {
444         StringBuilder triggerBody = new StringBuilder();
445         for (Object o : attributeList) {
446             StringBuilder signatureBody = new StringBuilder();
447             List<Object> connectTraps = (ArrayList<Object>) o;
448             for (Object connectTrap : connectTraps) {
449                 String connectBody = "";
450                 if (connectTrap instanceof LinkedHashMap<?, ?>) {
451                     String notBox = "";
452                     String trigger1 = "";
453                     String operatorBox = "";
454                     String trigger2 = "";
455                     if (((LinkedHashMap) connectTrap).keySet().contains(NOT_BOX)) {
456                         notBox = ((LinkedHashMap) connectTrap).get(NOT_BOX).toString();
457                     }
458                     if (((LinkedHashMap) connectTrap).get(TRIGGER_1) != null) {
459                         trigger1 = ((LinkedHashMap) connectTrap).get(TRIGGER_1).toString();
460                     }
461                     if (((LinkedHashMap) connectTrap).get(OPERATOR_BOX) != null) {
462                         operatorBox = ((LinkedHashMap) connectTrap).get(OPERATOR_BOX).toString();
463                     }
464                     if (((LinkedHashMap) connectTrap).get(TRIGGER_2) != null) {
465                         trigger2 = ((LinkedHashMap) connectTrap).get(TRIGGER_2).toString();
466                     }
467                     connectBody = notBox + "@!" + trigger1 + "@!" + operatorBox + "@!" + trigger2 + "#!";
468                 }
469                 signatureBody.append(connectBody);
470             }
471             triggerBody.append(signatureBody).append("?!");
472         }
473         return triggerBody.toString();
474     }
475
476     public void prePopulateClosedLoopFaultPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
477         if (policyAdapter.getPolicyData() instanceof PolicyType) {
478             PolicyType policy = (PolicyType) policyAdapter.getPolicyData();
479
480             // Set PolicyAdapter policyName, description
481             setPolicyAdapterPolicyNameAndDescription(policyAdapter, policy);
482
483             // Set PolicyAdapter JsonBodyData, timeout settings
484             setClosedLoopJSONFile(policyAdapter, entity);
485
486             // Get the target data under policy.
487             TargetType target = policy.getTarget();
488             if (target == null) {
489                 return;
490             }
491
492             // Under target we have AnyOFType
493             List<AnyOfType> anyOfList = target.getAnyOf();
494             if (anyOfList == null) {
495                 return;
496             }
497
498             // Set PolicyAdapter OnapNameField, riskType, riskLevel, guard, ttlDate from match attributes
499             setPolicyAdapterMatchAttributes(policyAdapter, anyOfList);
500         }
501     }
502
503     private void setPolicyAdapterMatchAttributes(PolicyRestAdapter policyAdapter, List<AnyOfType> anyOfList) {
504         anyOfList.stream()
505                 // Extract nonNull list of AllOfType objs from each AnyOfType obj
506                 .map(AnyOfType::getAllOf).filter(Objects::nonNull).forEach(allOfList ->
507                 // Extract nonNull list of MatchType objs from each AllOFType obj
508                 allOfList.stream().map(AllOfType::getMatch).filter(Objects::nonNull).flatMap(Collection::stream)
509                         .forEach(match -> {
510                             // Under the match we have attribute value and
511                             // attributeDesignator. So,finally down to the actual attribute.
512                             AttributeValueType attributeValue = match.getAttributeValue();
513                             String value = (String) attributeValue.getContent().get(0);
514                             AttributeDesignatorType designator = match.getAttributeDesignator();
515                             String attributeId = designator.getAttributeId();
516                             // First match in the target is OnapName, so set that value.
517                             if ("ONAPName".equals(attributeId)) {
518                                 policyAdapter.setOnapName(value);
519                                 OnapName onapName = new OnapName();
520                                 onapName.setOnapName(value);
521                                 policyAdapter.setOnapNameField(onapName);
522                             } else if ("RiskType".equals(attributeId)) {
523                                 policyAdapter.setRiskType(value);
524                             } else if ("RiskLevel".equals(attributeId)) {
525                                 policyAdapter.setRiskLevel(value);
526                             } else if ("guard".equals(attributeId)) {
527                                 policyAdapter.setGuard(value);
528                             } else if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
529                                 PolicyController controller = new PolicyController();
530                                 String newDate = controller.convertDate(value);
531                                 policyAdapter.setTtlDate(newDate);
532                             }
533                         }));
534     }
535
536     private void setPolicyAdapterPolicyNameAndDescription(PolicyRestAdapter policyAdapter, PolicyType policy) {
537         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
538         String policyNameValue =
539                 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("Fault_") + 6);
540         policyAdapter.setPolicyName(policyNameValue);
541         String description;
542         try {
543             description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
544         } catch (Exception e) {
545             policyLogger.error(
546                     "Error during collecting the description tag info for createClosedLoopFault " + policyNameValue, e);
547             description = policy.getDescription();
548         }
549         policyAdapter.setPolicyDescription(description);
550     }
551
552     private void setClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
553         ObjectMapper mapper = new ObjectMapper();
554         try {
555             ClosedLoopFaultBody closedLoopBody =
556                     mapper.readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopFaultBody.class);
557             if ("ACTIVE".equalsIgnoreCase(closedLoopBody.getClosedLoopPolicyStatus())) {
558                 closedLoopBody.setClosedLoopPolicyStatus("Active");
559             } else {
560                 closedLoopBody.setClosedLoopPolicyStatus("InActive");
561             }
562             policyAdapter.setJsonBodyData(closedLoopBody);
563             if (closedLoopBody.getTrapMaxAge() != null) {
564                 policyAdapter.setTrapMaxAge(closedLoopBody.getTrapMaxAge().toString());
565             }
566             if (closedLoopBody.getTriggerTimeWindowUsedForUI() != null) {
567                 policyAdapter.setClearTimeOut(closedLoopBody.getTriggerTimeWindowUsedForUI().toString());
568             }
569             if (closedLoopBody.getVerificationTimeWindowUsedForUI() != null) {
570                 policyAdapter
571                         .setVerificationclearTimeOut(closedLoopBody.getVerificationTimeWindowUsedForUI().toString());
572             }
573         } catch (Exception e) {
574             policyLogger.error("Exception Occured" + e);
575         }
576     }
577 }
578
579
580 class ClosedLoopGridJSONData {
581
582     private String clearTimeOut;
583     private String trapMaxAge;
584     private String verificationclearTimeOut;
585     private List<Object> connecttriggerSignatures;
586     private List<Object> connectVerificationSignatures;
587
588     public String getClearTimeOut() {
589         return clearTimeOut;
590     }
591
592     public void setClearTimeOut(String clearTimeOut) {
593         this.clearTimeOut = clearTimeOut;
594     }
595
596     public String getTrapMaxAge() {
597         return trapMaxAge;
598     }
599
600     public void setTrapMaxAge(String trapMaxAge) {
601         this.trapMaxAge = trapMaxAge;
602     }
603
604     public String getVerificationclearTimeOut() {
605         return verificationclearTimeOut;
606     }
607
608     public void setVerificationclearTimeOut(String verificationclearTimeOut) {
609         this.verificationclearTimeOut = verificationclearTimeOut;
610     }
611
612     public List<Object> getConnecttriggerSignatures() {
613         return connecttriggerSignatures;
614     }
615
616     public void setConnecttriggerSignatures(List<Object> connecttriggerSignatures) {
617         this.connecttriggerSignatures = connecttriggerSignatures;
618     }
619
620     public List<Object> getConnectVerificationSignatures() {
621         return connectVerificationSignatures;
622     }
623
624     public void setConnectVerificationSignatures(List<Object> connectVerificationSignatures) {
625         this.connectVerificationSignatures = connectVerificationSignatures;
626     }
627 }