summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/escape/verify/client/Neo4jManagerClient.java
blob: 044c1361afcc23818d7910775a862c92f1cec138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*******************************************************************************
 * Copyright (c) 2017 Politecnico di Torino and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/

package it.polito.escape.verify.client;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import it.polito.escape.verify.deserializer.PathsMessageBodyReader;
import it.polito.escape.verify.model.Entry;
import it.polito.nffg.neo4j.jaxb.ActionEnumType;
import it.polito.nffg.neo4j.jaxb.ActionType;
import it.polito.nffg.neo4j.jaxb.ActionsType;
import it.polito.nffg.neo4j.jaxb.CiType;
import it.polito.nffg.neo4j.jaxb.CiType.Attributes;
import it.polito.nffg.neo4j.jaxb.CiType.Attributes.Attribute;
import it.polito.nffg.neo4j.jaxb.CpType;
import it.polito.nffg.neo4j.jaxb.CpointsType;
import it.polito.nffg.neo4j.jaxb.CtrlInterfacesType;
import it.polito.nffg.neo4j.jaxb.EpCpType;
import it.polito.nffg.neo4j.jaxb.EpType;
import it.polito.nffg.neo4j.jaxb.EpType.Flowspace;
import it.polito.nffg.neo4j.jaxb.EpointsType;
import it.polito.nffg.neo4j.jaxb.EpsCpsType;
import it.polito.nffg.neo4j.jaxb.FlowrulesType;
import it.polito.nffg.neo4j.jaxb.FlowrulesType.Flowspace.Tcp;
import it.polito.nffg.neo4j.jaxb.MonParamsType;
import it.polito.nffg.neo4j.jaxb.MonParamsType.Parameter;
import it.polito.nffg.neo4j.jaxb.NeType;
import it.polito.nffg.neo4j.jaxb.NelementsType;
import it.polito.nffg.neo4j.jaxb.NfType;
import it.polito.nffg.neo4j.jaxb.Nffg;
import it.polito.nffg.neo4j.jaxb.NfunctionsType;
import it.polito.nffg.neo4j.jaxb.ObjectFactory;
import it.polito.nffg.neo4j.jaxb.Paths;
import it.polito.nffg.neo4j.jaxb.PortDirEnumType;
import it.polito.nffg.neo4j.jaxb.PortType;
import it.polito.nffg.neo4j.jaxb.SpecType;
import it.polito.nffg.neo4j.jaxb.SpecType.Cpu;
import it.polito.nffg.neo4j.jaxb.SpecType.Deployment;
import it.polito.nffg.neo4j.jaxb.SpecType.Image;
import it.polito.nffg.neo4j.jaxb.SpecType.Memory;
import it.polito.nffg.neo4j.jaxb.SpecType.Storage;

public class Neo4jManagerClient {

	private JAXBContext					jc;

	private String						address;

	private Nffg						nffg;

	private List<String>				endpoints		= new LinkedList<String>();

	private List<String>				firewalls		= new LinkedList<String>();

	private Map<String, List<Entry>>	routingTable	= new HashMap<String, List<Entry>>();

	private String						source;

	private String						destination;

	private String						xmlString;

	private WebTarget					baseTarget;

	public Neo4jManagerClient() {

	}

	public Neo4jManagerClient(	String address, String source, String destination, List<String> endpoints,
								List<String> firewalls, Map<String, List<Entry>> routingTable) {
		this.address = address;
		this.source = source;
		this.destination = destination;
		this.endpoints = endpoints;
		this.firewalls = firewalls;
		this.routingTable = routingTable;

		Client client = ClientBuilder.newBuilder().register(PathsMessageBodyReader.class).build();

		this.baseTarget = client.target(this.address);
	}

	public Paths getPaths() throws Exception {
		try {
			this.generateCustomXml();
		}
		catch (JAXBException e) {
			throw (e);
		}

		WebTarget graphsTarget = baseTarget.path("graphs");
		WebTarget pathSourceDestination = graphsTarget.path("{graphId}/paths");
		WebTarget deleteNffg = graphsTarget.path("{graphId}");

		Response deleteNffgResponse = deleteNffg.resolveTemplate("graphId", "1").request().delete();
		if (deleteNffgResponse.getStatus() != javax.ws.rs.core.Response.Status.NO_CONTENT.getStatusCode()
			&& deleteNffgResponse.getStatus() != javax.ws.rs.core.Response.Status.NOT_FOUND.getStatusCode()) {
			throw new Exception("graph deletion failed");
		}

		Response createNffgResponse = graphsTarget	.request("application/xml")
													.post(Entity.entity(this.xmlString, "application/xml"));
		if (createNffgResponse.getStatus() != javax.ws.rs.core.Response.Status.CREATED.getStatusCode()) {
			throw new Exception("graph creation failed");
		}

		System.out.println("Getting paths from node \"" + this.source + "\" to node \"" + this.destination + "\"...");
		Response getPath = pathSourceDestination.resolveTemplate("graphId", "1")
												.queryParam("src", this.source)
												.queryParam("dst", this.destination)
												.queryParam("dir", "outgoing")
												.request(MediaType.APPLICATION_XML)
												.get();

		System.out.println("Paths from node \"" + this.source + "\" to node \"" + this.destination + "\":");

		Paths paths = null;
		try {
			paths = getPath.readEntity(Paths.class);
		}
		catch (ProcessingException e) {
			throw (e);
		}
		catch (IllegalStateException e) {
			throw (e);
		}

		return paths;
	}

