Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / ServiceProxyConfigController.js
1 /*-
2 * ============LICENSE_START=======================================================
3 * VID
4 * ================================================================================
5 * Copyright (C) 2017 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 /**
22  * The Instantiation (or View/Edit) Controller controls the instantiation/removal of
23  * deployable objects (Services, VNFs, VF-Modules, Networks, and Volume-Groups)
24  */
25
26 "use strict";
27
28 appDS2.controller(
29     "ServiceProxyConfigController", ["COMPONENT", "$log", "FIELD", "PARAMETER", "DataService",
30     "CreationService", "$scope", "$window", "$location", "AaiService", "$uibModal", "UtilityService", "$timeout",
31     "featureFlags",
32     function (COMPONENT, $log, FIELD, PARAMETER, DataService,
33               CreationService, $scope, $window, $location, AaiService, $uibModal, UtilityService, $timeout,
34               featureFlags
35     ) {
36
37         $scope.selectedMetadata = {};
38
39         $scope.serviceMetadataFields = [];
40         $scope.nodeTemplateFields = {};
41
42         $scope.configurationByPolicy = DataService.getConfigurationByPolicy();
43
44         $scope.collectorType = $scope.configurationByPolicy ? 'pnf' : 'vnf'; //default
45         $scope.collectorInstance;
46         $scope.collectorInstanceName = "";
47         $scope.collectorInstanceList = null;
48         $scope.collectorMetadata = [];
49         $scope.collectorNoResults = false;
50
51         $scope.sourceInstance;
52         $scope.sourceInstanceName = "";
53         $scope.sourceInstanceList = null;
54         $scope.sourceMetadata = [];
55         $scope.sourceNoResults = false;
56
57         $scope.errorMsg = FIELD.ERROR.INSTANCE_NAME_VALIDATE;
58
59         $scope.modelName = DataService.getModelInfo(COMPONENT.VNF).modelCustomizationName;
60
61         $scope.serviceTypes = [];
62
63         function init() {
64             loadServiceTypes();
65
66             generateMetadata(sourceServiceProxy);
67             generateMetadata(collectorServiceProxy);
68
69         }
70
71         function setDefaultCollectorServiceType() {
72             const configServiceType = DataService.getServiceType();
73             $scope.collectorServiceType = mustFind($scope.serviceTypes, {"service-type": configServiceType});
74             loadCollectorProxies();
75         }
76
77         function handleGetServiceTypesResponse(response) {
78             $scope.serviceTypes = response.data;
79             setDefaultCollectorServiceType();
80         }
81
82         var handleGetParametersResponse = function(parameters) {
83             $scope.serviceMetadataFields = parameters.summaryList;
84             $scope.nodeTemplateFields =  DataService.getPortMirroningConfigFields();
85         };
86
87         var mustFind = function (collection, predicate) {
88             const result = _.find(collection, predicate);
89             const description = "result for find " + JSON.stringify(predicate);
90             UtilityService.checkUndefined(description, result);
91             $log.debug(description, result);
92             return result;
93         };
94
95
96         $scope.back = function()  {
97             $window.history.back();
98         };
99
100
101         function loadServiceTypes() {
102             const subscriberId = DataService.getGlobalCustomerId();
103             AaiService.getSubscriberServiceTypes(subscriberId)
104                 .then(handleGetServiceTypesResponse)
105                 .catch(function (error) {
106                     $log.error(error);
107                 });
108         }
109
110         var modalInstance;
111
112         $scope.create = function()  {
113             $scope.disableCreate= true;
114             var portMirroringConfigFields = DataService.getPortMirroningConfigFields();
115             portMirroringConfigFields.sourceInstance = mustFind($scope.sourceInstanceList, {'id': $scope.sourceInstance});
116             portMirroringConfigFields.destinationInstance = mustFind($scope.collectorInstanceList, {'id': $scope.collectorInstance});
117
118             var selectedVnfsList;
119
120             if ($scope.configurationByPolicy) {
121                 selectedVnfsList = [
122                     portMirroringConfigFields.sourceInstance.properties
123                 ];
124             } else {
125                 selectedVnfsList = [
126                     portMirroringConfigFields.sourceInstance.properties,
127                     portMirroringConfigFields.destinationInstance.properties
128                 ];
129             }
130
131             AaiService.getVnfVersionsByInvariantId(
132                 selectedVnfsList.map(function(x) {
133                     return UtilityService.checkUndefined("model-invariant-id", x['model-invariant-id']);
134                 })
135                 )
136                 .then(function (response) {
137                     $log.debug("getVnfVersionsByInvariantId: response", response);
138
139                     selectedVnfsList
140                         .map(function (inOutProperties) {
141                             const model = mustFind(response.data.model, {'model-invariant-id': inOutProperties['model-invariant-id']});
142
143                             const modelVer = mustFind(model["model-vers"]["model-ver"], {'model-version-id': inOutProperties['model-version-id']});
144
145                             inOutProperties['model-version'] = modelVer['model-version'];
146                             inOutProperties['model-name'] = modelVer['model-name'];
147                             UtilityService.checkUndefined("model-version", modelVer);
148                         });
149                 })
150
151                 .then(function () {
152                     var requestParams = {
153                         configurationModelInfo: DataService.getModelInfo(COMPONENT.VNF),
154                         relatedTopModelsInfo: DataService.getModelInfo(COMPONENT.SERVICE),
155                         portMirroringConfigFields:portMirroringConfigFields,
156                         attuuid: DataService.getLoggedInUserId(),
157                         topServiceInstanceId: DataService.getServiceInstanceId(),
158                         configurationByPolicy: $scope.configurationByPolicy,
159                         callbackFunction: updateViewCallbackFunction
160                     };
161
162                     modalInstance = $uibModal.open({
163                         templateUrl: 'app/vid/scripts/modals/mso-commit/mso-commit.html',
164                         controller : "msoCommitModalController",
165                         backdrop: true,
166                         resolve: {
167                             msoType: function () {
168                                 return COMPONENT.MSO_CREATE_CONFIGURATION_REQ;
169                             },
170                             requestParams: function () {
171                                 return requestParams;
172                             },
173                             configuration: function () {
174                                 return null;
175                             }
176                         }
177                     });
178                 })
179                 .catch(function (error) {
180                     $log.error("error while configuration create", error);
181                     $scope.disableCreate= false;
182                 });
183         };
184
185         $scope.openMetadataModal = function(name) {
186             $scope.selectedMetadata = $scope[name];
187             modalInstance = $uibModal.open({
188                 templateUrl: 'app/vid/scripts/modals/service-metadata/service-metadata.html',
189                 backdrop: false,
190                 scope : $scope,
191                 resolve: {
192                 }
193             });
194         };
195
196         $scope.cancel = function()  {
197             modalInstance.dismiss('cancel');
198         };
199
200         var updateViewCallbackFunction = function(response) {
201             $scope.callbackResults = "";
202             var color = FIELD.ID.COLOR_NONE;
203             $scope.callbackStyle = {
204                 "background-color" : color
205             };
206
207             /*
208              * This 1/2 delay was only added to visually highlight the status
209              * change. Probably not needed in the real application code.
210              */
211             $timeout(function() {
212                 $scope.callbackResults = UtilityService.getCurrentTime()
213                     + FIELD.STATUS.IS_SUCCESSFUL + response.isSuccessful;
214                 if (response.isSuccessful) {
215                     color = FIELD.ID.COLOR_8F8;
216                     $window.history.go(-2);
217                 } else {
218                     $scope.disableCreate=false;
219                     color = FIELD.ID.COLOR_F88;
220                 }
221                 $scope.callbackStyle = {
222                     "background-color" : color
223                 };
224             }, 500);
225         };
226
227         CreationService.initializeComponent(COMPONENT.VNF);
228         CreationService.initializeComponent(COMPONENT.SERVICE);
229         CreationService.getParameters(handleGetParametersResponse);
230
231         var sourceServiceProxies = DataService.getSourceServiceProxies();
232         var collectorServiceProxies = DataService.getCollectorServiceProxies();
233         var serviceProxiesList = DataService.getServiceProxies();
234
235         var sourceServiceProxy = {
236             serviceList: sourceServiceProxies,
237             instanceListScopePropertyName: "sourceInstanceList",
238             name: "sourceInstanceName",
239             metadata: "sourceMetadata",
240             noResults: "sourceNoResults"
241         };
242
243         var collectorServiceProxy = {
244             serviceList: collectorServiceProxies,
245             instanceListScopePropertyName: "collectorInstanceList",
246             name: "collectorInstanceName",
247             metadata: "collectorMetadata",
248             noResults: "collectorNoResults"
249         };
250
251         $scope.onSourceServiceTypeSelected = function() {
252             clearSourceProxySelection();
253             loadSourceProxies();
254         };
255
256         $scope.onCollectorServiceTypeSelected = function() {
257             clearCollectorProxySelection();
258             loadCollectorProxies();
259         };
260
261         $scope.shouldLetSelectingCollectorType = function() {
262             return $scope.collectorType === 'vnf' || featureFlags.isOn(COMPONENT.FEATURE_FLAGS.FLAG_1810_CR_LET_SELECTING_COLLECTOR_TYPE_UNCONDITIONALLY);
263         };
264
265         function clearSourceProxySelection() {
266             $scope.sourceInstance = undefined;
267         }
268
269         function clearCollectorProxySelection() {
270             $scope.collectorInstance = undefined;
271         }
272
273         function loadSourceProxies() {
274             var serviceProxy = serviceProxiesList[(sourceServiceProxy.serviceList)[0]];
275             var selectedServiceType = $scope.sourceServiceType['service-type'];
276             loadProxyInstances(sourceServiceProxy, selectedServiceType, serviceProxy);
277         }
278
279         function loadCollectorProxies() {
280             var serviceProxy = serviceProxiesList[(collectorServiceProxy.serviceList)[0]];
281             var selectedServiceType = $scope.collectorServiceType['service-type'];
282             loadProxyInstances(collectorServiceProxy, selectedServiceType, serviceProxy);
283         }
284
285         function loadProxyInstances(service, serviceType, serviceProxy) {
286             $scope[service.instanceListScopePropertyName] = null;
287             var configNodeTemplateFields = DataService.getPortMirroningConfigFields();
288             if (service.name === 'collectorInstanceName' && $scope.configurationByPolicy) {
289                 var configurationModel = DataService.getModelInfo(COMPONENT.VNF);
290                 AaiService.getPnfInstancesList(
291                     DataService.getGlobalCustomerId(),
292                     serviceType,
293                     serviceProxy.sourceModelUuid,
294                     serviceProxy.sourceModelInvariant,
295                     configNodeTemplateFields.lcpRegion.value,
296                     configurationModel.properties.equip_vendor,
297                     configurationModel.properties.equip_model
298                 )
299                     .then(function (response) {
300                         var results = response.results || [];
301                         $scope[service.instanceListScopePropertyName] = results;
302                         $scope[service.noResults] = (results.length === 0);
303                     })
304                     .catch(function (error) {
305                         $scope[service.noResults] = true;
306                         $log.error('No pnf instance found for ' + service.name, error);
307                     });
308             } else {
309                 AaiService.getVnfInstancesList(
310                     DataService.getGlobalCustomerId(),
311                     serviceType,
312                     serviceProxy.sourceModelUuid,
313                     serviceProxy.sourceModelInvariant,
314                     configNodeTemplateFields.lcpRegion.value
315                 )
316                     .then(function (response) {
317                         var results = response.results || [];
318                         $scope[service.instanceListScopePropertyName] = results;
319                         $scope[service.noResults] = (results.length === 0);
320                     })
321                     .catch(function (error) {
322                         $scope[service.noResults] = true;
323                         $log.error("No vnf instance found for " + service.name, error);
324                     });
325             }
326         }
327
328         function generateMetadata(service) {
329             const serviceProxy = serviceProxiesList[(service.serviceList)[0]];
330             $scope[service.name] = serviceProxy.name;
331
332             $scope[service.metadata] = [
333                 {"name" :"Name" ,"value" : serviceProxy.name},
334                 {"name" :"Version",value : serviceProxy.version},
335                 {"name" :"Description", value : serviceProxy.description},
336                 {"name" :"Type", value : serviceProxy.type},
337                 {"name" :"Invariant UUID", value : serviceProxy.invariantUuid},
338                 {"name" :"UUID", value : serviceProxy.uuid},
339                 {"name" :"Customization UUID", value : serviceProxy.customizationUuid},
340                 {"name" :"Source Model Uuid", value : serviceProxy.sourceModelUuid},
341                 {"name" :"Source Model Invariant", value : serviceProxy.sourceModelInvariant},
342                 {"name" :"Source Model Name", value : serviceProxy.sourceModelName}
343             ];
344         }
345
346         init();
347         $scope.$on('$routeChangeStart', function (event, next, current) {
348             if(next.$$route.originalPath!=="/addNetworkNode"){
349                 DataService.setPortMirroningConfigFields(null);
350             }
351         });
352     }]);
353
354