-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpm_led.lua
115 lines (98 loc) · 2.52 KB
/
pm_led.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env lua
-- -*-lua-*-
--
-- $Id: pm_led.lua $
--
-- Author: Markus Stenberg <markus [email protected]>
--
-- Copyright (c) 2013 cisco Systems, Inc.
--
-- Created: Wed Mar 13 09:38:57 2013 mstenber
-- Last modified: Mon Sep 30 13:33:12 2013 mstenber
-- Edit time: 21 min
--
-- This is used to indicate the pm.lua state to external parties. Two
-- notifications are currently supported:
-- 'pd' indicates there is PD lease received on interfaces
-- 'global' indicates global address has been assigned to an
-- interfaces (ospf-lap)
require 'pm_handler'
require 'mst'
LED_SCRIPT='/usr/share/hnet/led_handler.sh'
module(..., package.seeall)
local _pmh = pm_handler.pm_handler_with_pa
pm_led = _pmh:new_subclass{class='pm_led'}
function pm_led:init()
_pmh.init(self)
self.states = {}
self.enabled = true
end
function pm_led:set_state(ledname, value)
local old = self.states[ledname]
if old == value
then
return
end
self.states[ledname] = value
self:apply_state(ledname, value)
end
function pm_led:apply_state(ledname, value)
self:d('applying led state', ledname, value)
if not self.enabled
then
self:d('not enabled, skipping led update')
return
end
local s =
string.format('%s %s %s', LED_SCRIPT, ledname, value and '1' or 0)
self.shell(s)
end
function pm_led:have_pd_prefix_in_skv()
-- this is a hack; but it can remain a hack too..
self:d('have_pd_prefix_in_skv')
-- loop through skv, looking at stuff with prefix
for k, v in pairs(self._pm.skv:get_combined_state())
do
--self:d('key is', k)
if string.find(k, '^' .. elsa_pa.PD_SKVPREFIX)
then
self:d('considering', k)
for i, v in ipairs(v)
do
if v.prefix
then
self:d('found prefix!', v)
return true
end
end
end
end
return false
end
function pm_led:skv_changed(k, v)
if string.find(k, '^' .. elsa_pa.PD_SKVPREFIX)
then
for i, v in ipairs(v)
do
if v.prefix
then
self:queue()
end
end
end
end
function pm_led:have_global_ipv6()
for i, v in ipairs(self.usp:get_ipv6())
do
-- for the time being, we're happy with ULAs too if they're desired
-- (XXX - should this be the case?)
return true
end
return false
end
function pm_led:run()
local found = self:have_pd_prefix_in_skv()
self:set_state('pd', found)
local found = self:have_global_ipv6()
self:set_state('global', found)
end