Upgrade Java 17 in policy-drools-apps
[policy/drools-applications.git] / controlloop / common / coordination / src / main / java / org / onap / policy / coordination / Util.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
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.coordination;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.UUID;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33 import org.apache.commons.io.IOUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.yaml.snakeyaml.LoaderOptions;
37 import org.yaml.snakeyaml.Yaml;
38 import org.yaml.snakeyaml.constructor.Constructor;
39 import org.yaml.snakeyaml.inspector.TagInspector;
40
41
42 public final class Util {
43
44     private static final Logger logger = LoggerFactory.getLogger(Util.class);
45
46     private Util() {
47         super();
48     }
49
50     /**
51      * Load YAML coordination directive.
52      *
53      * @param directiveFilename yaml directive file to load
54      * @return the CoordinationDirective
55      */
56     public static CoordinationDirective loadCoordinationDirectiveFromFile(String directiveFilename) {
57         try (var is = new FileInputStream(directiveFilename)) {
58             var loaderoptions = new LoaderOptions();
59             TagInspector taginspector = tag -> tag.getClassName().equals(CoordinationDirective.class.getName());
60             loaderoptions.setTagInspector(taginspector);
61
62             var contents = IOUtils.toString(is, StandardCharsets.UTF_8);
63             //
64             // Read the yaml into our Java Object
65             //
66             var yaml = new Yaml(new Constructor(CoordinationDirective.class, loaderoptions));
67             Object obj = yaml.load(contents);
68
69             logger.debug(contents);
70
71             return (CoordinationDirective) obj;
72         } catch (IOException e) {
73             logger.error("Error while loading YAML coordination directive", e);
74         }
75         return null;
76     }
77
78     /**
79      * Generate Xacml rule implementing specified CoordinationDirective.
80      *
81      * @param cd the CoordinationDirective
82      * @param protoDir the directory containing Xacml implementation prototypes
83      * @return the generated Xacml policy
84      */
85     public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
86                                                                 String protoDir) {
87         /*
88          * Determine file names
89          */
90         String xacmlProtoFilename = protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
91         logger.debug("xacmlProtoFilename={}", xacmlProtoFilename);
92         /*
93          * Values to be used for placeholders
94          */
95         final var uniqueId = UUID.randomUUID().toString();
96         final String cLOne = cd.getControlLoop(0);
97         final String cLTwo = cd.getControlLoop(1);
98         /*
99          * Replace prototype placeholders with appropriate values
100          */
101         String xacmlPolicy = null;
102         try (Stream<String> stream = Files.lines(Paths.get(xacmlProtoFilename))) {
103             xacmlPolicy = stream.map(s -> s.replace("UNIQUE_ID", uniqueId))
104                 .map(s -> s.replace("CONTROL_LOOP_ONE", cLOne))
105                 .map(s -> s.replace("CONTROL_LOOP_TWO", cLTwo))
106                 .collect(Collectors.joining(System.lineSeparator()));
107         } catch (IOException e) {
108             logger.error("Error while generating XACML policy for coordination directive", e);
109         }
110         return xacmlPolicy;
111     }
112
113 }