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
28 #include <exceptions/BucketNotExistsException.h>
29 #include <exceptions/DatabaseException.h>
30 #include <exceptions/DefaultBucketDeletionException.h>
31 #include <exceptions/DefaultBucketSetNoneException.h>
32 #include <exceptions/InvalidBucketIdException.h>
33 #include <exceptions/PluginErrorException.h>
34 #include <exceptions/PluginNotFoundException.h>
35 #include <exceptions/UnexpectedErrorException.h>
36 #include <request/AdminCheckRequest.h>
37 #include <request/AgentRegisterRequest.h>
38 #include <request/CancelRequest.h>
39 #include <request/CheckRequest.h>
40 #include <request/InsertOrUpdateBucketRequest.h>
41 #include <request/RemoveBucketRequest.h>
42 #include <request/RequestContext.h>
43 #include <request/SetPoliciesRequest.h>
44 #include <request/SignalRequest.h>
45 #include <response/AgentRegisterResponse.h>
46 #include <response/CancelResponse.h>
47 #include <response/CheckResponse.h>
48 #include <response/CodeResponse.h>
50 #include <main/Cynara.h>
51 #include <agent/AgentManager.h>
52 #include <sockets/SocketManager.h>
53 #include <storage/Storage.h>
55 #include <cynara-plugin.h>
66 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
67 LOGD("Processing signal: [%d]", request->signalNumber());
69 switch (request->signalNumber()) {
71 LOGI("SIGTERM received!");
72 m_socketManager->mainLoopStop();
77 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
78 PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
79 request->recursive());
81 context->returnResponse(context, std::make_shared<CheckResponse>(result,
82 request->sequenceNumber()));
85 void Logic::execute(RequestContextPtr context, AgentRegisterRequestPtr request) {
86 auto result = m_agentManager->registerAgent(request->agentType(), context->responseQueue());
87 context->returnResponse(context, std::make_shared<AgentRegisterResponse>(
88 result, request->sequenceNumber()));
91 void Logic::execute(RequestContextPtr context, CancelRequestPtr request) {
93 context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
96 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
97 PolicyResult result(PredefinedPolicyType::DENY);
98 if (check(context, request->key(), result)) {
99 context->returnResponse(context, std::make_shared<CheckResponse>(result,
100 request->sequenceNumber()));
104 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
105 PolicyResult& result) {
106 result = m_storage->checkPolicy(key);
108 switch (result.policyType()) {
109 case PredefinedPolicyType::ALLOW :
110 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
112 case PredefinedPolicyType::DENY :
113 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
117 ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
119 throw PluginNotFoundException(result);
122 ServicePluginInterfacePtr servicePlugin =
123 std::dynamic_pointer_cast<ServicePluginInterface>(plugin);
125 throw PluginNotFoundException(result);
128 AgentType requiredAgent;
129 PluginData pluginData;
131 auto ret = servicePlugin->check(key.client().toString(), key.user().toString(),
132 key.privilege().toString(), result, requiredAgent, pluginData);
135 case ServicePluginInterface::PluginStatus::ANSWER_READY:
137 case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY:
138 //todo send request to agent
139 //context should be saved in plugin in order to return answer when ready
142 throw PluginErrorException(key);
146 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
147 auto code = CodeResponse::Code::OK;
150 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
152 } catch (const DatabaseException &ex) {
153 code = CodeResponse::Code::FAILED;
154 } catch (const DefaultBucketSetNoneException &ex) {
155 code = CodeResponse::Code::NOT_ALLOWED;
156 } catch (const InvalidBucketIdException &ex) {
157 code = CodeResponse::Code::NOT_ALLOWED;
160 context->returnResponse(context, std::make_shared<CodeResponse>(code,
161 request->sequenceNumber()));
164 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
165 auto code = CodeResponse::Code::OK;
167 m_storage->deleteBucket(request->bucketId());
169 } catch (const DatabaseException &ex) {
170 code = CodeResponse::Code::FAILED;
171 } catch (const BucketNotExistsException &ex) {
172 code = CodeResponse::Code::NO_BUCKET;
173 } catch (const DefaultBucketDeletionException &ex) {
174 code = CodeResponse::Code::NOT_ALLOWED;
176 context->returnResponse(context, std::make_shared<CodeResponse>(code,
177 request->sequenceNumber()));
180 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
181 auto code = CodeResponse::Code::OK;
183 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
184 m_storage->deletePolicies(request->policiesToBeRemoved());
186 } catch (const DatabaseException &ex) {
187 code = CodeResponse::Code::FAILED;
188 } catch (const BucketNotExistsException &ex) {
189 code = CodeResponse::Code::NO_BUCKET;
191 context->returnResponse(context, std::make_shared<CodeResponse>(code,
192 request->sequenceNumber()));
195 void Logic::contextClosed(RequestContextPtr context UNUSED) {
196 //We don't care now, but we will
199 void Logic::onPoliciesChanged(void) {
201 m_socketManager->disconnectAllClients();
202 //todo remove all saved contexts (if there will be any saved contexts)
205 } // namespace Cynara