Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / PolicyCreateUpdateRequest.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.api.PolicyParameters;
27 import org.openecomp.policy.pypdp.model_pojo.PepConfigPolicyRequest;
28 import org.openecomp.policy.std.StdPolicyEngine;
29
30 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
31
32 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
33
34 public class PolicyCreateUpdateRequest {
35         private StdPolicyEngine pe;
36         public PolicyCreateUpdateRequest(StdPolicyEngine pe){
37                 this.pe= pe;
38         }
39         
40         public String run(PolicyParameters pep, String requestID, String operation, String userID, String passcode) {
41                 String result = null;
42                 if(pep.getRequestID()==null){
43                         if (requestID != null && !requestID.isEmpty()) {
44                                 try {
45                                         pep.setRequestID(UUID.fromString(requestID));
46                                 }
47                                 catch (IllegalArgumentException e) {
48                                         pep.setRequestID(UUID.randomUUID());
49                                         PolicyLogger.info("Generated Random UUID: " + pep.getRequestID().toString());
50                                 }
51                         }
52                 }
53                 // check if this is create
54                 try{
55                         if (operation.equalsIgnoreCase("create")) {
56                                 result = pe.createPolicy(pep, userID, passcode ).getResponseMessage();
57                         }else{
58                                 // this is Update policy. 
59                                 result = pe.updatePolicy(pep, userID, passcode ).getResponseMessage();
60                         }
61                 }catch(Exception e){
62                         result = e.getMessage();
63                 }
64                 return result;
65         }
66         
67         public String run(PepConfigPolicyRequest pep, String requestID, String operation, String userID, String passcode) {
68
69                 String result = null;
70                 
71                 // construct a UUID from the request string
72                 UUID requestUUID = null;
73                 if (requestID != null && !requestID.isEmpty()) {
74                         try {
75                                 requestUUID = UUID.fromString(requestID);
76                         }
77                         catch (IllegalArgumentException e) {
78                                 requestUUID = UUID.randomUUID();
79                                 PolicyLogger.info("Generated Random UUID: " + requestUUID.toString());
80                         }
81                 }
82                 
83                 if (pep.getPolicyName()!= null && !pep.getPolicyName().isEmpty()) {
84                         if (pep.getEcompName() != null && !pep.getEcompName().isEmpty()) {
85                                 if (pep.getConfigName() != null && !pep.getConfigName().isEmpty()){
86                                         if (pep.getPolicyScope() != null && !pep.getPolicyScope().isEmpty()) {
87                                                 try {
88                                                         
89                                                         if (operation.equalsIgnoreCase("create")) {
90                                                                 
91                                                                 result = pe.createConfigPolicy(pep.getPolicyName(), pep.getPolicyDescription(), pep.getEcompName(), 
92                                                                                 pep.getConfigName(), pep.getConfigAttributes(), pep.getConfigType(), pep.getBody(), 
93                                                                         pep.getPolicyScope(), requestUUID, userID, passcode, pep.getRiskLevel(), pep.getRiskType(), pep.getGuard(), pep.getTtlDate());  
94                                                         } else {
95                                                                 result = pe.updateConfigPolicy(pep.getPolicyName(), pep.getPolicyDescription(), pep.getEcompName(), 
96                                                                                 pep.getConfigName(), pep.getConfigAttributes(), pep.getConfigType(), pep.getBody(), 
97                                                                         pep.getPolicyScope(), requestUUID, userID, passcode, pep.getRiskLevel(), pep.getRiskType(), pep.getGuard(), pep.getTtlDate());  
98                                                         }
99                                                                 
100
101                                                 } catch (PolicyConfigException e) {
102                                                         result = e.getMessage();
103                                                 } catch (Exception e) {
104                                                                 // TODO Auto-generated catch block
105                                                         e.printStackTrace();
106                                                 }
107                                                 
108                                         } else {
109                                                 result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyScope was null or empty.";
110                                         }
111
112                                 } else {
113                                         result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  configName was null or empty.";
114                                 }
115                         } else {
116                                 result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  ecompName was null or empty.";
117                         }
118                 } else {
119                         result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyName was null or empty.";
120                 }
121                 
122                 return result;
123         }
124 }