Removing old name references
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / chefapi / ApiMethod.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.chef.chefapi;
26
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.TimeZone;
30
31 import org.apache.http.HttpResponse;
32 import org.apache.http.Header;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.methods.HttpRequestBase;
35 import org.apache.http.util.EntityUtils;
36 import org.onap.appc.adapter.chef.chefclient.Utils;
37
38 import org.apache.http.HttpEntity;
39 import org.apache.http.impl.client.HttpClients;
40
41 public class ApiMethod {
42     private HttpClient client = null;
43     protected HttpRequestBase method = null;
44     protected HttpResponse response = null;
45     protected String reqBody = "";
46     protected String userId = "";
47     protected String pemPath = "";
48     protected String chefPath = "";
49     protected String organizations = "";
50     protected int resCode=0;
51     protected String responseBody="";
52     private String methodName = "GET";
53     public String test = "";
54     private int returnCode;
55     final String KEY_STORE_PATH = "/etc/chef/trusted_certs/mykeystore.jks";
56     final String KEY_STORE_PASSWORD = "adminadmin";
57     static
58    {
59     System.setProperty("javax.net.ssl.trustStore", "/opt/onap/appc/chef/chefServerSSL.jks");
60     System.setProperty("javax.net.ssl.trustStorePassword", "adminadmin");
61         }
62
63     public ApiMethod(String methodName) {
64         client=HttpClients.createDefault();
65         this.methodName = methodName;
66     }
67
68    
69
70     public ApiMethod execute() {
71         String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath);
72         String hashedBody = Utils.sha1AndBase64(reqBody);
73
74         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
75         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
76         String timeStamp = sdf.format(new Date());
77         timeStamp = timeStamp.replace(" ", "T");
78         timeStamp = timeStamp + "Z";
79
80         StringBuilder sb = new StringBuilder();
81         sb.append("Method:").append(methodName).append("\n");
82         sb.append("Hashed Path:").append(hashedPath).append("\n");
83         sb.append("X-Ops-Content-Hash:").append(hashedBody).append("\n");
84         sb.append("X-Ops-Timestamp:").append(timeStamp).append("\n");
85         sb.append("X-Ops-UserId:").append(userId);
86         test = test + "sb " + sb + "\n";
87
88         String auth_String = Utils.signWithRSA(sb.toString(), pemPath);
89         String[] auth_headers = Utils.splitAs60(auth_String);
90
91         method.addHeader("Content-type", "application/json");
92         method.addHeader("X-Ops-Timestamp", timeStamp);
93         method.addHeader("X-Ops-Userid", userId);
94         method.addHeader("X-Chef-Version", "12.4.1");
95         method.addHeader("Accept", "application/json");
96         method.addHeader("X-Ops-Content-Hash", hashedBody);
97         method.addHeader("X-Ops-Sign", "version=1.0");
98
99         for (int i = 0; i < auth_headers.length; i++) {
100             method.addHeader("X-Ops-Authorization-" + (i + 1), auth_headers[i]);
101         }
102         try{
103             response = client.execute(method);
104             resCode = response.getStatusLine().getStatusCode();
105             HttpEntity entity1 = response.getEntity();
106             responseBody = EntityUtils.toString(entity1);}
107         catch(Exception ex){
108             resCode=500;
109             responseBody=ex.getMessage();
110         }
111         return this;
112     }
113
114     public void setHeaders(Header[] headers) {
115         for (Header header : headers) {
116             this.method.addHeader(header);
117         }
118     }
119
120     public String getResponseBodyAsString() {
121         return responseBody;
122     }
123
124     public int getReturnCode() {
125         return resCode;
126     }
127
128     public String getReqBody() {
129         return reqBody;
130     }
131
132     public void setReqBody(String body) {
133         this.reqBody = body;
134     }
135
136     public String getUserId() {
137         return userId;
138     }
139
140     public void setUserId(String userId) {
141         this.userId = userId;
142     }
143
144     public String getPemPath() {
145         return pemPath;
146     }
147
148     public void setPemPath(String pemPath) {
149         this.pemPath = pemPath;
150     }
151
152     public String getChefPath() {
153         return chefPath;
154     }
155
156     public void setChefPath(String chefPath) {
157         this.chefPath = chefPath;
158     }
159
160     public String getOrganizations() {
161         return organizations;
162     }
163
164     public void setOrganizations(String organizations) {
165         this.organizations = organizations;
166     }
167 }