Merge "Disable syslog in heat-translator for functest integration"
[parser.git] / verigraph / src / it / polito / verigraph / 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 package it.polito.verigraph.mcnet.netobjs;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import com.microsoft.z3.BoolExpr;
15 import com.microsoft.z3.Context;
16 import com.microsoft.z3.DatatypeExpr;
17 import com.microsoft.z3.Expr;
18 import com.microsoft.z3.FuncDecl;
19 import com.microsoft.z3.IntExpr;
20 import com.microsoft.z3.Solver;
21 import com.microsoft.z3.Sort;
22
23 import it.polito.verigraph.mcnet.components.NetContext;
24 import it.polito.verigraph.mcnet.components.Network;
25 import it.polito.verigraph.mcnet.components.NetworkObject;
26
27 /** First example of custom network function: a simple filter
28  *
29  *
30  */
31 public class PolitoNF extends NetworkObject{
32
33     List<BoolExpr> constraints;
34     Context ctx;
35     DatatypeExpr politoNF;
36     Network net;
37     NetContext nctx;
38     FuncDecl isInBlacklist;
39
40
41     public PolitoNF(Context ctx, Object[]... args) {
42         super(ctx, args);
43     }
44
45     @Override
46     protected void init(Context ctx, Object[]... args) {
47         this.ctx = ctx;
48         isEndHost=false;
49         constraints = new ArrayList<BoolExpr>();
50         z3Node = ((NetworkObject)args[0][0]).getZ3Node();
51         politoNF = z3Node;
52         net = (Network)args[0][1];
53         nctx = (NetContext)args[0][2];
54         //net.saneSend(this);
55     }
56
57     public void politoNFRules (DatatypeExpr ipA,DatatypeExpr ipB){
58         //    System.out.println("[PolitoNf] Installing rules");
59         Expr n_0 = ctx.mkConst("politoNF_"+politoNF+"_n_0", nctx.node);
60         Expr n_1 = ctx.mkConst("politoNF_"+politoNF+"_n_1", nctx.node);
61         Expr p_0 = ctx.mkConst("politoNF_"+politoNF+"_p_0", nctx.packet);
62         IntExpr t_0 = ctx.mkIntConst("politoNF_"+politoNF+"_t_0");
63         IntExpr t_1 = ctx.mkIntConst("politoNF_"+politoNF+"_t_1");
64         Expr a_0 = ctx.mkConst(politoNF+"_politoNF_a_0", nctx.address);
65         Expr a_1 = ctx.mkConst(politoNF+"_politoNF_a_1", nctx.address);
66
67         FuncDecl myFunction = ctx.mkFuncDecl(politoNF+"_myFunction", new Sort[]{nctx.address,nctx.address}, ctx.mkBoolSort());
68
69         BoolExpr myConstraint = ctx.mkForall(new Expr[]{n_0, p_0, t_0},
70                 ctx.mkImplies((BoolExpr)nctx.send.apply(politoNF, n_0, p_0, t_0),
71                         ctx.mkExists(new Expr[]{n_1, t_1},
72                                 ctx.mkAnd((BoolExpr)nctx.recv.apply(n_1, politoNF, p_0, t_1),
73                                         ctx.mkLt(t_1 , t_0),
74                                         (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);
75
76         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)));
77
78         // Constraint1myFunction(a_0,a_1) == ((a_0 == ipA && a_1 == ipB) || (a_0 == ipB && a_1 == ipA))
79         constraints.add(
80                 ctx.mkForall(new Expr[]{a_0,a_1},
81                         ctx.mkEq(myFunction.apply(a_0, a_1), funcConstraint),1,null,null,null,null));
82
83         //Constraint2send(politoNF, n_0, p, t_0)  ->
84         //(exist n_1,t_1 : (t_1 < t_0 && recv(n_1, politoNF, p, t_1) && myFunction(p.src,p.dest))
85         constraints.add(myConstraint);
86
87     }
88
89     @Override
90     protected void addConstraints(Solver solver) {
91         BoolExpr[] constr = new BoolExpr[constraints.size()];
92         solver.add(constraints.toArray(constr));
93     }
94
95     @Override
96     public DatatypeExpr getZ3Node() {
97         return politoNF;
98     }
99
100 }