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>
30 #include <main/Cynara.h>
31 #include <request/CheckRequest.h>
32 #include <request/InsertOrUpdateBucketRequest.h>
33 #include <request/RemoveBucketRequest.h>
34 #include <request/RequestContext.h>
35 #include <request/SetPoliciesRequest.h>
36 #include <request/SignalRequest.h>
37 #include <response/CheckResponse.h>
38 #include <response/CodeResponse.h>
39 #include <storage/Storage.h>
41 #include <sockets/SocketManager.h>
52 void Logic::execute(RequestContextPtr context UNUSED, SignalRequestPtr request) {
53 LOGD("Processing signal: [%d]", request->signalNumber());
55 switch (request->signalNumber()) {
57 LOGI("SIGTERM received!");
58 m_socketManager->mainLoopStop();
63 void Logic::execute(RequestContextPtr context, CheckRequestPtr request) {
64 PolicyResult result(PredefinedPolicyType::DENY);
65 if (check(context, request->key(), result)) {
66 context->returnResponse(context, std::make_shared<CheckResponse>(result,
67 request->sequenceNumber()));
71 bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key,
72 PolicyResult& result) {
73 result = m_storage->checkPolicy(key);
75 switch (result.policyType()) {
76 case PredefinedPolicyType::ALLOW :
77 LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
79 case PredefinedPolicyType::DENY :
80 LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
83 //todo pass question to proper plugin that:
84 // 1) returns false when answer has to be waited for (UI)
85 // 2) returns true if Response is to be generated
86 // In case 1) context should be saved in plugin in order to return answer when ready.
88 //in case no proper plugin is found
89 throw PluginNotFoundException(result);
92 void Logic::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) {
93 m_storage->addOrUpdateBucket(request->bucketId(), request->result());
94 //todo add saving to database
95 context->returnResponse(context, std::make_shared<CodeResponse>(CodeResponse::Code::OK,
96 request->sequenceNumber()));
99 void Logic::execute(RequestContextPtr context, RemoveBucketRequestPtr request) {
100 auto code = CodeResponse::Code::OK;
102 m_storage->deleteBucket(request->bucketId());
103 //todo add saving to database
104 } catch (const BucketNotExistsException &ex) {
105 code = CodeResponse::Code::NO_BUCKET;
106 } catch (const DefaultBucketDeletionException &ex) {
107 code = CodeResponse::Code::NOT_ALLOWED;
109 context->returnResponse(context, std::make_shared<CodeResponse>(code,
110 request->sequenceNumber()));
113 void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
114 auto code = CodeResponse::Code::OK;
116 m_storage->insertPolicies(request->policiesToBeInsertedOrUpdated());
117 m_storage->deletePolicies(request->policiesToBeRemoved());
118 //todo add saving to database
119 } catch (const BucketNotExistsException &ex) {
120 code = CodeResponse::Code::NO_BUCKET;
122 context->returnResponse(context, std::make_shared<CodeResponse>(code,
123 request->sequenceNumber()));
126 } // namespace Cynara