org.onap migration
[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                         }
168                     });
169                 })
170                 .catch(function (error) {
171                     $log.error("error while configuration create", error);
172                     $scope.disableCreate= false;
173                 });
174         };
175
176         $scope.openMetadataModal = function(name) {
177             $scope.selectedMetadata = $scope[name];
178             modalInstance = $uibModal.open({
179                 templateUrl: 'app/vid/scripts/modals/service-metadata/service-metadata.html',
180                 backdrop: false,
181                 scope : $scope,
182                 resolve: {
183                 }
184             });
185         };
186
187         $scope.cancel = function()  {
188             modalInstance.dismiss('cancel');
189         };
190
191         var updateViewCallbackFunction = function(response) {
192             $scope.callbackResults = "";
193             var color = FIELD.ID.COLOR_NONE;
194             $scope.callbackStyle = {
195                 "background-color" : color
196             };
197
198             /*
199              * This 1/2 delay was only added to visually highlight the status
200              * change. Probably not needed in the real application code.
201              */
202             $timeout(function() {
203                 $scope.callbackResults = UtilityService.getCurrentTime()
204                     + FIELD.STATUS.IS_SUCCESSFUL + response.isSuccessful;
205                 if (response.isSuccessful) {
206                     color = FIELD.ID.COLOR_8F8;
207                     $window.history.go(-2);
208                 } else {
209                     $scope.disableCreate=false;
210                     color = FIELD.ID.COLOR_F88;
211                 }
212                 $scope.callbackStyle = {
213                     "background-color" : color
214                 };
215             }, 500);
216         };
217
218         CreationService.initializeComponent(COMPONENT.VNF);
219         CreationService.initializeComponent(COMPONENT.SERVICE);
220         CreationService.getParameters(handleGetParametersResponse);
221
222         var sourceServiceProxies = DataService.getSourceServiceProxies();
223         var collectorServiceProxies = DataService.getCollectorServiceProxies();
224         var serviceProxiesList = DataService.getServiceProxies();
225
226         var sourceServiceProxy = {
227             serviceList: sourceServiceProxies,
228             instanceListScopePropertyName: "sourceInstanceList",
229             name: "sourceInstanceName",
230             metadata: "sourceMetadata",
231             noResults: "sourceNoResults"
232         };
233
234         var collectorServiceProxy = {
235             serviceList: collectorServiceProxies,
236             instanceListScopePropertyName: "collectorInstanceList",
237             name: "collectorInstanceName",
238             metadata: "collectorMetadata",
239             noResults: "collectorNoResults"
240         };
241
242         $scope.onSourceServiceTypeSelected = function() {
243             clearSourceProxySelection();
244             loadSourceProxies();
245         };
246
247         $scope.onCollectorServiceTypeSelected = function() {
248             clearCollectorProxySelection();
249             loadCollectorProxies();
250         };
251
252         function clearSourceProxySelection() {
253             $scope.sourceInstance = undefined;
254         }
255
256         function clearCollectorProxySelection() {
257             $scope.collectorInstance = undefined;
258         }
259
260         function loadSourceProxies() {
261             var serviceProxy = serviceProxiesList[(sourceServiceProxy.serviceList)[0]];
262             var selectedServiceType = $scope.sourceServiceType['service-type'];
263             loadProxyInstances(sourceServiceProxy, selectedServiceType, serviceProxy);
264         }
265
266         function loadCollectorProxies() {
267             var serviceProxy = serviceProxiesList[(collectorServiceProxy.serviceList)[0]];
268             var selectedServiceType = $scope.collectorServiceType['service-type'];
269             loadProxyInstances(collectorServiceProxy, selectedServiceType, serviceProxy);
270         }
271
272         function loadProxyInstances(service, serviceType, serviceProxy) {
273             $scope[service.instanceListScopePropertyName] = null;
274             // $scope.collectorType = $scope.configurationByPolicy ? 'pnf' : 'vnf';
275             var configNodeTemplateFields = DataService.getPortMirroningConfigFields();
276             if (service.name == "collectorInstanceName" && $scope.configurationByPolicy) {
277                 var configurationModel = DataService.getModelInfo(COMPONENT.VNF);
278                 AaiService.getPnfInstancesList(
279                     DataService.getGlobalCustomerId(),
280                     serviceType,
281                     serviceProxy.sourceModelUuid,
282                     serviceProxy.sourceModelInvariant,
283                     configNodeTemplateFields.lcpRegion.value,
284                     configurationModel.properties.equip_vendor,
285                     configurationModel.properties.equip_model
286                 )
287                     .then(function (response) {
288                         var results = response.results || [];
289                         $scope[service.instanceListScopePropertyName] = results;
290                         $scope[service.noResults] = (results.length === 0);
291                     })
292                     .catch(function (error) {
293                         $scope[service.noResults] = true;
294                         $log.error("No pnf instance found for " + service.name, error);
295                     });
296             } else {
297                 AaiService.getVnfInstancesList(
298                     DataService.getGlobalCustomerId(),
299                     serviceType,
300                     serviceProxy.sourceModelUuid,
301                     serviceProxy.sourceModelInvariant,
302                     configNodeTemplateFields.lcpRegion.value
303                 )
304                     .then(function (response) {
305                         var results = response.results || [];
306                         $scope[service.instanceListScopePropertyName] = results;
307                         $scope[service.noResults] = (results.length === 0);
308                     })
309                     .catch(function (error) {
310                         $scope[service.noResults] = true;
311                         $log.error("No vnf instance found for " + service.name, error);
312                     });
313             }
314         }
315
316         function generateMetadata(service) {
317             const serviceProxy = serviceProxiesList[(service.serviceList)[0]];
318             $scope[service.name] = serviceProxy.name;
319
320             $scope[service.metadata] = [
321                 {"name" :"Name" ,"value" : serviceProxy.name},
322                 {"name" :"Version",value : serviceProxy.version},
323                 {"name" :"Description", value : serviceProxy.description},
324                 {"name" :"Type", value : serviceProxy.type},
325                 {"name" :"Invariant UUID", value : serviceProxy.invariantUuid},
326                 {"name" :"UUID", value : serviceProxy.uuid},
327                 {"name" :"Customization UUID", value : serviceProxy.customizationUuid},
328                 {"name" :"Source Model Uuid", value : serviceProxy.sourceModelUuid},
329                 {"name" :"Source Model Invariant", value : serviceProxy.sourceModelInvariant},
330                 {"name" :"Source Model Name", value : serviceProxy.sourceModelName}
331             ];
332         }
333
334         init();
335         $scope.$on('$routeChangeStart', function (event, next, current) {
336             if(next.$$route.originalPath!=="/addNetworkNode"){
337                 DataService.setPortMirroningConfigFields(null);
338             }
339         });
340     }]);
341
342