Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 IBM |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | window.Host = Backbone.Model.extend({ |
| 18 | |
| 19 | defaults: { |
| 20 | // vlan: -1, |
| 21 | lastSeen: 'never', |
| 22 | ip: ' ', |
| 23 | swport: ' ', |
| 24 | }, |
| 25 | |
| 26 | // initialize:function () {} |
| 27 | |
| 28 | }); |
| 29 | |
| 30 | window.HostCollection = Backbone.Collection.extend({ |
| 31 | |
| 32 | model:Host, |
| 33 | |
| 34 | fetch:function () { |
| 35 | var self = this; |
| 36 | //console.log("fetching host list") |
| 37 | $.ajax({ |
Naoki Shiota | b32edf5 | 2013-12-12 14:09:36 -0800 | [diff] [blame] | 38 | url:hackBase + "/fl/device/", |
Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 39 | dataType:"json", |
| 40 | success:function (data) { |
| 41 | //console.log("fetched host list: " + data.length); |
| 42 | // console.log(data); |
| 43 | // data is a list of device hashes |
| 44 | var old_ids = self.pluck('id'); |
| 45 | //console.log("old_ids" + old_ids); |
| 46 | _.each(data, function(h) { |
| 47 | h.id = h.mac[0]; |
| 48 | old_ids = _.without(old_ids, h.id); |
| 49 | if (h['attachmentPoint'].length > 0) { |
| 50 | h.swport = _.reduce(h['attachmentPoint'], function(memo, ap) { |
| 51 | return memo + ap.switchDPID + "-" + ap.port + " "}, ""); |
| 52 | //console.log(h.swport); |
| 53 | h.lastSeen = new Date(h.lastSeen).toLocaleString(); |
| 54 | self.add(h, {silent: true}); |
| 55 | } |
| 56 | }); |
| 57 | // old_ids now holds hosts that no longer exist; remove them |
| 58 | //console.log("old_ids" + old_ids); |
| 59 | _.each(old_ids, function(h) { |
| 60 | console.log("---removing host " + h); |
| 61 | self.remove({id:h}); |
| 62 | }); |
| 63 | self.trigger('add'); // batch redraws |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | }, |
| 68 | |
| 69 | /* |
| 70 | * findByName:function (key) { // TODO: Modify service to include firstName |
| 71 | * in search var url = (key == '') ? '/host/' : "/host/search/" + key; |
| 72 | * console.log('findByName: ' + key); var self = this; $.ajax({ url:url, |
| 73 | * dataType:"json", success:function (data) { console.log("search success: " + |
| 74 | * data.length); self.reset(data); } }); } |
| 75 | */ |
| 76 | |
| 77 | }); |