Changes for checkstyle 8.32
[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-2020 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.io.InputStream;
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.Yaml;
37 import org.yaml.snakeyaml.constructor.Constructor;
38
39
40 public final class Util {
41
42     private static final Logger logger = LoggerFactory.getLogger(Util.class);
43
44     private Util() {
45         super();
46     }
47
48     /**
49      * Load YAML coordination directive.
50      *
51      * @param directiveFilename yaml directive file to load
52      * @return the CoordinationDirective
53      */
54     public static CoordinationDirective loadCoordinationDirectiveFromFile(String directiveFilename) {
55         try (InputStream is = new FileInputStream(new File(directiveFilename))) {
56             String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
57             //
58             // Read the yaml into our Java Object
59             //
60             Yaml yaml = new Yaml(new Constructor(CoordinationDirective.class));
61             Object obj = yaml.load(contents);
62
63             logger.debug(contents);
64
65             return (CoordinationDirective) obj;
66         } catch (IOException e) {
67             logger.error("Error while loading YAML coordination directive", e);
68         }
69         return null;
70     }
71
72     /**
73      * Generate Xacml rule implementing specified CoordinationDirective.
74      *
75      * @param cd the CoordinationDirective
76      * @param protoDir the directory containing Xacml implementation prototypes
77      * @return the generated Xacml policy
78      */
79     public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
80                                                                 String protoDir) {
81         /*
82          * Determine file names
83          */
84         String xacmlProtoFilename = protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
85         logger.debug("xacmlProtoFilename={}", xacmlProtoFilename);
86         /*
87          * Values to be used for placeholders
88          */
89         final String uniqueId = UUID.randomUUID().toString();
90         final String cLOne = cd.getControlLoop(0);
91         final String cLTwo = cd.getControlLoop(1);
92         /*
93          * Replace prototype placeholders with appropriate values
94          */
95         String xacmlPolicy = null;
96         try (Stream<String> stream = Files.lines(Paths.get(xacmlProtoFilename))) {
97             xacmlPolicy = stream.map(s -> s.replace("UNIQUE_ID", uniqueId))
98                 .map(s -> s.replace("CONTROL_LOOP_ONE", cLOne))
99                 .map(s -> s.replace("CONTROL_LOOP_TWO", cLTwo))
100                 .collect(Collectors.joining(System.lineSeparator()));
101         } catch (IOException e) {
102             logger.error("Error while generating XACML policy for coordination directive", e);
103         }
104         return xacmlPolicy;
105     }
106
107 }