aboutsummaryrefslogtreecommitdiffstats
path: root/lib/snmp/src/agent/snmpa_mib_storage_mnesia.erl
blob: 5f66b2ed9f7e597b6b0953a06be3eb8d5b143fb7 (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2013-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(snmpa_mib_storage_mnesia).


-behaviour(snmpa_mib_storage).

%%%-----------------------------------------------------------------
%%% This module implements the snmpa_mib_storage behaviour. 
%%% It uses mnesia for storage. 
%%%-----------------------------------------------------------------

-export([
	 open/5, 
	 close/1, 
	 read/2, 
	 write/2, 
	 delete/1, 
	 delete/2, 
	 match_object/2, 
	 match_delete/2, 
	 tab2list/1, 
	 info/1, info/2, 
	 sync/1, 
	 backup/2
	]).


-define(VMODULE,"MS-MNESIA").
-include("snmp_verbosity.hrl").

-record(tab, {id}).


%% ---------------------------------------------------------------
%% open
%% 
%% Open or create a mnesia table. 
%% 
%% Opts    - A list of implementation dependent options
%%           mnesia_open_options() = [mnesia_open_option()]
%%           mnesia_open_option()  = {action, keep | clear} | 
%%                                   {nodes,  [node()]}
%% 
%% ---------------------------------------------------------------

open(Name, RecName, Fields, Type, Opts) ->
    ?vtrace("open ~p table ~p for record ~p",
	    [Type, Name, RecName]),
    Action  = get_action(Opts), 
    Nodes   = get_nodes(Opts), 
    case table_exists(Name) of
	true when (Action =:= keep) -> 
	    ?vtrace("open table ~p - exist (keep)", [Name]),
	    {ok, #tab{id = Name}};
	true when (Action =:= clear) -> 
	    ?vtrace("open table ~p - exist (clear)", [Name]),
	    F = fun() -> mnesia:clear_table(Name) end,
	    case mnesia:transaction(F) of
		{aborted, Reason} ->
		    {error, {clear, Reason}};
		{atomic, _} ->
		    {ok, #tab{id = Name}}
	    end;
	false ->
	    ?vtrace("open table ~p - does not exist", [Name]),
	    Args = [{record_name, RecName}, 
		    {attributes,  Fields},
		    {type,        Type}, 
		    {disc_copies, Nodes}],
	    case mnesia:create_table(Name, Args) of
		{atomic, ok} ->
		    ?vtrace("open table ~p - ok", [Name]),
		    {ok, #tab{id = Name}};
		{aborted, Reason} ->
		    ?vinfo("open table ~p - aborted"
			   "~n   Reason: ~p", [Name, Reason]),
		    {error, {create, Reason}}
	    end
    end.

table_exists(Name) ->
    case (catch mnesia:table_info(Name, type)) of
	{'EXIT', _Reason} ->
	    false;
	_ ->
	    true
    end.
   

%% ---------------------------------------------------------------
%% close
%% 
%% Close the mib-storage table. 
%% This does nothing in the mnesia case.
%% ---------------------------------------------------------------

close(_) ->
    ?vtrace("close mib-storage - ignore",[]),
    ok.


%% ---------------------------------------------------------------
%% read
%% 
%% Retrieve a record from the mib-storage table.
%% ---------------------------------------------------------------

read(#tab{id = ID}, Key) ->
    ?vtrace("read (dirty) from database ~p: ~p", [ID, Key]),
    case (catch mnesia:dirty_read(ID, Key)) of
	[Rec|_] -> {value,Rec};
	_ -> false
    end.
    

%% ---------------------------------------------------------------
%% write
%% 
%% Write a record to the mib-storage table.
%% ---------------------------------------------------------------

write(#tab{id = ID}, Rec) -> 
    ?vtrace("write to database ~p", [ID]),
    F = fun() -> mnesia:write(ID, Rec, write) end,
    case mnesia:transaction(F) of
	{aborted, _Reason} = ABORTED ->
	    {error, ABORTED};
	{atomic,_} ->
	    ok
    end.


%% ---------------------------------------------------------------
%% delete
%% 
%% Delete the mib-storage table. 
%% ---------------------------------------------------------------

delete(#tab{id = ID}) ->
    ?vtrace("delete database: ~p", [ID]),
    mnesia:delete_table(ID).


%% ---------------------------------------------------------------
%% delete
%% 
%% Delete a record from the mib-storage table.
%% ---------------------------------------------------------------

delete(#tab{id = ID}, Key) -> 
    ?vtrace("delete from database ~p: ~p", [ID, Key]),
    F = fun() -> mnesia:delete(ID, Key, write) end,
    case mnesia:transaction(F) of
	{aborted, _Reason} = ABORTED ->
	    {error, ABORTED};
	{atomic, _} ->
	    ok
    end.


%% ---------------------------------------------------------------
%% match_object
%% 
%% Search the mib-storage table for records witch matches 
%% the pattern.
%% ---------------------------------------------------------------

match_object(#tab{id = ID}, Pattern) ->
    ?vtrace("match_object in ~p of ~p", [ID, Pattern]),
    F = fun() -> mnesia:match_object(ID, Pattern, read) end,
    case mnesia:transaction(F) of
	{aborted, _Reason} = ABORTED ->
	    {error, ABORTED};
	{atomic, Rs} ->
	    Rs
    end.
    

%% ---------------------------------------------------------------
%% match_delete
%% 
%% Search the mib-storage table for records witch matches 
%% the pattern and deletes them from the table.
%% ---------------------------------------------------------------

match_delete(#tab{id = ID}, Pattern) -> 
    ?vtrace("match_delete in ~p with pattern ~p", [ID, Pattern]),
    F = fun() -> 
		Recs = mnesia:match_object(ID, Pattern, read),
		lists:foreach(fun(Rec) -> 
				      mnesia:delete_object(ID, Rec, write)
			      end, Recs),
		Recs
	end,
    case mnesia:transaction(F) of
	{aborted, _Reason} = ABORTED ->
	    {error, ABORTED};
	{atomic, Rs} ->
	    Rs
    end.


%% ---------------------------------------------------------------
%% tab2list
%% 
%% Return all records in the mib-storage table in the form of 
%% a list.
%% ---------------------------------------------------------------

tab2list(#tab{id = ID} = Tab) ->
    ?vtrace("tab2list -> list of ~p", [ID]),
    match_object(Tab, mnesia:table_info(ID, wild_pattern)).


%% ---------------------------------------------------------------
%% info
%% 
%% Retrieve implementation dependent mib-storage table 
%% information.
%% ---------------------------------------------------------------

info(#tab{id = ID}) ->
    case (catch mnesia:table_info(ID, all)) of
	Info when is_list(Info) ->
	    Info;
	{'EXIT', {aborted, Reason}} ->
	    {error, Reason}
    end.


info(#tab{id = ID}, Item) ->
    mnesia:table_info(ID, Item).


%% ---------------------------------------------------------------
%% sync
%% 
%% Ignore
%% ---------------------------------------------------------------

sync(_) ->
    ok.


%% ---------------------------------------------------------------
%% backup
%% 
%% Ignore. Mnesia handles its own backups. 
%% ---------------------------------------------------------------

backup(_, _) ->
    ok.

	      
%%----------------------------------------------------------------------

get_action(Opts) ->
    snmp_misc:get_option(action, Opts, keep). 

get_nodes(Opts) ->
    case snmp_misc:get_option(nodes, Opts, erlang:nodes()) of
	[] ->
	    [node()];
	Nodes when is_list(Nodes) ->
	    Nodes;
	all ->
	    erlang:nodes();
	visible ->
	    erlang:nodes(visible);
	connected ->
	    erlang:nodes(connected);
	db_nodes -> 
	    try mnesia:system_info(db_nodes) of
		DbNodes when is_list(DbNodes) ->
		    DbNodes;
		_ ->
		    erlang:nodes()
	    catch 
		_:_ ->
		    erlang:nodes()
	    end
    end.
	    
%% user_err(F, A) ->
%%     snmpa_error:user_err(F, A).