Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / PushPolicyRequest.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.UUID;
24
25 import org.openecomp.policy.api.PolicyConfigException;
26 import org.openecomp.policy.pypdp.model_pojo.PepPushPolicyRequest;
27 import org.openecomp.policy.std.StdPolicyEngine;
28
29 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
30
31 import org.openecomp.policy.common.logging.eelf.MessageCodes;
32 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
33
34 public class PushPolicyRequest {
35         private StdPolicyEngine pe;
36         public PushPolicyRequest(StdPolicyEngine pe){
37                 this.pe= pe;
38         }
39         
40         public String run(PepPushPolicyRequest pep, String requestID, String userID, String passcode) {
41
42                 String result = null;
43                 
44                 // construct a UUID from the request string
45                 UUID requestUUID = null;
46                 if (requestID != null && !requestID.isEmpty()) {
47                         try {
48                                 requestUUID = UUID.fromString(requestID);
49                         }
50                         catch (IllegalArgumentException e) {
51                                 requestUUID = UUID.randomUUID();
52                                 PolicyLogger.info("Generated Random UUID: " + requestUUID.toString());
53                         }
54                 }else{
55                         requestUUID = UUID.randomUUID();
56                         PolicyLogger.error("No Request UUID Given, hence generating one random ID: " + requestUUID.toString());
57                 }
58                 String policyName = pep.getPolicyName();
59                 String policyScope = pep.getPolicyScope();
60                 if(policyName==null || policyName.isEmpty()){
61                         return XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyName was null or empty.";
62                 }
63                 if(policyScope== null || policyScope.isEmpty()){
64                         try{
65                                 policyName = pep.getPolicyName().substring(pep.getPolicyName().lastIndexOf(".")+1, pep.getPolicyName().length());
66                                 policyScope = pep.getPolicyName().substring(0, pep.getPolicyName().lastIndexOf("."));
67                         } catch (Exception e){
68                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "BAD REQUEST:  policyScope was null or empty.");
69                                 return XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyScope was null or empty.";
70                         }
71                 }
72                 PolicyLogger.info("policyName: "  + policyName + " policyScope is : " + policyScope);
73                 if (pep.getPolicyType() != null && !pep.getPolicyType().isEmpty()) {
74                         if (pep.getPdpGroup() != null && !pep.getPdpGroup().isEmpty()) {
75                                 try {
76                                         result = pe.pushPolicy(policyScope ,policyName , pep.getPolicyType(), pep.getPdpGroup(), requestUUID, userID, passcode);
77                                 } catch (PolicyConfigException e) {
78                                         result = e.getMessage();
79                                 } catch (Exception e) {
80                                         result = e.getMessage();
81                                 }
82                         } else {
83                                 result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyGroup was null or empty.";
84                         }
85                 } else {
86                         result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyType was null or empty.";
87                 }
88                 return result;
89         }
90 }