	private void generateCustomXml() throws JAXBException {

		jc = JAXBContext.newInstance("it.polito.nffg.neo4j.jaxb");

		nffg = new Nffg();
		nffg.setId("nffg_1");

		generateEndpoints();
		generateFirewalls();
		generateConnections();

		MonParamsType monitoring_parameters = new MonParamsType();
		nffg.setMonitoringParameters(monitoring_parameters);

		JAXBElement<Nffg> root = (new ObjectFactory()).createNffg(nffg);

		Marshaller m;
		try {
			m = jc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

			StringWriter stringWriter = new StringWriter();
			try {
				XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter);
				m.marshal(root, xmlStreamWriter);
				xmlString = stringWriter.getBuffer().toString();
			}
			catch (XMLStreamException e) {
				e.printStackTrace();
			}
			catch (FactoryConfigurationError e) {

				e.printStackTrace();
			}
			// m.marshal( root, new File("nffg.xml") );
			System.out.println(xmlString);

		}
		catch (JAXBException e) {
			throw (e);
		}
	}

	private void generateConnections() {
		NelementsType network_elements = new NelementsType();
		NeType network_element = new NeType();
		network_element.setId("ne_1");
		network_element.setType("BiSBiS");
		EpsCpsType ep_cps = new EpsCpsType();

		for (String node : routingTable.keySet()) {
			EpCpType ep_cp = new EpCpType();
			ep_cp.setIdRef(node);
			FlowrulesType flowrules = new FlowrulesType();
			it.polito.nffg.neo4j.jaxb.FlowrulesType.Flowspace flowspace =
																		new it.polito.nffg.neo4j.jaxb.FlowrulesType.Flowspace();
			flowrules.setFlowspace(flowspace);
			ActionsType actions = new ActionsType();
			for (Entry e : routingTable.get(node)) {
				ActionType action = new ActionType();
				action.setType(ActionEnumType.fromValue(e.getDirection()));
				action.setPort(e.getDestination());
				actions.getAction().add(action);
			}
			flowrules.setActions(actions);

			ep_cp.getFlowrules().add(flowrules);
			ep_cps.getEpCp().add(ep_cp);
		}
		network_element.setEpsCps(ep_cps);

		MonParamsType monitoring_parameters = new MonParamsType();
		Parameter parameter = new Parameter();
		parameter.getValue().add("Bandwith ep_1 cp_1 100mbit");
		monitoring_parameters.getParameter().add(parameter);
		Parameter parameter2 = new Parameter();
		parameter2.getValue().add("Delay ep_1 cp_1 50ms");
		monitoring_parameters.getParameter().add(parameter2);

		network_element.setMonitoringParameters(monitoring_parameters);

		network_elements.getNetworkElement().add(network_element);

		nffg.setNetworkElements(network_elements);

	}

	private void generateFirewalls() {
		NfunctionsType network_functions = new NfunctionsType();

		for (String firewall : firewalls) {
			NfType nf = new NfType();
			nf.setId(firewall);
			nf.setFunctionalType("firewall");

			SpecType specification = new SpecType();
			Deployment deployment = new Deployment();
			deployment.setType("PolitoFirewall");
			Image image = new Image();
			image.setUri("http://www.polito.it");
			Cpu cpu = new Cpu();
			cpu.setNumCores((short) (7));
			Memory memory = new Memory();
			memory.setSize("10MiB");
			Storage storage = new Storage();
			storage.setSize("100MiB");
			specification.setDeployment(deployment);
			specification.setImage(image);
			specification.setCpu(cpu);
			specification.setMemory(memory);
			specification.setStorage(storage);

			CpointsType connection_points = new CpointsType();
			CpType connection_point = new CpType();
			connection_point.setId(firewall + "_in");
			PortType port = new PortType();
			port.setId(79);
			port.setDirection(PortDirEnumType.IN);
			port.setType("GbE");
			connection_point.setPort(port);
			connection_points.getConnectionPoint().add(connection_point);

			CpType connection_point2 = new CpType();
			connection_point2.setId(firewall + "_out");
			PortType port2 = new PortType();
			port2.setId(77);
			port2.setDirection(PortDirEnumType.OUT);
			port2.setType("10GbE");
			connection_point2.setPort(port2);

			connection_points.getConnectionPoint().add(connection_point2);

			CtrlInterfacesType control_interfaces = new CtrlInterfacesType();
			CiType control_interface = new CiType();
			control_interface.setId(firewall + "_ci");

			Attributes attributes = new Attributes();
			Attribute attribute = new Attribute();
			attribute.setValue("tcp://127.0.0.1:5555");
			attributes.getAttribute().add(attribute);
			Attribute attribute2 = new Attribute();
			attribute2.setValue("Netconf");
			attributes.getAttribute().add(attribute2);
			control_interface.setAttributes(attributes);

			control_interfaces.getControlInterface().add(control_interface);

			MonParamsType monitoring_parameters = new MonParamsType();
			Parameter parameter = new Parameter();
			parameter.getValue().add("Measure script");
			monitoring_parameters.getParameter().add(parameter);

			nf.setSpecification(specification);
			nf.setConnectionPoints(connection_points);
			nf.setControlInterfaces(control_interfaces);
			nf.setMonitoringParameters(monitoring_parameters);

			network_functions.getNetworkFunction().add(nf);

		}
		nffg.setNetworkFunctions(network_functions);

	}

	private void generateEndpoints() {
		EpointsType eps = new EpointsType();

		for (String e : endpoints) {
			EpType endpoint = new EpType();
			endpoint.setId(e);
			Flowspace flowspace = new Flowspace();
			flowspace.setIngPhysPort("10");
			Tcp tcp = new Tcp();
			tcp.setSrc(80);
			flowspace.setTcp(tcp);
			endpoint.setFlowspace(flowspace);
			eps.getEndpoint().add(endpoint);
		}
		nffg.setEndpoints(eps);
	}

}