From 9f199eb7318be6975cd20cc265836fb627a4267b Mon Sep 17 00:00:00 2001 From: sebdet Date: Wed, 17 Feb 2021 17:53:17 +0100 Subject: Add code in UI to create new policy version Add code to call the policy create backend endpoint + alert box to show status of the creation + Fix backend + Issue-ID: POLICY-2928 Signed-off-by: sebdet Change-Id: Ia8f2506ecc692ad68111ebe7a55a92579b951908 (cherry picked from commit c427e6459d066859e6da068bf50c78a0d120080a) --- .../clamp/policy/pdpgroup/PoliciesPdpMerger.java | 4 +- .../resources/clds/camel/rest/clamp-api-v2.xml | 212 +++++++++++---------- .../policy/pdpgroup/PoliciesPdpMergerTest.java | 5 +- ui-react/src/api/PolicyService.js | 23 +++ .../components/dialogs/Policy/ViewAllPolicies.js | 95 +++++++-- 5 files changed, 219 insertions(+), 120 deletions(-) diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java index cf3c1656c..44b11119b 100644 --- a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java +++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java @@ -105,9 +105,11 @@ public class PoliciesPdpMerger { /** * This method removes the pdp States added for one policy. * - * @param policyJsonNode The policy node in Json + * @param policyJsonNode The policy node Json as String + * @return The Json with pdp group info removed */ public static JsonObject removePdpStatesOnePolicy(JsonObject policyJsonNode) { + //JsonObject policyJson = JsonUtils.GSON.fromJson(policyJsonNode, JsonObject.class); // Simply remove the nodes we have added. policyJsonNode.remove(PdpGroupsAnalyzer.ASSIGNED_PDP_GROUPS_INFO); policyJsonNode.remove(PdpGroupsAnalyzer.SUPPORTED_PDP_GROUPS_INFO); diff --git a/src/main/resources/clds/camel/rest/clamp-api-v2.xml b/src/main/resources/clds/camel/rest/clamp-api-v2.xml index bcad3ea33..f924a8a24 100644 --- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml +++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml @@ -803,6 +803,107 @@ + + + + + + + + + + java.lang.Exception + + true + + + + + 500 + + + GET ALL templates FAILED + + + + + + + + + + + + + + + java.lang.Exception + + true + + + + + 500 + + + GET Template FAILED + + + + + + + + + + + + + + + java.lang.Exception + + true + + + + + 500 + + + GET All Template names FAILED + + + + + + + @@ -1217,106 +1318,6 @@ - - - - - - - - - - java.lang.Exception - - true - - - - - 500 - - - GET ALL templates FAILED - - - - - - - - - - - - - - - java.lang.Exception - - true - - - - - 500 - - - GET Template FAILED - - - - - - - - - - - - - - - java.lang.Exception - - true - - - - - 500 - - - GET All Template names FAILED - - - - - - @@ -1360,11 +1361,13 @@ - - - + @@ -1382,6 +1385,9 @@ + + + ${body.toString()} { this.setState({ - selectedRow: rowData.tableData.id, - selectedRowJsonSchema: respJsonPolicyTosca, - selectedRowPolicyProperties: rowData["properties"], + selectedRowId: rowData.tableData.id, + selectedRowIdJsonSchema: respJsonPolicyTosca, + selectedRowIdPolicyProperties: rowData["properties"], jsonEditorForPolicy: this.createJsonEditor(respJsonPolicyTosca, rowData["properties"]) }); }); @@ -253,8 +262,52 @@ export default class ViewAllPolicies extends React.Component { return null; } - handleCreateNewVersion(event,rowData) { - return null; + customValidation(editorData) { + // method for sub-classes to override with customized validation + return []; + } + + handleCreateNewVersion() { + var editorData = this.state.jsonEditorForPolicy.getValue(); + var errors = this.state.jsonEditorForPolicy.validate(); + errors = errors.concat(this.customValidation(editorData)); + + if (errors.length !== 0) { + console.error("Errors detected during policy data validation ", errors); + this.setState({ + showFailAlert: true, + showMessage: 'Errors detected during policy data validation:\n' + OnapUtils.jsonEditorErrorFormatter(errors) + }); + return; + } else { + console.info("NO validation errors found in policy data"); + let newPolicy = JSON.parse(JSON.stringify(this.state.policiesListData[this.state.selectedRowId])); + newPolicy["properties"] = editorData; + let newVersion = this.bumpVersion(newPolicy["version"]); + newPolicy["version"] = newVersion; + newPolicy["metadata"]["policy-version"] = newVersion; + // Remove stuff added by UI + delete newPolicy["tableData"]; + PolicyService.createNewPolicy(newPolicy["type"], newPolicy["type_version"], newPolicy).then(respPolicyCreation => { + if (respPolicyCreation === "") { + //it indicates a failure + this.setState({ + showFailAlert: true, + showMessage: 'Policy Creation Failure' + }); + } else { + this.setState({ + showSuccessAlert: true, + showMessage: 'Policy in version ' + newVersion + ' created successfully' + }); + } + }) + } + } + + bumpVersion(versionToBump) { + let semVer = versionToBump.split("."); + return parseInt(semVer[0])+1 + "." + semVer[1] + "." + semVer[2]; } handleUpdatePolicy() { @@ -262,11 +315,16 @@ export default class ViewAllPolicies extends React.Component { this.props.history.push('/') } + disableAlert() { + this.setState ({ showSuccessAlert: false, showFailAlert: false }); + } + render() { return ( + } @@ -283,7 +341,7 @@ export default class ViewAllPolicies extends React.Component { exportButton: true, headerStyle:rowHeaderStyle, rowStyle: rowData => ({ - backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF' + backgroundColor: (this.state.selectedRowId !== -1 && this.state.selectedRowId === rowData.tableData.id) ? '#EEE' : '#FFF' }) }} actions={[ @@ -293,14 +351,25 @@ export default class ViewAllPolicies extends React.Component { onClick: (event, rowData) => this.handleDeletePolicy(event, rowData) } ]} - />
Policy Properties Editor
- - + + + + + {this.state.showMessage} + + + + + {this.state.showMessage} + + -- cgit 1.2.3-korg