aboutsummaryrefslogtreecommitdiffstats
path: root/intentanalysis/src/main/java/org/onap/usecaseui
diff options
context:
space:
mode:
authorKeguang He <hekeguang@chinamobile.com>2022-08-31 09:02:55 +0000
committerGerrit Code Review <gerrit@onap.org>2022-08-31 09:02:55 +0000
commit938d71a7adbfa2d7221ee37ce44f9e2ca8e5213a (patch)
treece9b126474caf6b38baae0751442e97c90abc6ec /intentanalysis/src/main/java/org/onap/usecaseui
parent5fe39ec091801b3edee0c3e6f0e08ab99caaf053 (diff)
parentf1f1e6ea5205fe774e8410d9a6e1fe8faddd5260 (diff)
Merge "Update Rest Authorization from Config File instead of hard code."
Diffstat (limited to 'intentanalysis/src/main/java/org/onap/usecaseui')
-rw-r--r--intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAPICall.java14
-rw-r--r--intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAuthConfig.java33
-rw-r--r--intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/PolicyServiceImpl.java29
-rw-r--r--intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java50
4 files changed, 87 insertions, 39 deletions
diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAPICall.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAPICall.java
index 897aed2..8240661 100644
--- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAPICall.java
+++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAPICall.java
@@ -29,14 +29,14 @@ import retrofit2.http.Path;
public interface PolicyAPICall {
@Headers({
- "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "Accept: application/json",
+ "Accept: application/json",
"Content-Type: application/json"
})
@POST("/policy/api/v1/policytypes")
Call<ResponseBody> createPolicyType(@Body RequestBody body);
@Headers({
- "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "Accept: application/json",
+ "Accept: application/json",
"Content-Type: application/json"
})
@POST("/policy/api/v1/policytypes/{policyType}/versions/{policyTypeVersion}/policies")
@@ -44,32 +44,32 @@ public interface PolicyAPICall {
@Path("policyTypeVersion") String policyTypeVersion, @Body RequestBody body);
@Headers({
- "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "Accept: application/json",
+ "Accept: application/json",
"Content-Type: application/json"
})
@POST("/policy/pap/v1/pdps/policies")
Call<ResponseBody> deployPolicy(@Body RequestBody body);
@Headers({
- "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "Accept: application/json",
+ "Accept: application/json",
"Content-Type: application/json"
})
@DELETE("/policy/pap/v1/pdps/policies/{policyName}")
Call<ResponseBody> undeployPolicy(@Path("policyName") String policyName);
@Headers({
- "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "Accept: application/json",
+ "Accept: application/json",
"Content-Type: application/json"
})
@DELETE("/policy/api/v1/policies/{policyName}/versions/{policyVersion}")
Call<ResponseBody> removePolicy(@Path("policyName") String policyName, @Path("policyVersion") String policyVersion);
@Headers({
- "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "Accept: application/json",
+ "Accept: application/json",
"Content-Type: application/json"
})
@GET(
- "https://192.168.100.2:30283/policy/api/v1/policytypes/{policyType}/versions/{policyTypeVersion}/policies/{policyName}/versions/{policyVersiion}")
+ "/policy/api/v1/policytypes/{policyType}/versions/{policyTypeVersion}/policies/{policyName}/versions/{policyVersion}")
Call<ResponseBody> getPolicy(@Path("policyType") String policyType,
@Path("policyTypeVersion") String policyTypeVersion, @Path("policyName") String policyName,
@Path("policyVersion") String policyVersion);
diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAuthConfig.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAuthConfig.java
new file mode 100644
index 0000000..2ef1e26
--- /dev/null
+++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAuthConfig.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2022 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.usecaseui.intentanalysis.adapters.policy.apicall;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+@Getter
+@Setter
+public class PolicyAuthConfig {
+
+ @Value("${rest.policy.username}")
+ private String userName;
+
+ @Value("${rest.policy.password}")
+ private String password;
+}
diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/PolicyServiceImpl.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/PolicyServiceImpl.java
index 26b7add..984afc5 100644
--- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/PolicyServiceImpl.java
+++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/PolicyServiceImpl.java
@@ -26,9 +26,11 @@ import org.apache.commons.io.FileUtils;
import org.apache.ibatis.io.Resources;
import org.onap.usecaseui.intentanalysis.adapters.policy.PolicyService;
import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall;
+import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAuthConfig;
import org.onap.usecaseui.intentanalysis.util.RestfulServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import retrofit2.Response;
@@ -39,8 +41,15 @@ public class PolicyServiceImpl implements PolicyService {
private PolicyAPICall policyAPICall;
- public PolicyServiceImpl() {
- this.policyAPICall = RestfulServices.create(PolicyAPICall.class);
+ @Autowired
+ PolicyAuthConfig policyAuthConfig;
+
+ public PolicyAPICall getPolicyAPICall() {
+ if (null == policyAPICall) {
+ this.policyAPICall = RestfulServices.create(PolicyAPICall.class, policyAuthConfig.getUserName(),
+ policyAuthConfig.getPassword());
+ }
+ return this.policyAPICall;
}
@Override
@@ -51,7 +60,7 @@ public class PolicyServiceImpl implements PolicyService {
String policyBody = FileUtils.readFileToString(policyFile, StandardCharsets.UTF_8);
logger.info(String.format("Create policy, request body: %s", policyBody));
RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyBody.toString());
- Response<ResponseBody> policyResponse = policyAPICall.createPolicy(ModifyCLLPolicyConstants.policyType,
+ Response<ResponseBody> policyResponse = getPolicyAPICall().createPolicy(ModifyCLLPolicyConstants.policyType,
ModifyCLLPolicyConstants.policyTypeVersion, policyReq).execute();
logger.info(
String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body()));
@@ -66,7 +75,7 @@ public class PolicyServiceImpl implements PolicyService {
logger.info(String.format("Deploy policy, request body: %s", deployPolicyBody));
RequestBody deployPolicyReq = RequestBody.create(MediaType.parse("application/json"),
deployPolicyBody.toString());
- Response<ResponseBody> deployPolicyResponse = policyAPICall.deployPolicy(deployPolicyReq).execute();
+ Response<ResponseBody> deployPolicyResponse = getPolicyAPICall().deployPolicy(deployPolicyReq).execute();
logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
deployPolicyResponse.body()));
if (!deployPolicyResponse.isSuccessful()) {
@@ -125,7 +134,7 @@ public class PolicyServiceImpl implements PolicyService {
logger.info(String.format("Create policy type, request body: %s", policyTypeBody));
RequestBody policyTypeReq = RequestBody.create(MediaType.parse("application/json"),
policyTypeBody.toString());
- Response<ResponseBody> response = policyAPICall.createPolicyType(policyTypeReq).execute();
+ Response<ResponseBody> response = getPolicyAPICall().createPolicyType(policyTypeReq).execute();
logger.info(
String.format("Create policy type result, code: %d body: %s", response.code(), response.body()));
if (!response.isSuccessful()) {
@@ -140,7 +149,7 @@ public class PolicyServiceImpl implements PolicyService {
.replace("${ORIGINAL_BW}", originalBW);
logger.info(String.format("Create policy, request body: %s", policyBody));
RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyBody.toString());
- Response<ResponseBody> policyResponse = policyAPICall.createPolicy(IntentConfigPolicyConstants.policyType,
+ Response<ResponseBody> policyResponse = getPolicyAPICall().createPolicy(IntentConfigPolicyConstants.policyType,
IntentConfigPolicyConstants.policyTypeVersion, policyReq).execute();
logger.info(
String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body()));
@@ -155,7 +164,7 @@ public class PolicyServiceImpl implements PolicyService {
logger.info(String.format("Deploy policy, request body: %s", deployPolicyBody));
RequestBody deployPolicyReq = RequestBody.create(MediaType.parse("application/json"),
deployPolicyBody.toString());
- Response<ResponseBody> deployPolicyResponse = policyAPICall.deployPolicy(deployPolicyReq).execute();
+ Response<ResponseBody> deployPolicyResponse = getPolicyAPICall().deployPolicy(deployPolicyReq).execute();
logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
deployPolicyResponse.body()));
if (!deployPolicyResponse.isSuccessful()) {
@@ -179,17 +188,17 @@ public class PolicyServiceImpl implements PolicyService {
String policyVersion) {
try {
//check if the policy exists
- Response<ResponseBody> response = policyAPICall.getPolicy(policyType, policyTypeVersion, policyName,
+ Response<ResponseBody> response = getPolicyAPICall().getPolicy(policyType, policyTypeVersion, policyName,
policyVersion).execute();
logger.info(String.format("The policy query result, code: %d body: %s", response.code(), response.body()));
// remove the policy if exists.
if (response.isSuccessful()) {
logger.info("The policy exists, start to undeploy.");
- Response<ResponseBody> undeployResponse = policyAPICall.undeployPolicy(policyName).execute();
+ Response<ResponseBody> undeployResponse = getPolicyAPICall().undeployPolicy(policyName).execute();
logger.info(String.format("Undeploy policy result. code: %d body: %s", undeployResponse.code(),
undeployResponse.body()));
logger.info("Start to remove the policy.");
- Response<ResponseBody> removeResponse = policyAPICall.removePolicy(policyName, policyVersion).execute();
+ Response<ResponseBody> removeResponse = getPolicyAPICall().removePolicy(policyName, policyVersion).execute();
logger.info(String.format("Remove policy result. code: %d body: %s", removeResponse.code(),
removeResponse.body()));
return true;
diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java
index ff51dc4..eca627a 100644
--- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java
+++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/util/RestfulServices.java
@@ -13,11 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package org.onap.usecaseui.intentanalysis.util;
+import javax.annotation.Nullable;
+import okhttp3.Authenticator;
+import okhttp3.Credentials;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
+import okhttp3.Request;
import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.Route;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Retrofit;
@@ -35,48 +42,47 @@ public class RestfulServices {
private static final Logger logger = LoggerFactory.getLogger(RestfulServices.class);
private static final String DEFAULT_MSB_IAG_ADDRESS = "msb-iag.onap:443";
+
private static final String ENV_MSB_IAG_SERVICE_HOST = "MSB_IAG_SERVICE_HOST";
- private static final String ENV_MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
+ private static final String ENV_MSB_IAG_SERVICE_PORT = "MSB_IAG_SERVICE_PORT";
public static <T> T create(String baseUrl, Class<T> clazz) {
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl(baseUrl)
- .addConverterFactory(JacksonConverterFactory.create())
- .build();
+ Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
+ .addConverterFactory(JacksonConverterFactory.create()).build();
return retrofit.create(clazz);
}
- public static <T> T create(Class<T> clazz) {
+ public static <T> T create(Class<T> clazz, final String userName, final String passWord) {
//Set the interface response time
- OkHttpClient okHttpClient = new OkHttpClient.Builder()
- .connectTimeout(300, TimeUnit.SECONDS)
- .readTimeout(300, TimeUnit.SECONDS)
- .sslSocketFactory(getSSLSocketFactory(), new CustomTrustManager())
- .hostnameVerifier(getHostnameVerifier())
- .build();
+ OkHttpClient okHttpClient = new OkHttpClient.Builder().authenticator(new Authenticator() {
+ @Nullable
+ @Override
+ public Request authenticate(@Nullable Route route, Response response) throws IOException {
+ String authStr = Credentials.basic(userName, passWord);
+ return response.request().newBuilder().addHeader("Authorization", authStr).build();
+ }
+ }).connectTimeout(300, TimeUnit.SECONDS).readTimeout(300, TimeUnit.SECONDS)
+ .sslSocketFactory(getSSLSocketFactory(), new CustomTrustManager()).hostnameVerifier(getHostnameVerifier())
+ .build();
String msbUrl = getMSBIAGAddress();
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("https://" + msbUrl + "/")
- .client(okHttpClient)
- .addConverterFactory(JacksonConverterFactory.create())
- .build();
+ Retrofit retrofit = new Retrofit.Builder().baseUrl("https://" + msbUrl + "/").client(okHttpClient)
+ .addConverterFactory(JacksonConverterFactory.create()).build();
return retrofit.create(clazz);
}
- public static String getMSBIAGAddress(){
+ public static String getMSBIAGAddress() {
String iagAddress = System.getenv(ENV_MSB_IAG_SERVICE_HOST);
String iagPort = System.getenv(ENV_MSB_IAG_SERVICE_PORT);
- if(iagAddress == null || iagPort == null){
+ if (iagAddress == null || iagPort == null) {
return DEFAULT_MSB_IAG_ADDRESS;
}
return iagAddress + ":" + iagPort;
}
-
public static RequestBody extractBody(HttpServletRequest request) throws IOException {
BufferedReader br = null;
StringBuilder sb = new StringBuilder("");
@@ -104,7 +110,7 @@ public class RestfulServices {
try {
SSLContext sc = SSLContext.getInstance("TLS");
- sc.init(null, new TrustManager[]{new CustomTrustManager()}, new SecureRandom());
+ sc.init(null, new TrustManager[] {new CustomTrustManager()}, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception e) {
@@ -114,7 +120,7 @@ public class RestfulServices {
}
public static HostnameVerifier getHostnameVerifier() {
- HostnameVerifier hostnameVerifier= new HostnameVerifier() {
+ HostnameVerifier hostnameVerifier = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}