| 1 |
# Copyright (c) 2015 OpenStack Foundation. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# 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, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from neutron.agent import rpc as agent_rpc |
| 17 |
from neutron_lib import context |
| 18 |
|
| 19 |
from oslo_config import cfg |
| 20 |
from oslo_log import log as logging |
| 21 |
from oslo_service import loopingcall |
| 22 |
from oslo_service import periodic_task |
| 23 |
|
| 24 |
from networking_l2gw.services.l2gateway.agent import agent_api |
| 25 |
from networking_l2gw.services.l2gateway.common import constants as n_const |
| 26 |
from networking_l2gw.services.l2gateway.common import topics |
| 27 |
|
| 28 |
LOG = logging.getLogger(__name__) |
| 29 |
|
| 30 |
|
| 31 |
class BaseAgentManager(periodic_task.PeriodicTasks): |
| 32 |
"""Basic agent manager that handles basic RPCs and report states.""" |
| 33 |
|
| 34 |
def __init__(self, conf=None): |
| 35 |
conf = getattr(self, "conf", cfg.CONF) |
| 36 |
self.context = context.get_admin_context_without_session() |
| 37 |
super().__init__(conf) |
| 38 |
self.l2gw_agent_type = '' |
| 39 |
self.gateways = {} |
| 40 |
self.plugin_rpc = agent_api.L2GatewayAgentApi( |
| 41 |
topics.L2GATEWAY_PLUGIN, |
| 42 |
self.conf.host |
| 43 |
) |
| 44 |
self._get_agent_state() |
| 45 |
self.admin_state_up = True |
| 46 |
self._setup_state_rpc() |
| 47 |
self.use_call = True |
| 48 |
|
| 49 |
def _get_agent_state(self): |
| 50 |
self.agent_state = { |
| 51 |
'binary': 'neutron-l2gateway-agent', |
| 52 |
'host': self.conf.host, |
| 53 |
'topic': topics.L2GATEWAY_AGENT, |
| 54 |
'configurations': { |
| 55 |
'report_interval': self.conf.AGENT.report_interval, |
| 56 |
n_const.L2GW_AGENT_TYPE: self.l2gw_agent_type, |
| 57 |
}, |
| 58 |
'start_flag': True, |
| 59 |
'agent_type': n_const.AGENT_TYPE_L2GATEWAY} |
| 60 |
|
| 61 |
def _setup_state_rpc(self): |
| 62 |
self.state_rpc = agent_rpc.PluginReportStateAPI( |
| 63 |
topics.L2GATEWAY_PLUGIN) |
| 64 |
report_interval = self.conf.AGENT.report_interval |
| 65 |
if report_interval: |
| 66 |
heartbeat = loopingcall.FixedIntervalLoopingCall( |
| 67 |
self._report_state) |
| 68 |
heartbeat.start(interval=report_interval) |
| 69 |
|
| 70 |
def _report_state(self): |
| 71 |
try: |
| 72 |
self.state_rpc.report_state(self.context, self.agent_state, |
| 73 |
self.use_call) |
| 74 |
self.agent_state['start_flag'] = False |
| 75 |
self.use_call = False |
| 76 |
except Exception: |
| 77 |
LOG.exception("Failed reporting state!") |
| 78 |
self.handle_report_state_failure() |
| 79 |
return |
| 80 |
return |
| 81 |
|
| 82 |
def handle_report_state_failure(self): |
| 83 |
pass |
| 84 |
|
| 85 |
def agent_updated(self, context, payload): |
| 86 |
LOG.info("agent_updated by server side %s!", payload) |
| 87 |
|
| 88 |
def set_monitor_agent(self, context, hostname): |
| 89 |
"""Handle RPC call from plugin to update agent type. |
| 90 |
|
| 91 |
RPC call from the plugin to accept that I am a monitoring |
| 92 |
or a transact agent. This is a fanout cast message |
| 93 |
""" |
| 94 |
if hostname == self.conf.host: |
| 95 |
self.l2gw_agent_type = n_const.MONITOR |
| 96 |
else: |
| 97 |
self.l2gw_agent_type = '' |
| 98 |
|
| 99 |
self.agent_state.get('configurations')[n_const.L2GW_AGENT_TYPE |
| 100 |
] = self.l2gw_agent_type |