Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / ux / mwtnFault / mwtnFault-module / src / main / resources / mwtnFault / mwtnFault.directives.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/mwtnCommons/mwtnCommons.module'], function(mwtnCommonsApp) {
10   mwtnCommonsApp.register.directive('alarmStatus', function() {
11     return {
12       restrict : 'E',
13       templateUrl : 'src/app/mwtnFault/templates/alarmStatus.tpl.html',
14       controller :  ['$scope', '$mwtnCommons', '$mwtnDatabase', '$timeout', function($scope, $mwtnCommons, $mwtnDatabase, $timeout){
15         
16         $scope.link = '#/pnfFault/';
17           
18         $scope.nodeCount = 0;
19           
20         $scope.alarmStatus = {
21             Critical:0,
22             Major:0,
23             Minor:0,
24             Warning:0,
25         };
26         
27         $scope.getAlarmStatusSum = function(){
28           var sum = 0;
29           Object.keys($scope.alarmStatus).map(function(severity) {
30             sum = sum + $scope.alarmStatus[severity];
31           });
32           return sum;
33         };
34
35         // Update: request the number of alarms in current alarm list per severity
36         var update = function() {
37           $mwtnCommons.getMountPoints().then(function(mountpoints) {
38             $scope.nodeCount = mountpoints.filter(function(mountpoint) {
39               return mountpoint['netconf-node-topology:connection-status'] === 'connected';
40             }).length;
41           });
42           var functionId = 'sdnevents';
43           var docType = 'faultcurrent';
44           var aggregations = {
45             "size":0,
46             "aggregations": {
47               "severity": {
48                 "terms": {
49                   "field": "faultCurrent.severity"
50                 }
51               }
52             }
53           };
54           $mwtnDatabase.getAggregations(functionId, docType, aggregations).then(function (success) {
55             var found = success.data.aggregations['severity'].buckets.map(function(bucket){
56               $scope.alarmStatus[bucket.key] = bucket.doc_count;
57               return bucket.key;
58             });
59             Object.keys($scope.alarmStatus).map(function(key){
60               if (!found.contains(key)) {
61                 $scope.alarmStatus[key] = 0;
62               }
63             });
64           }, function (error) {
65             console.error(error);
66             $scope.alarmStatus = {
67                 Critical:0,
68                 Major:0,
69                 Minor:0,
70                 Warning:0,
71             };
72           });
73
74
75           // Object.keys($scope.alarmStatus).map(function(severity) {
76           //   // usage of the ElasticSearch Count API
77           //   $mwtnDatabase.getBase('sdnevents').then(function(success) {
78           //     var databaseRequest = {
79           //       base : success.base,
80           //       method : 'POST',
81           //       command: '_count',
82           //       index: success.index,
83           //       docType: 'faultcurrent',
84           //       query: {
85           //         match: {
86           //           'faultCurrent.severity': severity
87           //         }
88           //       }
89           //     };
90           //     $mwtnDatabase.genericRequest(databaseRequest).then(function(success){
91           //       $scope.alarmStatus[severity] = success.data.count;
92           //     }, function(error){
93           //       console.error('severity', severity, error);
94           //     });
95           //   }, function(error) {
96           //     console.error('severity', severity, error);
97           //   });
98           // });
99         };
100         update();
101         
102         var listenToNotifications = function() {
103           $mwtnCommons.getMwtnWebSocketUrl().then(function(success){
104             try {
105               var notificationSocket = new WebSocket(success);
106
107               notificationSocket.onmessage = function(event) {
108                 // we process our received event here
109                 if (typeof event.data === 'string') {
110                   // console.log('Client Received:\n', event.data);
111                   $mwtnCommons.formatData(event).then(function(formated) {
112                     switch (formated.notifType) {
113                     case 'ProblemNotification':
114                       $timeout(function(){update();}, 500);
115                       break;
116                     case 'AttributeValueChangedNotification':
117                     case 'ObjectCreationNotification':
118                     case 'ObjectDeletionNotification':
119                       // ignore
120                       break;
121                     default:
122                       console.error('Missing implementation for', formated.notifType);
123                     }
124                   }, function(error) {
125                     // do nothing
126                   });
127                 }
128               };
129
130               notificationSocket.onerror = function(error) {
131                 console.log("Socket error: " + error);
132               };
133
134               notificationSocket.onopen = function(event) {
135                 console.log("Socket connection opened.");
136                 function subscribe() {
137                   if (notificationSocket.readyState === notificationSocket.OPEN) {
138                     var data = {
139                       'data' : 'scopes',
140                       'scopes' : [ "ProblemNotification" ]
141                     };
142                     notificationSocket.send(JSON.stringify(data));
143                   }
144                 }
145                 subscribe();
146               };
147
148               notificationSocket.onclose = function(event) {
149                 console.log("Socket connection closed.");
150               };
151             } catch (e) {
152               console.error("Error when creating WebSocket.\n" + e);
153             }
154           }, function(error){
155             console.error("Error when creating WebSocket.\n" + error);
156           });
157         };
158         listenToNotifications();
159       }]
160     };
161   });
162
163 });