aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PDP/src/main/java/org/onap/policy/xacml/action/FindAction.java
blob: c256626e6f857ad6b6f50725a6256798e8da29af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PDP
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.xacml.action;

import com.att.research.xacml.api.Advice;
import com.att.research.xacml.api.Attribute;
import com.att.research.xacml.api.AttributeAssignment;
import com.att.research.xacml.api.AttributeValue;
import com.att.research.xacml.api.Identifier;
import com.att.research.xacml.api.Obligation;
import com.att.research.xacml.api.Request;
import com.att.research.xacml.api.RequestAttributes;
import com.att.research.xacml.api.Result;
import com.att.research.xacml.std.IdentifierImpl;
import com.att.research.xacml.std.StdAdvice;
import com.att.research.xacml.std.StdAttributeAssignment;
import com.att.research.xacml.std.StdAttributeValue;
import com.att.research.xacml.std.StdMutableResponse;
import com.att.research.xacml.std.StdMutableResult;
import com.att.research.xacml.std.StdObligation;
import com.att.research.xacml.util.XACMLProperties;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.json.Json;
import javax.json.JsonReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.rest.XACMLRestProperties;

@SuppressWarnings("deprecation")
public class FindAction {
    private Logger logger = FlexLogger.getLogger(this.getClass());
    private Boolean changeIt = false;
    private String configUrl = null;
    private StdMutableResponse newResponse = new StdMutableResponse();
    private StdMutableResult addResult = new StdMutableResult();

    /**
     * Generate {@link StdMutableResponse} based on input request.
     *
     * @param stdResponse the response
     * @param pepRequest the request
     * @return {@link StdMutableResponse}
     */
    public StdMutableResponse run(final StdMutableResponse stdResponse, final Request pepRequest) {
        int count = 0;
        boolean config = false;
        boolean decide = false;
        final Collection<RequestAttributes> requestAttributes = pepRequest.getRequestAttributes();
        for (final RequestAttributes requestAttribute : requestAttributes) {
            final Collection<Attribute> attributes = requestAttribute.getAttributes();
            for (final Attribute attribute : attributes) {
                if (attribute.getAttributeId().stringValue().equals("urn:oasis:names:tc:xacml:1.0:action:action-id")) {
                    for (final AttributeValue<?> attributeValue : attribute.getValues()) {
                        if (attributeValue.getValue().toString().equalsIgnoreCase("ACCESS")) {
                            count++;
                        }
                        if (attributeValue.getValue().toString().equalsIgnoreCase("DECIDE")) {
                            decide = true;
                        }
                    }
                }
                if (attribute.getAttributeId().stringValue()
                        .equals("urn:oasis:names:tc:xacml:1.0:resource:resource-id")) {
                    for (final AttributeValue<?> attributeValue : attribute.getValues()) {
                        if (attributeValue.getValue().toString().equalsIgnoreCase("Config")) {
                            count++;
                        }
                    }
                }
            }
        }
        if (count == 2) {
            config = true;
        }
        if (!config) {
            search(stdResponse);
        }
        addResults(stdResponse, config, decide);
        logger.info("Original Result is " + stdResponse.toString());
        logger.info("Generated Result is " + addResult.toString());
        return newResponse;
    }

    private Collection<Obligation> obligations = new ArrayList<>();
    private Map<String, String> matchValues = new HashMap<>();
    private Map<String, String> headers = new HashMap<>();
    private boolean header = false;

