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;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.Enumeration;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.TreeSet;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.GroupEntity;
39 import org.onap.policy.rest.jpa.PolicyEntity;
40 import org.onap.policy.xacml.api.pap.OnapPDP;
41 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
42 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
47 public class DataToNotifyPdp {
49 private static final Logger LOGGER = FlexLogger.getLogger(DataToNotifyPdp.class);
50 private static CommonClassDao commonClassDao;
51 private Properties policyLocations = null;
52 private static Properties pipProperties = null;
55 public DataToNotifyPdp(CommonClassDao commonClassDao) {
56 DataToNotifyPdp.commonClassDao = commonClassDao;
59 public DataToNotifyPdp() {
60 // default constructor.
64 private static Properties readPipProperties() throws IOException {
65 if (pipProperties == null) {
66 try (FileInputStream inputStream = new FileInputStream(Paths.get("pip.properties").toString())) {
67 pipProperties = new Properties();
68 pipProperties.load(inputStream);
75 * Sets the policy config properties.
78 * @param papEngine the pap engine
81 public List<Properties> setPolicyConfigProperties(OnapPDP pdp, PAPPolicyEngine papEngine) {
82 OnapPDPGroup group = null;
84 group = papEngine.getPDPGroup(pdp);
85 } catch (PAPException e) {
86 LOGGER.error("Pdp Id not found in PDP Groups.", e);
88 return setPolicyConfigProperties(group);
92 * This method is used to set the policyGroupEntity data to properties. So, we can update the
93 * pdp thread with the latest policy info. Hence, considering Database as master instead of File
94 * System. To overcome the redundancy issues.
96 * @param group Input is OnapPDP Group name.
98 public List<Properties> setPolicyConfigProperties(OnapPDPGroup group) {
99 boolean groupCheck = false;
100 List<Properties> properties = new ArrayList<>();
101 policyLocations = new Properties();
102 Properties policyProperties = new Properties();
103 Properties pipProps = new Properties();
104 if (group != null && group.getName() != null) {
106 (GroupEntity) commonClassDao.getEntityItem(GroupEntity.class, "groupName", group.getName());
108 policyProperties = setPolicyProperties(data);
110 pipProps = readPipProperties();
111 } catch (IOException e) {
112 LOGGER.error("Error Occured while reading the pip properties.", e);
116 LOGGER.info("Group Name exists, but not exists in DB. So, adding the empty properties list.");
117 setEmptyPolicyProperties(policyProperties, pipProps);
120 LOGGER.info("Group Name is null. So, adding the empty properties list.");
121 setEmptyPolicyProperties(policyProperties, pipProps);
123 properties.add(policyProperties);
124 properties.add(pipProps);
126 properties.add(policyLocations);
132 * Based on the Group Entity list, write the policyNames to properties.
134 * @param data group entity object.
135 * @return properties.
137 private Properties setPolicyProperties(GroupEntity data) {
138 GroupEntity entity = data;
139 Properties policyProperties = new Properties() {
140 private static final long serialVersionUID = 1L;
142 // For Debugging it is helpful for the file to be in a sorted order,
143 // any by returning the keys in the natural Alpha order for strings we get close enough.
144 // TreeSet is sorted, and this just overrides the normal Properties method to get the
147 public synchronized Enumeration<Object> keys() {
148 return Collections.enumeration(new TreeSet<Object>(super.keySet()));
151 List<String> roots = new ArrayList<>();
152 for (PolicyEntity policy : entity.getPolicies()) {
153 // for all policies need to tell PDP the "name", which is the base name for the file id
154 String policyName = policy.getScope() + "." + policy.getPolicyName();
155 String policyNameWithNoScope = policy.getPolicyName();
156 if (policyName != null) {
157 policyProperties.setProperty(policyName + ".name", policy.getScope() + "."
158 + policyNameWithNoScope.substring(0, policyNameWithNoScope.indexOf('.')));
159 policyLocations.put(policyName + ".url", XACMLPapServlet.getPapUrl() + "?id=" + policyName);
161 roots.add(policyName);
163 policyProperties.setProperty(XACMLProperties.PROP_ROOTPOLICIES, Joiner.on(',').join(roots));
164 policyProperties.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
165 return policyProperties;
169 * When Group name is null, then we need to consider group is deleted and notify pdp with empty
172 * @param policyProperties policyProperties input.
173 * @param pipProps pipProps input.
175 private void setEmptyPolicyProperties(Properties policyProperties, Properties pipProps) {
176 // create blank properties files
177 policyProperties.put(XACMLProperties.PROP_ROOTPOLICIES, "");
178 policyProperties.put(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
179 pipProps.setProperty(XACMLProperties.PROP_PIP_ENGINES, "");