2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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=========================================================
21 package org.onap.policy.pap.xacml.rest;
23 import com.att.research.xacml.api.pap.PAPException;
24 import com.att.research.xacml.util.XACMLProperties;
25 import com.google.common.base.Joiner;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.nio.file.Paths;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.Enumeration;
33 import java.util.List;
34 import java.util.Properties;
35 import java.util.TreeSet;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.GroupEntity;
41 import org.onap.policy.rest.jpa.PolicyEntity;
42 import org.onap.policy.xacml.api.pap.OnapPDP;
43 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
44 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
49 public class DataToNotifyPdp {
51 private static final Logger LOGGER = FlexLogger.getLogger(DataToNotifyPdp.class);
52 private static CommonClassDao commonClassDao;
53 private Properties policyLocations = null;
54 private static Properties pipProperties = null;
57 public DataToNotifyPdp(CommonClassDao commonClassDao) {
58 DataToNotifyPdp.commonClassDao = commonClassDao;
61 public DataToNotifyPdp() {
62 // default constructor.
65 private static Properties readPipProperties() throws IOException {
66 if (pipProperties == null) {
67 try (FileInputStream inputStream = new FileInputStream(Paths.get("pip.properties").toString())) {
68 pipProperties = new Properties();
69 pipProperties.load(inputStream);
76 * Sets the policy config properties.
79 * @param papEngine the pap engine
82 public List<Properties> setPolicyConfigProperties(OnapPDP pdp, PAPPolicyEngine papEngine) {
83 OnapPDPGroup group = null;
85 group = papEngine.getPDPGroup(pdp);
86 } catch (PAPException e) {
87 LOGGER.error("Pdp Id not found in PDP Groups.", e);
89 return setPolicyConfigProperties(group);
93 * This method is used to set the policyGroupEntity data to properties. So, we can update the
94 * pdp thread with the latest policy info. Hence, considering Database as master instead of File
95 * System. To overcome the redundancy issues.
97 * @param group Input is OnapPDP Group name.
99 public List<Properties> setPolicyConfigProperties(OnapPDPGroup group) {
100 boolean groupCheck = false;
101 List<Properties> properties = new ArrayList<>();
102 policyLocations = new Properties();
103 Properties policyProperties = new Properties();
104 Properties pipProps = new Properties();
105 if (group != null && group.getName() != null) {
107 (GroupEntity) commonClassDao.getEntityItem(GroupEntity.class, "groupName", group.getName());
109 policyProperties = setPolicyProperties(data);
111 pipProps = readPipProperties();
112 } catch (IOException e) {
113 LOGGER.error("Error Occured while reading the pip properties.", e);
117 LOGGER.info("Group Name exists, but not exists in DB. So, adding the empty properties list.");
118 setEmptyPolicyProperties(policyProperties, pipProps);
121 LOGGER.info("Group Name is null. So, adding the empty properties list.");
122 setEmptyPolicyProperties(policyProperties, pipProps);
124 properties.add(policyProperties);
125 properties.add(pipProps);
127 properties.add(policyLocations);
133 * Based on the Group Entity list, write the policyNames to properties.
135 * @param data group entity object.
136 * @return properties.
138 private Properties setPolicyProperties(GroupEntity data) {
139 GroupEntity entity = data;
140 Properties policyProperties = new Properties() {
141 private static final long serialVersionUID = 1L;
143 // For Debugging it is helpful for the file to be in a sorted order,
144 // any by returning the keys in the natural Alpha order for strings we get close enough.
145 // TreeSet is sorted, and this just overrides the normal Properties method to get the
148 public synchronized Enumeration<Object> keys() {
149 return Collections.enumeration(new TreeSet<Object>(super.keySet()));
152 List<String> roots = new ArrayList<>();
153 for (PolicyEntity policy : entity.getPolicies()) {
154 // for all policies need to tell PDP the "name", which is the base name for the file id
155 String policyName = policy.getScope() + "." + policy.getPolicyName();
156 String policyNameWithNoScope = policy.getPolicyName();
157 if (policyName != null) {
158 policyProperties.setProperty(policyName + ".name", policy.getScope() + "."
159 + policyNameWithNoScope.substring(0, policyNameWithNoScope.indexOf('.')));
160 policyLocations.put(policyName + ".url", XACMLPapServlet.getPapUrl() + "?id=" + policyName);
162 roots.add(policyName);
164 policyProperties.setProperty(XACMLProperties.PROP_ROOTPOLICIES, Joiner.on(',').join(roots));
165 policyProperties.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
166 return policyProperties;
170 * When Group name is null, then we need to consider group is deleted and notify pdp with empty
173 * @param policyProperties policyProperties input.
174 * @param pipProps pipProps input.
176 private void setEmptyPolicyProperties(Properties policyProperties, Properties pipProps) {
177 // create blank properties files
178 policyProperties.put(XACMLProperties.PROP_ROOTPOLICIES, "");
179 policyProperties.put(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
180 pipProps.setProperty(XACMLProperties.PROP_PIP_ENGINES, "");