Back to index

nordugrid-arc-nox  1.1.0~rc6
ArcEvaluationCtx.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <arc/security/ArcPDP/attr/AttributeValue.h>
00006 
00007 #include "ArcEvaluationCtx.h"
00008 
00009 using namespace Arc;
00010 using namespace ArcSec;
00011 
00012 Logger ArcEvaluationCtx::logger(Arc::Logger::rootLogger, "ArcEvaluationCtx");
00013 
00014 ArcRequestTuple::ArcRequestTuple() : RequestTuple() {
00015   NS ns;
00016   ns["ra"]="http://www.nordugrid.org/schemas/request-arc";
00017   XMLNode tupledoc(ns,"ra:RequestItem");
00018   tupledoc.New(tuple);
00019 }
00020 
00021 RequestTuple* ArcRequestTuple::duplicate(const RequestTuple* req_tpl) {  
00022   XMLNode root = tuple;
00023   int n;
00024   //ArcRequestTuple* tpl = dynamic_cast<ArcRequestTuple*>(req_tpl);
00025 
00026   //Reconstruct the XMLNode by using the information inside req_tpl
00027 
00028   //Reconstruct subject 
00029   Subject::iterator sit;
00030   Subject req_sub = req_tpl->sub;
00031   XMLNode subject;
00032   if(!req_sub.empty())
00033     subject = root.NewChild("ra:Subject");
00034   n = req_sub.size();
00035   for(sit = req_sub.begin(); sit != req_sub.end(); sit++){
00036     //Record the object of the Attribute
00037     RequestAttribute* attr = new RequestAttribute;
00038     attr->duplicate(*(*sit));
00039     sub.push_back(attr); 
00040      
00041     //Record the xml node of the Attribute
00042     XMLNode subjectattr = subject.NewChild("ra:Attribute");
00043     subjectattr = ((*sit)->getAttributeValue())->encode();
00044     XMLNode subjectattr_attr = subjectattr.NewAttribute("ra:Type");
00045     subjectattr_attr = ((*sit)->getAttributeValue())->getType();
00046     subjectattr_attr = subjectattr.NewAttribute("ra:AttributeId");
00047     subjectattr_attr = ((*sit)->getAttributeValue())->getId();
00048 
00049 /*  AttributeValue *attrval;
00050     attrval = (*sit)->getAttributeValue();
00051     if(attrval) std::cout<< "Attribute Value:"<< (attrval->encode()).c_str() << std::endl;
00052 */           
00053   }
00054   
00055   //Reconstruct resource
00056   Resource::iterator rit;
00057   Resource req_res = req_tpl->res;
00058   XMLNode resource;
00059   if(!req_res.empty())
00060     resource = root.NewChild("ra:Resource");
00061   n = req_res.size();
00062   for(rit = req_res.begin(); rit != req_res.end(); rit++){
00063     RequestAttribute* attr = new RequestAttribute;
00064     attr->duplicate(*(*rit));
00065     res.push_back(attr);
00066 
00067     XMLNode resourceattr = resource.NewChild("ra:Attribute");
00068     resourceattr = ((*rit)->getAttributeValue())->encode();
00069     XMLNode resourceattr_attr = resourceattr.NewAttribute("ra:Type");
00070     resourceattr_attr = ((*rit)->getAttributeValue())->getType();
00071     resourceattr_attr = resourceattr.NewAttribute("ra:AttributeId");
00072     resourceattr_attr = ((*rit)->getAttributeValue())->getId();
00073   }
00074 
00075   //Reconstruct action
00076   Action::iterator ait;
00077   Action req_act = req_tpl->act;
00078   XMLNode action;
00079   if(!req_act.empty())
00080     action = root.NewChild("ra:Action");
00081   n = req_act.size();
00082   for(ait = req_act.begin(); ait != req_act.end(); ait++){
00083     RequestAttribute* attr = new RequestAttribute;
00084     attr->duplicate(*(*ait));
00085     act.push_back(attr);
00086 
00087     XMLNode actionattr = action.NewChild("ra:Attribute");
00088     actionattr = ((*ait)->getAttributeValue())->encode();
00089     XMLNode actionattr_attr = actionattr.NewAttribute("ra:Type");
00090     actionattr_attr = ((*ait)->getAttributeValue())->getType();
00091     actionattr_attr = actionattr.NewAttribute("ra:AttributeId");
00092     actionattr_attr = ((*ait)->getAttributeValue())->getId();
00093   }
00094 
00095   //Reconstruct context
00096   Context::iterator cit;
00097   Context req_ctx = req_tpl->ctx;
00098   XMLNode context;
00099   if(!req_ctx.empty())
00100     context = root.NewChild("ra:Context");
00101   n = req_ctx.size();
00102   for(cit = req_ctx.begin(); cit != req_ctx.end(); cit++){
00103     RequestAttribute* attr = new RequestAttribute;
00104     attr->duplicate(*(*cit));
00105     ctx.push_back(attr);
00106 
00107     XMLNode contextattr = context.NewChild("ra:Attribute");
00108     contextattr = ((*cit)->getAttributeValue())->encode();
00109     XMLNode contextattr_attr = contextattr.NewAttribute("ra:Type");
00110     contextattr_attr = ((*cit)->getAttributeValue())->getType();
00111     contextattr_attr = contextattr.NewAttribute("ra:AttributeId");
00112     contextattr_attr = ((*cit)->getAttributeValue())->getId();
00113   }
00114 
00115   return this;
00116 }
00117 
00118 void ArcRequestTuple::erase() {
00119   while(!(sub.empty())){
00120     delete sub.back();
00121     sub.pop_back();
00122   }
00123 
00124   while(!(res.empty())){
00125     delete res.back();
00126     res.pop_back();
00127   }
00128 
00129   while(!(act.empty())){
00130     delete act.back();
00131     act.pop_back();
00132   }
00133 
00134   while(!(ctx.empty())){
00135     delete ctx.back();
00136     ctx.pop_back();
00137   }
00138 }
00139 
00140 ArcRequestTuple::~ArcRequestTuple() {
00141   while(!(sub.empty())){
00142     sub.pop_back();
00143   }
00144 
00145   while(!(res.empty())){
00146     res.pop_back();
00147   }
00148 
00149   while(!(act.empty())){
00150     act.pop_back();
00151   }
00152 
00153   while(!(ctx.empty())){
00154     ctx.pop_back();
00155   }
00156 }
00157 
00158 ArcEvaluationCtx::ArcEvaluationCtx(Request* request) : req(NULL), EvaluationCtx(req) {
00159   req = request;
00160 }
00161 
00162 ArcEvaluationCtx::~ArcEvaluationCtx(){
00163   //if(req)
00164   //  delete req;
00165   while(!(reqtuples.empty())) {
00166     delete reqtuples.back();
00167     reqtuples.pop_back();
00168   } 
00169 }
00170 
00171 Request* ArcEvaluationCtx::getRequest () const{
00172   return req;
00173 }
00174 /*
00175 ArrtibuteValue * EvaluationCtx::getSubjectAttribute(){
00176 
00177 }
00178 
00179 ArrtibuteValue * EvaluationCtx::getResourceAttribute(){
00180 
00181 }
00182 
00183 ArrtibuteValue * EvaluationCtx::getActionAttribute(){
00184 
00185 }
00186 
00187 ArrtibuteValue * EvaluationCtx::getContextAttribute(){
00188  
00189 }
00190 */
00191 
00192 static void add_tuple(std::list<RequestTuple*>& reqtuples,Subject* subject,Resource* resource,Action* action,Context* context) {
00193   if(subject || resource || action || context) {
00194     ArcRequestTuple* reqtuple = new ArcRequestTuple;
00195     if(subject) reqtuple->sub = *subject;
00196     if(resource) reqtuple->res = *resource;
00197     if(action) reqtuple->act = *action;
00198     if(context) reqtuple->ctx = *context;
00199     reqtuples.push_back(reqtuple);
00200   };
00201 }
00202 
00203 static void add_contexts(std::list<RequestTuple*>& reqtuples,Subject* subject,Resource* resource,Action* action,CtxList& contexts) {
00204   if(contexts.empty()) {
00205     add_tuple(reqtuples,subject,resource,action,NULL);
00206     return;
00207   }
00208   CtxList::iterator cit = contexts.begin();
00209   for(;cit != contexts.end();++cit) {
00210     add_tuple(reqtuples,subject,resource,action,&(*cit));
00211   }
00212 }
00213 
00214 static void add_actions(std::list<RequestTuple*>& reqtuples,Subject* subject,Resource* resource,ActList& actions,CtxList& contexts) {
00215   if(actions.empty()) {
00216     add_contexts(reqtuples,subject,resource,NULL,contexts);
00217     return;
00218   }
00219   ActList::iterator ait = actions.begin();
00220   for(;ait != actions.end();++ait) {
00221     add_contexts(reqtuples,subject,resource,&(*ait),contexts);
00222   }
00223 }
00224 
00225 static void add_resources(std::list<RequestTuple*>& reqtuples,Subject* subject,ResList& resources,ActList& actions,CtxList& contexts) {
00226   if(resources.empty()) {
00227     add_actions(reqtuples,subject,NULL,actions,contexts);
00228     return;
00229   }
00230   ResList::iterator rit = resources.begin();
00231   for(;rit != resources.end();++rit) {
00232     add_actions(reqtuples,subject,&(*rit),actions,contexts);
00233   }
00234 }
00235 
00236 static void add_subjects(std::list<RequestTuple*>& reqtuples,SubList& subjects,ResList& resources,ActList& actions,CtxList& contexts) {
00237   if(subjects.empty()) {
00238     add_resources(reqtuples,NULL,resources,actions,contexts);
00239     return;
00240   }
00241   SubList::iterator sit = subjects.begin();
00242   for(;sit != subjects.end();++sit) {
00243     add_resources(reqtuples,&(*sit),resources,actions,contexts);
00244   }
00245 }
00246 
00247 
00248 void ArcEvaluationCtx::split(){
00249   while(!reqtuples.empty()) { 
00250     delete reqtuples.back();
00251     reqtuples.pop_back(); 
00252   }
00253 
00254   ReqItemList reqlist = req->getRequestItems();
00255  
00256   logger.msg(INFO,"There is %d RequestItems", reqlist.size()); 
00257   
00258   std::list<RequestItem*>::iterator it;
00259   for (it = reqlist.begin(); it != reqlist.end(); it++) {
00260     SubList subjects = (*it)->getSubjects();
00261     SubList::iterator sit;
00262     ResList resources = (*it)->getResources();
00263     ResList::iterator rit;
00264     ActList actions = (*it)->getActions();
00265     ActList::iterator ait;
00266     CtxList contexts = (*it)->getContexts();
00267     CtxList::iterator cit;
00268    
00269     //Scan subjects, resources, actions and contexts inside one RequestItem object
00270     //to split subjects, resources, actions or contexts into some tuple with one subject, one resource, one action and context
00271     //See more descrioption in inteface RequestItem.h
00272     add_subjects(reqtuples,subjects,resources,actions,contexts);
00273 /*
00274     for(sit = subjects.begin(); sit != subjects.end(); sit++) { //The subject part will never be empty
00275       if(!resources.empty()) {
00276         for(rit = resources.begin(); rit != resources.end(); rit++){
00277           if(!actions.empty()){
00278             for(ait = actions.begin(); ait != actions.end(); ait++){
00279               if(!contexts.empty()){
00280                 for(cit = contexts.begin(); cit != contexts.end(); cit++){
00281                   RequestTuple* reqtuple = new RequestTuple;
00282                   reqtuple->sub = *sit;
00283                   reqtuple->res = *rit;
00284                   reqtuple->act = *ait;
00285                   reqtuple->ctx = *cit;
00286                   reqtuples.push_back(reqtuple);
00287                 }
00288               }
00289               else {
00290                 RequestTuple* reqtuple = new RequestTuple;
00291                 reqtuple->sub = *sit;
00292                 reqtuple->res = *rit;
00293                 reqtuple->act = *ait;
00294                 reqtuples.push_back(reqtuple);             
00295               }         
00296             }
00297           }
00298           else {
00299             if(!contexts.empty()){
00300               for(cit = contexts.begin(); cit != contexts.end(); cit++){
00301                 RequestTuple* reqtuple = new RequestTuple;
00302                 reqtuple->sub = *sit;
00303                 reqtuple->res = *rit;
00304                 reqtuple->ctx = *cit;
00305                 reqtuples.push_back(reqtuple);
00306               }
00307             }
00308             else {
00309               RequestTuple* reqtuple = new RequestTuple;
00310               reqtuple->sub = *sit;
00311               reqtuple->res = *rit;
00312               reqtuples.push_back(reqtuple);
00313             }
00314           }
00315         }
00316       }
00317       
00318       else{
00319         if(!actions.empty()){
00320           for(ait = actions.begin(); ait != actions.end(); ait++){
00321             if(!contexts.empty()){
00322               for(cit = contexts.begin(); cit != contexts.end(); cit++){
00323                 RequestTuple* reqtuple = new RequestTuple;
00324                 reqtuple->sub = *sit;
00325                 reqtuple->act = *ait;
00326                 reqtuple->ctx = *cit;
00327                 reqtuples.push_back(reqtuple);
00328               }
00329             }
00330             else {
00331               RequestTuple* reqtuple = new RequestTuple;
00332               reqtuple->sub = *sit;
00333               reqtuple->act = *ait;
00334               reqtuples.push_back(reqtuple);
00335             }
00336           }
00337         }
00338         else {
00339           if(!contexts.empty()){
00340             for(cit = contexts.begin(); cit != contexts.end(); cit++){
00341               RequestTuple* reqtuple = new RequestTuple;
00342               reqtuple->sub = *sit;
00343               reqtuple->ctx = *cit;
00344               reqtuples.push_back(reqtuple);
00345             }
00346           }
00347           else {
00348             RequestTuple* reqtuple = new RequestTuple;
00349             reqtuple->sub = *sit;
00350             reqtuples.push_back(reqtuple);
00351           }
00352         }
00353       }
00354     }
00355 */
00356 
00357 /*
00358     for(sit = subjects.begin(); sit != subjects.end(); sit++) {
00359       for(rit = resources.begin(); rit != resources.end(); rit++){
00360         for(ait = actions.begin(); ait != actions.end(); ait++){
00361           for(cit = contexts.begin(); cit != contexts.end(); cit++){
00362             RequestTuple* reqtuple = new RequestTuple;
00363             reqtuple->sub = *sit;
00364             reqtuple->res = *rit;
00365             reqtuple->act = *ait;
00366             reqtuple->ctx = *cit;
00367             reqtuples.push_back(reqtuple);  
00368 
00369             logger.msg(INFO, "Subject size:  %d", (*sit).size());
00370             Arc::Subject::iterator it;
00371             for (it = (*sit).begin(); it!= (*sit).end(); it++){
00372               AttributeValue *attr;
00373               attr = (*it)->getAttributeValue();
00374               if(attr!=NULL) 
00375                 logger.msg(INFO, "%s", attr->encode());
00376             }
00377           }
00378         }
00379       }
00380     }*/
00381   }
00382 
00383 }