1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
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 *******************************************************************************/
10 package it.polito.escape.verify.client;
12 import java.io.StringWriter;
13 import java.util.HashMap;
14 import java.util.LinkedList;
15 import java.util.List;
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;
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;
70 public class Neo4jManagerClient {
72 private JAXBContext jc;
74 private String address;
78 private List<String> endpoints = new LinkedList<String>();
80 private List<String> firewalls = new LinkedList<String>();
82 private Map<String, List<Entry>> routingTable = new HashMap<String, List<Entry>>();
84 private String source;
86 private String destination;
88 private String xmlString;
90 private WebTarget baseTarget;
92 public Neo4jManagerClient() {
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;
100 this.destination = destination;
101 this.endpoints = endpoints;
102 this.firewalls = firewalls;
103 this.routingTable = routingTable;
105 Client client = ClientBuilder.newBuilder().register(PathsMessageBodyReader.class).build();
107 this.baseTarget = client.target(this.address);
110 public Paths getPaths() throws Exception {
112 this.generateCustomXml();
114 catch (JAXBException e) {
118 WebTarget graphsTarget = baseTarget.path("graphs");
119 WebTarget pathSourceDestination = graphsTarget.path("{graphId}/paths");
120 WebTarget deleteNffg = graphsTarget.path("{graphId}");
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");
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");
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)
142 System.out.println("Paths from node \"" + this.source + "\" to node \"" + this.destination + "\":");
146 paths = getPath.readEntity(Paths.class);
148 catch (ProcessingException e) {
151 catch (IllegalStateException e) {
158 private void generateCustomXml() throws JAXBException {
160 jc = JAXBContext.newInstance("it.polito.nffg.neo4j.jaxb");
163 nffg.setId("nffg_1");
167 generateConnections();
169 MonParamsType monitoring_parameters = new MonParamsType();
170 nffg.setMonitoringParameters(monitoring_parameters);
172 JAXBElement<Nffg> root = (new ObjectFactory()).createNffg(nffg);
176 m = jc.createMarshaller();
177 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
179 StringWriter stringWriter = new StringWriter();
181 XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter);
182 m.marshal(root, xmlStreamWriter);
183 xmlString = stringWriter.getBuffer().toString();
185 catch (XMLStreamException e) {
188 catch (FactoryConfigurationError e) {
192 // m.marshal( root, new File("nffg.xml") );
193 System.out.println(xmlString);
196 catch (JAXBException e) {
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();
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);
222 flowrules.setActions(actions);
224 ep_cp.getFlowrules().add(flowrules);
225 ep_cps.getEpCp().add(ep_cp);
227 network_element.setEpsCps(ep_cps);
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);
237 network_element.setMonitoringParameters(monitoring_parameters);
239 network_elements.getNetworkElement().add(network_element);
241 nffg.setNetworkElements(network_elements);
245 private void generateFirewalls() {
246 NfunctionsType network_functions = new NfunctionsType();
248 for (String firewall : firewalls) {
249 NfType nf = new NfType();
251 nf.setFunctionalType("firewall");
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");
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);
270 CpointsType connection_points = new CpointsType();
271 CpType connection_point = new CpType();
272 connection_point.setId(firewall + "_in");
273 PortType port = new PortType();
275 port.setDirection(PortDirEnumType.IN);
277 connection_point.setPort(port);
278 connection_points.getConnectionPoint().add(connection_point);
280 CpType connection_point2 = new CpType();
281 connection_point2.setId(firewall + "_out");
282 PortType port2 = new PortType();
284 port2.setDirection(PortDirEnumType.OUT);
285 port2.setType("10GbE");
286 connection_point2.setPort(port2);
288 connection_points.getConnectionPoint().add(connection_point2);
290 CtrlInterfacesType control_interfaces = new CtrlInterfacesType();
291 CiType control_interface = new CiType();
292 control_interface.setId(firewall + "_ci");
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);
303 control_interfaces.getControlInterface().add(control_interface);
305 MonParamsType monitoring_parameters = new MonParamsType();
306 Parameter parameter = new Parameter();
307 parameter.getValue().add("Measure script");
308 monitoring_parameters.getParameter().add(parameter);
310 nf.setSpecification(specification);
311 nf.setConnectionPoints(connection_points);
312 nf.setControlInterfaces(control_interfaces);
313 nf.setMonitoringParameters(monitoring_parameters);
315 network_functions.getNetworkFunction().add(nf);
318 nffg.setNetworkFunctions(network_functions);
322 private void generateEndpoints() {
323 EpointsType eps = new EpointsType();
325 for (String e : endpoints) {
326 EpType endpoint = new EpType();
328 Flowspace flowspace = new Flowspace();
329 flowspace.setIngPhysPort("10");
332 flowspace.setTcp(tcp);
333 endpoint.setFlowspace(flowspace);
334 eps.getEndpoint().add(endpoint);
336 nffg.setEndpoints(eps);