2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.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.openecomp.policy.common.logging.eelf.MessageCodes;
39 import org.openecomp.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 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
149 return byteArrayInputStream;
151 } catch (JAXBException e) {
152 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
157 * Helper static class that does the work to write a policy set to an output stream.
161 public static void writePolicyFile(OutputStream os, PolicyType policy) {
162 JAXBElement<PolicyType> policySetElement = new ObjectFactory().createPolicy(policy);
164 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
165 Marshaller m = context.createMarshaller();
166 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
167 m.marshal(policySetElement, os);
168 } catch (JAXBException e) {
169 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPolicyWriter", "writePolicyFile failed");
173 @SuppressWarnings({ "unchecked", "rawtypes" })
174 public static String changeFileNameInXmlWhenRenamePolicy(Path filename) {
176 PolicyType policyType = null;
177 String extension = "";
178 String domain = null;
179 String repository = "repository";
180 if(filename.toString().contains("Config_")){
181 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.toString().length()+1), filename.toString().indexOf("Config_"));
182 }else if(filename.toString().contains("Action_")){
183 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.toString().length()+1), filename.toString().indexOf("Action_"));
184 }else if(filename.toString().contains("Decision_")){
185 domain = filename.toString().substring(filename.toString().indexOf(repository) + (repository.toString().length()+1), filename.toString().indexOf("Decision_"));
187 if(domain.contains(File.separator)){
188 domain = domain.replace(File.separator, ".");
191 JAXBContext context = JAXBContext.newInstance(PolicyType.class);
192 Unmarshaller m = context.createUnmarshaller();
193 JAXBElement<PolicyType> policyElement = (JAXBElement<PolicyType>) m.unmarshal(filename.toFile());
194 policyType = policyElement.getValue();
195 if (policyType != null) {
196 TargetType targetType = policyType.getTarget();
197 List<AnyOfType> anyOfTypes = targetType.getAnyOf();
198 for( Iterator anyOfIte = anyOfTypes.iterator(); anyOfIte.hasNext(); ){
199 AnyOfType anyOfType = (AnyOfType) anyOfIte.next();
200 List<AllOfType> allOf = anyOfType.getAllOf();
201 for( Iterator allOfIte = allOf.iterator(); allOfIte.hasNext(); ){
202 AllOfType allOfType = (AllOfType) allOfIte.next();
203 List<MatchType> match = allOfType.getMatch();
204 for( Iterator matchIte = match.iterator(); matchIte.hasNext();) {
205 MatchType matchType = (MatchType) matchIte.next();
206 if(matchType.getAttributeDesignator().getAttributeId().equals("PolicyName")){
207 AttributeValueType attributeValueType = matchType.getAttributeValue();
208 List<Object> contents = attributeValueType.getContent();
209 if (contents != null && contents.size() > 0) {
210 String value = (String) contents.get(0);
211 String version = value;
212 version = version.substring(0, version.lastIndexOf("."));
213 version = version.substring(version.lastIndexOf("."));
214 if(filename.toString().contains("Config_")){
215 value = value.substring(0, value.indexOf("Config_"));
217 value = value.substring(0, value.indexOf("Decision_"));
219 String tmp = filename.getFileName()+"";
220 String newName = tmp.substring(0, tmp.lastIndexOf("."));
221 attributeValueType.getContent().clear();
222 attributeValueType.getContent().add(domain + newName + "." + "xml");
228 if(filename.toString().contains("Config_") || filename.toString().contains("Action_")){
229 List<Object> objects = policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
230 if (objects != null && objects.size() > 0) {
231 for (Iterator ite = objects.iterator(); ite.hasNext();) {
233 RuleType ruleType = (RuleType ) ite.next();
234 AdviceExpressionsType adviceExpressionsType = ruleType.getAdviceExpressions();
235 if (adviceExpressionsType != null) {
236 List<AdviceExpressionType> adviceExpressionTypes = adviceExpressionsType.getAdviceExpression();
237 if (adviceExpressionTypes != null && adviceExpressionTypes.size() > 0) {
238 for (Iterator iterator = adviceExpressionTypes
239 .iterator(); iterator.hasNext();) {
240 AdviceExpressionType adviceExpressionType = (AdviceExpressionType) iterator
242 if (adviceExpressionType.getAdviceId() != null && !adviceExpressionType.getAdviceId().equals("") && (adviceExpressionType.getAdviceId().equals("configID")
243 || adviceExpressionType.getAdviceId().equals("faultID") || adviceExpressionType.getAdviceId().equals("PMID")||adviceExpressionType.getAdviceId().equals("firewallConfigID")
244 || adviceExpressionType.getAdviceId().equals("MSID")) || adviceExpressionType.getAdviceId().equals("GocID")||adviceExpressionType.getAdviceId().equals("GocHPID")||adviceExpressionType.getAdviceId().equals("BRMSRAWID")
245 ||adviceExpressionType.getAdviceId().equals("BRMSPARAMID")|| adviceExpressionType.getAdviceId().equals("HPSuppID") || adviceExpressionType.getAdviceId().equals("HPFlapID") || adviceExpressionType.getAdviceId().equals("HPOverID"))
247 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = adviceExpressionType.getAttributeAssignmentExpression();
248 if (attributeAssignmentExpressionTypes != null && attributeAssignmentExpressionTypes.size() > 0) {
249 for (Iterator iterator2 = attributeAssignmentExpressionTypes
250 .iterator(); iterator2.hasNext();) {
251 AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
253 if (attributeAssignmentExpressionType.getAttributeId().equals("URLID")) {
254 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
255 AttributeValueType attributeValueType1 = attributeValueType.getValue();
256 String configUrl = "$URL";
257 String urlVal = (String) attributeValueType1.getContent().get(0);
258 String origExtension = urlVal.substring(urlVal.lastIndexOf('.')+1).trim();
259 extension = origExtension;
260 attributeValueType1.getContent().clear();
261 String txtFileName = filename.getFileName().toString();
262 txtFileName = txtFileName.substring(0, txtFileName.lastIndexOf(".")+1) + origExtension;
263 txtFileName = configUrl+ File.separator + "Config" + File.separator + domain + txtFileName;
264 attributeValueType1.getContent().add(txtFileName);
265 } else if (attributeAssignmentExpressionType.getAttributeId().equals("PolicyName")) {
266 JAXBElement<AttributeValueType> attributeValueType = (JAXBElement<AttributeValueType>) attributeAssignmentExpressionType.getExpression();
267 AttributeValueType attributeValueType1 = attributeValueType.getValue();
268 List<Object> contents = attributeValueType1.getContent();
269 if (contents != null && contents.size() > 0) {
270 String value = (String) contents.get(0);
271 String version = value;
272 version = version.substring(0, version.lastIndexOf("."));
273 version = version.substring(version.lastIndexOf("."));
274 value = value.substring(0, value.indexOf("Config_"));
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.size() > 0) {
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.size() > 0) {
298 for (Iterator iterator = obligationExpressionType
299 .iterator(); iterator.hasNext();) {
300 ObligationExpressionType obligationExpressionTypes = (ObligationExpressionType) iterator
302 if (obligationExpressionTypes.getObligationId() != null && !obligationExpressionTypes.getObligationId().equals("")) {
303 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionTypes = obligationExpressionTypes.getAttributeAssignmentExpression();
304 if (attributeAssignmentExpressionTypes != null && attributeAssignmentExpressionTypes.size() > 0) {
305 for (Iterator iterator2 = attributeAssignmentExpressionTypes
306 .iterator(); iterator2.hasNext();) {
307 AttributeAssignmentExpressionType attributeAssignmentExpressionType = (AttributeAssignmentExpressionType) iterator2
309 if (attributeAssignmentExpressionType.getAttributeId().equals("body")) {
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");