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