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/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>
43 #include <sockets/SocketManager.h>
54 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
55 LOGD("Processing signal: [%d]", request->signalNumber());
57 switch (request->signalNumber()) {
59 LOGI("SIGTERM received!");
60 m_socketManager->mainLoopStop();
65 void Logic::execute(RequestContextPtr context, AdminCheckRequestPtr request) {
66 PolicyResult result = m_storage->checkPolicy(request->key(), request->startBucket(),
67 request->recursive());
69 context->returnResponse(context, std::make_shared<CheckResponse>(result,
70 request->sequenceNumber()));
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()));
81 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
82 PolicyResult& result) {
83 result = m_storage->checkPolicy(key);
85 switch (result.policyType()) {
86 case PredefinedPolicyType::ALLOW :
87 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
89 case PredefinedPolicyType::DENY :
90 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
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.
98 //in case no proper plugin is found
99 throw PluginNotFoundException(result);
102 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
103 auto code = CodeResponse::Code::OK;
106 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
108 } catch (const DefaultBucketSetNoneException &ex) {
109 code = CodeResponse::Code::NOT_ALLOWED;
112 context->returnResponse(context, std::make_shared<CodeResponse>(code,
113 request->sequenceNumber()));
116 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
117 auto code = CodeResponse::Code::OK;
119 m_storage->deleteBucket(request->bucketId());
121 } catch (const BucketNotExistsException &ex) {
122 code = CodeResponse::Code::NO_BUCKET;
123 } catch (const DefaultBucketDeletionException &ex) {
124 code = CodeResponse::Code::NOT_ALLOWED;
126 context->returnResponse(context, std::make_shared<CodeResponse>(code,
127 request->sequenceNumber()));
130 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
131 auto code = CodeResponse::Code::OK;
133 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
134 m_storage->deletePolicies(request->policiesToBeRemoved());
136 } catch (const BucketNotExistsException &ex) {
137 code = CodeResponse::Code::NO_BUCKET;
139 context->returnResponse(context, std::make_shared<CodeResponse>(code,
140 request->sequenceNumber()));
143 void Logic::onPoliciesChanged(void) {
145 m_socketManager->disconnectAllClients();
146 //todo remove all saved contexts (if there will be any saved contexts)
149 } // namespace Cynara