[DOC] [REF] Refactored architecture chapter.
[odoo/odoo.git] / doc / 02_architecture.rst
1 ========================================
2 Architecture
3 ========================================
4
5 OpenERP as a multitenant three-tiers architecture
6 =================================================
7
8 This section presents the OpenERP architecture along with technology details
9 of the application. The tiers composing OpenERP are presented. Communication
10 means and protocols between the application components are also presented.
11 Some details about used development languages and technology stack are then summarized.
12
13 OpenERP is a `multitenant <http://en.wikipedia.org/wiki/Multitenancy>`_, `three-tiers architecture 
14 <http://en.wikipedia.org/wiki/Multitier_architecture#Three-tier_architecture>`_:
15 database tier for data storage, application tier for processing and functionalities
16 and presentation tier providing user interface. Those are separate layers
17 inside OpenERP. The application tier itself is written as a core; multiple
18 additional modules can be installed in order to create a particular instance
19 of OpenERP adapted to specific needs and requirements. Moreover, OpenERP
20 follows the Model-View-Controller (MVC) architectural pattern.
21
22 A typical deployment of OpenERP is shown on `Figure 1`_. This deployment is
23 called Web embedded deployment. As shown, an OpenERP system consists of
24 three main components:
25
26 - a PostgreSQL database server which contains all OpenERP databases. 
27   Databases contain all application data, and also most of the OpenERP
28   system configuration elements. Note that this server can possibly be
29   deployed using clustered databases.
30 - the OpenERP Server, which contains all the enterprise logic and ensures
31   that OpenERP runs optimally. One layer of the server is dedicated to
32   communicate and interface with the PostgreSQL database, the ORM engine.
33   Another layer allows communications between the server and a web browser,
34   the Web layer. Having more than one server is possible, for example in
35   conjunction with a load balancing mechanism.
36 - the client, which allow users to access OpenERP. Two clients exist, a
37   desktop GTK client and a browser-based client
38
39  - the GTK client access directly to the OpenERP Server
40  - users can also use standard web browsers to access OpenERP. In that
41    case, an OpenERP application is loaded. It handles communications between
42    the browser and the Web layer of the server.
43
44 The database server and the OpenERP server can be installed on the same computer,
45 or distributed onto separate computer servers, for example for performance considerations.
46
47 .. _`Figure 1`:
48 .. figure:: _static/02_openerp_architecture.png
49    :width: 50%
50    :alt: OpenERP 6.1 architecture for embedded web deployment
51    :align: center
52    
53    OpenERP 6.1 architecture for embedded web deployment
54
55 The next subsections give details about the different tiers of the OpenERP
56 architecture.
57
58 PostgreSQL database
59 +++++++++++++++++++
60
61 The data tier of OpenERP is provided by a PostgreSQL relational database.
62 While direct SQL queries can be executed from OpenERP modules, most accesses
63 to the relational database are done through the server Object Relational
64 Mapping layer.
65
66 Databases contain all application data, and also most of the OpenERP system
67 configuration elements. Note that this server can possibly be deployed using
68 clustered databases.
69
70 OpenERP server
71 ++++++++++++++
72
73 OpenERP provides an application server on which specific business applications
74 can be built. It is also a complete development framework, offering a range
75 of features to write those applications. Among those features, the OpenERP
76 ORM provides functionalities and an interface on top of the PostgreSQL server.
77 The OpenERP server also features a specific layer designed to communicate
78 with the web browser-based client. This layer connects users using standard
79 browsers to the server.
80
81 From a developer perspective, the server acts both as a library which brings
82 the above benefits while hiding the low-level details, and as a simple way
83 to install, configure and run the written applications. The server also contains
84 other services, such as extensible data models and view, workflow engine or
85 reports engine. However, those are OpenERP services not specifically related
86 to security, and are therefore not discussed in details in this document.
87
88 **Server - ORM**
89
90 The Object Relational Mapping ORM layer is one of the salient features of
91 the OpenERP Server. It provides additional and essential functionalities
92 on top of PostgreSQL server. Data models are described in Python and OpenERP
93 creates the underlying database tables using this ORM. All the benefits of
94 RDBMS such as unique constraints, relational integrity or efficient querying
95 are used and completed by Python flexibility. For instance, arbitrary constraints
96 written in Python can be added to any model. Different modular extensibility
97 mechanisms are also afforded by OpenERP.
98
99 It is important to understand the ORM responsibility before attempting to
100 by-pass it and to access directly the underlying database via raw SQL queries.
101 When using the ORM, OpenERP can make sure the data remains free of any corruption.
102 For instance, a module can react to data creation in a particular table.
103 This behavior can occur only if queries go through the ORM.
104
105 The services granted by the ORM are among other :
106
107  - consistency validation by powerful validity checks,
108  - providing an interface on objects (methods, references, ...) allowing
109    to design and implement efficient modules,
110  - row-level security per user and group; more details about users and user
111    groups are given in the section Users and User Roles,
112  - complex actions on a group of resources,
113  - inheritance service allowing fine modeling of new resources
114
115 **Server - Web**
116
117 The web layer offers an interface to communicate with standard browsers.
118 In the 6.1 version of OpenERP, the web-client has been rewritten and integrated
119 into the OpenERP server tier. This layer relies on CherryPy for the routing
120 layer of communications, especially for session and cookies management.
121
122 **Modules**
123
124 By itself, the OpenERP server is a core. For any enterprise, the value of
125 OpenERP lies in its different modules. The role of the modules is to implement
126 any business requirement. The server is the only necessary component to
127 add modules. Any official OpenERP release includes a lot of modules, and
128 hundreds of modules are available thanks to the community. Examples of
129 such modules are Account, CRM, HR, Marketing, MRP, Sale, etc.
130
131 Clients
132 +++++++
133
134 As the application logic is mainly contained server-side, the client is
135 conceptually simple. It issues a request to the server, gets data back
136 and display the result (e.g. a list of customers) in different ways
137 (as forms, lists, calendars, ...). Upon user actions, it sends queries
138 to modify data to the server.
139
140 Two clients can be used for user access to OpenERP, a GTK client and a
141 browser-based client. The GTK client communicates directly with the server.
142 Using the GTK client requires the client to be installed on the workstation
143 of each user.
144
145 The browser-based client holds an OpenERP application that handles communications
146 between the browser and the Web layer of the server. The static code of the web
147 application is minimal. It consists of a minimal flow of HTML that is in charge
148 of loading the application code in Javascript. This client-side OpenERP application
149 sends user requests to the server, and gets data back. Data management is
150 done dynamically in this client. Using this client is therefore easy for
151 users, but also for administrators because it does not require any software
152 installation on the user machine.
153
154
155 MVC architecture in OpenERP
156 ===========================
157
158 According to `Wikipedia <http://en.wikipedia.org/wiki/Model-view-controller>`_,
159 "a Model-view-controller (MVC) is an architectural pattern used in software
160 engineering". In complex computer applications presenting lots of data to
161 the user, one often wishes to separate data (model) and user interface (view)
162 concerns. Changes to the user interface does therefore not impact data
163 management, and data can be reorganized without changing the user interface.
164 The model-view-controller solves this problem by decoupling data access
165 and business logic from data presentation and user interaction, by
166 introducing an intermediate component: the controller.
167
168 .. _`Figure 3`:
169 .. figure:: _static/02_mvc_diagram.png
170    :width: 35%
171    :alt: Model-View-Controller diagram
172    :align: center
173    
174    Model-View-Controller diagram
175
176 For example in the diagram above, the solid lines for the arrows starting
177 from the controller and going to both the view and the model mean that the
178 controller has a complete access to both the view and the model. The dashed
179 line for the arrow going from the view to the controller means that the view
180 has a limited access to the controller. The reasons of this design are :
181
182  - From **View** to **Model** : the model sends notification to the view
183    when its data has been modified in order the view to redraw its content.
184    The model doesn't need to know the inner workings of the view to perform
185    this operation. However, the view needs to access the internal parts of the model.
186  - From **View** to **Controller** : the reason why the view has limited
187    access to the controller is because the dependencies from the view to
188    the controller need to be minimal: the controller can be replaced at
189    any moment. 
190
191 OpenERP follows the MVC semantic with
192
193  - model : The PostgreSQL tables.
194  - view : views are defined in XML files in OpenERP.
195  - controller : The objects of OpenERP. 
196
197
198 Network communications
199 ======================
200
201 GTK clients communicate with the OpenERP server using XML-RPC protocol by
202 default. However, using a secured version XML-RPCS is possible when configurating
203 your OpenERP instance. In previous versions of OpenERP, a custom protocol
204 called NET-RPC was used. It was a binary version of the XML-RPC protocol,
205 allowing faster communications. However, this protocol will no longer be
206 used in OpenERP. The use of JSON-RPC is also planned for the 6.1 version
207 of OpenERP.
208
209 Web-based clients communicate using HTTP protocol. As for XML-RPC, it is
210 possible to configure OpenERP to use secured HTTPS connections.
211
212 Services and WSGI
213 =================
214
215 Everything in OpenERP, and objects methods in particular, are exposed via
216 the network and a security layer. Access to the data model is in fact a ‘service’
217 and it is possible to expose new services. For instance, a WebDAV service and
218 a FTP service are available.
219
220 While not mandatory, the services can make use of the `WSGI
221 <http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface>`_ stack. WSGI is
222 a standard solution in the Python ecosystem to write HTTP servers, applications,
223 and middleware which can be used in a mix-and-match fashion. By using WSGI,
224 it is possible to run OpenERP in any WSGI compliant server. It is also
225 possible to use OpenERP to host a WSGI application.
226
227 A striking example of this possibility is the OpenERP Web layer that is
228 the server-side counter part to the web clients. It provides the requested
229 data to the browser and manages web sessions. It is a WSGI-compliant application.
230 As such, it can be run as a stand-alone HTTP server or embedded inside OpenERP.