Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 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 | /* |
Andrea Campanella | a4ef01d | 2017-02-02 09:34:26 -0800 | [diff] [blame] | 18 | Module containing the "business logic" for the Path Painter topology overlay. |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 19 | */ |
| 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 Campanella | 0c17a0a | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 30 | swapMessage = 'ppTopovSwapSrcDst', |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 31 | modeMessage = 'ppTopovSetMode', |
| 32 | nextPathMessage = 'ppTopovNextPath', |
Andrea Campanella | 48c674c | 2015-12-04 14:48:04 -0800 | [diff] [blame] | 33 | clearMessage = 'ppTopovClear', |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 34 | prevPathMessage = 'ppTopovPrevPath'; |
| 35 | |
| 36 | // internal state |
| 37 | var currentMode = null; |
Andrea Campanella | a4ef01d | 2017-02-02 09:34:26 -0800 | [diff] [blame] | 38 | var selected = false; |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | // === --------------------------- |
| 42 | // === Helper functions |
| 43 | |
| 44 | |
| 45 | // === --------------------------- |
| 46 | // === Main API functions |
| 47 | |
Andrea Campanella | 48c674c | 2015-12-04 14:48:04 -0800 | [diff] [blame] | 48 | function clear() { |
Andrea Campanella | a4ef01d | 2017-02-02 09:34:26 -0800 | [diff] [blame] | 49 | if (selected) { |
| 50 | selected = false; |
| 51 | wss.sendEvent(clearMessage); |
| 52 | flash.flash('Cleared source and destination'); |
| 53 | return true; |
| 54 | } |
| 55 | return false; |
Andrea Campanella | 48c674c | 2015-12-04 14:48:04 -0800 | [diff] [blame] | 56 | } |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 57 | |
| 58 | function setSrc(node) { |
Andrea Campanella | a4ef01d | 2017-02-02 09:34:26 -0800 | [diff] [blame] | 59 | selected = true; |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 60 | wss.sendEvent(srcMessage, { |
Andrea Campanella | 490e839 | 2015-12-03 12:18:11 -0800 | [diff] [blame] | 61 | id: node.id, |
| 62 | type: node.type |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 63 | }); |
| 64 | flash.flash('Source node: ' + node.id); |
| 65 | } |
| 66 | |
| 67 | function setDst(node) { |
Andrea Campanella | a4ef01d | 2017-02-02 09:34:26 -0800 | [diff] [blame] | 68 | selected = true; |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 69 | wss.sendEvent(dstMessage, { |
Andrea Campanella | 490e839 | 2015-12-03 12:18:11 -0800 | [diff] [blame] | 70 | id: node.id, |
| 71 | type: node.type |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 72 | }); |
| 73 | flash.flash('Destination node: ' + node.id); |
| 74 | } |
| 75 | |
Andrea Campanella | 0c17a0a | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 76 | function swapSrcDst() { |
| 77 | wss.sendEvent(swapMessage) |
| 78 | flash.flash('Source and destination swap'); |
| 79 | } |
| 80 | |
| 81 | function nextPath() { |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 82 | wss.sendEvent(nextPathMessage); |
| 83 | } |
| 84 | |
Andrea Campanella | 0c17a0a | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 85 | function prevPath() { |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 86 | wss.sendEvent(prevPathMessage); |
| 87 | } |
| 88 | |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 89 | function setMode(mode) { |
| 90 | if (currentMode === mode) { |
| 91 | $log.debug('(in mode', mode, 'already)'); |
Andrea Campanella | 8583e6b | 2015-12-01 21:24:45 -0800 | [diff] [blame] | 92 | flash.flash('Already in ' + mode + ' mode'); |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 93 | } 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 Campanella | 0c17a0a | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 120 | prevPath: prevPath, |
Andrea Campanella | 48c674c | 2015-12-04 14:48:04 -0800 | [diff] [blame] | 121 | swapSrcDst: swapSrcDst, |
| 122 | clear: clear |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 123 | }; |
| 124 | }]); |
| 125 | }()); |