Disable syslog in heat-translator for functest integration
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / client / Neo4jManagerClient.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 it.polito.escape.verify.client;
11
12 import java.io.StringWriter;
13 import java.util.HashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17
18 import javax.ws.rs.ProcessingException;
19 import javax.ws.rs.client.Client;
20 import javax.ws.rs.client.ClientBuilder;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.client.WebTarget;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.JAXBElement;
27 import javax.xml.bind.JAXBException;
28 import javax.xml.bind.Marshaller;
29 import javax.xml.stream.FactoryConfigurationError;
30 import javax.xml.stream.XMLOutputFactory;
31 import javax.xml.stream.XMLStreamException;
32 import javax.xml.stream.XMLStreamWriter;
33
34 import it.polito.escape.verify.deserializer.PathsMessageBodyReader;
35 import it.polito.escape.verify.model.Entry;
36 import it.polito.nffg.neo4j.jaxb.ActionEnumType;
37 import it.polito.nffg.neo4j.jaxb.ActionType;
38 import it.polito.nffg.neo4j.jaxb.ActionsType;
39 import it.polito.nffg.neo4j.jaxb.CiType;
40 import it.polito.nffg.neo4j.jaxb.CiType.Attributes;
41 import it.polito.nffg.neo4j.jaxb.CiType.Attributes.Attribute;
42 import it.polito.nffg.neo4j.jaxb.CpType;
43 import it.polito.nffg.neo4j.jaxb.CpointsType;
44 import it.polito.nffg.neo4j.jaxb.CtrlInterfacesType;
45 import it.polito.nffg.neo4j.jaxb.EpCpType;
46 import it.polito.nffg.neo4j.jaxb.EpType;
47 import it.polito.nffg.neo4j.jaxb.EpType.Flowspace;
48 import it.polito.nffg.neo4j.jaxb.EpointsType;
49 import it.polito.nffg.neo4j.jaxb.EpsCpsType;
50 import it.polito.nffg.neo4j.jaxb.FlowrulesType;
51 import it.polito.nffg.neo4j.jaxb.FlowrulesType.Flowspace.Tcp;
52 import it.polito.nffg.neo4j.jaxb.MonParamsType;
53 import it.polito.nffg.neo4j.jaxb.MonParamsType.Parameter;
54 import it.polito.nffg.neo4j.jaxb.NeType;
55 import it.polito.nffg.neo4j.jaxb.NelementsType;
56 import it.polito.nffg.neo4j.jaxb.NfType;
57 import it.polito.nffg.neo4j.jaxb.Nffg;
58 import it.polito.nffg.neo4j.jaxb.NfunctionsType;
59 import it.polito.nffg.neo4j.jaxb.ObjectFactory;
60 import it.polito.nffg.neo4j.jaxb.Paths;
61 import it.polito.nffg.neo4j.jaxb.PortDirEnumType;
62 import it.polito.nffg.neo4j.jaxb.PortType;
63 import it.polito.nffg.neo4j.jaxb.SpecType;
64 import it.polito.nffg.neo4j.jaxb.SpecType.Cpu;
65 import it.polito.nffg.neo4j.jaxb.SpecType.Deployment;
66 import it.polito.nffg.neo4j.jaxb.SpecType.Image;
67 import it.polito.nffg.neo4j.jaxb.SpecType.Memory;
68 import it.polito.nffg.neo4j.jaxb.SpecType.Storage;
69
70 public class Neo4jManagerClient {
71
72         private JAXBContext                                     jc;
73
74         private String                                          address;
75
76         private Nffg                                            nffg;
77
78         private List<String>                            endpoints               = new LinkedList<String>();
79
80         private List<String>                            firewalls               = new LinkedList<String>();
81
82         private Map<String, List<Entry>>        routingTable    = new HashMap<String, List<Entry>>();
83
84         private String                                          source;
85
86         private String                                          destination;
87
88         private String                                          xmlString;
89
90         private WebTarget                                       baseTarget;
91
92         public Neo4jManagerClient() {
93
94         }
95
96         public Neo4jManagerClient(      String address, String source, String destination, List<String> endpoints,
97                                                                 List<String> firewalls, Map<String, List<Entry>> routingTable) {
98                 this.address = address;
99                 this.source = source;
100                 this.destination = destination;
101                 this.endpoints = endpoints;
102                 this.firewalls = firewalls;
103                 this.routingTable = routingTable;
104
105                 Client client = ClientBuilder.newBuilder().register(PathsMessageBodyReader.class).build();
106
107                 this.baseTarget = client.target(this.address);
108         }
109
110         public Paths getPaths() throws Exception {
111                 try {
112                         this.generateCustomXml();
113                 }
114                 catch (JAXBException e) {
115                         throw (e);
116                 }
117
118                 WebTarget graphsTarget = baseTarget.path("graphs");
119                 WebTarget pathSourceDestination = graphsTarget.path("{graphId}/paths");
120                 WebTarget deleteNffg = graphsTarget.path("{graphId}");
121
122                 Response deleteNffgResponse = deleteNffg.resolveTemplate("graphId", "1").request().delete();
123                 if (deleteNffgResponse.getStatus() != javax.ws.rs.core.Response.Status.NO_CONTENT.getStatusCode()
124                         && deleteNffgResponse.getStatus() != javax.ws.rs.core.Response.Status.NOT_FOUND.getStatusCode()) {
125                         throw new Exception("graph deletion failed");
126                 }
127
128                 Response createNffgResponse = graphsTarget      .request("application/xml")
129                                                                                                         .post(Entity.entity(this.xmlString, "application/xml"));
130                 if (createNffgResponse.getStatus() != javax.ws.rs.core.Response.Status.CREATED.getStatusCode()) {
131                         throw new Exception("graph creation failed");
132                 }
133
134                 System.out.println("Getting paths from node \"" + this.source + "\" to node \"" + this.destination + "\"...");
135                 Response getPath = pathSourceDestination.resolveTemplate("graphId", "1")
136                                                                                                 .queryParam("src", this.source)
137                                                                                                 .queryParam("dst", this.destination)
138                                                                                                 .queryParam("dir", "outgoing")
139                                                                                                 .request(MediaType.APPLICATION_XML)
140                                                                                                 .get();
141
142                 System.out.println("Paths from node \"" + this.source + "\" to node \"" + this.destination + "\":");
143
144                 Paths paths = null;
145                 try {
146                         paths = getPath.readEntity(Paths.class);
147                 }
148                 catch (ProcessingException e) {
149                         throw (e);
150                 }
151                 catch (IllegalStateException e) {
152                         throw (e);
153                 }
154
155                 return paths;
156         }
157
158         private void generateCustomXml() throws JAXBException {
159
160                 jc = JAXBContext.newInstance("it.polito.nffg.neo4j.jaxb");
161
162                 nffg = new Nffg();
163                 nffg.setId("nffg_1");
164
165                 generateEndpoints();
166                 generateFirewalls();
167                 generateConnections();
168
169                 MonParamsType monitoring_parameters = new MonParamsType();
170                 nffg.setMonitoringParameters(monitoring_parameters);
171
172                 JAXBElement<Nffg> root = (new ObjectFactory()).createNffg(nffg);
173
174                 Marshaller m;
175                 try {
176                         m = jc.createMarshaller();
177                         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
178
179                         StringWriter stringWriter = new StringWriter();
180                         try {
181                                 XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter);
182                                 m.marshal(root, xmlStreamWriter);
183                                 xmlString = stringWriter.getBuffer().toString();
184                         }
185                         catch (XMLStreamException e) {
186                                 e.printStackTrace();
187                         }
188                         catch (FactoryConfigurationError e) {
189
190                                 e.printStackTrace();
191                         }
192                         // m.marshal( root, new File("nffg.xml") );
193                         System.out.println(xmlString);
194
195                 }
196                 catch (JAXBException e) {
197                         throw (e);
198                 }
199         }
200
201         private void generateConnections() {
202                 NelementsType network_elements = new NelementsType();
203                 NeType network_element = new NeType();
204                 network_element.setId("ne_1");
205                 network_element.setType("BiSBiS");
206                 EpsCpsType ep_cps = new EpsCpsType();
207
208                 for (String node : routingTable.keySet()) {
209                         EpCpType ep_cp = new EpCpType();
210                         ep_cp.setIdRef(node);
211                         FlowrulesType flowrules = new FlowrulesType();
212                         it.polito.nffg.neo4j.jaxb.FlowrulesType.Flowspace flowspace =
213                                                                                                                                                 new it.polito.nffg.neo4j.jaxb.FlowrulesType.Flowspace();
214                         flowrules.setFlowspace(flowspace);
215                         ActionsType actions = new ActionsType();
216                         for (Entry e : routingTable.get(node)) {
217                                 ActionType action = new ActionType();
218                                 action.setType(ActionEnumType.fromValue(e.getDirection()));
219                                 action.setPort(e.getDestination());
220                                 actions.getAction().add(action);
221                         }
222                         flowrules.setActions(actions);
223
224                         ep_cp.getFlowrules().add(flowrules);
225                         ep_cps.getEpCp().add(ep_cp);
226                 }
227                 network_element.setEpsCps(ep_cps);
228
229                 MonParamsType monitoring_parameters = new MonParamsType();
230                 Parameter parameter = new Parameter();
231                 parameter.getValue().add("Bandwith ep_1 cp_1 100mbit");
232                 monitoring_parameters.getParameter().add(parameter);
233                 Parameter parameter2 = new Parameter();
234                 parameter2.getValue().add("Delay ep_1 cp_1 50ms");
235                 monitoring_parameters.getParameter().add(parameter2);
236
237                 network_element.setMonitoringParameters(monitoring_parameters);
238
239                 network_elements.getNetworkElement().add(network_element);
240
241                 nffg.setNetworkElements(network_elements);
242
243         }
244
245         private void generateFirewalls() {
246                 NfunctionsType network_functions = new NfunctionsType();
247
248                 for (String firewall : firewalls) {
249                         NfType nf = new NfType();
250                         nf.setId(firewall);
251                         nf.setFunctionalType("firewall");
252
253                         SpecType specification = new SpecType();
254                         Deployment deployment = new Deployment();
255                         deployment.setType("PolitoFirewall");
256                         Image image = new Image();
257                         image.setUri("http://www.polito.it");
258                         Cpu cpu = new Cpu();
259                         cpu.setNumCores((short) (7));
260                         Memory memory = new Memory();
261                         memory.setSize("10MiB");
262                         Storage storage = new Storage();
263                         storage.setSize("100MiB");
264                         specification.setDeployment(deployment);
265                         specification.setImage(image);
266                         specification.setCpu(cpu);
267                         specification.setMemory(memory);
268                         specification.setStorage(storage);
269
270                         CpointsType connection_points = new CpointsType();
271                         CpType connection_point = new CpType();
272                         connection_point.setId(firewall + "_in");
273                         PortType port = new PortType();
274                         port.setId(79);
275                         port.setDirection(PortDirEnumType.IN);
276                         port.setType("GbE");
277                         connection_point.setPort(port);
278                         connection_points.getConnectionPoint().add(connection_point);
279
280                         CpType connection_point2 = new CpType();
281                         connection_point2.setId(firewall + "_out");
282                         PortType port2 = new PortType();
283                         port2.setId(77);
284                         port2.setDirection(PortDirEnumType.OUT);
285                         port2.setType("10GbE");
286                         connection_point2.setPort(port2);
287
288                         connection_points.getConnectionPoint().add(connection_point2);
289
290                         CtrlInterfacesType control_interfaces = new CtrlInterfacesType();
291                         CiType control_interface = new CiType();
292                         control_interface.setId(firewall + "_ci");
293
294                         Attributes attributes = new Attributes();
295                         Attribute attribute = new Attribute();
296                         attribute.setValue("tcp://127.0.0.1:5555");
297                         attributes.getAttribute().add(attribute);
298                         Attribute attribute2 = new Attribute();
299                         attribute2.setValue("Netconf");
300                         attributes.getAttribute().add(attribute2);
301                         control_interface.setAttributes(attributes);
302
303                         control_interfaces.getControlInterface().add(control_interface);
304
305                         MonParamsType monitoring_parameters = new MonParamsType();
306                         Parameter parameter = new Parameter();
307                         parameter.getValue().add("Measure script");
308                         monitoring_parameters.getParameter().add(parameter);
309
310                         nf.setSpecification(specification);
311                         nf.setConnectionPoints(connection_points);
312                         nf.setControlInterfaces(control_interfaces);
313                         nf.setMonitoringParameters(monitoring_parameters);
314
315                         network_functions.getNetworkFunction().add(nf);
316
317                 }
318                 nffg.setNetworkFunctions(network_functions);
319
320         }
321
322         private void generateEndpoints() {
323                 EpointsType eps = new EpointsType();
324
325                 for (String e : endpoints) {
326                         EpType endpoint = new EpType();
327                         endpoint.setId(e);
328                         Flowspace flowspace = new Flowspace();
329                         flowspace.setIngPhysPort("10");
330                         Tcp tcp = new Tcp();
331                         tcp.setSrc(80);
332                         flowspace.setTcp(tcp);
333                         endpoint.setFlowspace(flowspace);
334                         eps.getEndpoint().add(endpoint);
335                 }
336                 nffg.setEndpoints(eps);
337         }
338
339 }