Merge "Refactor policy/drools-applications csit tests"
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.coordination;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.UUID;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32 import org.apache.commons.io.IOUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.yaml.snakeyaml.Yaml;
36 import org.yaml.snakeyaml.constructor.Constructor;
37
38
39 public final class Util {
40
41     private static final Logger logger = LoggerFactory.getLogger(Util.class);
42
43     private Util() {
44         super();
45     }
46
47     /**
48      * Load YAML coordination directive.
49      *
50      * @param directiveFilename yaml directive file to load
51      * @return the CoordinationDirective
52      */
53     public static CoordinationDirective loadCoordinationDirectiveFromFile(String directiveFilename) {
54         try (var is = new FileInputStream(new File(directiveFilename))) {
55             var contents = IOUtils.toString(is, StandardCharsets.UTF_8);
56             //
57             // Read the yaml into our Java Object
58             //
59             var yaml = new Yaml(new Constructor(CoordinationDirective.class));
60             Object obj = yaml.load(contents);
61
62             logger.debug(contents);
63
64             return (CoordinationDirective) obj;
65         } catch (IOException e) {
66             logger.error("Error while loading YAML coordination directive", e);
67         }
68         return null;
69     }
70
71     /**
72      * Generate Xacml rule implementing specified CoordinationDirective.
73      *
74      * @param cd the CoordinationDirective
75      * @param protoDir the directory containing Xacml implementation prototypes
76      * @return the generated Xacml policy
77      */
78     public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
79                                                                 String protoDir) {
80         /*
81          * Determine file names
82          */
83         String xacmlProtoFilename = protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
84         logger.debug("xacmlProtoFilename={}", xacmlProtoFilename);
85         /*
86          * Values to be used for placeholders
87          */
88         final var uniqueId = UUID.randomUUID().toString();
89         final String cLOne = cd.getControlLoop(0);
90         final String cLTwo = cd.getControlLoop(1);
91         /*
92          * Replace prototype placeholders with appropriate values
93          */
94         String xacmlPolicy = null;
95         try (Stream<String> stream = Files.lines(Paths.get(xacmlProtoFilename))) {
96             xacmlPolicy = stream.map(s -> s.replace("UNIQUE_ID", uniqueId))
97                 .map(s -> s.replace("CONTROL_LOOP_ONE", cLOne))
98                 .map(s -> s.replace("CONTROL_LOOP_TWO", cLTwo))
99                 .collect(Collectors.joining(System.lineSeparator()));
100         } catch (IOException e) {
101             logger.error("Error while generating XACML policy for coordination directive", e);
102         }
103         return xacmlPolicy;
104     }
105
106 }