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