Policy 1707 commit to LF
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / MicroServicesPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
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 package org.openecomp.policy.pdp.rest.api.services;
21
22 import javax.json.JsonException;
23 import javax.json.JsonObject;
24
25 import org.openecomp.policy.api.PolicyException;
26 import org.openecomp.policy.api.PolicyParameters;
27 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
28 import org.openecomp.policy.common.logging.flexlogger.Logger;
29 import org.openecomp.policy.pdp.rest.api.utils.PolicyApiUtils;
30 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
31 import org.openecomp.policy.xacml.std.pap.StdPAPPolicy;
32
33 /**
34  * MicroServices Policy implementation. 
35  * 
36  * @version 0.1
37  */
38 public class MicroServicesPolicyService{
39         private static Logger LOGGER = FlexLogger.getLogger(MicroServicesPolicyService.class.getName());
40         private static PAPServices papServices = null;
41         
42         private PolicyParameters policyParameters = null;
43         private String message = null;
44         private String policyName = null;
45         private String policyScope = null;
46         private String date = null;
47         private String ecompName = null;
48         private JsonObject microServiceAttributes = null;
49         private boolean levelCheck = false;
50         
51         public MicroServicesPolicyService(String policyName, String policyScope, PolicyParameters policyParameters, String date) {
52                 this.policyParameters = policyParameters;
53                 this.policyName = policyName;
54                 this.policyScope = policyScope;
55                 this.date = date;
56                 papServices = new PAPServices();
57         }
58
59         public Boolean getValidation() {
60                 if(policyParameters.getConfigBody()==null){
61                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " No Micro Service or Attributes Config Body Present";
62                         return false;
63                 }
64                 try{
65                         microServiceAttributes = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
66                 } catch(JsonException| IllegalStateException e){
67                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
68                         return false;
69                 }
70                 ecompName = policyParameters.getEcompName();
71                 if (ecompName==null||ecompName.trim().isEmpty()){
72                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Ecomp Name given.";
73                         return false;
74                 }
75                 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
76                 if (!levelCheck){
77                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
78                         return false;
79                 }
80                 return true;
81         }
82
83         public String getMessage() {
84                 return message;
85         }
86         
87         public String getResult(boolean updateFlag) throws PolicyException{
88             String response = null;
89         String operation = null;
90         if (updateFlag){
91             operation = "update";
92         } else {
93             operation = "create";
94         }
95         // get values and attributes from the JsonObject
96         String uuid = null;
97         String msLocation = null;
98         String configName = null;
99         String microService = microServiceAttributes.get("service").toString().replace("\"", "");
100         if (microServiceAttributes.get("uuid")!=null){
101             uuid = microServiceAttributes.get("uuid").toString().replace("\"", "");
102         }
103         if (microServiceAttributes.get("location")!=null){
104             msLocation = microServiceAttributes.get("location").toString().replace("\"", "");
105         }
106         if (microServiceAttributes.get("configName")!=null){
107             configName = microServiceAttributes.get("configName").toString().replace("\"", "");
108         }
109         String policyDescription = microServiceAttributes.get("description").toString().replace("\"", "");
110         String priority = microServiceAttributes.get("priority").toString().replace("\"", "");
111         String version = microServiceAttributes.get("version").toString().replace("\"", "");
112
113         // Create Policy. 
114         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Micro Service", policyName, policyDescription, ecompName, 
115                 configName, microService, uuid, msLocation, microServiceAttributes.toString(), priority, 
116                 version, updateFlag, policyScope, 0, policyParameters.getRiskLevel(),
117                 policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date); 
118         // Send JSON Object to PAP. 
119         response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigMS");
120         LOGGER.info("Policy MS created Response: " + response);
121         return response;
122         }
123 }