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 <sockets/SocketManager.h>
52 #include <storage/Storage.h>
54 #include <cynara-plugin.h>
65 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
66 LOGD("Processing signal: [%d]", request->signalNumber());
68 switch (request->signalNumber()) {
70 LOGI("SIGTERM received!");
71 m_socketManager->mainLoopStop();
76 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
77 PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
78 request->recursive());
80 context->returnResponse(context, std::make_shared<CheckResponse>(result,
81 request->sequenceNumber()));
84 void Logic::execute(RequestContextPtr context, AgentRegisterRequestPtr request) {
86 context->returnResponse(context, std::make_shared<AgentRegisterResponse>(
87 AgentRegisterResponse::DONE, request->sequenceNumber()));
90 void Logic::execute(RequestContextPtr context, CancelRequestPtr request) {
92 context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
95 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
96 PolicyResult result(PredefinedPolicyType::DENY);
97 if (check(context, request->key(), result)) {
98 context->returnResponse(context, std::make_shared<CheckResponse>(result,
99 request->sequenceNumber()));
103 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
104 PolicyResult& result) {
105 result = m_storage->checkPolicy(key);
107 switch (result.policyType()) {
108 case PredefinedPolicyType::ALLOW :
109 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
111 case PredefinedPolicyType::DENY :
112 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
116 ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
118 throw PluginNotFoundException(result);
121 ServicePluginInterfacePtr servicePlugin =
122 std::dynamic_pointer_cast<ServicePluginInterface>(plugin);
124 throw PluginNotFoundException(result);
127 AgentType requiredAgent;
128 PluginData pluginData;
130 auto ret = servicePlugin->check(key.client().toString(), key.user().toString(),
131 key.privilege().toString(), result, requiredAgent, pluginData);
134 case ServicePluginInterface::PluginStatus::ANSWER_READY:
136 case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY:
137 //todo send request to agent
138 //context should be saved in plugin in order to return answer when ready
141 throw PluginErrorException(key);
145 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
146 auto code = CodeResponse::Code::OK;
149 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
151 } catch (const DatabaseException &ex) {
152 code = CodeResponse::Code::FAILED;
153 } catch (const DefaultBucketSetNoneException &ex) {
154 code = CodeResponse::Code::NOT_ALLOWED;
155 } catch (const InvalidBucketIdException &ex) {
156 code = CodeResponse::Code::NOT_ALLOWED;
159 context->returnResponse(context, std::make_shared<CodeResponse>(code,
160 request->sequenceNumber()));
163 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
164 auto code = CodeResponse::Code::OK;
166 m_storage->deleteBucket(request->bucketId());
168 } catch (const DatabaseException &ex) {
169 code = CodeResponse::Code::FAILED;
170 } catch (const BucketNotExistsException &ex) {
171 code = CodeResponse::Code::NO_BUCKET;
172 } catch (const DefaultBucketDeletionException &ex) {
173 code = CodeResponse::Code::NOT_ALLOWED;
175 context->returnResponse(context, std::make_shared<CodeResponse>(code,
176 request->sequenceNumber()));
179 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
180 auto code = CodeResponse::Code::OK;
182 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
183 m_storage->deletePolicies(request->policiesToBeRemoved());
185 } catch (const DatabaseException &ex) {
186 code = CodeResponse::Code::FAILED;
187 } catch (const BucketNotExistsException &ex) {
188 code = CodeResponse::Code::NO_BUCKET;
190 context->returnResponse(context, std::make_shared<CodeResponse>(code,
191 request->sequenceNumber()));
194 void Logic::contextClosed(RequestContextPtr context UNUSED) {
195 //We don't care now, but we will
198 void Logic::onPoliciesChanged(void) {
200 m_socketManager->disconnectAllClients();
201 //todo remove all saved contexts (if there will be any saved contexts)
204 } // namespace Cynara