2 * Copyright 2022 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.usecaseui.intentanalysis.adapters.policy.impl;
20 import java.io.IOException;
21 import java.nio.charset.StandardCharsets;
22 import okhttp3.MediaType;
23 import okhttp3.RequestBody;
24 import okhttp3.ResponseBody;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.ibatis.io.Resources;
27 import org.onap.usecaseui.intentanalysis.adapters.policy.PolicyService;
28 import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall;
29 import org.onap.usecaseui.intentanalysis.util.RestfulServices;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
33 import retrofit2.Response;
36 public class PolicyServiceImpl implements PolicyService {
38 private static final Logger logger = LoggerFactory.getLogger(PolicyServiceImpl.class);
40 private PolicyAPICall policyAPICall;
42 public PolicyServiceImpl() {
43 this.policyAPICall = RestfulServices.create(PolicyAPICall.class);
47 public boolean createAndDeployModifyCLLPolicy() {
50 File policyFile = Resources.getResourceAsFile("intentPolicy/modifycll.json");
51 String policyBody = FileUtils.readFileToString(policyFile, StandardCharsets.UTF_8);
52 logger.info(String.format("Create policy, request body: %s", policyBody));
53 RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyBody.toString());
54 Response<ResponseBody> policyResponse = policyAPICall.createPolicy(ModifyCLLPolicyConstants.policyType,
55 ModifyCLLPolicyConstants.policyTypeVersion, policyReq).execute();
57 String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body()));
58 if (!policyResponse.isSuccessful()) {
59 logger.error("Create modify cll policy failed.");
64 File deployPolicyFile = Resources.getResourceAsFile("intentPolicy/deploy_modifycll.json");
65 String deployPolicyBody = FileUtils.readFileToString(deployPolicyFile, StandardCharsets.UTF_8);
66 logger.info(String.format("Deploy policy, request body: %s", deployPolicyBody));
67 RequestBody deployPolicyReq = RequestBody.create(MediaType.parse("application/json"),
68 deployPolicyBody.toString());
69 Response<ResponseBody> deployPolicyResponse = policyAPICall.deployPolicy(deployPolicyReq).execute();
70 logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
71 deployPolicyResponse.body()));
72 if (!deployPolicyResponse.isSuccessful()) {
73 logger.error("Deploy modify cll policy failed.");
77 } catch (IOException e) {
78 logger.error("Exception in create and deploy modify cll policy.", e);
85 public boolean undeployAndRemoveModifyCLLPolicy() {
86 return undeployAndRemovePolicyIfExist(ModifyCLLPolicyConstants.policyType,
87 ModifyCLLPolicyConstants.policyTypeVersion, ModifyCLLPolicyConstants.policyName,
88 ModifyCLLPolicyConstants.policyVersion);
92 public boolean updateIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) {
93 //the policy engine does not support update now. so we need to remove and recreate the policy now.
94 logger.info(String.format(
95 "Start to update the intent configuration policy, cllId: %s, originalBW: %s, closedLooopStatus:%b", cllId,
96 originalBW, closedLoopStatus));
97 //remove the configuration policy first
98 boolean res = undeployAndRemovePolicyIfExist(IntentConfigPolicyConstants.policyType,
99 IntentConfigPolicyConstants.policyTypeVersion, IntentConfigPolicyConstants.policyName,
100 IntentConfigPolicyConstants.policyVersion);
102 logger.warn("Undeploy and remove the intent configuration policy failed.");
104 res = createAndDeployIntentConfigPolicy(cllId, originalBW, closedLoopStatus);
106 logger.error("Create and deploy the intent configuration policy failed.");
108 logger.info(String.format("update intent configuration finished, result: %b", res));
113 * Create and deploy the configuration policy
117 * @param closedLoopStatus
120 public boolean createAndDeployIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) {
123 File policyTypeFile = Resources.getResourceAsFile("intentPolicy/intent_configs_policy_type.json");
124 String policyTypeBody = FileUtils.readFileToString(policyTypeFile, StandardCharsets.UTF_8);
125 logger.info(String.format("Create policy type, request body: %s", policyTypeBody));
126 RequestBody policyTypeReq = RequestBody.create(MediaType.parse("application/json"),
127 policyTypeBody.toString());
128 Response<ResponseBody> response = policyAPICall.createPolicyType(policyTypeReq).execute();
130 String.format("Create policy type result, code: %d body: %s", response.code(), response.body()));
131 if (!response.isSuccessful()) {
132 logger.error("Create intent configuration policy type failed.");
136 File policyFile = Resources.getResourceAsFile("intentPolicy/intent_configs_policy.json");
137 String policyBodyTemplate = FileUtils.readFileToString(policyFile, StandardCharsets.UTF_8);
138 String policyBody = policyBodyTemplate.replace("${CLL_ID}", cllId)
139 .replace("${CLOSED_LOOP_STATUS}", String.valueOf(closedLoopStatus))
140 .replace("${ORIGINAL_BW}", originalBW);
141 logger.info(String.format("Create policy, request body: %s", policyBody));
142 RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyBody.toString());
143 Response<ResponseBody> policyResponse = policyAPICall.createPolicy(IntentConfigPolicyConstants.policyType,
144 IntentConfigPolicyConstants.policyTypeVersion, policyReq).execute();
146 String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body()));
147 if (!policyResponse.isSuccessful()) {
148 logger.error("Create intent configuration policy failed.");
153 File deployPolicyFile = Resources.getResourceAsFile("intentPolicy/deploy_intent_configs.json");
154 String deployPolicyBody = FileUtils.readFileToString(deployPolicyFile, StandardCharsets.UTF_8);
155 logger.info(String.format("Deploy policy, request body: %s", deployPolicyBody));
156 RequestBody deployPolicyReq = RequestBody.create(MediaType.parse("application/json"),
157 deployPolicyBody.toString());
158 Response<ResponseBody> deployPolicyResponse = policyAPICall.deployPolicy(deployPolicyReq).execute();
159 logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
160 deployPolicyResponse.body()));
161 if (!deployPolicyResponse.isSuccessful()) {
162 logger.error("Deploy intent configuration policy failed.");
166 } catch (IOException e) {
167 logger.error("Exception in create and deploy intent config policy.", e);
174 * undeploy and remove the configuration policy
178 private boolean undeployAndRemovePolicyIfExist(String policyType, String policyTypeVersion, String policyName,
179 String policyVersion) {
181 //check if the policy exists
182 Response<ResponseBody> response = policyAPICall.getPolicy(policyType, policyTypeVersion, policyName,
183 policyVersion).execute();
184 logger.info(String.format("The policy query result, code: %d body: %s", response.code(), response.body()));
185 // remove the policy if exists.
186 if (response.isSuccessful()) {
187 logger.info("The policy exists, start to undeploy.");
188 Response<ResponseBody> undeployResponse = policyAPICall.undeployPolicy(policyName).execute();
189 logger.info(String.format("Undeploy policy result. code: %d body: %s", undeployResponse.code(),
190 undeployResponse.body()));
191 logger.info("Start to remove the policy.");
192 Response<ResponseBody> removeResponse = policyAPICall.removePolicy(policyName, policyVersion).execute();
193 logger.info(String.format("Remove policy result. code: %d body: %s", removeResponse.code(),
194 removeResponse.body()));
199 } catch (IOException e) {
200 logger.error("Exception in undeploy and remove policy", e);