FOSSology  4.4.0
Open Source License Compliance by Open Source Software
CompatibilityAgent.cc
1 /*
2  SPDX-FileCopyrightText: © 2024 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "CompatibilityAgent.hpp"
8 
9 #include "CompatibilityStatus.hpp"
10 
11 using namespace std;
12 
25 bool insertResultInDb(CompatibilityStatus status, bool verbosityDebug,
26  CompatibilityDatabaseHandler& databaseHandler,
27  unsigned long& pFileId, int agentId, unsigned long lid1,
28  unsigned long lid2, const string& ruleName)
29 {
30  string compatibility = (status == COMPATIBLE) ? "t" : "f";
31  if (verbosityDebug)
32  {
33  cout << ruleName << ((status == COMPATIBLE) ? "" : " not")
34  << " compatible, " << pFileId << '\n';
35  }
36  return databaseHandler.queryInsertResult(pFileId, agentId, lid1, lid2,
37  compatibility);
38 }
39 
43 CompatibilityAgent::CompatibilityAgent(int agentId, bool verbosityDebug) :
44  agentId(agentId), verbosityDebug(verbosityDebug)
45 {
46 }
47 
56  vector<unsigned long>& licId, unsigned long& pFileId,
57  CompatibilityDatabaseHandler& databaseHandler) const
58 {
59  vector<tuple<unsigned long, string>> licenseTypes;
60  CompatibilityStatus defaultStatus = databaseHandler.getDefaultRule();
61  string defaultRule = defaultStatus == COMPATIBLE ? "t" : "f";
62  bool res, resultExists;
63  licenseTypes = databaseHandler.queryLicDetails(licId);
64  size_t length = licenseTypes.size();
65  for (size_t first = 0; first < (length - 1); ++first)
66  {
67  for (size_t second = (first + 1); second < length; ++second)
68  {
69  CompatibilityStatus status;
70  unsigned long licenseId1, licenseId2;
71  licenseId1 = get<0>(licenseTypes[first]);
72  licenseId2 = get<0>(licenseTypes[second]);
73  resultExists = databaseHandler.check(licenseId1, licenseId2, pFileId);
74  if (resultExists)
75  {
76  continue;
77  }
78 
79  status = databaseHandler.queryRule1(licenseTypes[first],
80  licenseTypes[second]);
81  if (status != UNKNOWN)
82  {
83  res = insertResultInDb(status, verbosityDebug, databaseHandler, pFileId,
84  agentId, licenseId1, licenseId2, "rule1");
85  if (!res)
86  {
87  return res;
88  }
89  continue;
90  }
91 
92  status = databaseHandler.queryRule2(licenseTypes[first],
93  licenseTypes[second]);
94  if (status != UNKNOWN)
95  {
96  res = insertResultInDb(status, verbosityDebug, databaseHandler, pFileId,
97  agentId, licenseId1, licenseId2, "rule2");
98  if (!res)
99  {
100  return res;
101  }
102  continue;
103  }
104 
105  status = databaseHandler.queryRule3(licenseTypes[first],
106  licenseTypes[second]);
107  if (status != UNKNOWN)
108  {
109  res = insertResultInDb(status, verbosityDebug, databaseHandler, pFileId,
110  agentId, licenseId1, licenseId2, "rule3");
111  if (!res)
112  {
113  return res;
114  }
115  continue;
116  }
117 
118  res = databaseHandler.queryInsertResult(pFileId, agentId, licenseId1,
119  licenseId2, defaultRule);
120  if (verbosityDebug)
121  {
122  cout << "default rule " << pFileId << '\n';
123  }
124  }
125  }
126  return true;
127 }
128 
133 void CompatibilityAgent::setAgentId(const int agentId)
134 {
135  this->agentId = agentId;
136 }
CompatibilityAgent(int agentId, bool verbosityDebug)
bool checkCompatibilityForPfile(vector< unsigned long > &licId, unsigned long &pFileId, CompatibilityDatabaseHandler &databaseHandler) const
find the compatibility between the licenses using scheduler mode
void setAgentId(const int agentId)
Set the agent ID for the agent object.
bool queryInsertResult(unsigned long pFileId, int a_id, unsigned long id1, unsigned long id2, const string &comp)
insert the compatibility result in the comp_result table
bool check(unsigned long id1, unsigned long id2, unsigned long pFileId)
Checking whether the data is already present in database or not, to prevent redundancy.
CompatibilityStatus queryRule3(std::tuple< unsigned long, std::string > lic1, std::tuple< unsigned long, std::string > lic2) const
This rule uses license id and license type to find the compatibility.
std::vector< std::tuple< unsigned long, std::string > > queryLicDetails(const std::vector< unsigned long > &licId)
store the id and type of that respective license in vector of tuple for all the licenses
CompatibilityStatus queryRule2(std::tuple< unsigned long, std::string > lic1, std::tuple< unsigned long, std::string > lic2) const
This rule uses license type of both the licenses to find the compatibility.
CompatibilityStatus queryRule1(tuple< unsigned long, string > lic1, tuple< unsigned long, string > lic2) const
This rule uses id of both the licenses to find the compatibility.
CompatibilityStatus getDefaultRule() const
Get the default rule from DB as CompatibilityStatus.