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/PluginNotFoundException.h>
26 #include <exceptions/BucketNotExistsException.h>
27 #include <exceptions/DefaultBucketDeletionException.h>
28 #include <exceptions/DefaultBucketSetNoneException.h>
31 #include <main/Cynara.h>
32 #include <request/AdminCheckRequest.h>
33 #include <request/CancelRequest.h>
34 #include <request/CheckRequest.h>
35 #include <request/InsertOrUpdateBucketRequest.h>
36 #include <request/RemoveBucketRequest.h>
37 #include <request/RequestContext.h>
38 #include <request/SetPoliciesRequest.h>
39 #include <request/SignalRequest.h>
40 #include <response/CancelResponse.h>
41 #include <response/CheckResponse.h>
42 #include <response/CodeResponse.h>
43 #include <storage/Storage.h>
45 #include <sockets/SocketManager.h>
56 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
57 LOGD("Processing signal: [%d]", request->signalNumber());
59 switch (request->signalNumber()) {
61 LOGI("SIGTERM received!");
62 m_socketManager->mainLoopStop();
67 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
68 PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
69 request->recursive());
71 context->returnResponse(context, std::make_shared<CheckResponse>(result,
72 request->sequenceNumber()));
75 void Logic::execute(RequestContextPtr context, CancelRequestPtr request) {
77 context->returnResponse(context, std::make_shared<CancelResponse>(request->sequenceNumber()));
80 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
81 PolicyResult result(PredefinedPolicyType::DENY);
82 if (check(context, request->key(), result)) {
83 context->returnResponse(context, std::make_shared<CheckResponse>(result,
84 request->sequenceNumber()));
88 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
89 PolicyResult& result) {
90 result = m_storage->checkPolicy(key);
92 switch (result.policyType()) {
93 case PredefinedPolicyType::ALLOW :
94 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
96 case PredefinedPolicyType::DENY :
97 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
101 ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType());
103 throw PluginNotFoundException(result);
106 AgentType requiredAgent;
107 PluginData pluginData;
109 auto ret = plugin->check(key.client().toString(), key.user().toString(),
110 key.privilege().toString(), result, requiredAgent, pluginData);
113 case ExternalPluginInterface::PluginStatus::ANSWER_READY:
115 case ExternalPluginInterface::PluginStatus::ANSWER_NOTREADY:
116 //todo send request to agent
117 //context should be saved in plugin in order to return answer when ready
120 //todo make additional class
121 throw std::runtime_error("Plugin error");
125 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
126 auto code = CodeResponse::Code::OK;
129 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
131 } catch (const DefaultBucketSetNoneException &ex) {
132 code = CodeResponse::Code::NOT_ALLOWED;
135 context->returnResponse(context, std::make_shared<CodeResponse>(code,
136 request->sequenceNumber()));
139 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
140 auto code = CodeResponse::Code::OK;
142 m_storage->deleteBucket(request->bucketId());
144 } catch (const BucketNotExistsException &ex) {
145 code = CodeResponse::Code::NO_BUCKET;
146 } catch (const DefaultBucketDeletionException &ex) {
147 code = CodeResponse::Code::NOT_ALLOWED;
149 context->returnResponse(context, std::make_shared<CodeResponse>(code,
150 request->sequenceNumber()));
153 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
154 auto code = CodeResponse::Code::OK;
156 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
157 m_storage->deletePolicies(request->policiesToBeRemoved());
159 } catch (const BucketNotExistsException &ex) {
160 code = CodeResponse::Code::NO_BUCKET;
162 context->returnResponse(context, std::make_shared<CodeResponse>(code,
163 request->sequenceNumber()));
166 void Logic::onPoliciesChanged(void) {
168 m_socketManager->disconnectAllClients();
169 //todo remove all saved contexts (if there will be any saved contexts)
172 } // namespace Cynara