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