2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file src/service/logic/Logic.cpp
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
20 * @brief This file implements main class of logic layer in cynara service
25 #include <exceptions/BucketNotExistsException.h>
26 #include <exceptions/DatabaseException.h>
27 #include <exceptions/DefaultBucketDeletionException.h>
28 #include <exceptions/DefaultBucketSetNoneException.h>
29 #include <exceptions/PluginErrorException.h>
30 #include <exceptions/PluginNotFoundException.h>
34 #include <main/Cynara.h>
35 #include <request/AdminCheckRequest.h>
36 #include <request/CancelRequest.h>
37 #include <request/CheckRequest.h>
38 #include <request/InsertOrUpdateBucketRequest.h>
39 #include <request/RemoveBucketRequest.h>
40 #include <request/RequestContext.h>
41 #include <request/SetPoliciesRequest.h>
42 #include <request/SignalRequest.h>
43 #include <response/CancelResponse.h>
44 #include <response/CheckResponse.h>
45 #include <response/CodeResponse.h>
46 #include <storage/Storage.h>
48 #include <sockets/SocketManager.h>
59 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
60 LOGD("Processing signal: [%d]", request->signalNumber());
62 switch (request->signalNumber()) {
64 LOGI("SIGTERM received!");
65 m_socketManager->mainLoopStop();
70 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
71 PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
72 request->recursive());
74 context->returnResponse(context, std::make_shared<CheckResponse>(result,
75 request->sequenceNumber()));
78 void Logic::execute(RequestContextPtr context, CancelRequestPtr request) {
80 context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
83 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
84 PolicyResult result(PredefinedPolicyType::DENY);
85 if (check(context, request->key(), result)) {
86 context->returnResponse(context, std::make_shared<CheckResponse>(result,
87 request->sequenceNumber()));
91 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
92 PolicyResult& result) {
93 result = m_storage->checkPolicy(key);
95 switch (result.policyType()) {
96 case PredefinedPolicyType::ALLOW :
97 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
99 case PredefinedPolicyType::DENY :
100 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
104 ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
106 throw PluginNotFoundException(result);
109 AgentType requiredAgent;
110 PluginData pluginData;
112 auto ret = plugin->check(key.client().toString(), key.user().toString(),
113 key.privilege().toString(), result, requiredAgent, pluginData);
116 case ExternalPluginInterface::PluginStatus::ANSWER_READY:
118 case ExternalPluginInterface::PluginStatus::ANSWER_NOTREADY:
119 //todo send request to agent
120 //context should be saved in plugin in order to return answer when ready
123 throw PluginErrorException(key);
127 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
128 auto code = CodeResponse::Code::OK;
131 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
133 } catch (const DatabaseException &ex) {
134 code = CodeResponse::Code::FAILED;
135 } catch (const DefaultBucketSetNoneException &ex) {
136 code = CodeResponse::Code::NOT_ALLOWED;
139 context->returnResponse(context, std::make_shared<CodeResponse>(code,
140 request->sequenceNumber()));
143 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
144 auto code = CodeResponse::Code::OK;
146 m_storage->deleteBucket(request->bucketId());
148 } catch (const DatabaseException &ex) {
149 code = CodeResponse::Code::FAILED;
150 } catch (const BucketNotExistsException &ex) {
151 code = CodeResponse::Code::NO_BUCKET;
152 } catch (const DefaultBucketDeletionException &ex) {
153 code = CodeResponse::Code::NOT_ALLOWED;
155 context->returnResponse(context, std::make_shared<CodeResponse>(code,
156 request->sequenceNumber()));
159 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
160 auto code = CodeResponse::Code::OK;
162 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
163 m_storage->deletePolicies(request->policiesToBeRemoved());
165 } catch (const DatabaseException &ex) {
166 code = CodeResponse::Code::FAILED;
167 } catch (const BucketNotExistsException &ex) {
168 code = CodeResponse::Code::NO_BUCKET;
170 context->returnResponse(context, std::make_shared<CodeResponse>(code,
171 request->sequenceNumber()));
174 void Logic::onPoliciesChanged(void) {
176 m_socketManager->disconnectAllClients();
177 //todo remove all saved contexts (if there will be any saved contexts)
180 } // namespace Cynara