Merge "Add verigraph code base"
[parser.git] / verigraph / service / src / mcnet / netobjs / PolitoNF.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package mcnet.netobjs;
11
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import com.microsoft.z3.BoolExpr;
17 import com.microsoft.z3.Context;
18 import com.microsoft.z3.DatatypeExpr;
19 import com.microsoft.z3.Expr;
20 import com.microsoft.z3.FuncDecl;
21 import com.microsoft.z3.IntExpr;
22 import com.microsoft.z3.Solver;
23 import com.microsoft.z3.Sort;
24
25 import mcnet.components.NetContext;
26 import mcnet.components.Network;
27 import mcnet.components.NetworkObject;
28
29 /** First example of custom network function: a simple filter
30  *
31  */
32 public class PolitoNF extends NetworkObject{
33
34                 List<BoolExpr> constraints;
35                 Context ctx;
36                 DatatypeExpr politoNF;
37                 Network net;
38                 NetContext nctx;
39                 FuncDecl isInBlacklist;
40
41
42                 public PolitoNF(Context ctx, Object[]... args) {
43                         super(ctx, args);
44                 }
45
46                 @Override
47                 protected void init(Context ctx, Object[]... args) {
48                         this.ctx = ctx;
49                         isEndHost=false;
50                         constraints = new ArrayList<BoolExpr>();
51                 z3Node = ((NetworkObject)args[0][0]).getZ3Node();
52                 politoNF = z3Node;
53                 net = (Network)args[0][1];
54                 nctx = (NetContext)args[0][2];
55                 //net.saneSend(this);
56             }
57
58             public void politoNFRules (DatatypeExpr ipA,DatatypeExpr ipB){
59 //              System.out.println("[PolitoNf] Installing rules");
60                 Expr n_0 = ctx.mkConst("politoNF_"+politoNF+"_n_0", nctx.node);
61                 Expr n_1 = ctx.mkConst("politoNF_"+politoNF+"_n_1", nctx.node);
62                 Expr p_0 = ctx.mkConst("politoNF_"+politoNF+"_p_0", nctx.packet);
63                 IntExpr t_0 = ctx.mkIntConst("politoNF_"+politoNF+"_t_0");
64                 IntExpr t_1 = ctx.mkIntConst("politoNF_"+politoNF+"_t_1");
65                 Expr a_0 = ctx.mkConst(politoNF+"_politoNF_a_0", nctx.address);
66                 Expr a_1 = ctx.mkConst(politoNF+"_politoNF_a_1", nctx.address);
67
68                 FuncDecl myFunction = ctx.mkFuncDecl(politoNF+"_myFunction", new Sort[]{nctx.address,nctx.address}, ctx.mkBoolSort());
69
70                 BoolExpr myConstraint = ctx.mkForall(new Expr[]{n_0, p_0, t_0},
71                                  ctx.mkImplies((BoolExpr)nctx.send.apply(politoNF, n_0, p_0, t_0),
72                                                  ctx.mkExists(new Expr[]{n_1, t_1},
73                                                                  ctx.mkAnd((BoolExpr)nctx.recv.apply(n_1, politoNF, p_0, t_1),
74                                                                                         ctx.mkLt(t_1 , t_0),
75                                                                                         (BoolExpr)myFunction.apply(nctx.pf.get("src").apply(p_0), nctx.pf.get("dest").apply(p_0))),1,null,null,null,null)),1,null,null,null,null);
76
77                 BoolExpr funcConstraint = ctx.mkOr(ctx.mkAnd(ctx.mkEq(a_0, ipA), ctx.mkEq(a_1, ipB)), ctx.mkAnd(ctx.mkEq(a_0,ipB), ctx.mkEq(a_1,ipA)));
78
79                 // Constraint1          myFunction(a_0,a_1) == ((a_0 == ipA && a_1 == ipB) || (a_0 == ipB && a_1 == ipA))
80                          constraints.add(
81                                          ctx.mkForall(new Expr[]{a_0,a_1},
82                                                          ctx.mkEq(myFunction.apply(a_0, a_1), funcConstraint),1,null,null,null,null));
83
84                         //Constraint2           send(politoNF, n_0, p, t_0)  ->
85                          //                                     (exist n_1,t_1 : (t_1 < t_0 && recv(n_1, politoNF, p, t_1) && myFunction(p.src,p.dest))
86                          constraints.add(myConstraint);
87
88             }
89
90             @Override
91                 protected void addConstraints(Solver solver) {
92                                 BoolExpr[] constr = new BoolExpr[constraints.size()];
93                             solver.add(constraints.toArray(constr));
94                 }
95
96             @Override
97                 public DatatypeExpr getZ3Node() {
98                         return politoNF;
99                 }
100
101         }