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.
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/CheckRequest.h>
33 #include <request/InsertOrUpdateBucketRequest.h>
34 #include <request/RemoveBucketRequest.h>
35 #include <request/RequestContext.h>
36 #include <request/SetPoliciesRequest.h>
37 #include <request/SignalRequest.h>
38 #include <response/CheckResponse.h>
39 #include <response/CodeResponse.h>
40 #include <storage/Storage.h>
42 #include <sockets/SocketManager.h>
53 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
54 LOGD("Processing signal: [%d]", request->signalNumber());
56 switch (request->signalNumber()) {
58 LOGI("SIGTERM received!");
59 m_socketManager->mainLoopStop();
64 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
65 PolicyResult result(PredefinedPolicyType::DENY);
66 if (check(context, request->key(), result)) {
67 context->returnResponse(context, std::make_shared<CheckResponse>(result,
68 request->sequenceNumber()));
72 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
73 PolicyResult& result) {
74 result = m_storage->checkPolicy(key);
76 switch (result.policyType()) {
77 case PredefinedPolicyType::ALLOW :
78 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
80 case PredefinedPolicyType::DENY :
81 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
84 //todo pass question to proper plugin that:
85 // 1) returns false when answer has to be waited for (UI)
86 // 2) returns true if Response is to be generated
87 // In case 1) context should be saved in plugin in order to return answer when ready.
89 //in case no proper plugin is found
90 throw PluginNotFoundException(result);
93 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
94 auto code = CodeResponse::Code::OK;
97 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
99 } catch (const DefaultBucketSetNoneException &ex) {
100 code = CodeResponse::Code::NOT_ALLOWED;
103 context->returnResponse(context, std::make_shared<CodeResponse>(code,
104 request->sequenceNumber()));
107 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
108 auto code = CodeResponse::Code::OK;
110 m_storage->deleteBucket(request->bucketId());
112 } catch (const BucketNotExistsException &ex) {
113 code = CodeResponse::Code::NO_BUCKET;
114 } catch (const DefaultBucketDeletionException &ex) {
115 code = CodeResponse::Code::NOT_ALLOWED;
117 context->returnResponse(context, std::make_shared<CodeResponse>(code,
118 request->sequenceNumber()));
121 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
122 auto code = CodeResponse::Code::OK;
124 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
125 m_storage->deletePolicies(request->policiesToBeRemoved());
127 } catch (const BucketNotExistsException &ex) {
128 code = CodeResponse::Code::NO_BUCKET;
130 context->returnResponse(context, std::make_shared<CodeResponse>(code,
131 request->sequenceNumber()));
134 void Logic::onPoliciesChanged(void) {
136 m_socketManager->disconnectAllClients();
137 //todo remove all saved contexts (if there will be any saved contexts)
140 } // namespace Cynara