2 * ============LICENSE_START=======================================================
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
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.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;
40 public final class Util {
42 private static final Logger logger = LoggerFactory.getLogger(Util.class);
49 * Load YAML coordination directive.
51 * @param directiveFilename yaml directive file to load
52 * @return the CoordinationDirective
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);
58 // Read the yaml into our Java Object
60 Yaml yaml = new Yaml(new Constructor(CoordinationDirective.class));
61 Object obj = yaml.load(contents);
63 logger.debug(contents);
65 return (CoordinationDirective) obj;
66 } catch (IOException e) {
67 logger.error("Error while loading YAML coordination directive", e);
73 * Generate Xacml rule implementing specified CoordinationDirective.
75 * @param cd the CoordinationDirective
76 * @param protoDir the directory containing Xacml implementation prototypes
77 * @return the generated Xacml policy
79 public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
82 * Determine file names
84 String xacmlProtoFilename = protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
85 logger.debug("xacmlProtoFilename={}", xacmlProtoFilename);
87 * Values to be used for placeholders
89 final String uniqueId = UUID.randomUUID().toString();
90 final String cLOne = cd.getControlLoop(0);
91 final String cLTwo = cd.getControlLoop(1);
93 * Replace prototype placeholders with appropriate values
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);