Merge "Prevent adding same type of Op policy"
authorSébastien Determe <sebastien.determe@intl.att.com>
Tue, 21 Apr 2020 12:27:17 +0000 (12:27 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 21 Apr 2020 12:27:17 +0000 (12:27 +0000)
1  2 
src/main/java/org/onap/clamp/loop/LoopService.java
ui-react/src/api/LoopService.js
ui-react/src/components/dialogs/Loop/ModifyLoopModal.js

@@@ -118,6 -118,12 +118,12 @@@ public class LoopService 
      Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException {
          Loop loop = getLoop(loopName);
          PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion);
+         Set<OperationalPolicy> opPolicySet = loop.getOperationalPolicies();
+         for (OperationalPolicy opPolicy : opPolicySet) {
+               if (opPolicy.getPolicyModel().equals(policyModel)) {
+                       throw new IllegalArgumentException("This type of Operational Policy is already added to the loop. Please choose another one.");
+               }
+         }
          if (policyModel == null) {
              return null;
          }
       *
       * @param loopName      The loop name
       * @param policyType    The policy model type
 -     * @param policyVersion The policy model  version
 +     * @param policyVersion The policy model version
       * @return The loop modified
       */
      Loop removeOperationalPolicy(String loopName, String policyType, String policyVersion) {
@@@ -226,22 -226,21 +226,21 @@@ export default class LoopService 
                        credentials: 'same-origin'
                })
                                .then(function (response) {
-                                       console.debug("Add Operational Policy response received: ", response.status);
+                               console.debug("Add Operational Policy response received: ", response.status);
                                if (response.ok) {
                                        return response.json();
                                } else {
-                                       console.error("Add Operational Policy query failed");
-                                       return {};
+                                       return response.text();
                                }
                        })
-                       .catch(function (error) {
-                               console.error("Add Operational Policy error received", error);
-                               return {};
-                       });
+                       .then(function (object) {
+                               console.error("Add Operational Policy query failed");
+                               throw new Error(object);
+                       })
        }
  
 -      static removeOperationalPolicyType(loopName, policyType, policyVersion) {
 -              return fetch('/restservices/clds/v2/loop/removeOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion , {
 +      static removeOperationalPolicyType(loopName, policyType, policyVersion, policyName) {
 +              return fetch('/restservices/clds/v2/loop/removeOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion + '/' + policyName , {
                        method: 'PUT',
                        headers: {
                                "Content-Type": "application/json"
@@@ -37,7 -37,7 +37,7 @@@ import Search from '@material-ui/icons/
  import LoopService from '../../../api/LoopService';
  import Tabs from 'react-bootstrap/Tabs';
  import Tab from 'react-bootstrap/Tab';
+ import Alert from 'react-bootstrap/Alert';
  
  const ModalStyled = styled(Modal)`
        background-color: transparent;
@@@ -65,6 -65,7 +65,7 @@@ export default class ModifyLoopModal ex
                toscaPolicyModelsData: [],
                selectedPolicyModelsData: [],
                key: 'add',
+               showFailAlert: false,
                toscaColumns: [
                        { title: "#", field: "index", render: rowData => rowData.tableData.id + 1,
                                cellStyle: cellStyle,
                                cellStyle: cellStyle,
                                headerStyle: headerStyle
                        },
 +                      { title: "Policy Name", field: "policyName",
 +              cellStyle: cellStyle,
 +                headerStyle: headerStyle
 +            },
                        { title: "Version", field: "version",
                                cellStyle: cellStyle,
                                headerStyle: headerStyle
@@@ -94,7 -91,7 +95,7 @@@
                                cellStyle: cellStyle,
                                headerStyle: headerStyle
                        },
 -             { title: "Add", field: "updatedDate", editable: 'never',
 +             { title: "Created Date", field: "createdDate", editable: 'never',
                cellStyle: cellStyle,
                headerStyle: headerStyle
               }
                var operationalPolicies = this.state.loopCache.getOperationalPolicies();
                var selectedPolicyModels = [];
                for (var policy in operationalPolicies) {
 -                      selectedPolicyModels.push(operationalPolicies[policy]["policyModel"]);
 +                  var newRow = operationalPolicies[policy]["policyModel"];
 +                  newRow["policyName"] = operationalPolicies[policy].name;
 +                      selectedPolicyModels.push(newRow);
                }
  
                PolicyToscaService.getToscaPolicyModels().then(allToscaModels => {
                this.props.history.push('/');
        }
  
+       renderAlert() {
+               return (
+                       <div>
+                               <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible>
+                                       {this.state.showMessage}
+                               </Alert>
+                       </div>
+               );
+       }
        handleAdd() {
-               LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version);
-               this.props.loadLoopFunction(this.state.loopCache.getLoopName());
-               this.handleClose();
+               LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version)
+               .then(pars => {
+                       this.props.loadLoopFunction(this.state.loopCache.getLoopName());
+                       this.handleClose();
+               })
+               .catch(error => {
+                       this.setState({ showFailAlert: true, showMessage: "Adding failed with error: " + error.message});
+               });
        }
  
        handleRemove() {
 -              LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version);
 +              LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version,this.state.selectedRowData.policyName);
                this.props.loadLoopFunction(this.state.loopCache.getLoopName());
                this.handleClose();
        }
  
        render() {
                return (
 -                      <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose}>
 +                      <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} >
                                <Modal.Header closeButton>
                                        <Modal.Title>Modify Loop Operational Policies</Modal.Title>
                                </Modal.Header>
                                                                rowStyle: rowData => ({
                                                                        backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined
                                                                        && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF'
-                                                               })
+                                                                       })
                                                        }}
                                                        />
                                                        <div>
                                                                <TextModal value={this.state.content} onChange={this.handleYamlContent}/>
                                                        </div>
                                                </Modal.Body>
+                                               {this.renderAlert()}
                                        </Tab>
                                        <Tab eventKey="remove" title="Remove Operational Policies">
                                                <Modal.Body>
                                                        <MaterialTable
 -                                                      title={"Already added Tosca Policy Models"}
 +                                                      title={"Tosca Policy Models already added"}
                                                        data={this.state.selectedPolicyModelsData}
                                                        columns={this.state.toscaColumns}
                                                        icons={this.state.tableIcons}