blob: b397aa73fd1642cb20d55c5e1faf669589c22fe0 [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
19
20 @author Bri Prebilic Cole
21 @author Simon Hunt
22 */
23(function () {
24 'use strict';
25
26 var $log;
27
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080028 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,
38 "role": "MASTER",
39 "mfr": "Nicira, Inc.",
40 "hw": "Open vSwitch",
41 "sw": "2.0.1",
42 "serial": "None",
43 "annotations": {
44 "protocol": "OF_10"
45 }
46 },
47 {
48 "id": "of:0000000000000004",
49 "available": true,
50 "role": "MASTER",
51 "mfr": "Nicira, Inc.",
52 "hw": "Open vSwitch",
53 "sw": "2.0.1",
54 "serial": "None",
55 "annotations": {
56 "protocol": "OF_10"
57 }
58 }]
59 },
60 '2': {
61 "devices": [{
62 "id": "of:0000000000000002",
63 "available": true,
64 "role": "MASTER",
65 "mfr": "Nicira, Inc.",
66 "hw": "Open vSwitch",
67 "sw": "2.0.0",
68 "serial": "None",
69 "annotations": {
70 "protocol": "OF_10"
71 }
72 },
73 {
74 "id": "of:0000000000000006",
75 "available": true,
76 "role": "MASTER",
77 "mfr": "Nicira, Inc.",
78 "hw": "Open vSwitch",
79 "sw": "2.1.1",
80 "serial": "None",
81 "annotations": {
82 "protocol": "OF_10"
83 }
84 }]
85 },
86 'empty': {
87 devices: []
88 }
89 };
90
91 function getFakeData(url) {
92 var id = url.slice(5);
93
94 return fakeData[id] || fakeData.empty;
95 }
96
Simon Hunte6720442015-01-15 12:21:06 -080097 angular.module('onosRemote')
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -080098 .factory('RestService', ['$log', '$http', 'UrlFnService',
99 function (_$log_, $http, ufs) {
Simon Hunte6720442015-01-15 12:21:06 -0800100 $log = _$log_;
101
102 function get(url, callback) {
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -0800103 // TODO: remove temporary test code
104 if (url.match(/^test\//)) {
105 callback(getFakeData(url));
106 return;
107 }
108 var fullUrl = ufs.urlPrefix() + urlSuffix + url;
109
110 $http.get(fullUrl).then(function (response) {
Simon Hunte6720442015-01-15 12:21:06 -0800111 // success
112 callback(response.data);
113 }, function (response) {
114 // error
Bri Prebilic Cole4fab8af2015-01-15 16:40:47 -0800115 $log.warn('Failed to retrieve JSON data: ' + fullUrl,
Simon Hunte6720442015-01-15 12:21:06 -0800116 response.status, response.data);
117 });
118 }
119
120 return {
121 get: get
122 };
123 }]);
124
125}());