Merge "e2e test for instantiation template"
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / creationDialogController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
21 "use strict";
22
23 var creationDialogController = function (COMPONENT, FIELD, PARAMETER, $scope, $http, $timeout, $log,
24                                          CreationService, UtilityService, DataService, VIDCONFIGURATION, $location, $uibModal, featureFlags) {
25
26     $scope.isDialogVisible = false;
27     $scope.isServiceError = false;
28     $scope.summaryControl = {};
29     $scope.userProvidedControl = {};
30
31
32     var callbackFunction = undefined;
33     var componentId = undefined;
34
35     $scope.showReportWindow = function() {
36
37         let errorMsg;
38
39         if($scope.error !== undefined && $scope.error != null) {
40             errorMsg = $scope.error;
41         } else {
42             errorMsg = "";
43         }
44
45         const modalWindow = $uibModal.open({
46             templateUrl: 'app/vid/scripts/modals/report-modal/report-modal.html',
47             controller: 'reportModalController',
48             controllerAs: 'vm',
49             resolve: {
50                 errorMsg: function () {
51                     return errorMsg;
52                 }
53             }
54         });
55
56         $scope.isDialogVisible = false;
57         $scope.popup.isVisible = false;
58     };
59
60     $scope.isShowErrorReport = function() {
61         return featureFlags.isOn(COMPONENT.FEATURE_FLAGS.FLAG_CREATE_ERROR_REPORTS);
62     };
63
64     $scope.shouldShowOldPopup = function () {
65         return !DataService.getShouldIncludeInAsyncInstantiationFlow();
66     };
67
68     function receiveMessage(event) {
69         if (event.data == 'closeIframe') {
70             window.removeEventListener("message", receiveMessage, false);
71
72             $scope.cancel();
73         }
74         else if (event.data.eventId == 'submitIframe') {
75             {
76                 $location.path('/servicePlanning').search({serviceModelId: event.data.data.serviceModelId});
77             }
78         } else if (event.data.eventId == 'showPreviousInstantiations') {
79             {
80                 $location.path('/instantiationStatus').search({filterText: event.data.data.serviceModelId});
81             }
82         }
83         $scope.$apply();
84     }
85
86     $scope.$on(COMPONENT.CREATE_COMPONENT, function (event, request) {
87         $scope.isSpinnerVisible = true;
88         $scope.isErrorVisible = false;
89         $scope.isDataVisible = false;
90         $scope.isConfirmEnabled = false;
91         $scope.isDialogVisible = true;
92         $scope.popup.isVisible = true;
93
94
95         if (!$scope.shouldShowOldPopup()) {
96             let modelNameVersionId = request.modelNameVersionId ?
97                 request.modelNameVersionId :
98                 (DataService.getModelInfo(COMPONENT.SERVICE) ? DataService.getModelInfo(COMPONENT.SERVICE).modelNameVersionId : "");
99             if(DataService.getIsInstantiationTemplateExists()){
100                 $scope.url = COMPONENT.INSTANTIATION_TEMPLATES_IFRAME_URL + modelNameVersionId;
101                 window.addEventListener("message", receiveMessage, false);
102             }else {
103                 $scope.url = COMPONENT.SERVICE_POPUP_IFRAME_URL + modelNameVersionId + "&isCreate=true&r=" + Math.random();
104                 window.addEventListener("message", receiveMessage, false);
105             }
106         }
107         else {
108             callbackFunction = request.callbackFunction;
109             componentId = request.componentId;
110             CreationService.initializeComponent(request.componentId);
111
112             CreationService.setHttpErrorHandler(function (response) {
113                 $scope.isServiceError = true;
114                 showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService
115                     .getHttpErrorMessage(response));
116             });
117
118             $scope.componentName = CreationService.getComponentDisplayName();
119
120             CreationService.getParameters(handleGetParametersResponse);
121         }
122
123     });
124
125     var handleGetParametersResponse = function (parameters) {
126         $scope.summaryControl.setList(parameters.summaryList);
127         $scope.userProvidedControl.setList(parameters.userProvidedList);
128
129         $scope.isSpinnerVisible = false;
130         $scope.isDataVisible = true;
131         $scope.isConfirmEnabled = true;
132     };
133
134     var validateInstanceName = function (iname) {
135         var patt1 = /^([a-z])+([0-9a-z\-_\.]*)$/i;
136
137         if (iname == null) {
138             return false;
139         }
140         if (!iname.match(patt1)) {
141             return false;
142         }
143         return true;
144     };
145     var validateMap = function (mname) {
146         var patt1 = /^{(\s*\w+\s*:\s*\w+\s*)(\s*,\s*\w+\s*:\s*\w+\s*)*}$/im;
147         if (mname == null) {
148             return true;
149         }
150         if (!mname.match(patt1)) {
151             return false;
152         }
153         return true;
154     };
155
156     var validateList = function (lname) {
157         var patt1 = /^\[(\s*\w+\s*)(\s*,\s*\w+\s*)*\]$/i;
158         if (lname == null) {
159             return true;
160         }
161         if (!lname.match(patt1)) {
162             return false;
163         }
164         return true;
165     };
166
167     $scope.userParameterChanged = function (id) {
168         CreationService.updateUserParameterList(id, $scope.userProvidedControl);
169     };
170
171     $scope.confirm = function () {
172
173         var requiredFields = $scope.userProvidedControl.getRequiredFields();
174         if (requiredFields !== "") {
175             showError(FIELD.ERROR.MISSING_DATA, requiredFields);
176             return;
177         }
178
179         var isUploadAvailable = false;
180         var uploadIndex = 0;
181         var paramList = $scope.userProvidedControl.getList();
182         var isAnyError = false;
183         for (var i = 0; i < paramList.length; i++) {
184             if (paramList[i].id === FIELD.ID.SUPPLEMENTORY_DATA_FILE) {
185                 isUploadAvailable = true;
186                 uploadIndex = i;
187             }
188             if (paramList[i].id === FIELD.ID.UPLOAD_SUPPLEMENTORY_DATA_FILE && paramList[i].value && document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE).value == '') {
189                 isAnyError = true;
190             }
191         }
192
193         if (isUploadAvailable && isAnyError) {
194             showError(FIELD.ERROR.MISSING_DATA, FIELD.ERROR.MISSING_FILE);
195
196         } else if (isUploadAvailable && document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE).value != '') {
197             var errorMsg = "";
198             var fileInput = document.getElementById(FIELD.ID.SUPPLEMENTORY_DATA_FILE);
199             var file = fileInput.files[0];
200             var reader = new FileReader();
201             reader.onload = function (e) {
202                 try {
203                     paramList[uploadIndex].value = JSON.parse(reader.result);
204                     FIELD.PARAMETER.SUPPLEMENTORY_DATA_FILE['value'] = paramList[uploadIndex].value;
205
206                     var instanceName = "";
207
208                     if (DataService.getALaCarte()) {
209                         if (paramList != null) {
210                             for (var i = 0; i < paramList.length; i++) {
211                                 if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {
212                                     instanceName = paramList[i].value;
213                                     break;
214                                 }
215                             }
216                         }
217                         var isValid = validateInstanceName(instanceName);
218                         if (isValid) {
219                             $scope.isErrorVisible = false;
220                         } else {
221                             showError(FIELD.ERROR.INVALID_INSTANCE_NAME + instanceName,
222                                 FIELD.ERROR.INSTANCE_NAME_VALIDATE);
223                             return;
224                         }
225                     }
226                     var arbitraryParametersList = DataService.getArbitraryParameters();
227                     var p = null;
228                     if (UtilityService.hasContents(arbitraryParametersList)) {
229                         for (var i = 0; i < arbitraryParametersList.length; i++) {
230                             p = arbitraryParametersList[i];
231                             if (p.type === PARAMETER.MAP) {
232                                 //validate a map: { <entry_key_1>: <entry_value_1>, ... , <entry_key_n>: <entry_value_n> }
233                                 // need to find the value in paramList
234                                 for (var j = 0; j < paramList.length; j++) {
235                                     if (paramList[j].id === p.id) {
236                                         p.value = paramList[j].value;
237                                         var isValid = validateMap(p.value);
238                                         if (isValid) {
239                                             $scope.isErrorVisible = false;
240                                             break;
241                                         }
242                                         else {
243                                             showError(FIELD.ERROR.INVALID_MAP + p.id,
244                                                 FIELD.ERROR.MAP_VALIDATE);
245                                             return;
246                                         }
247                                     }
248                                 }
249                             } else if (p.type === PARAMETER.LIST) {
250                                 //validate a list: { value or a list of comma separated values }
251                                 // need to find the value in paramList
252                                 for (var j = 0; j < paramList.length; j++) {
253                                     if (paramList[j].id === p.id) {
254                                         p.value = paramList[j].value;
255                                         var isValid = validateList(p.value);
256                                         if (isValid) {
257                                             $scope.isErrorVisible = false;
258                                             break;
259                                         }
260                                         else {
261                                             showError(FIELD.ERROR.INVALID_LIST + p.id,
262                                                 FIELD.ERROR.LIST_VALIDATE);
263                                             return;
264                                         }
265                                     }
266                                 }
267                             }
268                         }
269                     }
270                     var requestDetails = CreationService
271                         .getMsoRequestDetails($scope.userProvidedControl.getList());
272
273                     $scope.isDialogVisible = false;
274
275                     $scope.$broadcast(COMPONENT.MSO_CREATE_REQ, {
276                         url: CreationService.getMsoUrl(),
277                         requestDetails: requestDetails,
278                         componentId: componentId,
279                         callbackFunction: function (response) {
280                             if (response.isSuccessful) {
281                                 $scope.popup.isVisible = false;
282                                 runCallback(response);
283                             } else {
284                                 $scope.isDialogVisible = false;
285                                 $scope.popup.isVisible = false;
286                             }
287                         }
288                     });
289
290                 } catch (e) {
291                     errorMsg = errorMsg + FIELD.ERROR.INVALID_DATA_FORMAT;
292                 }
293                 if (errorMsg !== "") {
294                     showError(FIELD.ERROR.SYSTEM_FAILURE, errorMsg);
295
296                 }
297             };
298             reader.readAsText(file);
299         } else {
300
301             var paramList = $scope.userProvidedControl.getList();
302             var instanceName = "";
303
304             if (DataService.getALaCarte()) {
305                 if (paramList != null) {
306                     for (var i = 0; i < paramList.length; i++) {
307                         if (paramList[i].id === FIELD.ID.INSTANCE_NAME) {
308                             instanceName = paramList[i].value;
309                             break;
310                         }
311                     }
312                 }
313                 var isValid = validateInstanceName(instanceName);
314                 if (isValid) {
315                     $scope.isErrorVisible = false;
316                 } else {
317                     showError(FIELD.ERROR.INVALID_INSTANCE_NAME + instanceName,
318                         FIELD.ERROR.INSTANCE_NAME_VALIDATE);
319                     return;
320                 }
321             }
322             var arbitraryParametersList = DataService.getArbitraryParameters();
323             var p = null;
324             if (UtilityService.hasContents(arbitraryParametersList)) {
325                 for (var i = 0; i < arbitraryParametersList.length; i++) {
326                     p = arbitraryParametersList[i];
327                     if (p.type === PARAMETER.MAP) {
328                         //validate a map: { <entry_key_1>: <entry_value_1>, ... , <entry_key_n>: <entry_value_n> }
329                         // need to find the value in paramList
330                         for (var j = 0; j < paramList.length; j++) {
331                             if (paramList[j].id === p.id) {
332                                 p.value = paramList[j].value;
333                                 var isValid = validateMap(p.value);
334                                 if (isValid) {
335                                     $scope.isErrorVisible = false;
336                                     break;
337                                 }
338                                 else {
339                                     showError(FIELD.ERROR.INVALID_MAP + p.id,
340                                         FIELD.ERROR.MAP_VALIDATE);
341                                     return;
342                                 }
343                             }
344                         }
345                     } else if (p.type === PARAMETER.LIST) {
346                         //validate a list: { value or a list of comma separated values }
347                         // need to find the value in paramList
348                         for (var j = 0; j < paramList.length; j++) {
349                             if (paramList[j].id === p.id) {
350                                 p.value = paramList[j].value;
351                                 var isValid = validateList(p.value);
352                                 if (isValid) {
353                                     $scope.isErrorVisible = false;
354                                     break;
355                                 }
356                                 else {
357                                     showError(FIELD.ERROR.INVALID_LIST + p.id,
358                                         FIELD.ERROR.LIST_VALIDATE);
359                                     return;
360                                 }
361                             }
362                         }
363                     }
364                 }
365             }
366             var requestDetails = CreationService
367                 .getMsoRequestDetails($scope.userProvidedControl.getList());
368
369             $scope.isDialogVisible = false;
370
371             $scope.$broadcast(COMPONENT.MSO_CREATE_REQ, {
372                 url: CreationService.getMsoUrl(),
373                 requestDetails: requestDetails,
374                 componentId: componentId,
375                 callbackFunction: function (response) {
376                     if (response.isSuccessful) {
377                         $scope.popup.isVisible = false;
378                         runCallback(response);
379                     } else {
380                         $scope.isDialogVisible = false;
381                         $scope.popup.isVisible = false;
382                         runCallback(response);
383                     }
384                 }
385             });
386         }
387     };
388
389     $scope.cancel = function () {
390         $scope.isDialogVisible = false;
391         $scope.popup.isVisible = false;
392         runCallback(false);
393     };
394
395
396     var runCallback = function (response) {
397         if (angular.isFunction(callbackFunction)) {
398             callbackFunction({
399                 isSuccessful: response.isSuccessful,
400                 control: $scope.userProvidedControl.getList(),
401                 instanceId: response.instanceId
402             });
403         }
404     };
405
406     var showError = function (summary, details) {
407         var message = summary;
408         if (UtilityService.hasContents(details)) {
409             message += " (" + details + ")";
410         }
411         $scope.isSpinnerVisible = false;
412         $scope.isErrorVisible = true;
413         $scope.error = message;
414     }
415
416 };
417
418 appDS2.controller("creationDialogController", ["COMPONENT", "FIELD", "PARAMETER", "$scope", "$http",
419     "$timeout", "$log", "CreationService", "UtilityService", "DataService", "VIDCONFIGURATION", "$location",
420     "$uibModal", "featureFlags",
421     creationDialogController]);