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