Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / ServiceModelController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 IBM.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 (function () {
23         'use strict';
24
25         appDS2.controller("ServiceModelController",function ($uibModal, $scope, $http, $location, COMPONENT, VIDCONFIGURATION, FIELD, DataService, vidService,
26                         PropertyService, UtilityService, AsdcService, $timeout, featureFlags) {
27
28                 $scope.popup = {};
29                 var defaultViewPerPage = 10;
30         //      var baseEndpoint = "vid";
31                 var pathQuery = COMPONENT.SERVICES_DIST_STATUS_PATH + VIDCONFIGURATION.ASDC_MODEL_STATUS;
32                 
33                 if ( VIDCONFIGURATION.ASDC_MODEL_STATUS === FIELD.STATUS.ALL) {
34                         pathQuery = COMPONENT.SERVICES_PATH;
35                 }
36         window.addEventListener("message", receiveMessage, false);
37
38                 function receiveMessage(event){
39             if(event.data == 'navigateTo') {
40                 $location.path('/models/services').search({});
41                 $scope.$apply();
42                 $scope.rememberFilter = true;
43             }
44             if(event.data == 'navigateToInstantiationStatus') {
45                 $location.path('/instantiationStatus').search({});
46                 $scope.$apply();
47             }
48                 }
49
50                 $scope.getServiceModels = function() {
51                         $scope.status = FIELD.STATUS.FETCHING_SERVICE_CATALOG_ASDC;
52
53                         $http.get(pathQuery)
54                         .then(function (response) {
55                                 $scope.services = [];
56                                 if (response.data && angular.isArray(response.data.services)) {
57                                         wholeData = response.data.services;
58                     $scope.services = $scope.filterDataWithHigherVersion(wholeData);
59                     $scope.viewPerPage = defaultViewPerPage;
60                     $scope.totalPage=$scope.services.length/$scope.viewPerPage;
61                     $scope.sortBy=COMPONENT.NAME;
62                     $scope.scrollViewPerPage=2;
63                     $scope.currentPage=1;
64                     $scope.currentPageNum=1;
65                     $scope.isSpinnerVisible = false;
66                     $scope.isProgressVisible = false;
67                                         var searchKey = sessionStorage.getItem("searchKey");
68                     if (searchKey != 'undefined' && searchKey!=null && ($scope.rememberFilter)) {
69                         var searchKey = JSON.parse(sessionStorage.getItem("searchKey"));
70                         $scope.searchString = searchKey.searchString || '';
71                         $scope.viewPerPage = searchKey.viewPerPage || defaultViewPerPage;
72                         $scope.totalPage = $scope.services.length / $scope.viewPerPage;
73                         $timeout(function () {
74                                 // the table controller handles the current page once
75                                                         // data is loaded, therefore we're delying the paging
76                                                         // override
77                             $scope.currentPage = $scope.currentPageNum = searchKey.currentPage ? parseInt(searchKey.currentPage) : 1;
78                         }, 0);
79                         $scope.rememberFilter = false;
80                     }
81                                 } else {
82                                         $scope.status = FIELD.STATUS.FAILED_SERVICE_MODELS_ASDC;
83                                         $scope.error = true;
84                                         $scope.isSpinnerVisible = false;
85                                 }
86                                 $scope.deployButtonType = response.data.readOnly ? 'disabled' : 'primary';
87                         }, function (response) {
88                                 console.log("Error: " + response);
89                         });
90                 };
91
92                 $scope.isShowOrchestrationType = function() {
93                         return featureFlags.isOn(COMPONENT.FEATURE_FLAGS.FLAG_SHOW_ORCHESTRATION_TYPE);
94                 };
95
96                 var wholeData=[];
97
98                 $scope.filterDataWithHigherVersion = function(serviceData){
99                         var delimiter = '$$';
100                         var fiterDataServices = {};
101                         for(var i=0;i<serviceData.length;i++) {
102                                 var index = serviceData[i].invariantUUID.trim() + delimiter + serviceData[i].name.trim();
103                                 if(!fiterDataServices[index]) {
104                     var hasPreviousVersion = false;
105                     fiterDataServices[index] = {
106                         service: serviceData[i],
107                         hasPreviousVersion: false
108                     };
109                 }
110                 else {
111                     fiterDataServices[index].hasPreviousVersion = true;
112                     if(parseFloat(serviceData[i].version.trim())>parseFloat(fiterDataServices[index].service.version.trim())) {
113                         fiterDataServices[index].service = serviceData[i];
114                                         }
115                                 }
116                         }
117             return Object.keys(fiterDataServices).map(function(key) {
118                 var service = fiterDataServices[key].service;
119                 service.hasPreviousVersion = fiterDataServices[key].hasPreviousVersion;
120                 return  service;
121             });
122                 };
123
124                 $scope.init = function() {
125                 var msecs = PropertyService.retrieveMsoMaxPollingIntervalMsec();
126                 PropertyService.setMsoMaxPollingIntervalMsec(msecs);
127                 
128                 var polls = PropertyService.retrieveMsoMaxPolls();
129                 PropertyService.setMsoMaxPolls(polls);
130         };
131                 
132                 $scope.prevPage = function() {
133                         $scope.currentPage--;
134                 };
135                 
136                 $scope.nextPage = function() {
137                         $scope.currentPage++;
138                 };
139
140                 $scope.showReportWindow = function() {
141
142                         const modalWindow = $uibModal.open({
143                                 templateUrl: 'app/vid/scripts/modals/report-modal/report-modal.html',
144                                 controller: 'reportModalController',
145                                 controllerAs: 'vm',
146                                 resolve: {
147                                         errorMsg: function () {
148                                                 return $scope.status;
149                                         }
150                                 }
151                         });
152
153                 };
154
155                 $scope.isShowErrorReport = function() {
156                         return featureFlags.isOn(COMPONENT.FEATURE_FLAGS.FLAG_CREATE_ERROR_REPORTS);
157                 };
158                 
159                 $scope.createType = COMPONENT.A_LA_CARTE;
160                 $scope.deployService = function(service) {
161                         var searchKey = {
162                                 searchString: $scope.searchString,
163                 viewPerPage: $scope.viewPerPage,
164                 currentPage: $scope.currentPage
165                         };
166                         DataService.setIsInstantiationTemplateExists(service.isInstantiationTemplateExists);
167                         sessionStorage.setItem("searchKey",JSON.stringify(searchKey));
168
169                         console.log("Instantiating SDC service " + service.uuid);
170
171                         $http.get(COMPONENT.SERVICES_PATH + service.uuid)
172                                 .then(function (getServiceResponse) {
173
174                                         var serviceModel = getServiceResponse.data;
175
176                                         //VID-233 bug fix when models doesn't exists
177                                         if(typeof(serviceModel)==="string"){  //not an object
178                                                 $scope.status = FIELD.STATUS.FAILED_SERVICE_MODELS_ASDC;
179                                                 $scope.error = true;
180                                                 $scope.isSpinnerVisible = false;
181                                                 $scope.isProgressVisible = true;
182                                                 return;
183                                         } else{ //clean error message
184                                                 $scope.status = "";
185                                                 $scope.error = false;
186                                                 $scope.isSpinnerVisible = false;
187                                                 $scope.isProgressVisible = false;
188                                         }
189
190                                         DataService.setServiceName(serviceModel.service.name);
191
192                                         //VOLTE services need input list generated and macro style
193                                         DataService.setE2EService(false);
194                                         if(serviceModel.service.category === 'E2E Service') {
195                                                 DataService.setE2EService(true);
196                                                 DataService.setHideServiceFields(false);
197                                                 VIDCONFIGURATION.MACRO_SERVICES.push(serviceModel.service.invariantUuid);
198                                         }
199
200                                         DataService.setModelInfo(COMPONENT.SERVICE, {
201                                                 "modelInvariantId": serviceModel.service.invariantUuid,
202                                                 "modelVersion": serviceModel.service.version,
203                                                 "serviceType" : serviceModel.service.serviceType,
204                                                 "serviceRole": serviceModel.service.serviceRole,
205                                                 "modelNameVersionId": serviceModel.service.uuid,
206                                                 "modelName": serviceModel.service.name,
207                                                 "description": serviceModel.service.description,
208                                                 "category":serviceModel.service.category
209                                         });
210
211                     var shouldTakeTheAsyncInstantiationFlow = AsdcService.shouldTakeTheAsyncInstantiationFlow(serviceModel);
212                     DataService.setShouldIncludeInAsyncInstantiationFlow(shouldTakeTheAsyncInstantiationFlow);
213
214                     DataService.setALaCarte (true);
215                     DataService.setPnf(!angular.equals(serviceModel.pnfs, {}));
216                                         $scope.createType = COMPONENT.A_LA_CARTE;
217                                         var broadcastType = COMPONENT.CREATE_COMPONENT;
218                     if (AsdcService.isMacro(serviceModel) || DataService.getE2EService()) {
219                         DataService.setALaCarte(false);
220                         if(!shouldTakeTheAsyncInstantiationFlow){
221                             $scope.createType = COMPONENT.MACRO;
222                             var convertedAsdcModel = UtilityService.convertModel(serviceModel);
223
224                             DataService.setModelInfo(COMPONENT.SERVICE, {
225                                 "modelInvariantId": serviceModel.service.invariantUuid,
226                                 "modelVersion": serviceModel.service.version,
227                                 "modelNameVersionId": serviceModel.service.uuid,
228                                 "modelName": serviceModel.service.name,
229                                 "description": serviceModel.service.description,
230                                 "category": serviceModel.service.category,
231                                 "serviceEcompNaming": serviceModel.service.serviceEcompNaming,
232                                 "inputs": serviceModel.service.inputs,
233                                 "serviceType": serviceModel.service.serviceType,
234                                 "serviceRole": serviceModel.service.serviceRole,
235                                 "displayInputs": convertedAsdcModel.completeDisplayInputs
236                             });
237                         }
238                     }
239
240                                         $scope.$broadcast(broadcastType, {
241                                             componentId : COMPONENT.SERVICE,
242                         modelNameVersionId: serviceModel.service.uuid,
243                                             callbackFunction : function(response) {
244                                                 if (response.isSuccessful) {
245                                                                 vidService.setModel(serviceModel);
246
247                                                                 var subscriberId = FIELD.STATUS.NOT_FOUND;
248                                                                 var serviceType = FIELD.STATUS.NOT_FOUND;
249
250                                                                 var serviceInstanceId = response.instanceId;
251
252                                                                 for (var i = 0; i < response.control.length; i++) {
253                                                                         if (response.control[i].id == COMPONENT.SUBSCRIBER_NAME) {
254                                                                                 subscriberId = response.control[i].value;
255                                                                         } else if (response.control[i].id == FIELD.ID.SERVICE_TYPE) {
256                                                                                 serviceType = response.control[i].value;
257                                                                         }
258                                                                 }
259
260
261                                                                 $scope.refreshSubs(subscriberId,serviceType,serviceInstanceId);
262
263                                                 }
264                                             }
265                                         });
266                                 }, function (response) {
267                                         console.log("Error: " + response);
268                                 });
269                 };
270                 
271                 $scope.tableData=[];
272                 var oldData=[];
273                 $scope.loadPreviousVersionData=function(invariantUUID , name, version){
274                         $scope.tableData =[];
275                         oldData=[];
276                         for(var i=0;i<wholeData.length;i++){
277                                 if(wholeData[i].invariantUUID == invariantUUID && wholeData[i].name == name && version!=wholeData[i].version){
278                                         oldData.push(wholeData[i]);
279                                 }
280                         }
281                         $scope.tableData = oldData;
282                         $scope.createType = "Previous Version";
283                         var broadcastType = "createTableComponent";
284                         $scope.componentName = name;
285                         $scope.$broadcast(broadcastType, {
286                             componentId : COMPONENT.OLDVERSION,
287                             callbackFunction : function(response) {
288                             }
289                         });
290                 };
291
292                 $scope.refreshSubs = function(subscriberId, serviceType, serviceInstanceId) {
293                         $scope.status = FIELD.STATUS.FETCHING_SUBSCRIBER_LIST_AAI;
294                         $scope.init();
295                         $http.get( FIELD.ID.AAI_REFRESH_FULL_SUBSCRIBERS, {
296
297                         },{
298                                 timeout: $scope.responseTimeoutMsec
299                         }).then(function(response){
300                                 
301                                 if (response.data.status < 200 || response.data.status > 202) {
302                                         $scope.showError(FIELD.ERROR.MSO);
303                                         return;
304                                 }
305
306                                 $scope.customer = response.data.customer; // get data from json
307
308                                 $scope.customerList = [];
309
310                                 $scope.serviceInstanceToCustomer = [];
311                                 
312                                 angular.forEach($scope.customer, function(subVal, subKey) {
313                                         var cust = { "globalCustomerId": subVal[FIELD.ID.GLOBAL_CUSTOMER_ID], "subscriberName": subVal[FIELD.ID.SUBNAME] };
314                                         $scope.customerList.push(cust);
315                                         if (subVal[FIELD.ID.SERVICE_SUBSCRIPTIONS] != null) {
316                                                         angular.forEach(subVal[FIELD.ID.SERVICE_SUBSCRIPTIONS][FIELD.ID.SERVICE_SUBSCRIPTION], function(serviceSubscription, key) {
317                                                                 $scope.serviceInstanceId = [];
318                                                                 if (serviceSubscription[FIELD.ID.SERVICETYPE] != null) {
319                                                                         $scope.serviceType = serviceSubscription[FIELD.ID.SERVICETYPE];
320                                                                 } else {
321                                                                         $scope.serviceType = FIELD.STATUS.NO_SERVICE_SUBSCRIPTION_FOUND;
322                                                                 }
323                                                                 if (serviceSubscription[FIELD.ID.SERVICE_INSTANCES] != null) {
324                                                                         angular.forEach(serviceSubscription[FIELD.ID.SERVICE_INSTANCES][FIELD.ID.SERVICE_INSTANCE], function(instValue, instKey) {
325                                                                                 var foo = { "serviceInstanceId": instValue[FIELD.ID.SERVICE_INSTANCE_ID], 
326                                                                                                 "globalCustomerId": subVal[FIELD.ID.GLOBAL_CUSTOMER_ID],
327                                                                                                 "subscriberName": subVal[FIELD.ID.SUBNAME] };
328                                                                                 $scope.serviceInstanceToCustomer.push(foo);
329                                                                         });
330                                                                 }
331                                                         });
332                                         }
333                                 });     
334                                 DataService.setServiceInstanceToCustomer($scope.serviceInstanceToCustomer);
335                                 var serviceIdList = [];
336                                 $http.get( FIELD.ID.AAI_GET_SERVICES, {
337                                 },{
338                                         timeout: $scope.responseTimeoutMsec
339                                 }).then(function(response) {
340                                         angular.forEach(response.data, function(value, key) {
341                                                 angular.forEach(value, function(subVal, key) {
342                                                         var newVal = { "id" : subVal[FIELD.ID.SERVICE_ID], "description" : subVal[FIELD.ID.SERVICE_DESCRIPTION] ,"isPermitted" : subVal[FIELD.ID.IS_PERMITTED] };
343                                                         serviceIdList.push(newVal);
344                                                         DataService.setServiceIdList(serviceIdList);
345                                                         
346                                                         $location.search({
347                                                                 "subscriberId": subscriberId,
348                                                                 "serviceType": serviceType,
349                                                                 "serviceInstanceId": serviceInstanceId,
350                                                                 "isPermitted": newVal.isPermitted.toString()
351                                                         });
352                                                         
353                                                         $location.path(COMPONENT.INSTANTIATE_PATH);
354                                                 });
355                                         });
356                                 });     
357                         })
358                         ["catch"]($scope.handleServerError);
359                 };
360         });
361 })();