Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / EventRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.policy.pypdp;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.UUID;
26
27 import org.openecomp.policy.api.EventRequestParameters;
28 import org.openecomp.policy.api.PolicyResponse;
29 import org.openecomp.policy.api.PolicyResponseStatus;
30 import org.openecomp.policy.std.StdPolicyEngine;
31 import org.openecomp.policy.std.StdPolicyResponse;
32
33 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
34
35 public class EventRequest {
36         
37         private StdPolicyEngine pe;
38         public EventRequest(StdPolicyEngine pe){
39                 this.pe= pe;
40         }
41         
42         public Collection<PolicyResponse> run(EventRequestParameters pep, String requestID, String userID, String passcode){
43                 StdPolicyResponse policyResponse = new StdPolicyResponse();
44                 Collection<PolicyResponse> result = new ArrayList<PolicyResponse>();
45                 // construct a UUID from the request string
46                 if(pep.getRequestID()==null){ 
47                         UUID requestUUID = null;
48                         if (requestID != null && !requestID.isEmpty()) {
49                                 try {
50                                         requestUUID = UUID.fromString(requestID);
51                                 }
52                                 catch (IllegalArgumentException e) {
53                                         requestUUID = UUID.randomUUID();
54                                         PolicyLogger.info("Generated Random UUID: " + requestUUID.toString());
55                                 }
56                         }
57                         pep.setRequestID(requestUUID);
58                 }
59                 try {
60                         Collection<PolicyResponse> pResponses = pe.event(pep.getEventAttributes(), pep.getRequestID(), userID, passcode);
61                         for(PolicyResponse pResponse: pResponses){
62                                 pResponse = checkResponse(pResponse);
63                                 result.add(pResponse);
64                         }
65                         return result;
66                 } catch(Exception e){
67                         policyResponse.setPolicyResponseStatus(e.getMessage(), PolicyResponseStatus.NO_ACTION_REQUIRED);
68                         policyResponse = checkResponse(policyResponse);
69                         result.add(policyResponse);
70                         return result;
71                 }
72         }
73         
74         private StdPolicyResponse checkResponse(PolicyResponse pResponse) {
75                 StdPolicyResponse policyResponse= new StdPolicyResponse();
76                 policyResponse.setActionAdvised(pResponse.getActionAdvised());
77                 policyResponse.setActionTaken(pResponse.getActionTaken());
78                 policyResponse.setPolicyResponseMessage(pResponse.getPolicyResponseMessage());
79                 policyResponse.setPolicyResponseStatus(pResponse.getPolicyResponseStatus());
80                 policyResponse.setRequestAttributes(pResponse.getRequestAttributes());
81                 return policyResponse;
82         }
83 }