2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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=========================================================
21 package org.onap.policy.xacml.util;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.util.Iterator;
31 import java.util.List;
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.JAXBElement;
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.Marshaller;
37 import javax.xml.bind.Unmarshaller;
39 import org.onap.policy.common.logging.eelf.MessageCodes;
40 import org.onap.policy.common.logging.eelf.PolicyLogger;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
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");
136 * Helper static class that does the work to write a policy to a file on disk.
140 public static InputStream getXmlAsInputStream(Object policy) {
141 JAXBElement<?> policyElement;
142 if (policy instanceof PolicyType) {
143 policyElement = new ObjectFactory().createPolicy((PolicyType) policy);
144 return getByteArrayInputStream(policyElement, PolicyType.class);
145 } else if (policy instanceof PolicySetType) {
146 policyElement = new ObjectFactory().createPolicySet((PolicySetType) policy);
147 return getByteArrayInputStream(policyElement, PolicySetType.class);
153 * 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);
172 * Helper static class that does the work to write a policy set to an output stream.
176 public static void writePolicyFile(OutputStream os, PolicyType policy) {
177 JAXBElement<PolicyType> policySetElement = new ObjectFactory().createPolicy(policy);
179 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
180 Marshaller m = context.createMarshaller();
181 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
182 m.marshal(policySetElement, os);
183 } catch (JAXBException e) {
184 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
188 @SuppressWarnings({ "unchecked", "rawtypes" })
189 public static String changeFileNameInXmlWhenRenamePolicy(Path filename) {
191 String extension = "";
192 String domain = null;
193 String repository = "repository";
194 if(filename.toString().contains("Config_")){
195 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Config_"));
196 }else if(filename.toString().contains("Action_")){
197 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Action_"));
198 }else if(filename.toString().contains("Decision_")){
199 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Decision_"));
201 if(domain.contains(File.separator)){
202 domain = domain.replace(File.separator, ".");
205 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
206 Unmarshaller m = context.createUnmarshaller();
207 JAXBElement<PolicyType> policyElement = (JAXBElement<PolicyType>) m.unmarshal(filename.toFile());
208 PolicyType policyType = policyElement.getValue();
209 if (policyType != null) {
210 TargetType targetType = policyType.getTarget();
211 List<AnyOfType> anyOfTypes = targetType.getAnyOf();
212 for( Iterator anyOfIte = anyOfTypes.iterator(); anyOfIte.hasNext(); ){
213 AnyOfType anyOfType = (AnyOfType) anyOfIte.next();
214 List<AllOfType> allOf = anyOfType.getAllOf();
215 for( Iterator allOfIte = allOf.iterator(); allOfIte.hasNext(); ){
216 AllOfType allOfType = (AllOfType) allOfIte.next();
217 List<MatchType> match = allOfType.getMatch();
218 for( Iterator matchIte = match.iterator(); matchIte.hasNext();) {
219 MatchType matchType = (MatchType) matchIte.next();
220 if("PolicyName".equals(matchType.getAttributeDesignator().getAttributeId())){
221 AttributeValueType attributeValueType = matchType.getAttributeValue();
222 List<Object> contents = attributeValueType.getContent();
223 if (contents != null && !contents.isEmpty()) {
224 String tmp = filename.getFileName()+"";
225 String newName = tmp.substring(0, tmp.lastIndexOf("."));
226 attributeValueType.getContent().clear();
227 attributeValueType.getContent().add(domain + newName + "." + "xml");
233 if(filename.toString().contains("Config_") || filename.toString().contains("Action_")){
234 List<Object> objects = policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
235 if (objects != null && !objects.isEmpty()) {
236 for (Iterator ite = objects.iterator(); ite.hasNext();) {
238 RuleType ruleType = (RuleType ) ite.next();
239 AdviceExpressionsType adviceExpressionsType = ruleType.getAdviceExpressions();
240 if (adviceExpressionsType != null) {
241 List<AdviceExpressionType> adviceExpressionTypes = adviceExpressionsType.getAdviceExpression();
242 if (adviceExpressionTypes != null && !adviceExpressionTypes.isEmpty()) {
243 for (Iterator iterator = adviceExpressionTypes
244 .iterator(); iterator.hasNext();) {
245 AdviceExpressionType adviceExpressionType = (AdviceExpressionType) iterator
247 if (adviceExpressionType.getAdviceId() != null && !"".equals(adviceExpressionType.getAdviceId()) && ("configID".equals(adviceExpressionType.getAdviceId())
248 || "faultID".equals(adviceExpressionType.getAdviceId()) || "PMID".equals(adviceExpressionType.getAdviceId())||"firewallConfigID".equals(adviceExpressionType.getAdviceId()) || "OptimizationID".equals(adviceExpressionType.getAdviceId())
249 || "MSID".equals(adviceExpressionType.getAdviceId())) || "GocID".equals(adviceExpressionType.getAdviceId())||"GocHPID".equals(adviceExpressionType.getAdviceId())||"BRMSRAWID".equals(adviceExpressionType.getAdviceId())
250 || "BRMSPARAMID".equals(adviceExpressionType.getAdviceId())|| "HPSuppID".equals(adviceExpressionType.getAdviceId()) || "HPFlapID".equals(adviceExpressionType.getAdviceId()) || "HPOverID".equals(adviceExpressionType.getAdviceId()))
252 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = adviceExpressionType.getAttributeAssignmentExpression();
253 if (attributeAssignmentExpressionTypes != null && !attributeAssignmentExpressionTypes.isEmpty()) {
254 for (Iterator iterator2 = attributeAssignmentExpressionTypes
255 .iterator(); iterator2.hasNext();) {
256 AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
258 if ("URLID".equals(attributeAssignmentExpressionType.getAttributeId())) {
259 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
260 AttributeValueType attributeValueType1 = attributeValueType.getValue();
261 String configUrl = "$URL";
262 String urlVal = (String) attributeValueType1.getContent().get(0);
263 String origExtension = urlVal.substring(urlVal.lastIndexOf('.')+1).trim();
264 extension = origExtension;
265 attributeValueType1.getContent().clear();
266 String txtFileName = filename.getFileName().toString();
267 txtFileName = txtFileName.substring(0, txtFileName.lastIndexOf(".")+1) + origExtension;
268 txtFileName = configUrl+ File.separator + "Config" + File.separator + domain + txtFileName;
269 attributeValueType1.getContent().add(txtFileName);
270 } else if ("PolicyName".equals(attributeAssignmentExpressionType.getAttributeId())) {
271 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
272 AttributeValueType attributeValueType1 = attributeValueType.getValue();
273 List<Object> contents = attributeValueType1.getContent();
274 if (contents != null && !contents.isEmpty()) {
275 String tmp = filename.getFileName()+"";
276 String newName = tmp.substring(0, tmp.lastIndexOf("."));
277 attributeValueType1.getContent().clear();
278 attributeValueType1.getContent().add(domain + newName + "." + "xml");
290 if (objects != null && !objects.isEmpty()) {
291 for (Iterator ite1 = objects.iterator(); ite1.hasNext();) {
293 RuleType ruleType1 = (RuleType ) ite1.next();
294 ObligationExpressionsType obligationExpressionsType = ruleType1.getObligationExpressions();
295 if (obligationExpressionsType != null) {
296 List<ObligationExpressionType> obligationExpressionType = obligationExpressionsType.getObligationExpression();
297 if (obligationExpressionType != null && !obligationExpressionType.isEmpty()) {
298 for (Iterator iterator = obligationExpressionType
299 .iterator(); iterator.hasNext();) {
300 ObligationExpressionType obligationExpressionTypes = (ObligationExpressionType) iterator
302 if (obligationExpressionTypes.getObligationId() != null && !"".equals(obligationExpressionTypes.getObligationId())) {
303 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = obligationExpressionTypes.getAttributeAssignmentExpression();
304 if (attributeAssignmentExpressionTypes != null && !attributeAssignmentExpressionTypes.isEmpty()) {
305 for (Iterator iterator2 = attributeAssignmentExpressionTypes
306 .iterator(); iterator2.hasNext();) {
307 AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
309 if ("body".equals(attributeAssignmentExpressionType.getAttributeId())) {
310 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
311 AttributeValueType attributeValueType1 = attributeValueType.getValue();
312 String configUrl = "$URL";
313 String urlVal = (String) attributeValueType1.getContent().get(0);
314 String origExtension = urlVal.substring(urlVal.lastIndexOf('.')+1).trim();
316 attributeValueType1.getContent().clear();
317 String txtFileName = filename.getFileName().toString();
318 txtFileName = txtFileName.substring(0, txtFileName.lastIndexOf(".")+1) + origExtension;
319 txtFileName = configUrl+ File.separator + "Action" + File.separator + domain + txtFileName;
320 attributeValueType1.getContent().add(txtFileName);
335 writePolicyFile(filename, policyType);
337 }catch (JAXBException e) {
338 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");