Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | /* |
| 18 | ONOS GUI -- Remote Communications Module -- REST Service |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
| 23 | var $log; |
| 24 | |
Simon Hunt | aa26adf | 2015-01-20 10:32:49 -0800 | [diff] [blame] | 25 | var urlSuffix = '/onos/ui/rs/'; |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | |
| 29 | // TODO: remove temporary test code |
| 30 | var fakeData = { |
| 31 | '1': { |
| 32 | "devices": [{ |
| 33 | "id": "of:0000000000000001", |
| 34 | "available": true, |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 35 | "_iconid_available": "deviceOnline", |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 36 | "role": "MASTER", |
| 37 | "mfr": "Nicira, Inc.", |
| 38 | "hw": "Open vSwitch", |
| 39 | "sw": "2.0.1", |
| 40 | "serial": "None", |
| 41 | "annotations": { |
| 42 | "protocol": "OF_10" |
| 43 | } |
| 44 | }, |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 45 | { |
| 46 | "id": "of:0000000000000004", |
| 47 | "available": false, |
| 48 | "_iconid_available": "deviceOffline", |
| 49 | "role": "MASTER", |
| 50 | "mfr": "Nicira, Inc.", |
| 51 | "hw": "Open vSwitch", |
| 52 | "sw": "2.0.1", |
| 53 | "serial": "None", |
| 54 | "annotations": { |
| 55 | "protocol": "OF_10" |
| 56 | } |
| 57 | }, |
| 58 | { |
| 59 | "id": "of:0000000000000092", |
| 60 | "available": false, |
| 61 | "_iconid_available": "deviceOffline", |
| 62 | "role": "MASTER", |
| 63 | "mfr": "Nicira, Inc.", |
| 64 | "hw": "Open vSwitch", |
| 65 | "sw": "2.0.1", |
| 66 | "serial": "None", |
| 67 | "annotations": { |
| 68 | "protocol": "OF_10" |
| 69 | } |
| 70 | }] |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 71 | }, |
| 72 | '2': { |
| 73 | "devices": [{ |
| 74 | "id": "of:0000000000000002", |
| 75 | "available": true, |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 76 | "_iconid_available": "deviceOnline", |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 77 | "role": "MASTER", |
| 78 | "mfr": "Nicira, Inc.", |
| 79 | "hw": "Open vSwitch", |
| 80 | "sw": "2.0.0", |
| 81 | "serial": "None", |
| 82 | "annotations": { |
| 83 | "protocol": "OF_10" |
| 84 | } |
| 85 | }, |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 86 | { |
| 87 | "id": "of:0000000000000006", |
| 88 | "available": true, |
| 89 | "_iconid_available": "deviceOnline", |
| 90 | "role": "MASTER", |
| 91 | "mfr": "Nicira, Inc.", |
| 92 | "hw": "Open vSwitch", |
| 93 | "sw": "2.1.1", |
| 94 | "serial": "None", |
| 95 | "annotations": { |
| 96 | "protocol": "OF_10" |
| 97 | } |
| 98 | }] |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 99 | }, |
| 100 | 'empty': { |
| 101 | devices: [] |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | function getFakeData(url) { |
| 106 | var id = url.slice(5); |
| 107 | |
| 108 | return fakeData[id] || fakeData.empty; |
| 109 | } |
| 110 | |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 111 | angular.module('onosRemote') |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 112 | .factory('RestService', ['$log', '$http', 'UrlFnService', |
| 113 | function (_$log_, $http, ufs) { |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 114 | $log = _$log_; |
| 115 | |
| 116 | function get(url, callback) { |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 117 | // TODO: remove temporary test code |
| 118 | if (url.match(/^test\//)) { |
| 119 | callback(getFakeData(url)); |
| 120 | return; |
| 121 | } |
| 122 | var fullUrl = ufs.urlPrefix() + urlSuffix + url; |
| 123 | |
| 124 | $http.get(fullUrl).then(function (response) { |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 125 | // success |
| 126 | callback(response.data); |
| 127 | }, function (response) { |
| 128 | // error |
Bri Prebilic Cole | 4fab8af | 2015-01-15 16:40:47 -0800 | [diff] [blame] | 129 | $log.warn('Failed to retrieve JSON data: ' + fullUrl, |
Simon Hunt | e672044 | 2015-01-15 12:21:06 -0800 | [diff] [blame] | 130 | response.status, response.data); |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | return { |
| 135 | get: get |
| 136 | }; |
| 137 | }]); |
| 138 | |
| 139 | }()); |