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