PageRenderTime 48ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/GeneralPDP/Scenario/PolicyRepositoryEndPoint.fs

#
F# | 95 lines | 70 code | 19 blank | 6 comment | 5 complexity | 307f7a7d0c9a3d43a902f9c4b54e1522 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, LGPL-3.0, BSD-3-Clause
  1. namespace Microsoft.Research.GeneralPDP.Scenario
  2. open Microsoft.Research.GeneralPDP.Translations.ToXACML.DkalPolicyTranslator
  3. open Microsoft.Research.GeneralPDP.Translations.ToDKAL.XacmlPolicyTranslator
  4. open Microsoft.Research.GeneralPDP.XACML.Ast
  5. open Microsoft.Research.GeneralPDP.XACML.PDP.Engine
  6. open Microsoft.Research.GeneralPDP.DKAL.Engine.Basics
  7. open Microsoft.Research.GeneralPDP.DKAL.Engine.ParsingCtxFactory
  8. open Basics
  9. open Message
  10. open EndPoint
  11. open EndPointImageFactory
  12. open Microsoft.Msagl.Drawing
  13. open System.Drawing
  14. open System.Collections.Generic
  15. module PolicyRepositoryEndPoint =
  16. type PolicyRepositoryEndPoint(id: EndPointId, xacmlPolicies: Policy list, dkalPolicies: DkalPolicy list) =
  17. inherit EndPoint(id)
  18. let d2xTranslator = DkalPolicyTranslator()
  19. let pctx, _ = xacmlAwareParsingCtx id
  20. let x2dTranslator = XacmlPolicyTranslator("A","B", pctx) // TODO A & B should go
  21. let emptyXacmlPolicy pid = Policy(pid, None, "first-applicable", [], [])
  22. let emptyDkalPolicy pid = DkalPolicy(pid, [])
  23. let xPoliciesDict = new Dictionary<PolicyId, Policy>()
  24. let dPoliciesDict = new Dictionary<PolicyId, DkalPolicy>()
  25. do
  26. for policy in xacmlPolicies do
  27. xPoliciesDict.Add(policy.PolicyId, policy)
  28. for policy in dkalPolicies do
  29. dPoliciesDict.Add(policy.PolicyId, policy)
  30. override ep.Process(m: IMessage) =
  31. match m.Content with
  32. | XacmlPolicyRequestContent(pr) ->
  33. // first try dkal
  34. if dPoliciesDict.ContainsKey(pr.PolicyId) then
  35. let pcy, unusedAssertions = d2xTranslator.TranslateCommRules(dPoliciesDict.[pr.PolicyId])
  36. ep.Send({sender= id;
  37. receiver= m.Sender;
  38. content= XacmlPolicyContent(pcy)})
  39. // otherwise, xacml
  40. elif xPoliciesDict.ContainsKey(pr.PolicyId) then
  41. ep.Send({sender= id;
  42. receiver= m.Sender;
  43. content= XacmlPolicyContent(xPoliciesDict.[pr.PolicyId])})
  44. // otherwise, send an empty policy
  45. else
  46. ep.Send({sender= id;
  47. receiver= m.Sender;
  48. content= XacmlPolicyContent(emptyXacmlPolicy pr.PolicyId)})
  49. | DkalPolicyRequestContent(dpr) ->
  50. // first try dkal
  51. if dPoliciesDict.ContainsKey(dpr.PolicyId) then
  52. ep.Send({sender= id;
  53. receiver= m.Sender;
  54. content= DkalPolicyContent(dPoliciesDict.[dpr.PolicyId])})
  55. // otherwise, xacml
  56. elif xPoliciesDict.ContainsKey(dpr.PolicyId) then
  57. let pcy = x2dTranslator.TranslatePolicyToCommRules(xPoliciesDict.[dpr.PolicyId])
  58. ep.Send({sender= id;
  59. receiver= m.Sender;
  60. content= DkalPolicyContent(pcy)})
  61. // otherwise, send an empty policy
  62. else
  63. ep.Send({sender= id;
  64. receiver= m.Sender;
  65. content= DkalPolicyContent(emptyDkalPolicy dpr.PolicyId)})
  66. | XacmlPolicyContent(pcy) ->
  67. xPoliciesDict.Add(pcy.PolicyId, pcy)
  68. | DkalPolicyContent(pcy) ->
  69. dPoliciesDict.Add(pcy.PolicyId, pcy)
  70. | _ -> "I don't understand content " + m.Content.ToString() |> ep.Fail
  71. override ep.StartUp() = ()
  72. override ep.CleanUp() = ()
  73. override ep.Image = Some (image (ep :> IEndPoint).Color DatabaseDrawing)
  74. override ep.ApplyStyle (n: Node) =
  75. n.LabelText <- ep.Description
  76. override ep.Description = ep.Id + ": PcyRepository"