4c3249311f51d3d644e18874bcfe1daf487eaab6
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateClosedLoopFaultController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * 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 java.io.UnsupportedEncodingException;
25 import java.net.URLEncoder;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import java.util.Objects;
34 import java.util.stream.IntStream;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.rest.adapter.ClosedLoopFaultBody;
38 import org.onap.policy.rest.adapter.ClosedLoopFaultTrapDatas;
39 import org.onap.policy.rest.adapter.ClosedLoopFaultTriggerUISignatures;
40 import org.onap.policy.rest.adapter.ClosedLoopSignatures;
41 import org.onap.policy.rest.adapter.PolicyRestAdapter;
42 import org.onap.policy.rest.dao.CommonClassDao;
43 import org.onap.policy.rest.jpa.OnapName;
44 import org.onap.policy.rest.jpa.PolicyEntity;
45 import org.onap.policy.rest.jpa.VarbindDictionary;
46 import org.onap.portalsdk.core.controller.RestrictedBaseController;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.stereotype.Controller;
49 import org.springframework.web.bind.annotation.RequestMapping;
50
51 import com.fasterxml.jackson.databind.DeserializationFeature;
52 import com.fasterxml.jackson.databind.JsonNode;
53 import com.fasterxml.jackson.databind.ObjectMapper;
54 import com.fasterxml.jackson.databind.ObjectWriter;
55
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
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 = mapper
98                 .readValue(root.get("trapData").toString(), ClosedLoopFaultTrapDatas.class);
99             ClosedLoopFaultTrapDatas faultDatas = mapper
100                 .readValue(root.get("faultData").toString(), ClosedLoopFaultTrapDatas.class);
101             ClosedLoopGridJSONData policyJsonData = mapper
102                 .readValue(root.get("policyData").get("policy").toString(), ClosedLoopGridJSONData.class);
103             ClosedLoopFaultBody jsonBody = mapper
104                 .readValue(root.get("policyData").get("policy").get("jsonBodyData").toString(),
105                     ClosedLoopFaultBody.class);
106
107             // Build trapSignatureDatas list from faultData
108             List<Object> trapSignatureDatas = new ArrayList<>();
109             if (trapDatas.getTrap1() != null) {
110                 trapSignatureDatas.add(trapDatas);
111             }
112             // Extract resultBody and set jsonBody with trap ClosedLoopSignatures
113             String resultBody = getResultBody(policyJsonData, trapSignatureDatas);
114             ClosedLoopSignatures triggerSignatures = new ClosedLoopSignatures();
115             triggerSignatures.setSignatures(resultBody);
116             if (policyData.getClearTimeOut() != null) {
117                 triggerSignatures.setTimeWindow(Integer.parseInt(policyData.getClearTimeOut()));
118                 triggerSignatures.setTrapMaxAge(Integer.parseInt(policyData.getTrapMaxAge()));
119                 ClosedLoopFaultTriggerUISignatures uiTriggerSignatures = new ClosedLoopFaultTriggerUISignatures();
120                 if (!trapSignatureDatas.isEmpty()) {
121                     uiTriggerSignatures.setSignatures(getUITriggerSignature(TRAP, trapSignatureDatas.get(0)));
122                     if (!policyJsonData.getConnecttriggerSignatures().isEmpty()) {
123                         uiTriggerSignatures
124                             .setConnectSignatures(getUIConnectTraps(policyJsonData.getConnecttriggerSignatures()));
125                     }
126                 }
127                 jsonBody.setTriggerSignaturesUsedForUI(uiTriggerSignatures);
128                 jsonBody.setTriggerTimeWindowUsedForUI(Integer.parseInt(policyData.getClearTimeOut()));
129                 jsonBody.setTrapMaxAge(Integer.parseInt(policyData.getTrapMaxAge()));
130             }
131
132             jsonBody.setTriggerSignatures(triggerSignatures);
133
134             // Build faultSignatureData list from faultData
135             List<Object> faultSignatureDatas = new ArrayList<>();
136             if (faultDatas.getTrap1() != null) {
137                 faultSignatureDatas.add(faultDatas);
138             }
139             // Extract faultBody and set jsonBody with fault ClosedLoopSignatures
140             String faultBody = getFaultBody(policyJsonData, faultSignatureDatas);
141             ClosedLoopSignatures faultSignatures = new ClosedLoopSignatures();
142             faultSignatures.setSignatures(faultBody);
143             if (policyData.getVerificationclearTimeOut() != null) {
144                 faultSignatures.setTimeWindow(Integer.parseInt(policyData.getVerificationclearTimeOut()));
145                 ClosedLoopFaultTriggerUISignatures uifaultSignatures = new ClosedLoopFaultTriggerUISignatures();
146                 if (!faultSignatureDatas.isEmpty()) {
147                     uifaultSignatures.setSignatures(getUITriggerSignature(FAULT, faultSignatureDatas.get(0)));
148                     if (!policyJsonData.getConnectVerificationSignatures().isEmpty()) {
149                         uifaultSignatures
150                             .setConnectSignatures(getUIConnectTraps(policyJsonData.getConnectVerificationSignatures()));
151                     }
152                 }
153                 jsonBody.setVerificationSignaturesUsedForUI(uifaultSignatures);
154                 jsonBody.setVerificationTimeWindowUsedForUI(Integer.parseInt(policyData.getVerificationclearTimeOut()));
155             }
156             jsonBody.setVerificationSignatures(faultSignatures);
157             ObjectWriter om = new ObjectMapper().writer();
158             String json = om.writeValueAsString(jsonBody);
159             policyData.setJsonBody(json);
160
161         } catch (Exception e) {
162             policyLogger.error("Exception Occured while setting data to Adapter", e);
163         }
164         return policyData;
165     }
166
167     // TODO: Can getResultBody() and getFaultBody() be merged?
168     private String getResultBody(final ClosedLoopGridJSONData policyJsonData, final List<Object> trapSignatureDatas) {
169         StringBuilder resultBody = new StringBuilder();
170         if (!policyJsonData.getConnecttriggerSignatures().isEmpty()) {
171             resultBody.append("(");
172             IntStream.range(0, policyJsonData.getConnecttriggerSignatures().size())
173                 .mapToObj(i -> connectTriggerSignature(i, policyJsonData.getConnecttriggerSignatures(),
174                     trapSignatureDatas.get(0))).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))).forEach(faultBody::append);
191             faultBody.append(")");
192         } else {
193             if (!faultSignatureDatas.isEmpty()) {
194                 faultBody.append(callTrap("nill", faultSignatureDatas.get(0)));
195             }
196         }
197         return faultBody.toString();
198     }
199
200     @SuppressWarnings("unchecked")
201     private String connectTriggerSignature(int index, List<Object> triggerSignatures, Object object) {
202         StringBuilder resultBody = new StringBuilder();
203         Map<String, String> connectTraps = (Map<String, String>) triggerSignatures.get(index);
204         try {
205             String notBox = "";
206             if (connectTraps.keySet().contains(NOT_BOX)) {
207                 notBox = connectTraps.get(NOT_BOX);
208             }
209             resultBody.append("(").append(notBox);
210         } catch (NullPointerException e) {
211             policyLogger.info("General error", e);
212             resultBody.append("(");
213         }
214
215         try {
216             // Append connectTrap1 body to resultBody
217             appendTrapToResultBody(triggerSignatures, object, resultBody, connectTraps, CONNECT_TRAP_1);
218
219             // Update trap1 count to resultBody
220             String trapCount1 = connectTraps.get(TRAP_COUNT_1);
221             resultBody.append(", Time = ").append(trapCount1).append(")");
222
223             // Append connectTrap2 body to resultBody
224             appendTrapToResultBody(triggerSignatures, object, resultBody, connectTraps, CONNECT_TRAP_2);
225
226             // Update operatorBox to resultBody
227             String operatorBox = connectTraps.get(OPERATOR_BOX);
228             resultBody.append(operatorBox).append("(");
229
230             // Update trap2 count to resultBody
231             String trapCount2 = connectTraps.get(TRAP_COUNT_2);
232             resultBody.append(", Time = ").append(trapCount2).append(")");
233         } catch (NullPointerException e) {
234             policyLogger.info("General error", e);
235         }
236         return resultBody.toString();
237     }
238
239     private void appendTrapToResultBody(List<Object> triggerSignatures, Object object, StringBuilder resultBody,
240         Map<String, String> connectTraps, String connectTrapName) {
241         String connectTrap = connectTraps.get(connectTrapName);
242         if (connectTrap.startsWith(TRAP) || connectTrap.startsWith(FAULT)) {
243             String trapBody = callTrap(connectTrap, object);
244             resultBody.append(trapBody);
245         } else if (connectTrap.startsWith("C")) {
246             for (int i = 0; i <= triggerSignatures.size(); i++) {
247                 Map<String, String> triggerSignature = (Map<String, String>) triggerSignatures.get(i);
248                 if (triggerSignature.get("id").equals(connectTrap)) {
249                     resultBody.append("(");
250                     String connectBody = connectTriggerSignature(i, triggerSignatures, object);
251                     resultBody.append(connectBody).append(")");
252                 } else { // FIXME: Is this a bug and can it be removed?
253                     i++;
254                 }
255             }
256         }
257     }
258
259     private String callTrap(String trap, Object object) {
260         String signatureBody = "";
261         ClosedLoopFaultTrapDatas trapDatas = (ClosedLoopFaultTrapDatas) object;
262         List<Object> attributeList = new ArrayList<>();
263         // Read the Trap
264         if (!"nill".equals(trap)) {
265             List<String> trapTypes = new ArrayList<>();
266             if (trap.startsWith(TRAP)) {
267                 trapTypes = Arrays.asList("Trap1", "Trap2", "Trap3", "Trap4", "Trap5", "Trap6");
268             } else if (trap.startsWith(FAULT)) {
269                 trapTypes = Arrays.asList("Fault1", "Fault2", "Fault3", "Fault4", "Fault5", "Fault6");
270             }
271             try {
272                 if (trapTypes.get(0).equals(trap)) {
273                     attributeList = trapDatas.getTrap1();
274                 } else if (trapTypes.get(1).equals(trap)) {
275                     attributeList = trapDatas.getTrap2();
276                 } else if (trapTypes.get(2).equals(trap)) {
277                     attributeList = trapDatas.getTrap3();
278                 } else if (trapTypes.get(3).equals(trap)) {
279                     attributeList = trapDatas.getTrap4();
280                 } else if (trapTypes.get(4).equals(trap)) {
281                     attributeList = trapDatas.getTrap5();
282                 } else if (trapTypes.get(5).equals(trap)) {
283                     attributeList = trapDatas.getTrap6();
284                 }
285             } catch (Exception e) {
286                 policyLogger.warn("Error during callTrap", e);
287                 return "(" + trap + ")";
288             }
289         } else {
290             if (trapDatas.getTrap1() == null) {
291                 return "";
292             }
293             attributeList = trapDatas.getTrap1();
294         }
295         signatureBody = signatureBody + "(" + readAttributes(attributeList, attributeList.size() - 1) + ")";
296         return signatureBody;
297     }
298
299     @SuppressWarnings("unchecked")
300     private String readAttributes(List<Object> object, int index) {
301         String attributes = "";
302         Map<String, String> trapSignatures = (Map<String, String>) object.get(index);
303         // Read the Elements.
304         Object notBox = "";
305         if (trapSignatures.keySet().contains(NOT_BOX)) {
306             notBox = trapSignatures.get(NOT_BOX);
307         }
308         if (notBox != null) {
309             attributes = attributes + notBox.toString();
310         }
311
312         // Get Attributes for trap1 name
313         Object trapName1 = trapSignatures.get(TRIGGER_1);
314         if (trapName1 == null) {
315             return "";
316         }
317         attributes = getTrapAttributesString(object, attributes, trapName1);
318
319         Object comboBox = trapSignatures.get(OPERATOR_BOX);
320         if (comboBox != null) {
321             attributes = attributes + comboBox.toString();
322         } else {
323             return attributes;
324         }
325
326         // Get Attributes for trap1 name
327         Object trapName2 = trapSignatures.get(TRIGGER_2);
328         if (trapName2 != null) {
329             attributes = getTrapAttributesString(object, attributes, trapName2);
330         }
331         return attributes;
332     }
333
334     private String getTrapAttributesString(List<Object> objectList, String attributesStr, Object trapName) {
335         String trap1Attrib = trapName.toString();
336         if (trap1Attrib.startsWith("A")) {
337             try {
338                 int iy = Integer.parseInt(trap1Attrib.substring(1)) - 1;
339                 attributesStr = attributesStr + "(" + readAttributes(objectList, iy) + ")";
340             } catch (NumberFormatException e) {
341                 try {
342                     trap1Attrib = getVarbindOID(trap1Attrib);
343                     attributesStr = attributesStr + "(" + URLEncoder.encode(trap1Attrib, ENC_UTF_8) + ")";
344                 } catch (UnsupportedEncodingException e1) {
345                     policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values", e1);
346                 }
347             }
348         } else {
349             try {
350                 trap1Attrib = getVarbindOID(trap1Attrib);
351                 attributesStr = attributesStr + "(" + URLEncoder.encode(trap1Attrib, ENC_UTF_8) + ")";
352             } catch (UnsupportedEncodingException e) {
353                 policyLogger.error("Caused Exception while Encoding Varbind Dictionary Values", e);
354             }
355         }
356         return attributesStr;
357     }
358
359     private String getVarbindOID(String attrib) {
360         VarbindDictionary varbindId;
361         try {
362             varbindId = (VarbindDictionary) commonclassdao
363                 .getEntityItem(VarbindDictionary.class, "varbindName", attrib);
364             return varbindId.getVarbindOID();
365         } catch (Exception e) {
366             policyLogger.error("Error during retrieving varbindName " + attrib, e);
367             return attrib;
368         }
369     }
370
371     //connect traps data set to JSON Body as String
372     @SuppressWarnings({"unchecked", "rawtypes"})
373     private String getUIConnectTraps(List<Object> connectTrapSignatures) {
374         StringBuilder resultBody = new StringBuilder();
375         String connectMainBody = "";
376         for (Object connectTrapSignature : connectTrapSignatures) {
377             Map<String, String> connectTraps = (Map<String, String>) connectTrapSignature;
378             String connectBody = "";
379             if (connectTraps instanceof LinkedHashMap<?, ?>) {
380                 String notBox = "";
381                 String connectTrap1 = "";
382                 String trapCount1 = "";
383                 String operatorBox = "";
384                 String connectTrap2 = "";
385                 String trapCount2 = "";
386                 if (((LinkedHashMap) connectTraps).keySet().contains(NOT_BOX)) {
387                     notBox = ((LinkedHashMap) connectTraps).get(NOT_BOX).toString();
388                 }
389                 if (((LinkedHashMap) connectTraps).get(CONNECT_TRAP_1) != null) {
390                     connectTrap1 = ((LinkedHashMap) connectTraps).get(CONNECT_TRAP_1).toString();
391                 }
392                 if (((LinkedHashMap) connectTraps).get(TRAP_COUNT_1) != null) {
393                     trapCount1 = ((LinkedHashMap) connectTraps).get(TRAP_COUNT_1).toString();
394                 }
395                 if (((LinkedHashMap) connectTraps).get(OPERATOR_BOX) != null) {
396                     operatorBox = ((LinkedHashMap) connectTraps).get(OPERATOR_BOX).toString();
397                 }
398                 if (((LinkedHashMap) connectTraps).get(CONNECT_TRAP_2) != null) {
399                     connectTrap2 = ((LinkedHashMap) connectTraps).get(CONNECT_TRAP_2).toString();
400                 }
401                 if (((LinkedHashMap) connectTraps).get(TRAP_COUNT_2) != null) {
402                     trapCount2 = ((LinkedHashMap) connectTraps).get(TRAP_COUNT_2).toString();
403                 }
404                 connectBody =
405                     notBox + "@!" + connectTrap1 + "@!" + trapCount1 + "@!" + operatorBox + "@!" + connectTrap2 + "@!"
406                         + 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)
507             .forEach(allOfList ->
508                 //Extract nonNull list of MatchType objs from each AllOFType obj
509                 allOfList.stream().map(AllOfType::getMatch).filter(Objects::nonNull)
510                     .flatMap(Collection::stream)
511                     .forEach(match -> {
512                         // Under the match we have attribute value and
513                         // attributeDesignator. So,finally down to the actual attribute.
514                         AttributeValueType attributeValue = match.getAttributeValue();
515                         String value = (String) attributeValue.getContent().get(0);
516                         AttributeDesignatorType designator = match.getAttributeDesignator();
517                         String attributeId = designator.getAttributeId();
518                         // First match in the target is OnapName, so set that value.
519                         if ("ONAPName".equals(attributeId)) {
520                             policyAdapter.setOnapName(value);
521                             OnapName onapName = new OnapName();
522                             onapName.setOnapName(value);
523                             policyAdapter.setOnapNameField(onapName);
524                         } else if ("RiskType".equals(attributeId)) {
525                             policyAdapter.setRiskType(value);
526                         } else if ("RiskLevel".equals(attributeId)) {
527                             policyAdapter.setRiskLevel(value);
528                         } else if ("guard".equals(attributeId)) {
529                             policyAdapter.setGuard(value);
530                         } else if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
531                             PolicyController controller = new PolicyController();
532                             String newDate = controller.convertDate(value);
533                             policyAdapter.setTtlDate(newDate);
534                         }
535                     }));
536     }
537
538     private void setPolicyAdapterPolicyNameAndDescription(PolicyRestAdapter policyAdapter, PolicyType policy) {
539         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
540         String policyNameValue = policyAdapter.getPolicyName()
541             .substring(policyAdapter.getPolicyName().indexOf("Fault_") + 6);
542         policyAdapter.setPolicyName(policyNameValue);
543         String description;
544         try {
545             description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
546         } catch (Exception e) {
547             policyLogger.error(
548                 "Error during collecting the description tag info for createClosedLoopFault " + policyNameValue, e);
549             description = policy.getDescription();
550         }
551         policyAdapter.setPolicyDescription(description);
552     }
553
554     private void setClosedLoopJSONFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
555         ObjectMapper mapper = new ObjectMapper();
556         try {
557             ClosedLoopFaultBody closedLoopBody = mapper
558                 .readValue(entity.getConfigurationData().getConfigBody(), ClosedLoopFaultBody.class);
559             if ("ACTIVE".equalsIgnoreCase(closedLoopBody.getClosedLoopPolicyStatus())) {
560                 closedLoopBody.setClosedLoopPolicyStatus("Active");
561             } else {
562                 closedLoopBody.setClosedLoopPolicyStatus("InActive");
563             }
564             policyAdapter.setJsonBodyData(closedLoopBody);
565             if (closedLoopBody.getTrapMaxAge() != null) {
566                 policyAdapter.setTrapMaxAge(closedLoopBody.getTrapMaxAge().toString());
567             }
568             if (closedLoopBody.getTriggerTimeWindowUsedForUI() != null) {
569                 policyAdapter.setClearTimeOut(closedLoopBody.getTriggerTimeWindowUsedForUI().toString());
570             }
571             if (closedLoopBody.getVerificationTimeWindowUsedForUI() != null) {
572                 policyAdapter
573                     .setVerificationclearTimeOut(closedLoopBody.getVerificationTimeWindowUsedForUI().toString());
574             }
575         } catch (Exception e) {
576             policyLogger.error("Exception Occured" + e);
577         }
578     }
579 }
580
581 class ClosedLoopGridJSONData {
582
583     private String clearTimeOut;
584     private String trapMaxAge;
585     private String verificationclearTimeOut;
586     private List<Object> connecttriggerSignatures;
587     private List<Object> connectVerificationSignatures;
588
589     public String getClearTimeOut() {
590         return clearTimeOut;
591     }
592
593     public void setClearTimeOut(String clearTimeOut) {
594         this.clearTimeOut = clearTimeOut;
595     }
596
597     public String getTrapMaxAge() {
598         return trapMaxAge;
599     }
600
601     public void setTrapMaxAge(String trapMaxAge) {
602         this.trapMaxAge = trapMaxAge;
603     }
604
605     public String getVerificationclearTimeOut() {
606         return verificationclearTimeOut;
607     }
608
609     public void setVerificationclearTimeOut(String verificationclearTimeOut) {
610         this.verificationclearTimeOut = verificationclearTimeOut;
611     }
612
613
614     public List<Object> getConnecttriggerSignatures() {
615         return connecttriggerSignatures;
616     }
617
618     public void setConnecttriggerSignatures(List<Object> connecttriggerSignatures) {
619         this.connecttriggerSignatures = connecttriggerSignatures;
620     }
621
622     public List<Object> getConnectVerificationSignatures() {
623         return connectVerificationSignatures;
624     }
625
626     public void setConnectVerificationSignatures(List<Object> connectVerificationSignatures) {
627         this.connectVerificationSignatures = connectVerificationSignatures;
628     }
629 }