Replace @file commands for Doxygen compatibility
[platform/core/security/cynara.git] / src / service / logic / Logic.cpp
1 /*
2  * Copyright (c) 2014 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        src/service/logic/Logic.cpp
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @version     1.0
20  * @brief       This file implements main class of logic layer in cynara service
21  */
22
23 #include <log/log.h>
24 #include <common.h>
25 #include <exceptions/PluginNotFoundException.h>
26 #include <exceptions/BucketNotExistsException.h>
27 #include <exceptions/DefaultBucketDeletionException.h>
28 #include <exceptions/DefaultBucketSetNoneException.h>
29 #include <signal.h>
30
31 #include <main/Cynara.h>
32 #include <request/AdminCheckRequest.h>
33 #include <request/CheckRequest.h>
34 #include <request/InsertOrUpdateBucketRequest.h>
35 #include <request/RemoveBucketRequest.h>
36 #include <request/RequestContext.h>
37 #include <request/SetPoliciesRequest.h>
38 #include <request/SignalRequest.h>
39 #include <response/CheckResponse.h>
40 #include <response/CodeResponse.h>
41 #include <storage/Storage.h>
42
43 #include <sockets/SocketManager.h>
44
45 #include "Logic.h"
46
47 namespace Cynara {
48 Logic::Logic() {
49 }
50
51 Logic::~Logic() {
52 }
53
54 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
55     LOGD("Processing signal: [%d]", request->signalNumber());
56
57     switch (request->signalNumber()) {
58     case SIGTERM:
59         LOGI("SIGTERM received!");
60         m_socketManager->mainLoopStop();
61         break;
62     }
63 }
64
65 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
66     PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
67                                                  request->recursive());
68
69     context->returnResponse(context, std::make_shared<CheckResponse>(result,
70                             request->sequenceNumber()));
71 }
72
73 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
74     PolicyResult result(PredefinedPolicyType::DENY);
75     if (check(context, request->key(), result)) {
76         context->returnResponse(context, std::make_shared<CheckResponse>(result,
77                                 request->sequenceNumber()));
78     }
79 }
80
81 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
82                   PolicyResult& result) {
83     result = m_storage->checkPolicy(key);
84
85     switch (result.policyType()) {
86         case PredefinedPolicyType::ALLOW :
87             LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
88             return true;
89         case PredefinedPolicyType::DENY :
90             LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
91             return true;
92     }
93     //todo pass question to proper plugin that:
94     //  1) returns false when answer has to be waited for (UI)
95     //  2) returns true if Response is to be generated
96     // In case 1) context should be saved in plugin in order to return answer when ready.
97
98     //in case no proper plugin is found
99     throw PluginNotFoundException(result);
100 }
101
102 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
103     auto code = CodeResponse::Code::OK;
104
105     try {
106         m_storage->addOrUpdateBucket(request->bucketId(), request->result());
107         onPoliciesChanged();
108     } catch (const DefaultBucketSetNoneException &ex) {
109         code = CodeResponse::Code::NOT_ALLOWED;
110     }
111
112     context->returnResponse(context, std::make_shared<CodeResponse>(code,
113                             request->sequenceNumber()));
114 }
115
116 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
117     auto code = CodeResponse::Code::OK;
118     try {
119         m_storage->deleteBucket(request->bucketId());
120         onPoliciesChanged();
121     } catch (const BucketNotExistsException &ex) {
122         code = CodeResponse::Code::NO_BUCKET;
123     } catch (const DefaultBucketDeletionException &ex) {
124         code = CodeResponse::Code::NOT_ALLOWED;
125     }
126     context->returnResponse(context, std::make_shared<CodeResponse>(code,
127                             request->sequenceNumber()));
128 }
129
130 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
131     auto code = CodeResponse::Code::OK;
132     try {
133         m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
134         m_storage->deletePolicies(request->policiesToBeRemoved());
135         onPoliciesChanged();
136     } catch (const BucketNotExistsException &ex) {
137         code = CodeResponse::Code::NO_BUCKET;
138     }
139     context->returnResponse(context, std::make_shared<CodeResponse>(code,
140                             request->sequenceNumber()));
141 }
142
143 void Logic::onPoliciesChanged(void) {
144     m_storage->save();
145     m_socketManager->disconnectAllClients();
146     //todo remove all saved contexts (if there will be any saved contexts)
147 }
148
149 } // namespace Cynara