Merge "Add nemo-impl test file"
[nemo.git] / nemo-ui / src / main / resources / nemo / nemo.services.js
1 /*
2  * Copyright (c) 2015 Huawei, Inc 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 define(['app/nemo/nemo.module'], function(nemo) {
9   nemo.register.factory('NemoRestangular', function(Restangular, ENV) {
10     var NemoRestangular = Restangular.withConfig(function(RestangularConfig) {
11       RestangularConfig.setBaseUrl(ENV.getBaseURL("MD_SAL"));
12     });
13     return NemoRestangular;
14   });
15   nemo.register.factory('NemoService', function(NemoRestangular) {
16     var org = org || {};
17     org.opendaylight = org.opendaylight || {};
18     org.opendaylight.nemo = org.opendaylight.nemo || {};
19     org.opendaylight.nemo.utils = org.opendaylight.nemo.utils || {};
20     org.opendaylight.nemo.utils.get = function(id) {
21       return document.getElementById(id);
22     };
23     org.opendaylight.nemo.utils.create = function(tag) {
24       return document.createElement(tag);
25     };
26     org.opendaylight.nemo.update = function() {
27       jQuery.ajax({
28         type: "POST",
29         "url": "/restconf/operations/nemo-channel:view-channel", //
30         processData: false,
31         contentType: "application/json",
32         data: JSON.stringify({"input":{"topology-id":"nemo"}}),
33         success:function (response) {
34           org.opendaylight.nemo.process(response);
35         }
36       });
37       return true;
38     };
39     org.opendaylight.nemo.process = function(data) {
40       var channelPannelHTML = "";
41       channelPannelHTML += '<div style="color:#ffffff;padding-top:15px;padding-bottom:15px;">';
42       channelPannelHTML += '<table style="width:100%;" cellspacing="1" cellpadding="1"><tr>';
43       channelPannelHTML += '<td style="width:50px;"><div>Alarms</div></td>';
44       channelPannelHTML += '<td><div>Description</div></td>';
45       channelPannelHTML += '<td style="width:100px;"><div>Bytes In</div></td>';
46       channelPannelHTML += '<td style="width:100px;"><div>Bytes Out</div></td>';
47       channelPannelHTML += '</tr></table>';
48       channelPannelHTML += '</div>';  
49       var topologies = data["output"]["topology"];
50       for (var i = 0; i < topologies.length; i++) {
51         var channels = topologies[i]["channel"];
52         if (typeof channels == "undefined" || channels == null) {
53           break;
54         }
55         for (var j = 0; j < channels.length; j++) {
56           var channel = channels[j];
57           channelPannelHTML += '<div style="color:#ffffff;padding-top:15px;padding-bottom:15px;border-top: 1px solid #888888;">';
58           channelPannelHTML += '<table style="width:100%;" cellspacing="1" cellpadding="1"><tr>';
59           channelPannelHTML += '<td style="width:50px;vertical-align:top;"><div><div class="' + (channel["channel-alarms"] > 0 ? 'label-danger' : 'label-success') + '" style="background-color:' + (channel["channel-alarms"] > 0 ? '#d9534f' : '#5cb85c') + '; border-radius:35px;width:35px;height:35px;line-height:35px;text-align:center;font-size:10px;">'+channel["channel-alarms"]+'</div></div></td>';
60           channelPannelHTML += '<td style="vertical-align:top;"><div>';
61           channelPannelHTML += '<div><span>Channel </span><span>'+channel["channel-id"]+'</span></div>';
62           channelPannelHTML += '<div><span style="color:#888888;">Controller: </span><span>'+channel["source"]["source-node"]+'</span></div>';
63           channelPannelHTML += '<div><span style="color:#888888;">Device: </span><span>'+channel["destination"]["dest-node"]+'</span></div>';
64           channelPannelHTML += '<div><span style="color:#888888;">Type: </span><span>'+channel["channel-type"]+'</span></div>';
65           channelPannelHTML += '<div><span style="color:#888888;">Call Home: </span><span>'+channel["call-home"]+'</span></div>';
66           channelPannelHTML += '<div><span style="color:#888888;">Number of Sessions: </span><span>'+channel["sessions"]+'</span></div>';
67           channelPannelHTML += '</div></td>';
68           channelPannelHTML += '<td style="width:100px;vertical-align:top;"><div>'+channel["bytes-in"]+' B'+'</div></td>';
69           channelPannelHTML += '<td style="width:100px;vertical-align:top;"><div>'+channel["bytes-out"]+' B'+'</div></td>';
70           channelPannelHTML += '</tr></table>';
71           channelPannelHTML += '</div>';
72           var sessions = channel["session"];
73           if (typeof sessions != "undefined" && sessions != null) {
74             for (var k = 0; k < sessions.length; k++) {
75               var session = sessions[k];
76               channelPannelHTML += '<div style="color:#ffffff;padding-top:15px;padding-bottom:15px;border-top: 1px solid #888888;">';
77               channelPannelHTML += '<table style="width:100%;" cellspacing="1" cellpadding="1"><tr>';
78               channelPannelHTML += '<td style="width:100px;vertical-align:top;"><div style="padding-left:50px;"><div class="' + (session["session-alarms"] > 0 ? 'label-danger' : 'label-success') + '" style="background-color:' + (session["session-alarms"] > 0 ? '#d9534f' : '#5cb85c') + '; border-radius:35px;width:35px;height:35px;line-height:35px;text-align:center;font-size:10px;">'+session["session-alarms"]+'</div></div></td>';
79               channelPannelHTML += '<td style="vertical-align:top;"><div>';
80               channelPannelHTML += '<div><span>Session </span><span>'+session["session-id"]+'</span></div>';
81               channelPannelHTML += '<div><span style="color:#888888;">Port: </span><span>'+session["termination-point"]["termination-point-id"]+'</span></div>';
82               channelPannelHTML += '</div></td>';
83               channelPannelHTML += '<td style="width:100px;vertical-align:top;"><div>'+session["bytes-in"]+' B'+'</div></td>';
84               channelPannelHTML += '<td style="width:100px;vertical-align:top;"><div>'+session["bytes-out"]+' B'+'</div></td>';
85               channelPannelHTML += '</tr></table>';
86               channelPannelHTML += '</div>';
87             }
88           }
89         }
90       }  
91       var channelPanel = org.opendaylight.nemo.utils.get("channelPanel");
92       channelPanel.innerHTML = channelPannelHTML;
93       return true;
94     };
95     org.opendaylight.nemo.initialize = function() {
96       org.opendaylight.nemo.update();
97     };
98     window.org = org;
99     var NemoService = {
100       updateChannels: function() {
101         org.opendaylight.nemo.initialize();
102       }
103     };
104     return NemoService;
105   });
106 });