2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
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.xacml.util;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.util.Iterator;
32 import java.util.List;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBElement;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.Marshaller;
38 import javax.xml.bind.Unmarshaller;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
55 import org.onap.policy.common.logging.eelf.MessageCodes;
56 import org.onap.policy.common.logging.eelf.PolicyLogger;
59 * Helper static class for policy writing.
63 public class XACMLPolicyWriter {
66 * Helper static class that does the work to write a policy set to a file on disk.
70 public static Path writePolicyFile(Path filename, PolicySetType policySet) {
71 JAXBElement<PolicySetType> policySetElement = new ObjectFactory().createPolicySet(policySet);
73 JAXBContext context = JAXBContext.newInstance(PolicySetType.class);
74 Marshaller m = context.createMarshaller();
75 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
76 m.marshal(policySetElement, filename.toFile());
78 if (Files.exists(filename)) {
81 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "File does not exist after marshalling.");
85 } catch (JAXBException e) {
86 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
92 * Helper static class that does the work to write a policy set to an output stream.
96 public static void writePolicyFile(OutputStream os, PolicySetType policySet) {
97 JAXBElement<PolicySetType> policySetElement = new ObjectFactory().createPolicySet(policySet);
99 JAXBContext context = JAXBContext.newInstance(PolicySetType.class);
100 Marshaller m = context.createMarshaller();
101 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
102 m.marshal(policySetElement, os);
103 } catch (JAXBException e) {
104 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
109 * Helper static class that does the work to write a policy to a file on disk.
113 public static Path writePolicyFile(Path filename, PolicyType policy) {
114 JAXBElement<PolicyType> policyElement = new ObjectFactory().createPolicy(policy);
116 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
117 Marshaller m = context.createMarshaller();
118 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
119 m.marshal(policyElement, filename.toFile());
121 if (Files.exists(filename)) {
124 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "File does not exist after marshalling.");
128 } catch (JAXBException e) {
129 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
135 * Helper static class that does the work to write a policy to a file on disk.
139 public static InputStream getXmlAsInputStream(Object policy) {
140 JAXBElement<?> policyElement;
141 if (policy instanceof PolicyType) {
142 policyElement = new ObjectFactory().createPolicy((PolicyType) policy);
143 return getByteArrayInputStream(policyElement, PolicyType.class);
144 } else if (policy instanceof PolicySetType) {
145 policyElement = new ObjectFactory().createPolicySet((PolicySetType) policy);
146 return getByteArrayInputStream(policyElement, PolicySetType.class);
152 * Helper static class that reads the JAXB element and return policy input stream.
154 * @param policyElement
155 * @param className (PolicyType or PolicySetType ?).
156 * @return ByteArrayInputStream.
158 public static InputStream getByteArrayInputStream(JAXBElement<?> policyElement, Class<?> className) {
160 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
161 JAXBContext context = JAXBContext.newInstance(className);
162 Marshaller m = context.createMarshaller();
163 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
164 m.marshal(policyElement, byteArrayOutputStream);
165 return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
166 } catch (JAXBException e) {
167 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
168 throw new IllegalArgumentException("XACMLPolicyWriter writePolicyFile failed", e);
173 * Helper static class that does the work to write a policy set.
177 public static InputStream getPolicySetXmlAsInputStream(PolicySetType policy) {
178 JAXBElement<PolicySetType> policyElement = new ObjectFactory().createPolicySet(policy);
180 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
181 JAXBContext context = JAXBContext.newInstance(PolicySetType.class);
182 Marshaller m = context.createMarshaller();
183 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
184 m.marshal(policyElement, byteArrayOutputStream);
185 return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
186 } catch (JAXBException e) {
187 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
188 throw new IllegalArgumentException("XACMLPolicyWriter writePolicyFile failed", e);
193 * Helper static class that does the work to write a policy set to an output stream.
197 public static void writePolicyFile(OutputStream os, PolicyType policy) {
198 JAXBElement<PolicyType> policySetElement = new ObjectFactory().createPolicy(policy);
200 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
201 Marshaller m = context.createMarshaller();
202 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
203 m.marshal(policySetElement, os);
204 } catch (JAXBException e) {
205 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
209 @SuppressWarnings({"unchecked", "rawtypes"})
210 public static String changeFileNameInXmlWhenRenamePolicy(Path filename) {
212 String extension = "";
213 String domain = null;
214 String repository = "repository";
215 if (filename.toString().contains("Config_")) {
216 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length() + 1),
217 filename.toString().indexOf("Config_"));
218 } else if (filename.toString().contains("Action_")) {
219 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length() + 1),
220 filename.toString().indexOf("Action_"));
221 } else if (filename.toString().contains("Decision_")) {
222 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length() + 1),
223 filename.toString().indexOf("Decision_"));
225 if (domain.contains(File.separator)) {
226 domain = domain.replace(File.separator, ".");
229 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
230 Unmarshaller m = context.createUnmarshaller();
231 JAXBElement<PolicyType> policyElement = (JAXBElement<PolicyType>) m.unmarshal(filename.toFile());
232 PolicyType policyType = policyElement.getValue();
233 if (policyType != null) {
234 TargetType targetType = policyType.getTarget();
235 List<AnyOfType> anyOfTypes = targetType.getAnyOf();
236 for (Iterator anyOfIte = anyOfTypes.iterator(); anyOfIte.hasNext();) {
237 AnyOfType anyOfType = (AnyOfType) anyOfIte.next();
238 List<AllOfType> allOf = anyOfType.getAllOf();
239 for (Iterator allOfIte = allOf.iterator(); allOfIte.hasNext();) {
240 AllOfType allOfType = (AllOfType) allOfIte.next();
241 List<MatchType> match = allOfType.getMatch();
242 for (Iterator matchIte = match.iterator(); matchIte.hasNext();) {
243 MatchType matchType = (MatchType) matchIte.next();
244 if ("PolicyName".equals(matchType.getAttributeDesignator().getAttributeId())) {
245 AttributeValueType attributeValueType = matchType.getAttributeValue();
246 List<Object> contents = attributeValueType.getContent();
247 if (contents != null && !contents.isEmpty()) {
248 String tmp = filename.getFileName() + "";
249 String newName = tmp.substring(0, tmp.lastIndexOf("."));
250 attributeValueType.getContent().clear();
251 attributeValueType.getContent().add(domain + newName + "." + "xml");
257 if (filename.toString().contains("Config_") || filename.toString().contains("Action_")) {
258 List<Object> objects =
259 policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
260 if (objects != null && !objects.isEmpty()) {
261 for (Iterator ite = objects.iterator(); ite.hasNext();) {
263 RuleType ruleType = (RuleType) ite.next();
264 AdviceExpressionsType adviceExpressionsType = ruleType.getAdviceExpressions();
265 if (adviceExpressionsType != null) {
266 List<AdviceExpressionType> adviceExpressionTypes =
267 adviceExpressionsType.getAdviceExpression();
268 if (adviceExpressionTypes != null && !adviceExpressionTypes.isEmpty()) {
269 for (Iterator iterator = adviceExpressionTypes.iterator(); iterator.hasNext();) {
270 AdviceExpressionType adviceExpressionType =
271 (AdviceExpressionType) iterator.next();
272 if (adviceExpressionType.getAdviceId() != null
273 && !"".equals(adviceExpressionType.getAdviceId())
274 && ("configID".equals(adviceExpressionType.getAdviceId())
275 || "faultID".equals(adviceExpressionType.getAdviceId())
276 || "PMID".equals(adviceExpressionType.getAdviceId())
277 || "firewallConfigID".equals(adviceExpressionType.getAdviceId())
278 || "OptimizationID".equals(adviceExpressionType.getAdviceId())
279 || "MSID".equals(adviceExpressionType.getAdviceId()))
280 || "GocID".equals(adviceExpressionType.getAdviceId())
281 || "GocHPID".equals(adviceExpressionType.getAdviceId())
282 || "BRMSRAWID".equals(adviceExpressionType.getAdviceId())
283 || "BRMSPARAMID".equals(adviceExpressionType.getAdviceId())
284 || "HPSuppID".equals(adviceExpressionType.getAdviceId())
285 || "HPFlapID".equals(adviceExpressionType.getAdviceId())
286 || "HPOverID".equals(adviceExpressionType.getAdviceId())) {
287 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes =
288 adviceExpressionType.getAttributeAssignmentExpression();
289 if (attributeAssignmentExpressionTypes != null
290 && !attributeAssignmentExpressionTypes.isEmpty()) {
291 for (Iterator iterator2 =
292 attributeAssignmentExpressionTypes.iterator(); iterator2
294 AttributeAssignmentExpressionType attributeAssignmentExpressionType =
295 (AttributeAssignmentExpressionType) iterator2.next();
297 attributeAssignmentExpressionType.getAttributeId())) {
298 JAXBElement<AttributeValueType> attributeValueType =
299 (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType
301 AttributeValueType attributeValueType1 =
302 attributeValueType.getValue();
303 String configUrl = "$URL";
305 (String) attributeValueType1.getContent().get(0);
306 String origExtension =
307 urlVal.substring(urlVal.lastIndexOf('.') + 1).trim();
308 extension = origExtension;
309 attributeValueType1.getContent().clear();
310 String txtFileName = filename.getFileName().toString();
311 txtFileName = txtFileName.substring(0,
312 txtFileName.lastIndexOf(".") + 1) + origExtension;
313 txtFileName = configUrl + File.separator + "Config"
314 + File.separator + domain + txtFileName;
315 attributeValueType1.getContent().add(txtFileName);
316 } else if ("PolicyName".equals(
317 attributeAssignmentExpressionType.getAttributeId())) {
318 JAXBElement<AttributeValueType> attributeValueType =
319 (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType
321 AttributeValueType attributeValueType1 =
322 attributeValueType.getValue();
323 List<Object> contents = attributeValueType1.getContent();
324 if (contents != null && !contents.isEmpty()) {
325 String tmp = filename.getFileName() + "";
326 String newName = tmp.substring(0, tmp.lastIndexOf("."));
327 attributeValueType1.getContent().clear();
328 attributeValueType1.getContent()
329 .add(domain + newName + "." + "xml");
341 if (objects != null && !objects.isEmpty()) {
342 for (Iterator ite1 = objects.iterator(); ite1.hasNext();) {
344 RuleType ruleType1 = (RuleType) ite1.next();
345 ObligationExpressionsType obligationExpressionsType =
346 ruleType1.getObligationExpressions();
347 if (obligationExpressionsType != null) {
348 List<ObligationExpressionType> obligationExpressionType =
349 obligationExpressionsType.getObligationExpression();
350 if (obligationExpressionType != null && !obligationExpressionType.isEmpty()) {
351 for (Iterator iterator = obligationExpressionType.iterator(); iterator
353 ObligationExpressionType obligationExpressionTypes =
354 (ObligationExpressionType) iterator.next();
355 if (obligationExpressionTypes.getObligationId() != null
356 && !"".equals(obligationExpressionTypes.getObligationId())) {
357 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes =
358 obligationExpressionTypes.getAttributeAssignmentExpression();
359 if (attributeAssignmentExpressionTypes != null
360 && !attributeAssignmentExpressionTypes.isEmpty()) {
361 for (Iterator iterator2 =
362 attributeAssignmentExpressionTypes.iterator(); iterator2
364 AttributeAssignmentExpressionType attributeAssignmentExpressionType =
365 (AttributeAssignmentExpressionType) iterator2.next();
367 attributeAssignmentExpressionType.getAttributeId())) {
368 JAXBElement<AttributeValueType> attributeValueType =
369 (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType
371 AttributeValueType attributeValueType1 =
372 attributeValueType.getValue();
373 String configUrl = "$URL";
375 (String) attributeValueType1.getContent().get(0);
376 String origExtension = urlVal
377 .substring(urlVal.lastIndexOf('.') + 1).trim();
379 attributeValueType1.getContent().clear();
380 String txtFileName = filename.getFileName().toString();
381 txtFileName = txtFileName.substring(0,
382 txtFileName.lastIndexOf(".") + 1) + origExtension;
383 txtFileName = configUrl + File.separator + "Action"
384 + File.separator + domain + txtFileName;
385 attributeValueType1.getContent().add(txtFileName);
400 writePolicyFile(filename, policyType);
402 } catch (JAXBException e) {
403 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");