Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / DataToNotifyPdp.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest;
22
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
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;
36
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;
47
48 @Component
49 public class DataToNotifyPdp {
50
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;
55
56     @Autowired
57     public DataToNotifyPdp(CommonClassDao commonClassDao) {
58         DataToNotifyPdp.commonClassDao = commonClassDao;
59     }
60
61     public DataToNotifyPdp() {
62         // default constructor.
63     }
64
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);
70             }
71         }
72         return pipProperties;
73     }
74
75     /**
76      * Sets the policy config properties.
77      *
78      * @param pdp the pdp
79      * @param papEngine the pap engine
80      * @return the list
81      */
82     public List<Properties> setPolicyConfigProperties(OnapPDP pdp, PAPPolicyEngine papEngine) {
83         OnapPDPGroup group = null;
84         try {
85             group = papEngine.getPDPGroup(pdp);
86         } catch (PAPException e) {
87             LOGGER.error("Pdp Id not found in PDP Groups.", e);
88         }
89         return setPolicyConfigProperties(group);
90     }
91
92     /**
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.
96      *
97      * @param group Input is OnapPDP Group name.
98      */
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) {
106             GroupEntity data =
107                     (GroupEntity) commonClassDao.getEntityItem(GroupEntity.class, "groupName", group.getName());
108             if (data != null) {
109                 policyProperties = setPolicyProperties(data);
110                 try {
111                     pipProps = readPipProperties();
112                 } catch (IOException e) {
113                     LOGGER.error("Error Occured while reading the pip properties.", e);
114                 }
115                 groupCheck = true;
116             } else {
117                 LOGGER.info("Group Name exists, but not exists in DB. So, adding the empty properties list.");
118                 setEmptyPolicyProperties(policyProperties, pipProps);
119             }
120         } else {
121             LOGGER.info("Group Name is null. So, adding the empty properties list.");
122             setEmptyPolicyProperties(policyProperties, pipProps);
123         }
124         properties.add(policyProperties);
125         properties.add(pipProps);
126         if (groupCheck) {
127             properties.add(policyLocations);
128         }
129         return properties;
130     }
131
132     /**
133      * Based on the Group Entity list, write the policyNames to properties.
134      *
135      * @param data group entity object.
136      * @return properties.
137      */
138     private Properties setPolicyProperties(GroupEntity data) {
139         GroupEntity entity = data;
140         Properties policyProperties = new Properties() {
141             private static final long serialVersionUID = 1L;
142
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
146             // keys.
147             @Override
148             public synchronized Enumeration<Object> keys() {
149                 return Collections.enumeration(new TreeSet<Object>(super.keySet()));
150             }
151         };
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);
161             }
162             roots.add(policyName);
163         }
164         policyProperties.setProperty(XACMLProperties.PROP_ROOTPOLICIES, Joiner.on(',').join(roots));
165         policyProperties.setProperty(XACMLProperties.PROP_REFERENCEDPOLICIES, "");
166         return policyProperties;
167     }
168
169     /**
170      * When Group name is null, then we need to consider group is deleted and notify pdp with empty
171      * properties.
172      *
173      * @param policyProperties policyProperties input.
174      * @param pipProps pipProps input.
175      */
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, "");
181     }
182
183 }