    private void search(final StdMutableResponse stdResponse) {
        for (final Result result : stdResponse.getResults()) {
            if (!result.getObligations().isEmpty()) {
                System.out.println("Obligation Received");
                // Is there any action that PDP needs to take
                for (final Obligation obligation : result.getObligations()) {
                    int count = 0;
                    int uri = 0;
                    int pep = 0;
                    header = false;
                    changeIt = false;
                    final Collection<AttributeAssignment> afterRemoveAssignments = new ArrayList<>();
                    final Identifier oblId = new IdentifierImpl(obligation.getId().stringValue());
                    StdAttributeAssignment attributeUri = null;
                    for (final AttributeAssignment attribute : obligation.getAttributeAssignments()) {
                        matchValues.put(attribute.getAttributeId().stringValue(),
                                attribute.getAttributeValue().getValue().toString());
                        if (attribute.getAttributeId().stringValue().equalsIgnoreCase("performer")) {
                            if (attribute.getAttributeValue().getValue().toString().equalsIgnoreCase("PEPACTION")) {
                                pep++;
                            } else if (attribute.getAttributeValue().getValue().toString()
                                    .equalsIgnoreCase("PDPACTION")) {
                                count++;
                            }
                        } else if (attribute.getAttributeId().stringValue().equalsIgnoreCase("URL")) {
                            uri++;
                            if (uri == 1) {
                                configUrl = attribute.getAttributeValue().getValue().toString();
                                attributeUri = new StdAttributeAssignment(attribute);
                            }
                        } else if (attribute.getAttributeId().stringValue().startsWith("headers")) {
                            logger.info("Headers are : " + attribute.getAttributeValue().getValue().toString());
                            header = true;
                            headers.put(attribute.getAttributeId().stringValue().replaceFirst("(headers).", ""),
                                    attribute.getAttributeValue().getValue().toString());
                            afterRemoveAssignments.add(attribute);
                        } else if (attribute.getAttributeId().stringValue().equalsIgnoreCase("body")) {
                            String papPath = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_URL);
                            papPath = papPath.replace("/pap", "");
                            matchValues.put("body",
                                    attribute.getAttributeValue().getValue().toString().replace("$URL", papPath));
                        } else {
                            final StdAttributeAssignment attributeObligation = new StdAttributeAssignment(attribute);
                            afterRemoveAssignments.add(attributeObligation);
                        }
                    }
                    if (count == 1 && uri == 1 && pep == 0) {
                        // Remove Obligation and add Advice
                        changeIt = true;
                        takeAction(stdResponse, oblId, afterRemoveAssignments);
                    } else if (pep == 1 && count == 0) {
                        // Strip the PEPACTION if available
                        if (uri == 1) {
                            afterRemoveAssignments.add(attributeUri);
                        }
                        final Obligation afterRemoveObligation = new StdObligation(oblId, afterRemoveAssignments);
                        obligations.add(afterRemoveObligation);
                    } else {
                        obligations.add(obligation);
                    }
                }
            }
        }
    }

    private void takeAction(final StdMutableResponse stdResponse, final Identifier advId,
            final Collection<AttributeAssignment> afterRemoveAssignments) {
        if (changeIt) {
            logger.info("the URL is :" + configUrl);
            // Calling Rest URL..
            callRest();
            // Including the Results in an Advice
            final Identifier id = new IdentifierImpl("org.onap.policy:pdp:reply");
            final Identifier statId = new IdentifierImpl("org:onap:onap:policy:pdp:reply:status");
            final Identifier statCategory =
                    new IdentifierImpl("urn:oasis:names:tc:xacml:1.0:subject-category:recipient-subject");
            final Identifier strId = new IdentifierImpl("http://www.w3.org/2001/XMLSchema#string");
            final Identifier resId = new IdentifierImpl("org:onap:onap:policy:pdp:reply:resource");
            final Identifier resCategory =
                    new IdentifierImpl("urn:oasis:names:tc:xacml:3.0:attribute-category:resource");
            final Identifier urlId = new IdentifierImpl("http://www.w3.org/2001/XMLSchema#anyURI");
            final AttributeValue<String> attributeStatusValue = new StdAttributeValue<>(strId, status + response);
            final AttributeValue<String> attributeResourceValue = new StdAttributeValue<>(urlId, configUrl);
            final StdAttributeAssignment attributeStatus =
                    new StdAttributeAssignment(statCategory, statId, "PDP", attributeStatusValue);
            final StdAttributeAssignment attributeResouce =
                    new StdAttributeAssignment(resCategory, resId, "PDP", attributeResourceValue);
            afterRemoveAssignments.add(attributeStatus);
            afterRemoveAssignments.add(attributeResouce);
            final Advice advice = new StdAdvice(id, afterRemoveAssignments);
            addResult.addAdvice(advice);
        }
    }

    private void addResults(final StdMutableResponse stdResponse, final boolean config, final boolean decide) {
        if (decide) {
            newResponse = stdResponse;
            return;
        }
        for (final Result result : stdResponse.getResults()) {
            if (config) {
                addResult.addAdvice(result.getAssociatedAdvice());
            }
            addResult.addAttributeCategories(result.getAttributes());
            addResult.addPolicyIdentifiers(result.getPolicyIdentifiers());
            addResult.addPolicySetIdentifiers(result.getPolicySetIdentifiers());
            addResult.setStatus(result.getStatus());
            addResult.setDecision(result.getDecision());
            if (!config) {
                addResult.addObligations(obligations);
            }
        }
        newResponse.add(addResult);
    }

    private int status;
    private String response;
    private DefaultHttpClient httpClient;

    private void callRest() {
        // Finding the Macros in the URL..
        final Pattern pattern = Pattern.compile("\\$([a-zA-Z0-9.:]*)");
        final Matcher match = pattern.matcher(configUrl);
        final StringBuffer sb = new StringBuffer();
        JsonReader jsonReader = null;
        while (match.find()) {
            logger.info("Found Macro : " + match.group(1));
            final String replaceValue = matchValues.get(match.group(1));
            logger.info("Replacing with :" + replaceValue);
            match.appendReplacement(sb, replaceValue);
        }
        match.appendTail(sb);
        logger.info("URL is : " + sb.toString());
        configUrl = sb.toString();
        // Calling the Requested service.
        if (matchValues.get("method").equalsIgnoreCase("GET")) {
            httpClient = new DefaultHttpClient();
            try {
                final HttpGet getRequest = new HttpGet(configUrl);
                // Adding Headers here
                if (header) {
                    for (final String key : headers.keySet()) {
                        getRequest.addHeader(key, headers.get(key));
                    }
                }
                final HttpResponse result = httpClient.execute(getRequest);
                status = result.getStatusLine().getStatusCode();
                final BufferedReader br = new BufferedReader(new InputStreamReader((result.getEntity().getContent())));
                String output = " ";
                String out;
                while ((out = br.readLine()) != null) {
                    output = output + out;
                }
                response = output;
            } catch (final Exception e) {
                logger.error(e.getMessage() + e);
                response = e.getMessage();
            } finally {
                httpClient.getConnectionManager().shutdown();
            }
        } else if (matchValues.get("method").equalsIgnoreCase("POST")) {
            httpClient = new DefaultHttpClient();
            try {
                final HttpPost postRequest = new HttpPost(configUrl);
                // Adding Headers here
                if (header) {
                    for (final String key : headers.keySet()) {
                        postRequest.addHeader(key, headers.get(key));
                    }
                }
                // Adding the Body.
                final URL configUrl = new URL(matchValues.get("body"));
                URLConnection connection = null;
                connection = configUrl.openConnection();
                // InputStream in = connection.getInputStrem();
                // LOGGER.info("The Body Content is : " + IOUtils.toString(in));
                jsonReader = Json.createReader(connection.getInputStream());
                final StringEntity input = new StringEntity(jsonReader.readObject().toString());
                input.setContentType("application/json");
                postRequest.setEntity(input);
                // Executing the Request.
                final HttpResponse result = httpClient.execute(postRequest);
                logger.info("Result Headers are : " + result.getAllHeaders());
                status = result.getStatusLine().getStatusCode();
                final BufferedReader br = new BufferedReader(new InputStreamReader((result.getEntity().getContent())));
                String output = " ";
                String out;
                while ((out = br.readLine()) != null) {
                    output = output + out;
                }
                response = output;
            } catch (final Exception e) {
                logger.error(e.getMessage() + e);
                response = e.getMessage();
            } finally {
                if (jsonReader != null) {
                    try {
                        jsonReader.close();
                    } catch (final Exception e) {
                        logger.error("Exception Occured while closing the JsonReader" + e);
                    }
                }
                httpClient.getConnectionManager().shutdown();
            }
        } else if (matchValues.get("method").equalsIgnoreCase("PUT")) {
            httpClient = new DefaultHttpClient();
            try {
                final HttpPut putRequest = new HttpPut(configUrl);
                // Adding Headers here
                if (header) {
                    for (final String key : headers.keySet()) {
                        putRequest.addHeader(key, headers.get(key));
                    }
                }
                // Adding the Body.
                final URL configUrl = new URL(matchValues.get("body"));
                URLConnection connection = null;
                connection = configUrl.openConnection();
                // InputStream in = connection.getInputStream();
                // LOGGER.info("The Body Content is : " + IOUtils.toString(in));
                jsonReader = Json.createReader(connection.getInputStream());
                final StringEntity input = new StringEntity(jsonReader.readObject().toString());
                input.setContentType("application/json");
                putRequest.setEntity(input);
                // Executing the Request.
                final HttpResponse result = httpClient.execute(putRequest);
                status = result.getStatusLine().getStatusCode();
                final BufferedReader br = new BufferedReader(new InputStreamReader((result.getEntity().getContent())));
                String output = " ";
                String out;
                while ((out = br.readLine()) != null) {
                    output = output + out;
                }
                response = output;
            } catch (final Exception e) {
                logger.error(e.getMessage() + e);
                response = e.getMessage();
            } finally {
                if (jsonReader != null) {
                    try {
                        jsonReader.close();
                    } catch (final Exception e) {
                        logger.error("Exception Occured while closing the JsonReader" + e);
                    }
                }
                httpClient.getConnectionManager().shutdown();
            }
        }
    }
}