Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / mwtnCompare / mwtnCompare-module / src / main / resources / mwtnCompare / mwtnCompare.controller.js
1 /*
2  * Copyright (c) 2017 highstreet technologies GmbH and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 define(['app/mwtnCompare/mwtnCompare.module',
10         'app/mwtnCompare/mwtnCompare.services',
11         'app/mwtnCommons/mwtnCommons.services', 
12         'app/mwtnCompare/mwtnCompare.directives',
13         'app/mwtnCommons/bower_components/angular-ui-grid/ui-grid.min'], function(mwtnCompareApp) {
14
15   mwtnCompareApp.register.controller('mwtnCompareCtrl', ['$scope', '$rootScope', '$mwtnCompare', '$mwtnLog', 'orderByFilter', 'OnfNetworkElement', 'MicrowavePhysicalSection', 'MicrowaveSection',  
16                                                          function($scope, $rootScope, $mwtnCompare, $mwtnLog, orderBy, OnfNetworkElement, MicrowavePhysicalSection, MicrowaveSection) {
17
18     $rootScope.section_logo = 'src/app/mwtnCompare/images/mwtnCompare.png'; // Add your topbar logo location here such as 'assets/images/logo_topology.gif'
19
20     var rOnfNe;
21     var aOnfNe;
22     
23     $scope.status = {ne:false};
24     $scope.spinner = {ne:false};
25     $scope.separator = $mwtnCompare.separator; //' '
26     $scope.connectionStatus = 'disconnected';
27     $scope.oneATime = true;
28     $scope.match = {
29         numberOfLTPs: false,
30         numberOfLTPsStatus: 'unknown',
31         radioSignalIds: false,
32         radioSignalIdsStatus: 'unknown',
33         mapping: {},
34         addActualRadioSignalId: function(rsId) {
35           if (!this.actualRadioSignalIds) {
36             this.actualRadioSignalIds = [];
37           }
38           if (this.actualRadioSignalIds.contains(rsId)) {
39             return;
40           }
41           this.actualRadioSignalIds.push(rsId);
42           this.actualRadioSignalIds.sort();
43           this.checkRadioSignalIdsStatus();
44         },
45         clearActualNumberOfLtps: function() {
46           this.actualNumberOfLtps = undefined;
47           this.numberOfLTPs = false;
48           this.numberOfLTPsStatus = this.requiredNumberOfLtps;
49         },
50         clearActualRadioSignalIds: function() {
51           this.actualRadioSignalIds = undefined;
52           this.radioSignalIds = false;
53           this.radioSignalIdsStatus = this.requiredRadioSignalIds;
54         },
55         setRequiredNumberOfLtps: function(rNoLtps) {
56           this.requiredNumberOfLtps = rNoLtps;
57           this.checkNumberOfLTPs();
58         },
59         setRequiredRadioSignalIds: function(rRsIds) {
60           this.requiredRadioSignalIds = rRsIds.sort();
61           this.checkRadioSignalIdsStatus();
62         },
63         setActualNumberOfLtps: function(aNoLtps) {
64           this.actualNumberOfLtps = aNoLtps;
65           this.checkNumberOfLTPs();
66         },
67         setActualRadioSignalIds: function(aRsIds) {
68           this.actualRadioSignalIds = aRsIds.sort();
69           this.checkRadioSignalIdsStatus();
70         },
71         checkNumberOfLTPs: function() {
72           if (!this.requiredNumberOfLtps) {
73             this.numberOfLTPs = false;
74             this.numberOfLTPsStatus = 'unknown';
75           } else {
76             if (!this.actualNumberOfLtps) {
77               this.numberOfLTPs = false;
78               this.numberOfLTPsStatus = this.requiredNumberOfLtps;
79             } else {
80               if (this.requiredNumberOfLtps === this.actualNumberOfLtps) {
81                 this.numberOfLTPs = true;
82                 this.numberOfLTPsStatus = this.requiredNumberOfLtps;
83               } else {
84                 this.numberOfLTPs = false;
85                 this.numberOfLTPsStatus = ['required:', this.requiredNumberOfLtps, 'does not match actual:',this.actualNumberOfLtps].join(' ');
86               }
87             }
88           }
89         },
90         checkRadioSignalIdsStatus: function() {
91           if (!this.requiredRadioSignalIds) {
92             this.radioSignalIds = false;
93             this.radioSignalIdsStatus = 'unknown';
94           } else {
95             if (!this.actualRadioSignalIds) {
96               this.radioSignalIds = false;
97               this.radioSignalIdsStatus = this.requiredRadioSignalIds;
98             } else {
99               if (JSON.stringify(this.requiredRadioSignalIds) === JSON.stringify(this.actualRadioSignalIds)) {
100                 this.radioSignalIds = true;
101                 this.radioSignalIdsStatus = this.requiredRadioSignalIds;
102               } else {
103                 this.radioSignalIds = false;
104                 this.radioSignalIdsStatus = ['required:', this.requiredRadioSignalIds, 'does not match actual:',this.actualRadioSignalIds].join(' ');
105               }
106             }
107           }
108         }
109         
110     };
111     
112     $scope.schema = {initShowObjectCtrl:false};
113     $mwtnCompare.getSchema().then(function(data){
114       $scope.schema = data;
115     });
116     
117     var initNodeList = function(nodes){
118       $scope.neSelection = [];
119       if (nodes.length > 0) {
120         nodes.map(function(ne) {
121           if (ne._source.onfAirInterfaceRevision) {
122             $scope.neSelection.push({id:ne._id, revision:ne._source.onfAirInterfaceRevision});
123             
124           }
125         });
126         $scope.neSelection.sort(function(a, b){
127           if(a.id < b.id) return -1;
128           if(a.id > b.id) return 1;
129           return 0;
130         });
131       }
132     };
133     $scope.requiredNetworkElements = [];
134     $mwtnCompare.getRequiredNetworkElements(true).then(function(nodes){
135       $scope.requiredNetworkElements = nodes;
136       initNodeList(nodes);
137     }, function(error){
138       $scope.neSelection = [];
139       $scope.requiredNetworkElements = [];
140     });
141
142     $scope.collapseAll = function() {
143       // close all groups
144       Object.keys($scope.status).map(function(group){
145         $scope.status[group] = false;
146       });
147       Object.keys($scope.spinner).map(function(group){
148         $scope.spinner[group] = false;
149       });
150     };
151
152
153   // events
154     $scope.$watch('selection', function(neId, oldValue) {
155       if (neId && neId !== '' && neId !== oldValue) {
156         $scope.collapseAll();
157         $scope.connectionStatus = 'disconnected';
158         $scope.match.clearActualNumberOfLtps();
159         $scope.match.clearActualRadioSignalIds();
160         $mwtnCompare.getConnectionStatus(neId).then(function(connectionStatus){
161           $scope.connectionStatus = connectionStatus;
162          // get actual data
163           if ($scope.connectionStatus === 'connected') {
164             var key = 'ne';
165             var spec = {
166                 nodeId: $scope.selection,
167                 revision: $scope.requiredNetworkElement.onfAirInterfaceRevision,
168                 pacId: key,
169               };
170               $mwtnCompare.getPacParts(spec).then(function(success){
171                 updatePart(spec, success);
172                 $scope.spinner[key] = false;
173               }, function(error){
174                 updatePart(spec, error);
175                 $scope.spinner[key] = false;
176               });
177           }
178           
179         },function(error){
180           $scope.connectionStatus = 'disconnected';
181         });
182
183         $scope.requiredNetworkElements.map(function(rne){
184           console.warn(JSON.stringify(rne._id, neId, rne._id === neId));
185           if (rne._id === neId) {
186             $scope.requiredNetworkElement = rne._source;
187             rOnfNe = new OnfNetworkElement(rne._source['core-model:network-element']);
188             $scope.match.setRequiredNumberOfLtps(rOnfNe.getNumberOfLtps());
189             // required NE
190             var rMwpsList = rOnfNe.getLTPMwpsList().map(function(mwpsLtp){
191               var key = 'microwave-model:mw-air-interface-pac';
192               if ($scope.requiredNetworkElement.onfAirInterfaceRevision.contains('2016')) {
193                  key = 'MicrowaveModel-ObjectClasses-AirInterface:MW_AirInterface_Pac';
194               }
195               var rMwpsList = $scope.requiredNetworkElement[key].map(function(mwps){
196                 if (mwps.layerProtocol === mwpsLtp.getLayerProtocols()[0].getId()) {
197                   return new MicrowavePhysicalSection(mwps);
198                 }
199               });
200               
201               if (rMwpsList) {
202                 return rMwpsList.clean(undefined)[0];
203               } else {
204                 return {};
205               }
206             });
207             
208             var rRadioSignalIds = rMwpsList.map(function(rMwps){
209              return rMwps.getRadioSignalId();
210             });
211             $scope.match.setRequiredRadioSignalIds(rRadioSignalIds);
212           }
213         });
214       }
215     });
216
217     var getArrayValueToHtml = function(array) {
218       if (array === undefined) {
219         return '';
220       }
221       if (array.length > 0 && ($mwtnCompare.getType(array[0]) === 'object' || $mwtnCompare.getType(array[0]) === 'array') ) {
222         var converted = array.map(function(item){
223           if ($mwtnCompare.getType(item) === 'object' && Object.keys(item).length === 2 ) {
224             return [item[Object.keys(item)[0]], item[Object.keys(item)[1]]].join(':');            
225           } else {
226             return JSON.stringify(item);            
227           }
228         });
229         return converted.join('<br/>');
230       } else {
231         return array.join('<br/>');
232       }
233     };
234     
235     var getCompares = function(obj, actualData) {
236       var compares = [];
237
238       for (var labelId in obj) {
239         var missingActualValueLabelId = 'not connected';
240         var match = false;
241
242         switch (labelId) {
243         case 'compares':
244           break;
245         case 'open':
246           break;
247 //        case 'nameList':
248 //            var requiredName = obj[labelId][0].value;
249 //            var actualName = '';
250 //            if (actualData) {
251 //              actualName = actualData[labelId][0].value;
252 //            }
253 //            match = ((requiredName === '' || actualName === '') || (requiredName === actualName));
254 //            // console.log('NAME', requiredName, actualName, match);
255 //            compares.push({
256 //              labelId : 'NE name',
257 //              requiredValue : requiredName,
258 //              actualValue : actualName,
259 //              match : match,
260 //              missingActualValueLabelId : missingActualValueLabelId,
261 ////              unit : $scope.schema[labelId].unit,
262 ////              description : $scope.schema[labelId].description,
263 //              showDescriptions : false
264 //            });
265 //            break;
266           case '_ltpRefList':
267             var requiredLtpLength = obj[labelId].length;
268             var actualLtpLength = '';
269             if (actualData) {
270               actualLtpLength = actualData[labelId].length;
271             }
272             match = ((requiredLtpLength === '' || actualLtpLength === '') || (requiredLtpLength === actualLtpLength));
273             compares.push({
274               labelId : 'numberOfLTPs',
275               requiredValue : requiredLtpLength,
276               actualValue : actualLtpLength,
277               match : match,
278               missingActualValueLabelId : missingActualValueLabelId,
279               unit : $scope.schema[labelId].unit,
280               order: $scope.schema[labelId]['order-number'],
281               description :  $scope.schema[labelId].description,
282               showDescriptions : false
283             });
284             break;
285           default:
286             if (labelId === 'installedCapacity') {
287               missingActualValueLabelId = '<pure planning value>';
288             }
289             if (labelId === 'timeSlotIDList') {
290               var requiredTimeSlotIDListLength = obj[labelId].length;
291               var actualTimeSlotIDListLength = '';
292               if (actualData) {
293                 actualTimeSlotIDListLength = actualData[labelId].length;
294               }
295               compares.push({
296                 labelId : 'currentNumberOfTimeSlots',
297                 requiredValue : requiredTimeSlotIDListLength,
298                 actualValue : actualTimeSlotIDListLength,
299                 match : true,
300                 missingActualValueLabelId : missingActualValueLabelId,
301                 unit : $scope.schema[labelId].unit,
302                 order: $scope.schema[labelId]['order-number'],
303                 description :  $scope.schema[labelId].description,
304                 showDescriptions : false
305               });
306             }
307             
308             switch($mwtnCompare.getType(obj[labelId])) {
309             case 'object':
310               if (actualData) {
311                 var withActual = getCompares(obj[labelId], actualData[labelId]); 
312                 compares = compares.concat(withActual);
313               } else {
314                 var withoutActual = getCompares(obj[labelId]); 
315                 compares = compares.concat(withoutActual);
316               }
317               break;
318             case 'array':
319               var requiredDataValue = getArrayValueToHtml(obj[labelId]);
320               var actualDataValue = '';
321               if (actualData) {
322                 actualDataValue = getArrayValueToHtml(actualData[labelId]);
323               }
324               match = ((requiredDataValue === '' || actualDataValue === '') || (requiredDataValue === actualDataValue));
325               compares.push({
326                 labelId : labelId,
327                 requiredValue : requiredDataValue,
328                 actualValue : actualDataValue,
329                 match : match,
330                 missingActualValueLabelId : missingActualValueLabelId,
331                 unit : $scope.schema[labelId].unit,
332                 order: $scope.schema[labelId]['order-number'],
333                 description :  $scope.schema[labelId].description,
334                 showDescriptions : false
335               });
336               break;
337             default:
338               var requiredValue = obj[labelId];
339               var actualValue = actualData ? actualData[labelId] : '';
340 //              // console.log('match');
341 //              // console.log('match1', requiredValue, (requiredValue === ''));
342 //              // console.log('match2', actualValue, (actualValue === ''));
343 //              // console.log('match3', requiredValue, actualValue, (requiredValue === actualValue));
344 //              // console.log('match4', (requiredValue === '' || actualValue === '') || (requiredValue === actualValue));
345               match = (requiredValue === '' || actualValue === '') || (requiredValue === actualValue);
346               compares.push({
347                 labelId : labelId,
348                 requiredValue : requiredValue,
349                 actualValue : actualValue,
350                 match : match,
351                 missingActualValueLabelId : missingActualValueLabelId,
352                 unit : $scope.schema[labelId].unit,
353                 order: $scope.schema[labelId]['order-number'],
354                 description :  $scope.schema[labelId].description,
355                 showDescriptions : false
356               });
357             }
358           }
359       }
360       return orderBy(compares, 'order', false);
361     };
362     
363     var updateNe = function(data) {
364
365       var rne = $scope.requiredNetworkElement;
366       if (!data) {
367         rne['core-model:network-element'].compares = getCompares(rne['core-model:network-element']);
368         return;        
369       }
370       rne['core-model:network-element'].compares = getCompares(rne['core-model:network-element'], data['core-model:network-element']);
371       console.error(JSON.stringify(data));
372       aOnfNe = new OnfNetworkElement(data['core-model:network-element']);
373       $scope.match.setActualNumberOfLtps(aOnfNe.getNumberOfLtps());
374       var aMwpsList = aOnfNe.getLTPMwpsList().map(function(mwpsLtp){
375         
376         var spec = {
377           nodeId: $scope.selection,
378           revision: $scope.requiredNetworkElement.onfAirInterfaceRevision,
379           pacId: 'microwave-model:mw-air-interface-pac',
380           layerProtocolId: mwpsLtp.getLayerProtocols()[0].getId(),
381           partId: 'configuration'
382         };
383
384         $mwtnCompare.getPacParts(spec).then(function(success){
385           updatePart(spec, success);
386         }, function(error){
387           updatePart(spec, error);
388         });
389         return mwpsLtp._lpList[0].uuid;
390       });
391       // console.log(aMwpsList)
392       var mwClient = rne['microwave-model:mw-ethernet-container-pac'][0];
393       $scope.match.mapping[mwClient.layerProtocol] = aOnfNe.getLTPEthCtpList()[0].getLayerProtocols()[0].getId();
394     };
395
396     var addShowDescriptionEvent = function(obj) {
397       $scope.$watch(function() {
398         return obj.showDescriptions;
399       }, function(newValue, oldValue) {
400         if (newValue !== oldValue) {
401           obj.compares.map(function(compare){
402             compare.showDescriptions = newValue;
403           });
404         }
405       });
406     };
407
408     var updateAirInterface = function(spec, data) {
409       if (!data) {
410         $scope.requiredNetworkElement['microwave-model:mw-air-interface-pac'].map(function(mwps){
411           mwps.compares = getCompares(mwps.airInterfaceConfiguration);
412           addShowDescriptionEvent(mwps);
413         });
414       } else {
415         var actual = new MicrowavePhysicalSection(data);
416         $scope.match.addActualRadioSignalId(actual.getRadioSignalId());
417         // console.log(actual.getRadioSignalId());
418         $scope.requiredNetworkElement['microwave-model:mw-air-interface-pac'].map(function(mwps){
419           var required = new MicrowavePhysicalSection(mwps);
420           if (actual.getRadioSignalId() === required.getRadioSignalId()) {
421             $scope.match.mapping[required.getLayerProtocolId()] = actual.getLayerProtocolId();
422             var actualData = data.airInterfaceConfiguration;
423             mwps.compares = getCompares(mwps.airInterfaceConfiguration, actualData);
424             addShowDescriptionEvent(mwps);
425   
426             // MWS mapping
427             var rMws = rOnfNe.getClientLtpIds(required.getLayerProtocolId());
428             var aMws = aOnfNe.getClientLtpIds(actual.getLayerProtocolId());
429             // In PoC just a 1:1 relation between MWPS and MWS
430             $scope.match.mapping[rOnfNe.getLpByLtpRef(rMws[0]).uuid] = aOnfNe.getLpByLtpRef(aMws[0]).uuid;
431           }
432         });
433      }
434     };
435
436     var updateStructure = function(spec, data) {
437       if (!data) {
438         $scope.requiredNetworkElement.MW_PureEthernetStructure_Pac.map(function(mws){
439           mws.compares = getCompares(mws.pureEthernetStructureConfiguration);
440           addShowDescriptionEvent(mws);
441         });
442       } else {
443         $scope.requiredNetworkElement.MW_PureEthernetStructure_Pac.map(function(mws){
444           if ($scope.match.mapping[mws.layerProtocol] === data.layerProtocol) {
445             var actualData = data.pureEthernetStructureConfiguration;
446             mws.compares = getCompares(mws.pureEthernetStructureConfiguration, actualData);
447             addShowDescriptionEvent(mws);
448           }
449         });
450       }
451     };
452
453     var updateContainer = function(spec, data) {
454       // there is only one container in PoCs
455       var mwClient = $scope.requiredNetworkElement.MW_EthernetContainer_Pac[0];
456       if (!data) {
457         mwClient.compares = getCompares(mwClient.ethernetContainerConfiguration);
458         addShowDescriptionEvent(mwClient);
459       } else {
460         var actualData = data.ethernetContainerConfiguration;
461         mwClient.compares = getCompares(mwClient.ethernetContainerConfiguration, actualData);
462         addShowDescriptionEvent(mwClient);
463       }
464     };
465
466     var updatePart = function(spec, data) {
467       switch (spec.pacId) {
468       case 'ne':
469         updateNe(data);
470         break;
471       case 'airinterface':
472         // console.log(JSON.stringify(spec, JSON.stringify(data)));
473         updateAirInterface(spec, data);
474         break;
475       case 'structure':
476         // console.log(JSON.stringify(data));
477         updateStructure(spec, data);
478         break;
479       case 'container':
480         // console.log(JSON.stringify(data));
481         updateContainer(spec, data);
482         break;
483       }
484     };
485
486     $scope.$watch('status', function(status, oldValue) {
487       Object.keys(status).map(function(key){
488         if ($scope.selection && status[key] && status[key] !== oldValue[key]) {
489           var info = key.split($scope.separator);
490           var spec = {
491             nodeId: $scope.selection,
492             revision: $scope.requiredNetworkElement.onfAirInterfaceRevision,
493             pacId: info[0],
494             requiredLayerProtocolId: info[1],
495             layerProtocolId: $scope.match.mapping[info[1]],
496             partId: 'Configuration'
497           };
498
499           if ($scope.connectionStatus !== 'connected') {
500             updatePart(spec, undefined);
501             return;
502           }
503           if (info.length > 1 && !$scope.match.mapping[info[1]]) {
504             updatePart(spec, undefined);
505             return;
506           } 
507
508           $scope.spinner[key] = true;
509           $mwtnCompare.getPacParts(spec).then(function(success){
510             updatePart(spec, success);
511             $scope.spinner[key] = false;
512           }, function(error){
513             updatePart(spec, error);
514             $scope.spinner[key] = false;
515           });
516
517         }
518       });   
519     }, true);
520
521     
522   
523   }]);
524
525
526 });