2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.xacml.util;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.Iterator;
30 import java.util.List;
32 import javax.xml.bind.JAXBContext;
33 import javax.xml.bind.JAXBElement;
34 import javax.xml.bind.JAXBException;
35 import javax.xml.bind.Marshaller;
36 import javax.xml.bind.Unmarshaller;
38 import org.onap.policy.common.logging.eelf.MessageCodes;
39 import org.onap.policy.common.logging.eelf.PolicyLogger;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
58 * Helper static class for policy writing.
62 public class XACMLPolicyWriter {
65 * Helper static class that does the work to write a policy set to a file on disk.
69 public static Path writePolicyFile(Path filename, PolicySetType policySet) {
70 JAXBElement<PolicySetType> policySetElement = new ObjectFactory().createPolicySet(policySet);
72 JAXBContext context = JAXBContext.newInstance(PolicySetType.class);
73 Marshaller m = context.createMarshaller();
74 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
75 m.marshal(policySetElement, filename.toFile());
77 if (Files.exists(filename)) {
80 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "File does not exist after marshalling.");
84 } catch (JAXBException e) {
85 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
91 * Helper static class that does the work to write a policy set to an output stream.
95 public static void writePolicyFile(OutputStream os, PolicySetType policySet) {
96 JAXBElement<PolicySetType> policySetElement = new ObjectFactory().createPolicySet(policySet);
98 JAXBContext context = JAXBContext.newInstance(PolicySetType.class);
99 Marshaller m = context.createMarshaller();
100 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
101 m.marshal(policySetElement, os);
102 } catch (JAXBException e) {
103 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
108 * Helper static class that does the work to write a policy to a file on disk.
112 public static Path writePolicyFile(Path filename, PolicyType policy) {
113 JAXBElement<PolicyType> policyElement = new ObjectFactory().createPolicy(policy);
115 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
116 Marshaller m = context.createMarshaller();
117 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
118 m.marshal(policyElement, filename.toFile());
120 if (Files.exists(filename)) {
123 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "File does not exist after marshalling.");
127 } catch (JAXBException e) {
128 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(PolicyType policy) {
140 JAXBElement<PolicyType> policyElement = new ObjectFactory().createPolicy(policy);
142 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
143 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
144 Marshaller m = context.createMarshaller();
145 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
146 m.marshal(policyElement, byteArrayOutputStream);
147 return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
148 } catch (JAXBException e) {
149 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
154 * Helper static class that does the work to write a policy set to an output stream.
158 public static void writePolicyFile(OutputStream os, PolicyType policy) {
159 JAXBElement<PolicyType> policySetElement = new ObjectFactory().createPolicy(policy);
161 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
162 Marshaller m = context.createMarshaller();
163 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
164 m.marshal(policySetElement, os);
165 } catch (JAXBException e) {
166 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
170 @SuppressWarnings({ "unchecked", "rawtypes" })
171 public static String changeFileNameInXmlWhenRenamePolicy(Path filename) {
173 String extension = "";
174 String domain = null;
175 String repository = "repository";
176 if(filename.toString().contains("Config_")){
177 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Config_"));
178 }else if(filename.toString().contains("Action_")){
179 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Action_"));
180 }else if(filename.toString().contains("Decision_")){
181 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.length()+1), filename.toString().indexOf("Decision_"));
183 if(domain.contains(File.separator)){
184 domain = domain.replace(File.separator, ".");
187 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
188 Unmarshaller m = context.createUnmarshaller();
189 JAXBElement<PolicyType> policyElement = (JAXBElement<PolicyType>) m.unmarshal(filename.toFile());
190 PolicyType policyType = policyElement.getValue();
191 if (policyType != null) {
192 TargetType targetType = policyType.getTarget();
193 List<AnyOfType> anyOfTypes = targetType.getAnyOf();
194 for( Iterator anyOfIte = anyOfTypes.iterator(); anyOfIte.hasNext(); ){
195 AnyOfType anyOfType = (AnyOfType) anyOfIte.next();
196 List<AllOfType> allOf = anyOfType.getAllOf();
197 for( Iterator allOfIte = allOf.iterator(); allOfIte.hasNext(); ){
198 AllOfType allOfType = (AllOfType) allOfIte.next();
199 List<MatchType> match = allOfType.getMatch();
200 for( Iterator matchIte = match.iterator(); matchIte.hasNext();) {
201 MatchType matchType = (MatchType) matchIte.next();
202 if("PolicyName".equals(matchType.getAttributeDesignator().getAttributeId())){
203 AttributeValueType attributeValueType = matchType.getAttributeValue();
204 List<Object> contents = attributeValueType.getContent();
205 if (contents != null && !contents.isEmpty()) {
206 String tmp = filename.getFileName()+"";
207 String newName = tmp.substring(0, tmp.lastIndexOf("."));
208 attributeValueType.getContent().clear();
209 attributeValueType.getContent().add(domain + newName + "." + "xml");
215 if(filename.toString().contains("Config_") || filename.toString().contains("Action_")){
216 List<Object> objects = policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
217 if (objects != null && !objects.isEmpty()) {
218 for (Iterator ite = objects.iterator(); ite.hasNext();) {
220 RuleType ruleType = (RuleType ) ite.next();
221 AdviceExpressionsType adviceExpressionsType = ruleType.getAdviceExpressions();
222 if (adviceExpressionsType != null) {
223 List<AdviceExpressionType> adviceExpressionTypes = adviceExpressionsType.getAdviceExpression();
224 if (adviceExpressionTypes != null && !adviceExpressionTypes.isEmpty()) {
225 for (Iterator iterator = adviceExpressionTypes
226 .iterator(); iterator.hasNext();) {
227 AdviceExpressionType adviceExpressionType = (AdviceExpressionType) iterator
229 if (adviceExpressionType.getAdviceId() != null && !"".equals(adviceExpressionType.getAdviceId()) && ("configID".equals(adviceExpressionType.getAdviceId())
230 || "faultID".equals(adviceExpressionType.getAdviceId()) || "PMID".equals(adviceExpressionType.getAdviceId())||"firewallConfigID".equals(adviceExpressionType.getAdviceId())
231 || "MSID".equals(adviceExpressionType.getAdviceId())) || "GocID".equals(adviceExpressionType.getAdviceId())||"GocHPID".equals(adviceExpressionType.getAdviceId())||"BRMSRAWID".equals(adviceExpressionType.getAdviceId())
232 || "BRMSPARAMID".equals(adviceExpressionType.getAdviceId())|| "HPSuppID".equals(adviceExpressionType.getAdviceId()) || "HPFlapID".equals(adviceExpressionType.getAdviceId()) || "HPOverID".equals(adviceExpressionType.getAdviceId()))
234 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = adviceExpressionType.getAttributeAssignmentExpression();
235 if (attributeAssignmentExpressionTypes != null && !attributeAssignmentExpressionTypes.isEmpty()) {
236 for (Iterator iterator2 = attributeAssignmentExpressionTypes
237 .iterator(); iterator2.hasNext();) {
238 AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
240 if ("URLID".equals(attributeAssignmentExpressionType.getAttributeId())) {
241 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
242 AttributeValueType attributeValueType1 = attributeValueType.getValue();
243 String configUrl = "$URL";
244 String urlVal = (String) attributeValueType1.getContent().get(0);
245 String origExtension = urlVal.substring(urlVal.lastIndexOf('.')+1).trim();
246 extension = origExtension;
247 attributeValueType1.getContent().clear();
248 String txtFileName = filename.getFileName().toString();
249 txtFileName = txtFileName.substring(0, txtFileName.lastIndexOf(".")+1) + origExtension;
250 txtFileName = configUrl+ File.separator + "Config" + File.separator + domain + txtFileName;
251 attributeValueType1.getContent().add(txtFileName);
252 } else if ("PolicyName".equals(attributeAssignmentExpressionType.getAttributeId())) {
253 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
254 AttributeValueType attributeValueType1 = attributeValueType.getValue();
255 List<Object> contents = attributeValueType1.getContent();
256 if (contents != null && !contents.isEmpty()) {
257 String tmp = filename.getFileName()+"";
258 String newName = tmp.substring(0, tmp.lastIndexOf("."));
259 attributeValueType1.getContent().clear();
260 attributeValueType1.getContent().add(domain + newName + "." + "xml");
272 if (objects != null && !objects.isEmpty()) {
273 for (Iterator ite1 = objects.iterator(); ite1.hasNext();) {
275 RuleType ruleType1 = (RuleType ) ite1.next();
276 ObligationExpressionsType obligationExpressionsType = ruleType1.getObligationExpressions();
277 if (obligationExpressionsType != null) {
278 List<ObligationExpressionType> obligationExpressionType = obligationExpressionsType.getObligationExpression();
279 if (obligationExpressionType != null && !obligationExpressionType.isEmpty()) {
280 for (Iterator iterator = obligationExpressionType
281 .iterator(); iterator.hasNext();) {
282 ObligationExpressionType obligationExpressionTypes = (ObligationExpressionType) iterator
284 if (obligationExpressionTypes.getObligationId() != null && !"".equals(obligationExpressionTypes.getObligationId())) {
285 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = obligationExpressionTypes.getAttributeAssignmentExpression();
286 if (attributeAssignmentExpressionTypes != null && !attributeAssignmentExpressionTypes.isEmpty()) {
287 for (Iterator iterator2 = attributeAssignmentExpressionTypes
288 .iterator(); iterator2.hasNext();) {
289 AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
291 if ("body".equals(attributeAssignmentExpressionType.getAttributeId())) {
292 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
293 AttributeValueType attributeValueType1 = attributeValueType.getValue();
294 String configUrl = "$URL";
295 String urlVal = (String) attributeValueType1.getContent().get(0);
296 String origExtension = urlVal.substring(urlVal.lastIndexOf('.')+1).trim();
298 attributeValueType1.getContent().clear();
299 String txtFileName = filename.getFileName().toString();
300 txtFileName = txtFileName.substring(0, txtFileName.lastIndexOf(".")+1) + origExtension;
301 txtFileName = configUrl+ File.separator + "Action" + File.separator + domain + txtFileName;
302 attributeValueType1.getContent().add(txtFileName);
317 writePolicyFile(filename, policyType);
319 }catch (JAXBException e) {
320 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");