1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1997-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20-module(snmpa_misc_sup).
21
22-include("snmp_debug.hrl").
23
24-behaviour(supervisor).
25
26%% External exports
27-export([
28	 start_link/0,
29	 start_mib_server/4, stop_mib_server/1,
30	 start_net_if/6, stop_net_if/1,
31	 start_note_store/3, stop_note_store/1
32	]).
33
34%% Internal exports
35-export([init/1]).
36
37-define(SERVER, ?MODULE).
38
39
40%%%-----------------------------------------------------------------
41%%% This is a supervisor for the mib and net_ifprocesses.
42%%% Each agent has one mib process.
43%%%-----------------------------------------------------------------
44
45start_link() ->
46    ?d("start_link -> entry", []),
47    supervisor:start_link({local, ?SERVER}, ?MODULE, []).
48
49
50%%-----------------------------------------------------------------
51%% When the agent starts, it calls this function. If there already
52%% exist a mib process for the agent, this one is used. Otherwise
53%% a new one is started.
54%%-----------------------------------------------------------------
55start_mib_server(Prio, Ref, Mibs, Opts) ->
56    ?d("start_mib_server -> entry with"
57	"~n   Prio:    ~p"
58	"~n   Ref:     ~p"
59	"~n   Mibs:    ~p"
60	"~n   Opts:    ~p", [Prio, Ref, Mibs, Opts]),
61    SupName = ?SERVER,
62    start_mibserver(SupName, Ref, [Prio, Mibs, Opts]).
63
64start_mibserver(SupName, Ref, Args) ->
65    Children = supervisor:which_children(SupName),
66    case lists:keysearch({mib, Ref}, 1, Children) of
67	{value, {_, Pid, _, _}} -> {ok, Pid};
68	_ ->
69	    Mib = {{mib, Ref},
70		   {snmpa_mib, start_link, Args},
71		   transient, 10000, worker, [snmpa_mib]},
72	    supervisor:start_child(SupName, Mib)
73    end.
74
75stop_mib_server(Ref) ->
76    SupName = ?SERVER,
77    case whereis(SupName) of
78	undefined ->
79	    ok;
80	_ ->
81	    supervisor:terminate_child(SupName, {mib, Ref}),
82	    supervisor:delete_child(SupName, {mib, Ref})
83    end.
84
85
86start_net_if(Prio, NoteStore, Ref, Master, Mod, Opts) ->
87    ?d("start_mib -> entry with"
88	"~n   Prio:      ~p"
89	"~n   NoteStore: ~p"
90	"~n   Ref:       ~p"
91	"~n   Master:    ~p"
92	"~n   Mod:       ~p"
93	"~n   Opts:      ~p",
94       [Prio, NoteStore, Ref, Master, Mod, Opts]),
95    SupName = ?SERVER,
96    start_netif(SupName, Ref, Mod, [Prio, NoteStore, Master, Opts]).
97
98start_netif(SupName, Ref, Mod, Args) ->
99    %% make sure we start from scratch...
100    Children = supervisor:which_children(SupName),
101    case lists:keysearch({net_if, Ref}, 1, Children) of
102	{value, {_, _Pid, _, _}} ->
103	    stop_net_if(Ref);
104	_ ->
105	    ok
106    end,
107    NetIf = {{net_if, Ref},
108	     {Mod, start_link, Args},
109	     permanent, 2000, worker, [Mod]},
110    supervisor:start_child(SupName, NetIf).
111
112stop_net_if(Ref) ->
113    SupName = ?SERVER,
114    case whereis(SupName) of
115	undefined ->
116	    ok;
117	_ ->
118	    supervisor:terminate_child(SupName, {net_if, Ref}),
119	    supervisor:delete_child(SupName, {net_if, Ref})
120    end.
121
122
123start_note_store(Prio, Ref, Opts) ->
124    ?d("start_note_store -> entry with"
125	"~n   Prio:    ~p"
126	"~n   Ref:     ~p"
127	"~n   Opts:    ~p", [Prio, Ref, Opts]),
128    SupName = ?SERVER,
129    start_notestore(SupName, Ref, [Prio, snmpa, Opts]).
130
131start_notestore(SupName, Ref, Args) ->
132    %% make sure we start from scratch...
133    Children = supervisor:which_children(SupName),
134    case lists:keysearch({note_store, Ref}, 1, Children) of
135	{value, {_, _Pid, _, _}} ->
136	    stop_note_store(Ref);
137	_ ->
138	    ok
139    end,
140    Mod = snmp_note_store,
141    Note = {{note_store, Ref},
142	    {Mod, start_link, Args},
143	    permanent, 2000, worker, [Mod]},
144    supervisor:start_child(SupName, Note).
145
146stop_note_store(Ref) ->
147    SupName = ?SERVER,
148    case whereis(SupName) of
149	undefined ->
150	    ok;
151	_ ->
152	    supervisor:terminate_child(SupName, {note_store, Ref}),
153	    supervisor:delete_child(SupName, {note_store, Ref})
154    end.
155
156
157init([]) ->
158    SupFlags = {one_for_all, 0, 3600},
159    {ok, {SupFlags, []}}.
160