blob: a7640cdf5646d160734b73fc756bd6526235311d [file] [log] [blame]
Simon Hunte6720442015-01-15 12:21:06 -08001/*
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 Hunte6720442015-01-15 12:21:06 -080019 */
20(function () {
21 'use strict';
22
23 var $log;
24
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080025
26 // TODO: remove temporary test code
27 var fakeData = {
28 '1': {
29 "devices": [{
30 "id": "of:0000000000000001",
31 "available": true,
Simon Hunt97225382015-01-19 13:33:09 -080032 "_iconid_available": "deviceOnline",
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080033 "role": "MASTER",
34 "mfr": "Nicira, Inc.",
35 "hw": "Open vSwitch",
36 "sw": "2.0.1",
37 "serial": "None",
38 "annotations": {
39 "protocol": "OF_10"
40 }
41 },
Simon Hunt97225382015-01-19 13:33:09 -080042 {
43 "id": "of:0000000000000004",
44 "available": false,
45 "_iconid_available": "deviceOffline",
46 "role": "MASTER",
47 "mfr": "Nicira, Inc.",
48 "hw": "Open vSwitch",
49 "sw": "2.0.1",
50 "serial": "None",
51 "annotations": {
52 "protocol": "OF_10"
53 }
54 },
55 {
56 "id": "of:0000000000000092",
57 "available": false,
58 "_iconid_available": "deviceOffline",
59 "role": "MASTER",
60 "mfr": "Nicira, Inc.",
61 "hw": "Open vSwitch",
62 "sw": "2.0.1",
63 "serial": "None",
64 "annotations": {
65 "protocol": "OF_10"
66 }
67 }]
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080068 },
69 '2': {
70 "devices": [{
71 "id": "of:0000000000000002",
72 "available": true,
Simon Hunt97225382015-01-19 13:33:09 -080073 "_iconid_available": "deviceOnline",
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080074 "role": "MASTER",
75 "mfr": "Nicira, Inc.",
76 "hw": "Open vSwitch",
77 "sw": "2.0.0",
78 "serial": "None",
79 "annotations": {
80 "protocol": "OF_10"
81 }
82 },
Simon Hunt97225382015-01-19 13:33:09 -080083 {
84 "id": "of:0000000000000006",
85 "available": true,
86 "_iconid_available": "deviceOnline",
87 "role": "MASTER",
88 "mfr": "Nicira, Inc.",
89 "hw": "Open vSwitch",
90 "sw": "2.1.1",
91 "serial": "None",
92 "annotations": {
93 "protocol": "OF_10"
94 }
95 }]
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080096 },
97 'empty': {
98 devices: []
99 }
100 };
101
102 function getFakeData(url) {
103 var id = url.slice(5);
104
105 return fakeData[id] || fakeData.empty;
106 }
107
Simon Hunte6720442015-01-15 12:21:06 -0800108 angular.module('onosRemote')
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -0800109 .factory('RestService', ['$log', '$http', 'UrlFnService',
110 function (_$log_, $http, ufs) {
Simon Hunte6720442015-01-15 12:21:06 -0800111 $log = _$log_;
112
113 function get(url, callback) {
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -0800114 // TODO: remove temporary test code
115 if (url.match(/^test\//)) {
116 callback(getFakeData(url));
117 return;
118 }
Simon Hunt1e4a0012015-01-21 11:36:08 -0800119 var fullUrl = ufs.rsUrl(url);
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -0800120
121 $http.get(fullUrl).then(function (response) {
Simon Hunte6720442015-01-15 12:21:06 -0800122 // success
123 callback(response.data);
124 }, function (response) {
125 // error
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -0800126 $log.warn('Failed to retrieve JSON data: ' + fullUrl,
Simon Hunte6720442015-01-15 12:21:06 -0800127 response.status, response.data);
128 });
129 }
130
131 return {
132 get: get
133 };
134 }]);
135
136}());