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