Add drop down Source subscriber name on the PMC instantiation form
[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 - 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 /**
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         $scope.isSourceSubscriberEnabled = function() {
64             return featureFlags.isOn(COMPONENT.FEATURE_FLAGS.FLAG_2006_PORT_MIRRORING_LET_SELECTING_SOURCE_SUBSCRIBER)
65                 && !$scope.configurationByPolicy;
66         };
67
68         function init() {
69             if ($scope.isSourceSubscriberEnabled())  {
70                 loadSourceSubscriber();
71             }
72             loadServiceTypes(DataService.getGlobalCustomerId(), handleGetSourceAndCollectorServiceTypesResponse);
73             generateMetadata(sourceServiceProxy);
74             generateMetadata(collectorServiceProxy);
75
76         }
77
78         function loadSourceSubscriber() {
79             AaiService.getSubList(function (response) {
80                 $scope.sourceSubscribers = response;
81                 $scope.sourceSubscriber = response.find((subscriber)=>{
82                     return subscriber.globalCustomerId === DataService.getGlobalCustomerId();
83                 });
84             }, function (response) { // failure
85                 $scope.sourceSubscribers = [];
86             });
87         }
88
89         function getSourceSubscriberId() {
90             return $scope.isSourceSubscriberEnabled()
91                 ? $scope.sourceSubscriber && $scope.sourceSubscriber.globalCustomerId
92                 : DataService.getGlobalCustomerId()
93         }
94
95         function setDefaultCollectorServiceType() {
96             const configServiceType = DataService.getServiceType();
97             $scope.collectorServiceType = mustFind($scope.serviceTypes, {"service-type": configServiceType});
98             loadCollectorProxies();
99         }
100
101         function handleGetSourceAndCollectorServiceTypesResponse(response) {
102             handleGetSourceServiceTypesResponse(response)
103             handleGetCollectorServiceTypesResponse(response)
104         }
105
106         function handleGetCollectorServiceTypesResponse(response) {
107             $scope.serviceTypes = response.data;
108             setDefaultCollectorServiceType();
109         }
110
111         function handleGetSourceServiceTypesResponse(response) {
112             $scope.sourceServiceTypes = response.data;
113         }
114
115         var handleGetParametersResponse = function(parameters) {
116             $scope.serviceMetadataFields = parameters.summaryList;
117             $scope.nodeTemplateFields =  DataService.getPortMirroningConfigFields();
118         };
119
120         var mustFind = function (collection, predicate) {
121             const result = _.find(collection, predicate);
122             const description = "result for find " + JSON.stringify(predicate);
123             UtilityService.checkUndefined(description, result);
124             $log.debug(description, result);
125             return result;
126         };
127
128
129         $scope.back = function()  {
130             $window.history.back();
131         };
132
133         $scope.onSourceSubscriberChange = function(selectedSubscriber) {
134             $scope.sourceSubscriber = selectedSubscriber
135             loadServiceTypes(selectedSubscriber.globalCustomerId, handleGetSourceServiceTypesResponse)
136         }
137
138         function loadServiceTypes(subscriberId, handleGetServiceTypesResponseCallback) {
139             AaiService.getSubscriberServiceTypes(subscriberId)
140                 .then(handleGetServiceTypesResponseCallback)
141                 .catch(function (error) {
142                     $log.error(error);
143                 });
144         }
145
146         var modalInstance;
147
148         $scope.create = function()  {
149             $scope.disableCreate= true;
150             var portMirroringConfigFields = DataService.getPortMirroningConfigFields();
151             portMirroringConfigFields.sourceInstance = mustFind($scope.sourceInstanceList, {'id': $scope.sourceInstance});
152             portMirroringConfigFields.destinationInstance = mustFind($scope.collectorInstanceList, {'id': $scope.collectorInstance});
153
154             var selectedVnfsList;
155
156             if ($scope.configurationByPolicy) {
157                 selectedVnfsList = [
158                     portMirroringConfigFields.sourceInstance.properties
159                 ];
160             } else {
161                 selectedVnfsList = [
162                     portMirroringConfigFields.sourceInstance.properties,
163                     portMirroringConfigFields.destinationInstance.properties
164                 ];
165             }
166
167             AaiService.getVnfVersionsByInvariantId(
168                 selectedVnfsList.map(function(x) {
169                     return UtilityService.checkUndefined("model-invariant-id", x['model-invariant-id']);
170                 })
171                 )
172                 .then(function (response) {
173                     $log.debug("getVnfVersionsByInvariantId: response", response);
174
175                     selectedVnfsList
176                         .map(function (inOutProperties) {
177                             const model = mustFind(response.data.model, {'model-invariant-id': inOutProperties['model-invariant-id']});
178
179                             const modelVer = mustFind(model["model-vers"]["model-ver"], {'model-version-id': inOutProperties['model-version-id']});
180
181                             inOutProperties['model-version'] = modelVer['model-version'];
182                             inOutProperties['model-name'] = modelVer['model-name'];
183                             UtilityService.checkUndefined("model-version", modelVer);
184                         });
185                 })
186
187                 .then(function () {
188                     var requestParams = {
189                         configurationModelInfo: DataService.getModelInfo(COMPONENT.VNF),
190                         relatedTopModelsInfo: DataService.getModelInfo(COMPONENT.SERVICE),
191                         portMirroringConfigFields:portMirroringConfigFields,
192                         attuuid: DataService.getLoggedInUserId(),
193                         topServiceInstanceId: DataService.getServiceInstanceId(),
194                         configurationByPolicy: $scope.configurationByPolicy,
195                         callbackFunction: updateViewCallbackFunction
196                     };
197
198                     modalInstance = $uibModal.open({
199                         templateUrl: 'app/vid/scripts/modals/mso-commit/mso-commit.html',
200                         controller : "msoCommitModalController",
201                         backdrop: true,
202                         resolve: {
203                             msoType: function () {
204                                 return COMPONENT.MSO_CREATE_CONFIGURATION_REQ;
205                             },
206                             requestParams: function () {
207                                 return requestParams;
208                             },
209                             configuration: function () {
210                                 return null;
211                             }
212                         }
213                     });
214                 })
215                 .catch(function (error) {
216                     $log.error("error while configuration create", error);
217                     $scope.disableCreate= false;
218                 });
219         };
220
221         $scope.openMetadataModal = function(name) {
222             $scope.selectedMetadata = $scope[name];
223             modalInstance = $uibModal.open({
224                 templateUrl: 'app/vid/scripts/modals/service-metadata/service-metadata.html',
225                 backdrop: false,
226                 scope : $scope,
227                 resolve: {
228                 }
229             });
230         };
231
232         $scope.cancel = function()  {
233             modalInstance.dismiss('cancel');
234         };
235
236         var updateViewCallbackFunction = function(response) {
237             $scope.callbackResults = "";
238             var color = FIELD.ID.COLOR_NONE;
239             $scope.callbackStyle = {
240                 "background-color" : color
241             };
242
243             /*
244              * This 1/2 delay was only added to visually highlight the status
245              * change. Probably not needed in the real application code.
246              */
247             $timeout(function() {
248                 $scope.callbackResults = UtilityService.getCurrentTime()
249                     + FIELD.STATUS.IS_SUCCESSFUL + response.isSuccessful;
250                 if (response.isSuccessful) {
251                     color = FIELD.ID.COLOR_8F8;
252                     $window.history.go(-2);
253                 } else {
254                     $scope.disableCreate=false;
255                     color = FIELD.ID.COLOR_F88;
256                 }
257                 $scope.callbackStyle = {
258                     "background-color" : color
259                 };
260             }, 500);
261         };
262
263         CreationService.initializeComponent(COMPONENT.VNF);
264         CreationService.initializeComponent(COMPONENT.SERVICE);
265         CreationService.getParameters(handleGetParametersResponse);
266
267         var sourceServiceProxies = DataService.getSourceServiceProxies();
268         var collectorServiceProxies = DataService.getCollectorServiceProxies();
269         var serviceProxiesList = DataService.getServiceProxies();
270
271         var sourceServiceProxy = {
272             serviceList: sourceServiceProxies,
273             instanceListScopePropertyName: "sourceInstanceList",
274             name: "sourceInstanceName",
275             metadata: "sourceMetadata",
276             noResults: "sourceNoResults"
277         };
278
279         var collectorServiceProxy = {
280             serviceList: collectorServiceProxies,
281             instanceListScopePropertyName: "collectorInstanceList",
282             name: "collectorInstanceName",
283             metadata: "collectorMetadata",
284             noResults: "collectorNoResults"
285         };
286
287         $scope.onSourceServiceTypeSelected = function() {
288             clearSourceProxySelection();
289             loadSourceProxies();
290         };
291
292         $scope.onCollectorServiceTypeSelected = function() {
293             clearCollectorProxySelection();
294             loadCollectorProxies();
295         };
296
297         $scope.shouldLetSelectingCollectorType = function() {
298             return $scope.collectorType === 'vnf' || featureFlags.isOn(COMPONENT.FEATURE_FLAGS.FLAG_1810_CR_LET_SELECTING_COLLECTOR_TYPE_UNCONDITIONALLY);
299         };
300
301         function clearSourceProxySelection() {
302             $scope.sourceInstance = undefined;
303         }
304
305         function clearCollectorProxySelection() {
306             $scope.collectorInstance = undefined;
307         }
308
309         function loadSourceProxies() {
310             var serviceProxy = serviceProxiesList[(sourceServiceProxy.serviceList)[0]];
311             var selectedServiceType = $scope.sourceServiceType['service-type'];
312             loadProxyInstances(sourceServiceProxy, selectedServiceType, serviceProxy);
313         }
314
315         function loadCollectorProxies() {
316             var serviceProxy = serviceProxiesList[(collectorServiceProxy.serviceList)[0]];
317             var selectedServiceType = $scope.collectorServiceType['service-type'];
318             loadProxyInstances(collectorServiceProxy, selectedServiceType, serviceProxy);
319         }
320
321         function loadProxyInstances(service, serviceType, serviceProxy) {
322             $scope[service.instanceListScopePropertyName] = null;
323             var configNodeTemplateFields = DataService.getPortMirroningConfigFields();
324             if (service.name === 'collectorInstanceName' && $scope.configurationByPolicy) {
325                 var configurationModel = DataService.getModelInfo(COMPONENT.VNF);
326                 AaiService.getPnfInstancesList(
327                     getSourceSubscriberId(),
328                     serviceType,
329                     serviceProxy.sourceModelUuid,
330                     serviceProxy.sourceModelInvariant,
331                     configNodeTemplateFields.cloudRegionId,
332                     configurationModel.properties.equip_vendor,
333                     configurationModel.properties.equip_model
334                 )
335                     .then(function (response) {
336                         var results = response.results || [];
337                         $scope[service.instanceListScopePropertyName] = results;
338                         $scope[service.noResults] = (results.length === 0);
339                     })
340                     .catch(function (error) {
341                         $scope[service.noResults] = true;
342                         $log.error('No pnf instance found for ' + service.name, error);
343                     });
344             } else {
345                 AaiService.getVnfInstancesList(
346                     getSourceSubscriberId(),
347                     serviceType,
348                     serviceProxy.sourceModelUuid,
349                     serviceProxy.sourceModelInvariant,
350                     configNodeTemplateFields.cloudRegionId
351                 )
352                     .then(function (response) {
353                         var results = response.results || [];
354                         $scope[service.instanceListScopePropertyName] = results;
355                         $scope[service.noResults] = (results.length === 0);
356                     })
357                     .catch(function (error) {
358                         $scope[service.noResults] = true;
359                         $log.error("No vnf instance found for " + service.name, error);
360                     });
361             }
362         }
363
364         function generateMetadata(service) {
365             const serviceProxy = serviceProxiesList[(service.serviceList)[0]];
366             $scope[service.name] = serviceProxy.name;
367
368             $scope[service.metadata] = [
369                 {"name" :"Name" ,"value" : serviceProxy.name},
370                 {"name" :"Version",value : serviceProxy.version},
371                 {"name" :"Description", value : serviceProxy.description},
372                 {"name" :"Type", value : serviceProxy.type},
373                 {"name" :"Invariant UUID", value : serviceProxy.invariantUuid},
374                 {"name" :"UUID", value : serviceProxy.uuid},
375                 {"name" :"Customization UUID", value : serviceProxy.customizationUuid},
376                 {"name" :"Source Model Uuid", value : serviceProxy.sourceModelUuid},
377                 {"name" :"Source Model Invariant", value : serviceProxy.sourceModelInvariant},
378                 {"name" :"Source Model Name", value : serviceProxy.sourceModelName}
379             ];
380         }
381
382         init();
383         $scope.$on('$routeChangeStart', function (event, next, current) {
384             if(next.$$route.originalPath!=="/addNetworkNode"){
385                 DataService.setPortMirroningConfigFields(null);
386             }
387         });
388     }]);
389
390