Better error handling for decisions
[policy/xacml-pdp.git] / applications / guard / src / main / java / org / onap / policy / xacml / pdp / application / guard / CoordinationGuardTranslator.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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.guard;
24
25 import com.att.research.xacml.api.Request;
26 import com.att.research.xacml.api.Response;
27 import com.att.research.xacml.util.XACMLPolicyScanner;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.File;
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;
38 import java.util.Map;
39 import java.util.UUID;
40 import java.util.stream.Collectors;
41 import java.util.stream.Stream;
42
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
44
45 import org.apache.commons.io.IOUtils;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardYamlCoder;
48 import org.onap.policy.models.decisions.concepts.DecisionRequest;
49 import org.onap.policy.models.decisions.concepts.DecisionResponse;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
51 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
52 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
53 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
58
59     private static final Logger LOGGER = LoggerFactory.getLogger(CoordinationGuardTranslator.class);
60
61     public CoordinationGuardTranslator() {
62         super();
63     }
64
65     @Override
66     public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
67         LOGGER.debug("Using CoordinationGuardTranslator.convertPolicy");
68         //
69         // Policy name should be at the root
70         //
71         String type = toscaPolicy.getType();
72         String coordinationFunctionPath = "src/main/resources/coordination/function";
73         Map<String, Object> policyProps = toscaPolicy.getProperties();
74         LOGGER.debug("path = {}", coordinationFunctionPath);
75         LOGGER.debug("props = {}", policyProps);
76         @SuppressWarnings("unchecked")
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);
82         //
83         // Generate the xacml policy as a string
84         //
85         String xacmlStr = generateXacmlFromCoordinationDirective(cd, coordinationFunctionPath);
86         LOGGER.debug("xacmlStr\n{}", xacmlStr);
87         //
88         // Scan the string and convert to PoilcyType
89         //
90         try (InputStream is = new ByteArrayInputStream(xacmlStr.getBytes(StandardCharsets.UTF_8))) {
91             return (PolicyType) XACMLPolicyScanner.readPolicy(is);
92         } catch (IOException e) {
93             throw new ToscaPolicyConversionException("Failed to read policy", e);
94         }
95     }
96
97     /**
98      * This function is not used for CLC instead
99      * the one in LegacyGuardTranslator is used.
100      */
101     @Override
102     public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
103         throw new ToscaPolicyConversionException("this convertRequest shouldn't be used");
104     }
105
106     /**
107      * This function is not used for CLC instead
108      * the one in LegacyGuardTranslator is used.
109      */
110     @Override
111     public DecisionResponse convertResponse(Response xacmlResponse) {
112         LOGGER.info("this convertResponse shouldn't be used");
113         return null;
114     }
115
116     /**
117      * Load YAML coordination directive.
118      *
119      * @param directiveFilename yaml directive file to load
120      * @return the CoordinationDirective
121      */
122     public static CoordinationDirective loadCoordinationDirectiveFromFile(
123         String directiveFilename) {
124         try (InputStream is = new FileInputStream(new File(directiveFilename))) {
125             String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
126             //
127             // Read the yaml into our Java Object
128             //
129             CoordinationDirective obj =
130                 new StandardYamlCoder().decode(contents, CoordinationDirective.class);
131             LOGGER.debug(contents);
132             return obj;
133         } catch (IOException | CoderException e) {
134             LOGGER.error("Error while loading YAML coordination directive", e);
135         }
136         return null;
137     }
138
139     /**
140      * Generate Xacml rule implementing specified CoordinationDirective.
141      *
142      * @param cd the CoordinationDirective
143      * @param protoDir the directory containing Xacml implementation prototypes
144      * @return the generated Xacml policy
145      */
146     public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
147         String protoDir) throws ToscaPolicyConversionException {
148         /*
149          * Determine file names
150          */
151         String xacmlProtoFilename =
152             protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
153         LOGGER.debug("xacmlProtoFilename={}", xacmlProtoFilename);
154         /*
155          * Values to be used for placeholders
156          */
157         final String uniqueId = UUID.randomUUID().toString();
158         final String cLOne = cd.getControlLoop(0);
159         final String cLTwo = cd.getControlLoop(1);
160         /*
161          * Replace function placeholders with appropriate values
162          */
163         try (Stream<String> stream = Files.lines(Paths.get(xacmlProtoFilename))) {
164             return stream.map(s -> s.replace("UNIQUE_ID", uniqueId))
165                 .map(s -> s.replace("CONTROL_LOOP_ONE", cLOne))
166                 .map(s -> s.replace("CONTROL_LOOP_TWO", cLTwo))
167                 .collect(Collectors.joining(XacmlPolicyUtils.LINE_SEPARATOR));
168         } catch (IOException e) {
169             throw new ToscaPolicyConversionException(
170                 "Error while generating XACML policy for coordination directive", e);
171         }
172     }
173
174 }