2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.coordination;
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;
42 public final class Util {
44 private static final Logger logger = LoggerFactory.getLogger(Util.class);
51 * Load YAML coordination directive.
53 * @param directiveFilename yaml directive file to load
54 * @return the CoordinationDirective
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);
62 var contents = IOUtils.toString(is, StandardCharsets.UTF_8);
64 // Read the yaml into our Java Object
66 var yaml = new Yaml(new Constructor(CoordinationDirective.class, loaderoptions));
67 Object obj = yaml.load(contents);
69 logger.debug(contents);
71 return (CoordinationDirective) obj;
72 } catch (IOException e) {
73 logger.error("Error while loading YAML coordination directive", e);
79 * Generate Xacml rule implementing specified CoordinationDirective.
81 * @param cd the CoordinationDirective
82 * @param protoDir the directory containing Xacml implementation prototypes
83 * @return the generated Xacml policy
85 public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
88 * Determine file names
90 String xacmlProtoFilename = protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
91 logger.debug("xacmlProtoFilename={}", xacmlProtoFilename);
93 * Values to be used for placeholders
95 final var uniqueId = UUID.randomUUID().toString();
96 final String cLOne = cd.getControlLoop(0);
97 final String cLTwo = cd.getControlLoop(1);
99 * Replace prototype placeholders with appropriate values
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);