aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_manager/moon_manager/messenger.py
blob: 76ba6dad0ffe8988f96bbeda6329e32606977c74 (plain)
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
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import time
from oslo_config import cfg
import oslo_messaging
from oslo_log import log as logging
from moon_manager.api.generic import Status, Logs
from moon_utilities.api import APIList
from moon_manager.api.models import Models, MetaRules, MetaData
from moon_manager.api.policies import Policies, Perimeter, Data, Assignments, Rules
from moon_manager.api.pdp import PDP
from moon_manager.api.master import Master
from moon_utilities.security_functions import call
from moon_utilities.exceptions import IntraExtensionUnknown
from moon_utilities import configuration

LOG = logging.getLogger("moon.manager.messenger")
CONF = cfg.CONF


class Server:

    def __init__(self):
        self.TOPIC = "moon_manager"
        cfg.CONF.transport_url = self.__get_transport_url()
        self.transport = oslo_messaging.get_transport(cfg.CONF)
        self.target = oslo_messaging.Target(topic=self.TOPIC, server='moon_manager_server1')
        # ctx = {'user_id': 'admin', 'id': intra_extension_id, 'method': 'get_intra_extensions'}
        # if CONF.slave.slave_name:
        #     ctx['call_master'] = True
        # intra_extension = call(
        #     endpoint="security_router",
        #     ctx=ctx,
        #     method='route',
        #     args={}
        # )
        LOG.info("Starting MQ server with topic: {}".format(self.TOPIC))
        # if "intra_extensions" not in intra_extension:
        #     LOG.error("Error reading intra_extension from router")
        #     LOG.error("intra_extension: {}".format(intra_extension))
        #     raise IntraExtensionUnknown
        # intra_extension_id = list(intra_extension["intra_extensions"].keys())[0]
        self.endpoints = [
            APIList((Status, Logs)),
            Status(),
            Logs(),
            Models(),
            MetaRules(),
            MetaData(),
            Policies(),
            Perimeter(),
            Data(),
            Assignments(),
            Rules(),
            PDP(),
            Master()
        ]
        self.server = oslo_messaging.get_rpc_server(self.transport, self.target, self.endpoints,
                                                    executor='threading',
                                                    access_policy=oslo_messaging.DefaultRPCAccessPolicy)

    @staticmethod
    def __get_transport_url():
        messenger = configuration.get_configuration(configuration.MESSENGER)["messenger"]
        return messenger['url']

    def run(self):
        try:
            self.server.start()
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            print("Stopping server by crtl+c")
        except SystemExit:
            print("Stopping server")

        self.server.stop()
        self.server.wait()