blob: d2b1fa27b59817635237254504e83f5e748c30f1 [file] [log] [blame]
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -08003 *
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/*
Andrea Campanellaa4ef01d2017-02-02 09:34:26 -080018 Module containing the "business logic" for the Path Painter topology overlay.
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080019 */
20
21(function () {
22 'use strict';
23
24 // injected refs
25 var $log, fs, flash, wss;
26
27 // constants
28 var srcMessage = 'ppTopovSetSrc',
29 dstMessage = 'ppTopovSetDst',
Andrea Campanella0c17a0a2015-12-01 09:53:51 -080030 swapMessage = 'ppTopovSwapSrcDst',
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080031 modeMessage = 'ppTopovSetMode',
32 nextPathMessage = 'ppTopovNextPath',
Andrea Campanella48c674c2015-12-04 14:48:04 -080033 clearMessage = 'ppTopovClear',
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080034 prevPathMessage = 'ppTopovPrevPath';
35
36 // internal state
37 var currentMode = null;
Andrea Campanellaa4ef01d2017-02-02 09:34:26 -080038 var selected = false;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080039
40
41 // === ---------------------------
42 // === Helper functions
43
44
45 // === ---------------------------
46 // === Main API functions
47
Andrea Campanella48c674c2015-12-04 14:48:04 -080048 function clear() {
Andrea Campanellaa4ef01d2017-02-02 09:34:26 -080049 if (selected) {
50 selected = false;
51 wss.sendEvent(clearMessage);
52 flash.flash('Cleared source and destination');
53 return true;
54 }
55 return false;
Andrea Campanella48c674c2015-12-04 14:48:04 -080056 }
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080057
58 function setSrc(node) {
Andrea Campanellaa4ef01d2017-02-02 09:34:26 -080059 selected = true;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080060 wss.sendEvent(srcMessage, {
Andrea Campanella490e8392015-12-03 12:18:11 -080061 id: node.id,
62 type: node.type
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080063 });
64 flash.flash('Source node: ' + node.id);
65 }
66
67 function setDst(node) {
Andrea Campanellaa4ef01d2017-02-02 09:34:26 -080068 selected = true;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080069 wss.sendEvent(dstMessage, {
Andrea Campanella490e8392015-12-03 12:18:11 -080070 id: node.id,
71 type: node.type
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080072 });
73 flash.flash('Destination node: ' + node.id);
74 }
75
Andrea Campanella0c17a0a2015-12-01 09:53:51 -080076 function swapSrcDst() {
77 wss.sendEvent(swapMessage)
78 flash.flash('Source and destination swap');
79 }
80
81 function nextPath() {
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080082 wss.sendEvent(nextPathMessage);
83 }
84
Andrea Campanella0c17a0a2015-12-01 09:53:51 -080085 function prevPath() {
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080086 wss.sendEvent(prevPathMessage);
87 }
88
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080089 function setMode(mode) {
90 if (currentMode === mode) {
91 $log.debug('(in mode', mode, 'already)');
Andrea Campanella8583e6b2015-12-01 21:24:45 -080092 flash.flash('Already in ' + mode + ' mode');
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080093 } else {
94 currentMode = mode;
95 wss.sendEvent(modeMessage, {
96 mode: mode
97 });
98 flash.flash('Path mode: ' + mode);
99 }
100 }
101
102 // === ---------------------------
103 // === Module Factory Definition
104
105 angular.module('ovPpTopov', [])
106 .factory('PathPainterTopovService',
107 ['$log', 'FnService', 'FlashService', 'WebSocketService',
108
109 function (_$log_, _fs_, _flash_, _wss_) {
110 $log = _$log_;
111 fs = _fs_;
112 flash = _flash_;
113 wss = _wss_;
114
115 return {
116 setSrc: setSrc,
117 setDst: setDst,
118 setMode: setMode,
119 nextPath: nextPath,
Andrea Campanella0c17a0a2015-12-01 09:53:51 -0800120 prevPath: prevPath,
Andrea Campanella48c674c2015-12-04 14:48:04 -0800121 swapSrcDst: swapSrcDst,
122 clear: clear
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800123 };
124 }]);
125}());