CLAMP Model policy creation support
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / controller / dictionaryController / MSModelsDictController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 app.controller('editMSModelController' ,  function ($scope, $modalInstance, message, $http, UserInfoServiceDS2, Notification){
21     if(message.microServiceModelsDictionaryData==null)
22         $scope.label='Add Micro Service Model'
23     else{
24         $scope.label='Edit Micro Service Model'
25         $scope.disableCd=true;
26     }
27         
28         /*getting user info from session*/
29         var userid = null;
30         UserInfoServiceDS2.getFunctionalMenuStaticDetailSession()
31                 .then(function (response) {                     
32                         userid = response.userid;               
33          });
34         
35         var valid = true;
36     $scope.editMSmodelName = message.microServiceModelsDictionaryData;
37
38     $scope.uploadFile = function(files) {
39                 valid = true;
40         var extn = files[0].name.substr(files[0].name.lastIndexOf('.')+1);
41         if(extn == 'zip' || extn == 'xmi'||  extn == 'yml'){
42                 if(extn == 'yml'){
43                         if(!files[0].name.includes("-v")){
44                                 Notification.error("File name should contain -v, such as myModel-v123.yml");
45                                 valid = false;
46                                 return;
47                         }
48                 }
49                 var fd = new FormData();
50                 fd.append("file", files[0]);
51                 $http.post("ms_dictionary/set_MSModelData", fd, {
52                         withCredentials: false,
53                         headers: {'Content-Type': undefined },
54                         transformRequest: angular.identity
55                 }).success(function(data){
56                         if(data.errorMsg != undefined || data.errorMsg != null){
57                                 Notification.error(data.errorMsg);
58                                 valid = false;
59                                 return;
60                         }                       
61                 if(data.classListDatas  == "EMPTY"){
62                         Notification.error("No Micro Services Avaialble.")
63                 }else{      
64                         $scope.classListDatas=data.classListDatas;
65                         $scope.modalDatas = data.modelDatas;
66                         $scope.modelType= data.modelType;
67                         $scope.dataOrderInfo= data.dataOrderInfo;
68                         $scope.ruleFormation = data.ruleFormation;
69                         console.log($scope.classListDatas);
70                 }
71             }).error( );
72         }else{
73                 Notification.error("Micro Service Model Upload file should ends with .zip .yml or .xmi extension");
74                 valid = false;
75         }
76
77     };
78     
79     $scope.saveMSModel = function(microServiceModelsDictionaryData) {
80         if(valid){
81                  var uuu = "saveDictionary/ms_dictionary/save_model";
82                 var postData={microServiceModelsDictionaryData: microServiceModelsDictionaryData, userid: userid, classMap: $scope.modalDatas,modelType:$scope.modelType, dataOrderInfo:$scope.dataOrderInfo, ruleFormation:$scope.ruleFormation};
83                 $.ajax({
84                     type : 'POST',
85                     url : uuu,
86                     dataType: 'json',
87                     contentType: 'application/json',
88                     data: JSON.stringify(postData),
89                     success : function(data){
90                         $scope.$apply(function(){
91                             $scope.microServiceModelsDictionaryDatas=data.microServiceModelsDictionaryDatas;});
92                         if($scope.microServiceModelsDictionaryDatas == "Duplicate"){
93                                 Notification.error("MS Models Dictionary exists with Same Model Name.")
94                         }else{      
95                                 console.log($scope.microServiceModelsDictionaryDatas);
96                             $modalInstance.close({microServiceModelsDictionaryDatas:$scope.microServiceModelsDictionaryDatas});
97                         }
98                     },
99                     error : function(data){
100                         Notification.error("Error while saving.");
101                     }
102                 });
103         }else{
104                 Notification.error("Please check Micro Service Model Upload file format.");
105         }
106        
107     };
108
109     $scope.close = function() {
110         $modalInstance.close();
111     };
112 });