tizen beta release
[framework/web/wrt-commons.git] / modules / ace / engine / PolicyEnforcementPoint.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain 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,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    security_logic.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @author  Ming Jin(ming79.jin@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for security logic
22  */
23 #include <dpl/ace/PolicyEnforcementPoint.h>
24
25 #include <sstream>
26 #include <algorithm>
27 #include <list>
28 #include <string>
29 #include <sstream>
30 #include <stdexcept>
31 #include <cstdlib>
32 #include <map>
33
34 #include <dpl/assert.h>
35 #include <dpl/exception.h>
36 #include <dpl/log/log.h>
37
38 #include <dpl/ace/PolicyEvaluatorFactory.h>
39 #include <dpl/ace/PolicyResult.h>
40 #include <dpl/ace/Request.h>
41
42 using namespace WrtDB;
43
44 PolicyEnforcementPoint::PolicyEnforcementPoint() :
45     m_wrt(0),
46     m_res(0),
47     m_sys(0),
48     m_pdp(0),
49     m_pip(0)
50 {}
51
52 void PolicyEnforcementPoint::terminate()
53 {
54     LogInfo("PolicyEnforcementPoint is being deinitialized.");
55
56     delete m_sys;
57     delete m_res;
58     delete m_wrt;
59     delete m_pdp;
60     delete m_pip;
61     m_sys = 0;
62     m_res = 0;
63     m_wrt = 0;
64     m_pdp = 0;
65     m_pip = 0;
66 }
67
68 PolicyEnforcementPoint::~PolicyEnforcementPoint()
69 {
70     Assert((m_sys == 0) && "You must run "
71            "PolicyEnforcementPoint::Deinitialize before exit program!");
72 }
73
74 void PolicyEnforcementPoint::initialize(
75         IWebRuntime *wrt,
76         IResourceInformation *resource,
77         IOperationSystem *operation)
78 {
79     if (m_wrt) {
80         ThrowMsg(PEPException::AlreadyInitialized,
81                  "Policy Enforcement Point is already initialzed");
82     }
83
84     m_wrt = wrt;
85     m_res = resource;
86     m_sys = operation;
87
88     if (this->m_pip != NULL) {
89         this->m_pip->update(m_wrt, m_res, m_sys);
90         return;
91     }
92
93     this->m_pip = new PolicyInformationPoint(wrt, m_res, m_sys);
94     this->m_pdp = new PolicyEvaluator(m_pip);
95
96     if (!this->m_pdp->initPDP()) {
97         Assert(0);
98     }
99 }
100
101 PolicyResult PolicyEnforcementPoint::check(Request &request)
102 {
103     return m_pdp->getPolicyForRequest(request);
104 }
105
106 void PolicyEnforcementPoint::updatePolicy(const std::string &policy)
107 {
108     LogDebug("ACE updatePolicy: " << policy);
109     int errorCode = 0;
110
111     if (m_pdp == NULL) {
112         LogError("Evaluator not set. Ignoring message.");
113         Assert(false && "UpdateClient error on receiving event");
114     } else {
115         LogDebug("Emitting update signal.");
116         errorCode = m_pdp->updatePolicy(policy.c_str());
117     }
118
119     LogDebug("Sending reponse: " << errorCode);
120 }
121
122 OptionalPolicyResult PolicyEnforcementPoint::checkFromCache(Request &request)
123 {
124    return m_pdp->getPolicyForRequestFromCache(request);
125    // return OptionalPolicyResult();
126 }
127
128 OptionalPolicyResult PolicyEnforcementPoint::check(Request &request,
129                                                    bool fromCacheOnly)
130 {
131    return m_pdp->getPolicyForRequest(request, fromCacheOnly);
132    // return OptionalPolicyResult();
133 }