2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.coordination;
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;
39 public final class Util {
41 private static final Logger logger = LoggerFactory.getLogger(Util.class);
48 * Load YAML coordination directive.
50 * @param directiveFilename yaml directive file to load
51 * @return the CoordinationDirective
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);
57 // Read the yaml into our Java Object
59 var yaml = new Yaml(new Constructor(CoordinationDirective.class));
60 Object obj = yaml.load(contents);
62 logger.debug(contents);
64 return (CoordinationDirective) obj;
65 } catch (IOException e) {
66 logger.error("Error while loading YAML coordination directive", e);
72 * Generate Xacml rule implementing specified CoordinationDirective.
74 * @param cd the CoordinationDirective
75 * @param protoDir the directory containing Xacml implementation prototypes
76 * @return the generated Xacml policy
78 public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
81 * Determine file names
83 String xacmlProtoFilename = protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
84 logger.debug("xacmlProtoFilename={}", xacmlProtoFilename);
86 * Values to be used for placeholders
88 final var uniqueId = UUID.randomUUID().toString();
89 final String cLOne = cd.getControlLoop(0);
90 final String cLTwo = cd.getControlLoop(1);
92 * Replace prototype placeholders with appropriate values
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);