Merge "Add verigraph code base"
[parser.git] / verigraph / service / src / mcnet / netobjs / EndHost.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 import java.util.ArrayList;
13 import java.util.List;
14
15 import com.microsoft.z3.BoolExpr;
16 import com.microsoft.z3.Context;
17 import com.microsoft.z3.DatatypeExpr;
18 import com.microsoft.z3.Expr;
19 import com.microsoft.z3.IntExpr;
20 import com.microsoft.z3.Solver;
21
22 import mcnet.components.NetContext;
23 import mcnet.components.Network;
24 import mcnet.components.NetworkObject;
25 /**
26  * End host network objects
27  *
28  */
29 public class EndHost extends NetworkObject{
30
31         List<BoolExpr> constraints;
32         Context ctx;
33         DatatypeExpr node;
34         Network net;
35         NetContext nctx;
36
37         public EndHost(Context ctx, Object[]... args) {
38                 super(ctx, args);
39         }
40
41         @Override
42         protected void init(Context ctx, Object[]... args) {
43                 this.ctx = ctx;
44                 isEndHost=true;
45                 constraints = new ArrayList<BoolExpr>();
46         z3Node = ((NetworkObject)args[0][0]).getZ3Node();
47         node = z3Node;
48         net = (Network)args[0][1];
49         nctx = (NetContext)args[0][2];
50                 endHostRules();
51         }
52
53         @Override
54         public DatatypeExpr getZ3Node() {
55                 return node;
56         }
57
58         @Override
59         protected void addConstraints(Solver solver) {
60                         BoolExpr[] constr = new BoolExpr[constraints.size()];
61                     solver.add(constraints.toArray(constr));
62         }
63
64     public void endHostRules (){
65         Expr n_0 = ctx.mkConst("eh_"+node+"_n_0", nctx.node);
66         IntExpr t_0 = ctx.mkIntConst("eh_"+node+"_t_0");
67         Expr p_0 = ctx.mkConst("eh_"+node+"_p_0", nctx.packet);
68
69         //Constraint1           send(node, n_0, p, t_0) -> nodeHasAddr(node,p.src)
70         constraints.add(
71                 ctx.mkForall(new Expr[]{n_0, p_0, t_0},
72                     ctx.mkImplies(
73                         (BoolExpr)nctx.send.apply(      new Expr[]{ node, n_0, p_0, t_0}),
74                         (BoolExpr)nctx.nodeHasAddr.apply(new Expr[]{node, nctx.pf.get("src").apply(p_0)})),1,null,null,null,null));
75         //Constraint2           send(node, n_0, p, t_0) -> p.origin == node
76         constraints.add(
77                 ctx.mkForall(new Expr[]{n_0, p_0, t_0},
78                 ctx.mkImplies(
79                                 (BoolExpr)nctx.send.apply(      new Expr[]{ node, n_0, p_0, t_0}),
80                                 ctx.mkEq(nctx.pf.get("origin").apply(p_0),node)),1,null,null,null,null));
81         //Constraint3      send(node, n_0, p, t_0) -> p.orig_body == p.body
82         constraints.add(
83                 ctx.mkForall(new Expr[]{n_0, p_0, t_0},
84                 ctx.mkImplies(
85                                 (BoolExpr)nctx.send.apply(new Expr[]{ node, n_0, p_0, t_0}),
86                                 ctx.mkEq(nctx.pf.get("orig_body").apply(p_0),nctx.pf.get("body").apply(p_0))),1,null,null,null,null));
87         //Constraint4           recv(n_0, node, p, t_0) -> nodeHasAddr(node,p.dest)
88         constraints.add(
89                 ctx.mkForall(new Expr[]{n_0, p_0, t_0},
90                 ctx.mkImplies(
91                                 (BoolExpr)nctx.recv.apply(      new Expr[]{ n_0, node, p_0, t_0}),
92                         (BoolExpr)nctx.nodeHasAddr.apply(new Expr[]{node, nctx.pf.get("dest").apply(p_0)})),1,null,null,null,null));
93
94 //       Just a try: here we state that an endhost is not able to issue a HTTP response traffic
95 //       See PolitoCache.py model for constants definition (2 means HTTP_RESPONSE, 1 means HTTP_REQUEST)
96 //         constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
97 //                 ctx.mkImplies((BoolExpr)nctx.send.apply(node, n_0, p_0, t_0),
98 //                            ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(1))),1,null,null,null,null));
99     }
100 }