diff options
author | ChuanyuChen <chenchuanyu@huawei.com> | 2022-08-31 15:13:47 +0800 |
---|---|---|
committer | ChuanyuChen <chenchuanyu@huawei.com> | 2022-08-31 15:13:47 +0800 |
commit | 83b6c03d6c54c1b305d5f050036985f5aafb06bd (patch) | |
tree | 529945bd84ee3a0b0d446efb011973832e64efc2 /intentanalysis/src/main/java/org/onap | |
parent | 7679786f7dd60e0ca5e2b1d0f93e235cf0df0011 (diff) |
Add Policy adapter to communicate with Policy module
Add Policy adapter to communicate with Policy module
Issue-ID: USECASEUI-714
Signed-off-by: ChuanyuChen <chenchuanyu@huawei.com>
Change-Id: I26445e2c99ca9db960deb7a56166196444c39708
Diffstat (limited to 'intentanalysis/src/main/java/org/onap')
8 files changed, 428 insertions, 6 deletions
diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/aai/AAIService.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/aai/AAIService.java new file mode 100644 index 0000000..d688c16 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/aai/AAIService.java @@ -0,0 +1,18 @@ +/* + * 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.aai; + +public interface AAIService { } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/PolicyService.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/PolicyService.java new file mode 100644 index 0000000..4511ed0 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/PolicyService.java @@ -0,0 +1,42 @@ +/* + * 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; + +public interface PolicyService { + /** + * Add modify CLL bandwidth policy + * @return + */ + boolean createAndDeployModifyCLLPolicy(); + + /** + * Remove modify CLL bandwidth policy + * @return + */ + boolean undeployAndRemoveModifyCLLPolicy(); + + /** + * Update the intent configuration policy. Configuration policy is used to control the monitor of the CLL service bandwidth + * When closedLoopStatus set as true, DCAE will get the configuration and monitor the bandwidth of the specified CLL. + * When closedLoopStatus set as false, DCAE will stop monitor the bandwidth of the specified CLL. + * @param cllId The CLL service instance + * @param originalBW original bandwidth of the CLL service, it is used for DCAE to compare the CLL current bandwidth. + * @param closedLoopStatus True: start monitor, False: stop monitor + * @return + */ + boolean updateIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus); + +} 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 new file mode 100644 index 0000000..897aed2 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/apicall/PolicyAPICall.java @@ -0,0 +1,77 @@ +/* + * 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 okhttp3.RequestBody; +import okhttp3.ResponseBody; +import retrofit2.Call; +import retrofit2.http.Body; +import retrofit2.http.DELETE; +import retrofit2.http.GET; +import retrofit2.http.Headers; +import retrofit2.http.POST; +import retrofit2.http.Path; + +public interface PolicyAPICall { + + @Headers({ + "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "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", + "Content-Type: application/json" + }) + @POST("/policy/api/v1/policytypes/{policyType}/versions/{policyTypeVersion}/policies") + Call<ResponseBody> createPolicy(@Path("policyType") String policyType, + @Path("policyTypeVersion") String policyTypeVersion, @Body RequestBody body); + + @Headers({ + "Authorization: Basic cG9saWN5YWRtaW46emIhWHp0RzM0", "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", + "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", + "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", + "Content-Type: application/json" + }) + @GET( + "https://192.168.100.2:30283/policy/api/v1/policytypes/{policyType}/versions/{policyTypeVersion}/policies/{policyName}/versions/{policyVersiion}") + 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/impl/IntentConfigPolicyConstants.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/IntentConfigPolicyConstants.java new file mode 100644 index 0000000..4635d1c --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/IntentConfigPolicyConstants.java @@ -0,0 +1,28 @@ +/* + * 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.impl; + +public class IntentConfigPolicyConstants { + + //In CLL IBN Use Case, slice micro-service does the analysis together with the slice Use Case. + public static final String policyType = "onap.policies.monitoring.docker.slicems.app"; + + public static final String policyTypeVersion = "1.0.0"; + + public static final String policyName = "onap.dcae.slicems.config"; + + public static final String policyVersion = "1.0.0"; +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/ModifyCLLPolicyConstants.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/ModifyCLLPolicyConstants.java new file mode 100644 index 0000000..b223361 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/ModifyCLLPolicyConstants.java @@ -0,0 +1,27 @@ +/* + * 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.impl; + +public class ModifyCLLPolicyConstants { + public static final String policyType = "onap.policies.controlloop.operational.common.Drools"; + + public static final String policyTypeVersion = "1.0.0"; + + public static final String policyName = "operational.modifycll"; + + public static final String policyVersion = "1.0.0"; +} 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 new file mode 100644 index 0000000..26b7add --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/policy/impl/PolicyServiceImpl.java @@ -0,0 +1,205 @@ +/* + * 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.impl; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import okhttp3.ResponseBody; +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.util.RestfulServices; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import retrofit2.Response; + +@Service +public class PolicyServiceImpl implements PolicyService { + + private static final Logger logger = LoggerFactory.getLogger(PolicyServiceImpl.class); + + private PolicyAPICall policyAPICall; + + public PolicyServiceImpl() { + this.policyAPICall = RestfulServices.create(PolicyAPICall.class); + } + + @Override + public boolean createAndDeployModifyCLLPolicy() { + try { + //Create policy + File policyFile = Resources.getResourceAsFile("intentPolicy/modifycll.json"); + 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, + ModifyCLLPolicyConstants.policyTypeVersion, policyReq).execute(); + logger.info( + String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body())); + if (!policyResponse.isSuccessful()) { + logger.error("Create modify cll policy failed."); + return false; + } + + //Deploy policy + File deployPolicyFile = Resources.getResourceAsFile("intentPolicy/deploy_modifycll.json"); + String deployPolicyBody = FileUtils.readFileToString(deployPolicyFile, StandardCharsets.UTF_8); + 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(); + logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(), + deployPolicyResponse.body())); + if (!deployPolicyResponse.isSuccessful()) { + logger.error("Deploy modify cll policy failed."); + return false; + } + + } catch (IOException e) { + logger.error("Exception in create and deploy modify cll policy.", e); + return false; + } + return true; + } + + @Override + public boolean undeployAndRemoveModifyCLLPolicy() { + return undeployAndRemovePolicyIfExist(ModifyCLLPolicyConstants.policyType, + ModifyCLLPolicyConstants.policyTypeVersion, ModifyCLLPolicyConstants.policyName, + ModifyCLLPolicyConstants.policyVersion); + } + + @Override + public boolean updateIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) { + //the policy engine does not support update now. so we need to remove and recreate the policy now. + logger.info(String.format( + "Start to update the intent configuration policy, cllId: %s, originalBW: %s, closedLooopStatus:%b", cllId, + originalBW, closedLoopStatus)); + //remove the configuration policy first + boolean res = undeployAndRemovePolicyIfExist(IntentConfigPolicyConstants.policyType, + IntentConfigPolicyConstants.policyTypeVersion, IntentConfigPolicyConstants.policyName, + IntentConfigPolicyConstants.policyVersion); + if (!res) { + logger.warn("Undeploy and remove the intent configuration policy failed."); + } + res = createAndDeployIntentConfigPolicy(cllId, originalBW, closedLoopStatus); + if (!res) { + logger.error("Create and deploy the intent configuration policy failed."); + } + logger.info(String.format("update intent configuration finished, result: %b", res)); + return res; + } + + /** + * Create and deploy the configuration policy + * + * @param cllId + * @param originalBW + * @param closedLoopStatus + * @return + */ + public boolean createAndDeployIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) { + try { + //Create policy type + File policyTypeFile = Resources.getResourceAsFile("intentPolicy/intent_configs_policy_type.json"); + String policyTypeBody = FileUtils.readFileToString(policyTypeFile, StandardCharsets.UTF_8); + 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(); + logger.info( + String.format("Create policy type result, code: %d body: %s", response.code(), response.body())); + if (!response.isSuccessful()) { + logger.error("Create intent configuration policy type failed."); + return false; + } + //Create policy + File policyFile = Resources.getResourceAsFile("intentPolicy/intent_configs_policy.json"); + String policyBodyTemplate = FileUtils.readFileToString(policyFile, StandardCharsets.UTF_8); + String policyBody = policyBodyTemplate.replace("${CLL_ID}", cllId) + .replace("${CLOSED_LOOP_STATUS}", String.valueOf(closedLoopStatus)) + .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, + IntentConfigPolicyConstants.policyTypeVersion, policyReq).execute(); + logger.info( + String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body())); + if (!policyResponse.isSuccessful()) { + logger.error("Create intent configuration policy failed."); + return false; + } + + //Deploy policy + File deployPolicyFile = Resources.getResourceAsFile("intentPolicy/deploy_intent_configs.json"); + String deployPolicyBody = FileUtils.readFileToString(deployPolicyFile, StandardCharsets.UTF_8); + 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(); + logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(), + deployPolicyResponse.body())); + if (!deployPolicyResponse.isSuccessful()) { + logger.error("Deploy intent configuration policy failed."); + return false; + } + + } catch (IOException e) { + logger.error("Exception in create and deploy intent config policy.", e); + return false; + } + return true; + } + + /** + * undeploy and remove the configuration policy + * + * @return + */ + private boolean undeployAndRemovePolicyIfExist(String policyType, String policyTypeVersion, String policyName, + String policyVersion) { + try { + //check if the policy exists + Response<ResponseBody> response = policyAPICall.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(); + 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(); + logger.info(String.format("Remove policy result. code: %d body: %s", removeResponse.code(), + removeResponse.body())); + return true; + } + return true; + + } catch (IOException e) { + logger.error("Exception in undeploy and remove policy", e); + return false; + } + + } +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/so/SOService.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/so/SOService.java new file mode 100644 index 0000000..92ef65c --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/adapters/so/SOService.java @@ -0,0 +1,18 @@ +/* + * 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.so; + +public interface SOService { } 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 7e7e6c2..ff51dc4 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 @@ -34,6 +34,11 @@ 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"; + + public static <T> T create(String baseUrl, Class<T> clazz) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) @@ -52,7 +57,7 @@ public class RestfulServices { .hostnameVerifier(getHostnameVerifier()) .build(); - String msbUrl = getMsbAddress(); + String msbUrl = getMSBIAGAddress(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + msbUrl + "/") .client(okHttpClient) @@ -62,14 +67,16 @@ public class RestfulServices { return retrofit.create(clazz); } - public static String getMsbAddress() { - String msbAddress = System.getenv("MSB_ADDR"); - if (msbAddress == null) { - return ""; + 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){ + return DEFAULT_MSB_IAG_ADDRESS; } - return msbAddress; + return iagAddress + ":" + iagPort; } + public static RequestBody extractBody(HttpServletRequest request) throws IOException { BufferedReader br = null; StringBuilder sb = new StringBuilder(""); |