2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.guard;
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.att.research.xacml.util.XACMLPolicyScanner;
29 import java.io.ByteArrayInputStream;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.util.List;
39 import java.util.Map.Entry;
40 import java.util.UUID;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
46 import org.apache.commons.io.IOUtils;
47 import org.onap.policy.models.decisions.concepts.DecisionRequest;
48 import org.onap.policy.models.decisions.concepts.DecisionResponse;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
51 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.yaml.snakeyaml.Yaml;
55 import org.yaml.snakeyaml.constructor.Constructor;
57 public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
59 private static final Logger LOGGER = LoggerFactory.getLogger(CoordinationGuardTranslator.class);
61 public CoordinationGuardTranslator() {
66 public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
67 LOGGER.debug("Using CoordinationGuardTranslator.convertPolicy");
69 // Policy name should be at the root
71 String policyName = toscaPolicy.getMetadata().get("policy-id");
72 String type = toscaPolicy.getType();
73 String coordinationFunctionPath = "src/main/resources/coordination/function";
74 Map<String, Object> policyProps = toscaPolicy.getProperties();
75 LOGGER.debug("path = {}", coordinationFunctionPath);
76 LOGGER.debug("props = {}", policyProps);
77 List<String> controlLoop = (List<String>) policyProps.get("controlLoop");
78 CoordinationDirective cd = new CoordinationDirective();
79 cd.setCoordinationFunction(type);
80 cd.setControlLoop(controlLoop);
81 LOGGER.debug("CoordinationDirective = {}", cd);
83 String xacmlStr = generateXacmlFromCoordinationDirective(cd, coordinationFunctionPath);
85 LOGGER.debug("xacmlStr\n{}", xacmlStr);
86 PolicyType scannedPolicy = null;
87 try (InputStream is = new ByteArrayInputStream(xacmlStr.getBytes(StandardCharsets.UTF_8))) {
88 scannedPolicy = (PolicyType) XACMLPolicyScanner.readPolicy(is);
89 } catch (IOException e) {
90 LOGGER.error("Failed to read policy", e);
96 public Request convertRequest(DecisionRequest request) {
97 LOGGER.info("this convertRequest shouldn't be used");
102 public DecisionResponse convertResponse(Response xacmlResponse) {
103 LOGGER.info("this convertRequest shouldn't be used");
108 * Load YAML coordination directive.
110 * @param directiveFilename yaml directive file to load
111 * @return the CoordinationDirective
113 public static CoordinationDirective loadCoordinationDirectiveFromFile(String directiveFilename) {
114 try (InputStream is = new FileInputStream(new File(directiveFilename))) {
115 String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
117 // Read the yaml into our Java Object
119 Yaml yaml = new Yaml(new Constructor(CoordinationDirective.class));
120 Object obj = yaml.load(contents);
122 LOGGER.debug(contents);
124 return (CoordinationDirective) obj;
125 } catch (IOException e) {
126 LOGGER.error("Error while loading YAML coordination directive", e);
132 * Generate Xacml rule implementing specified CoordinationDirective.
134 * @param cd the CoordinationDirective
135 * @param protoDir the directory containing Xacml implementation prototypes
136 * @return the generated Xacml policy
138 public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
141 * Determine file names
143 String xacmlProtoFilename = protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
144 LOGGER.debug("xacmlProtoFilename={}", xacmlProtoFilename);
146 * Values to be used for placeholders
148 final String uniqueId = UUID.randomUUID().toString();
149 final String cLOne = cd.getControlLoop(0);
150 final String cLTwo = cd.getControlLoop(1);
152 * Replace prototype placeholders with appropriate values
154 String xacmlPolicy = null;
155 try (Stream<String> stream = Files.lines(Paths.get(xacmlProtoFilename))) {
156 xacmlPolicy = stream.map(s -> s.replaceAll("UNIQUE_ID", uniqueId))
157 .map(s -> s.replaceAll("CONTROL_LOOP_ONE", cLOne))
158 .map(s -> s.replaceAll("CONTROL_LOOP_TWO", cLTwo))
159 .collect(Collectors.joining(System.lineSeparator()));
160 } catch (IOException e) {
161 LOGGER.error("Error while generating XACML policy for coordination directive", e);