import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.util.LinkedHashMap;
+
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
private PolicyAPICall policyAPICall;
+ private static StringBuilder policyVersion = new StringBuilder("0.0.0") ;
+
@Autowired
PolicyAuthConfig policyAuthConfig;
public PolicyAPICall getPolicyAPICall() {
if (null == policyAPICall) {
this.policyAPICall = RestfulServices.create(PolicyAPICall.class, policyAuthConfig.getUserName(),
- policyAuthConfig.getPassword());
+ policyAuthConfig.getPassword());
}
return this.policyAPICall;
}
logger.info(String.format("Create policy, request body: %s", policyBody));
RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyBody.toString());
Response<ResponseBody> policyResponse = getPolicyAPICall().createPolicy(ModifyCLLPolicyConstants.policyType,
- ModifyCLLPolicyConstants.policyTypeVersion, policyReq).execute();
+ ModifyCLLPolicyConstants.policyTypeVersion, policyReq).execute();
logger.info(String.format("Create policy result, code: %d body: %s", policyResponse.code(),
- getResponseBodyStr(policyResponse)));
+ getResponseBodyStr(policyResponse)));
if (!policyResponse.isSuccessful()) {
logger.error("Create modify cll policy failed.");
return false;
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());
+ deployPolicyBody.toString());
Response<ResponseBody> deployPolicyResponse = getPolicyAPICall().deployPolicy(deployPolicyReq).execute();
logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
- getResponseBodyStr(deployPolicyResponse)));
+ getResponseBodyStr(deployPolicyResponse)));
if (!deployPolicyResponse.isSuccessful()) {
logger.error("Deploy modify cll policy failed.");
return false;
@Override
public boolean undeployAndRemoveModifyCLLPolicy() {
return undeployAndRemovePolicyIfExist(ModifyCLLPolicyConstants.policyType,
- ModifyCLLPolicyConstants.policyTypeVersion, ModifyCLLPolicyConstants.policyName,
- ModifyCLLPolicyConstants.policyVersion);
+ ModifyCLLPolicyConstants.policyTypeVersion, ModifyCLLPolicyConstants.policyName,
+ ModifyCLLPolicyConstants.policyVersion);
}
@Override
- public boolean updateIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) {
+ public boolean updateIntentConfigPolicy(String cllId, String originalBW, String 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));
+ "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);
+ IntentConfigPolicyConstants.policyTypeVersion, IntentConfigPolicyConstants.policyName,
+ IntentConfigPolicyConstants.policyVersion);
if (!res) {
logger.warn("Undeploy and remove the intent configuration policy failed.");
}
* @param closedLoopStatus
* @return
*/
- public boolean createAndDeployIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) {
+ public boolean createAndDeployIntentConfigPolicy(String cllId, String originalBW, String 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());
+ policyTypeBody.toString());
Response<ResponseBody> response = getPolicyAPICall().createPolicyType(policyTypeReq).execute();
logger.info(String.format("Create policy type result, code: %d body: %s", response.code(),
- getResponseBodyStr(response)));
+ getResponseBodyStr(response)));
if (!response.isSuccessful()) {
logger.error("Create intent configuration policy type failed.");
return false;
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());
+ .replace("${CLOSED_LOOP_STATUS}", closedLoopStatus)
+ .replace("${ORIGINAL_BW}", originalBW);
+ String policyJsonBody = updatePolicyVersion(policyBody);
+ logger.info(String.format("Create policy, request body: %s", policyJsonBody));
+ RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyJsonBody);
Response<ResponseBody> policyResponse = getPolicyAPICall().createPolicy(
- IntentConfigPolicyConstants.policyType, IntentConfigPolicyConstants.policyTypeVersion, policyReq)
- .execute();
+ IntentConfigPolicyConstants.policyType, policyVersion.toString(), policyReq)
+ .execute();
logger.info(String.format("Create policy result, code: %d body: %s", policyResponse.code(),
- getResponseBodyStr(policyResponse)));
+ getResponseBodyStr(policyResponse)));
if (!policyResponse.isSuccessful()) {
logger.error("Create intent configuration policy failed.");
return false;
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());
+ deployPolicyBody.toString());
Response<ResponseBody> deployPolicyResponse = getPolicyAPICall().deployPolicy(deployPolicyReq).execute();
logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
- getResponseBodyStr(deployPolicyResponse)));
+ getResponseBodyStr(deployPolicyResponse)));
if (!deployPolicyResponse.isSuccessful()) {
logger.error("Deploy intent configuration policy failed.");
return false;
* @return
*/
private boolean undeployAndRemovePolicyIfExist(String policyType, String policyTypeVersion, String policyName,
- String policyVersion) {
+ String policyVersion) {
try {
//check if the policy exists
Response<ResponseBody> response = getPolicyAPICall().getPolicy(policyType, policyTypeVersion, policyName,
- policyVersion).execute();
+ policyVersion).execute();
logger.info(String.format("The policy query result, code: %d body: %s", response.code(),
- getResponseBodyStr(response)));
+ getResponseBodyStr(response)));
// remove the policy if exists.
if (response.isSuccessful()) {
logger.info("The policy exists, start to undeploy.");
Response<ResponseBody> undeployResponse = getPolicyAPICall().undeployPolicy(policyName).execute();
logger.info(String.format("Undeploy policy result. code: %d body: %s", undeployResponse.code(),
- getResponseBodyStr(undeployResponse)));
+ getResponseBodyStr(undeployResponse)));
logger.info("Start to remove the policy.");
Response<ResponseBody> removeResponse = getPolicyAPICall().removePolicy(policyName, policyVersion)
- .execute();
+ .execute();
logger.info(String.format("Remove policy result. code: %d body: %s", removeResponse.code(),
- getResponseBodyStr(removeResponse)));
+ getResponseBodyStr(removeResponse)));
return true;
}
return true;
}
return null;
}
+
+ private String updatePolicyVersion(String policyBody) {
+ JSONObject policyJsonBody = JSONObject.parseObject(policyBody);
+ if (policyVersion.charAt(0) == '0') {
+ policyVersion.setCharAt(0, (char)(policyVersion.charAt(0) + 1));
+ }
+ else {
+ policyVersion.setCharAt(4, (char)(policyVersion.charAt(4) + 1));
+ }
+ JSONObject topologyTemplateObject = policyJsonBody.getJSONObject("topology_template");
+ JSONArray policiesArray = topologyTemplateObject.getJSONArray("policies");
+ JSONObject configObject = policiesArray.getJSONObject(0);
+ JSONObject versionObject = configObject.getJSONObject("onap.dcae.slicems.config");
+ versionObject.put("version", policyVersion.toString());
+ configObject.put("onap.dcae.slicems.config", versionObject);
+ policiesArray.set(0, configObject);
+ topologyTemplateObject.put("policies", policiesArray);
+ policyJsonBody.put("topology_template", topologyTemplateObject);
+ return policyJsonBody.toString();
+ }
}
-- Records of intent_management_function_reg_info
-- ----------------------------
INSERT INTO intent_management_function_reg_info(imfr_info_id,imfr_info_description,support_area,support_model,support_interfaces,handle_name,intent_function_type)
-VALUES ('CLLBusinessId','CLLBusiness','CLLBUSINESS',null,'CREATE,DELETE,UPDATE,SEARCH}','CLLBusinessIntentManagementFunction','INTERNALFUNCTION');
+VALUES ('CLLBusinessId','CLLBusiness','CLLBUSINESS',null,'CREATE,DELETE,UPDATE,SEARCH','CLLBusinessIntentManagementFunction','INTERNALFUNCTION');
INSERT INTO intent_management_function_reg_info(imfr_info_id,imfr_info_description,support_area,support_model,support_interfaces,handle_name,intent_function_type)
-VALUES ('CLLDeliveryId','CLLDelivery','CLLBUSINESS,DELIVERY',null,'CREATE,DELETE,UPDATE,SEARCH}','CLLDeliveryIntentManagementFunction','INTERNALFUNCTION');
+VALUES ('CLLDeliveryId','CLLDelivery','CLLBUSINESS,DELIVERY',null,'CREATE,DELETE,UPDATE,SEARCH','CLLDeliveryIntentManagementFunction','INTERNALFUNCTION');
INSERT INTO intent_management_function_reg_info(imfr_info_id,imfr_info_description,support_area,support_model,support_interfaces,handle_name,intent_function_type)
VALUES ('CLLAssuranceId','CLLAssurance','CLLBUSINESS,ASSURANCE',null,'CREATE,DELETE,UPDATE,SEARCH','CLLAssuranceIntentManagementFunction','INTERNALFUNCTION');
\ No newline at end of file