Open Kilda Java Documentation
FlowUtils.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.flow;
17 
18 import static java.lang.String.format;
19 import static java.util.Base64.getEncoder;
20 import static java.util.Collections.singletonList;
21 import static org.junit.Assert.assertEquals;
26 
51 
52 import com.fasterxml.jackson.core.JsonProcessingException;
53 import com.fasterxml.jackson.core.type.TypeReference;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import org.glassfish.jersey.client.ClientConfig;
56 import org.glassfish.jersey.client.HttpUrlConnectorProvider;
57 import org.glassfish.jersey.jackson.JacksonFeature;
58 
59 import java.io.IOException;
60 import java.util.ArrayList;
61 import java.util.Collections;
62 import java.util.HashSet;
63 import java.util.List;
64 import java.util.Set;
65 import java.util.concurrent.TimeUnit;
66 import javax.ws.rs.client.Client;
67 import javax.ws.rs.client.ClientBuilder;
68 import javax.ws.rs.client.Entity;
69 import javax.ws.rs.core.GenericType;
70 import javax.ws.rs.core.HttpHeaders;
71 import javax.ws.rs.core.MediaType;
72 import javax.ws.rs.core.Response;
73 
74 public final class FlowUtils {
75 
76  private static final String auth = topologyUsername + ":" + topologyPassword;
77  private static final String authHeaderValue = "Basic " + getEncoder().encodeToString(auth.getBytes());
78  private static final String FEATURE_TIME = String.valueOf(System.currentTimeMillis());
79  private static final int WAIT_ATTEMPTS = 10;
80  private static final int WAIT_DELAY = 2;
81 
82  private static final Client client = clientFactory();
83 
84  public static Client clientFactory() {
85  return ClientBuilder.newClient(new ClientConfig()).register(JacksonFeature.class);
86  }
87 
91  public static int getHealthCheck() {
92  System.out.println("\n==> Northbound Health-Check");
93 
94  long current = System.currentTimeMillis();
95  Client client = clientFactory();
96 
97  Response response = client
98  .target(northboundEndpoint)
99  .path("/api/v1/health-check")
100  .request(MediaType.APPLICATION_JSON)
101  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
102  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
103  .get();
104 
105  System.out.println(format("===> Response = %s", response.toString()));
106  System.out.println(format("===> Northbound Health-Check Time: %,.3f", getTimeDuration(current)));
107 
108  int responseCode = response.getStatus();
109  if (responseCode == 200) {
110  System.out.println(format("====> Health-Check = %s",
111  response.readEntity(HealthCheck.class)));
112  } else {
113  System.out.println(format("====> Error: Health-Check = %s",
114  response.readEntity(MessageError.class)));
115  }
116 
117  return responseCode;
118  }
119 
126  public static FlowPayload getFlow(final String flowId) {
127  System.out.println("\n==> Northbound Get Flow");
128 
129  long current = System.currentTimeMillis();
130  Client client = clientFactory();
131 
132  Response response = client
133  .target(northboundEndpoint)
134  .path("/api/v1/flows")
135  .path("{flowid}")
136  .resolveTemplate("flowid", flowId)
137  .request(MediaType.APPLICATION_JSON)
138  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
139  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
140  .get();
141 
142  System.out.println(format("===> Response = %s", response.toString()));
143  System.out.println(format("===> Northbound Get Flow Time: %,.3f", getTimeDuration(current)));
144 
145  int responseCode = response.getStatus();
146  if (responseCode == 200) {
147  FlowPayload flow = response.readEntity(FlowPayload.class);
148  System.out.println(format("====> Northbound Get Flow = %s", flow));
149  return flow;
150  } else {
151  System.out.println(format("====> Error: Northbound Get Flow = %s",
152  response.readEntity(MessageError.class)));
153  return null;
154  }
155  }
156 
163  public static FlowPayload putFlow(final FlowPayload payload) {
164  System.out.println("\n==> Northbound Create Flow");
165 
166  long current = System.currentTimeMillis();
167  Client client = clientFactory();
168 
169  Response response = client
170  .target(northboundEndpoint)
171  .path("/api/v1/flows")
172  .request(MediaType.APPLICATION_JSON)
173  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
174  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
175  .put(Entity.json(payload));
176 
177  System.out.println(format("===> Request Payload = %s", Entity.json(payload).getEntity()));
178  System.out.println(format("===> Response = %s", response.toString()));
179  System.out.println(format("===> Northbound Create Flow Time: %,.3f", getTimeDuration(current)));
180 
181  int responseCode = response.getStatus();
182  if (responseCode == 200) {
183  FlowPayload flow = response.readEntity(FlowPayload.class);
184  System.out.println(format("====> Northbound Create Flow = %s", flow));
185  return flow;
186  } else {
187  System.out.println(format("====> Error: Northbound Create Flow = %s",
188  response.readEntity(MessageError.class)));
189  return null;
190  }
191  }
192 
200  public static FlowPayload updateFlow(final String flowId, final FlowPayload payload) {
201  System.out.println("\n==> Northbound Update Flow");
202 
203  long current = System.currentTimeMillis();
204  Client client = clientFactory();
205 
206  Response response = client
207  .target(northboundEndpoint)
208  .path("/api/v1/flows")
209  .path("{flowid}")
210  .resolveTemplate("flowid", flowId)
211  .request(MediaType.APPLICATION_JSON)
212  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
213  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
214  .put(Entity.json(payload));
215 
216  System.out.println(format("===> Request Payload = %s", Entity.json(payload).getEntity()));
217  System.out.println(format("===> Response = %s", response.toString()));
218  System.out.println(format("===> Northbound Update Flow Time: %,.3f", getTimeDuration(current)));
219 
220  int responseCode = response.getStatus();
221  if (responseCode == 200) {
222  FlowPayload flow = response.readEntity(FlowPayload.class);
223  System.out.println(format("====> Northbound Update Flow = %s", flow));
224  return flow;
225  } else {
226  System.out.println(format("====> Error: Northbound Update Flow = %s",
227  response.readEntity(MessageError.class)));
228  return null;
229  }
230  }
231 
238  public static FlowPayload deleteFlow(final String flowId) {
239  System.out.println("\n==> Northbound Delete Flow");
240 
241  long current = System.currentTimeMillis();
242  Client client = clientFactory();
243 
244  Response response = client
245  .target(northboundEndpoint)
246  .path("/api/v1/flows")
247  .path("{flowid}")
248  .resolveTemplate("flowid", flowId)
249  .request(MediaType.APPLICATION_JSON)
250  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
251  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
252  .delete();
253 
254  System.out.println(format("===> Response = %s", response.toString()));
255  System.out.println(format("===> Northbound Delete Flow Time: %,.3f", getTimeDuration(current)));
256 
257  int responseCode = response.getStatus();
258  if (responseCode == 200) {
259  FlowPayload flow = response.readEntity(FlowPayload.class);
260  System.out.println(format("====> Northbound Delete Flow = %s", flow));
261  return flow;
262  } else {
263  System.out.println(format("====> Error: Northbound Delete Flow = %s",
264  response.readEntity(MessageError.class)));
265  return null;
266  }
267  }
268 
275  public static FlowPathPayload getFlowPath(final String flowId) {
276  System.out.println("\n==> Northbound Get Flow Path");
277 
278  long current = System.currentTimeMillis();
279  Client client = clientFactory();
280 
281  Response response = client
282  .target(northboundEndpoint)
283  .path("/api/v1/flows/{flowid}/path")
284  .resolveTemplate("flowid", flowId)
285  .request(MediaType.APPLICATION_JSON)
286  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
287  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
288  .get();
289 
290  System.out.println(format("===> Response = %s", response.toString()));
291  System.out.println(format("===> Northbound Get Flow Path Time: %,.3f", getTimeDuration(current)));
292 
293  int responseCode = response.getStatus();
294  if (responseCode == 200) {
295  FlowPathPayload flowPath = response.readEntity(FlowPathPayload.class);
296  System.out.println(format("====> Northbound Get Flow Path = %s", flowPath));
297  return flowPath;
298  } else {
299  System.out.println(format("====> Error: Northbound Get Flow Path = %s",
300  response.readEntity(MessageError.class)));
301  return null;
302  }
303  }
304 
312  throws InterruptedException, UnroutablePathException, RecoverableException {
313  Thread.sleep(1000);
315  return pathComputer.getPath(flow, PathComputer.Strategy.COST);
316  }
317 
325  public static FlowIdStatusPayload waitFlowStatus(String flowName, FlowState expected) throws InterruptedException {
326  FlowIdStatusPayload current = null;
327  for (int i = 0; i < WAIT_ATTEMPTS; i++) {
328  current = getFlowStatus(flowName);
329  if (current != null && expected.equals(current.getStatus())) {
330  break;
331  }
332  TimeUnit.SECONDS.sleep(WAIT_DELAY);
333  }
334  return current;
335  }
336 
341  public static void waitFlowDeletion(String flowId) throws InterruptedException, FlowOperationException {
342  for (int attempt = 0; attempt < WAIT_ATTEMPTS; attempt += 1) {
343  Response response = doGetFlowStatusRequest(flowId);
344  int status = response.getStatus();
345 
346  if (200 <= status && status < 300) {
347  TimeUnit.SECONDS.sleep(WAIT_DELAY);
348  continue;
349  }
350 
351  if (status != 404) {
352  throw new FlowOperationException(
353  response,
354  format("Flow status request for flow %s ens with %d", flowId, status));
355  }
356 
357  break;
358  }
359  }
360 
367  public static FlowIdStatusPayload getFlowStatus(final String flowId) {
368  Response response = doGetFlowStatusRequest(flowId);
369 
370  int responseCode = response.getStatus();
371  if (responseCode != 200) {
372  System.out.println(format("====> Error: Northbound Get Flow Status = %s",
373  response.readEntity(MessageError.class)));
374  return null;
375  }
376  return response.readEntity(FlowIdStatusPayload.class);
377  }
378 
379  private static Response doGetFlowStatusRequest(final String flowId) {
380  System.out.println("\n==> Northbound Get Flow Status");
381 
382  long current = System.currentTimeMillis();
383  Client client = clientFactory();
384 
385  Response response = client
386  .target(northboundEndpoint)
387  .path("/api/v1/flows/status")
388  .path("{flowid}")
389  .resolveTemplate("flowid", flowId)
390  .request(MediaType.APPLICATION_JSON)
391  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
392  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
393  .get();
394 
395  System.out.println(format("===> Response = %s", response.toString()));
396  System.out.println(format("===> Northbound Get Flow Status Time: %,.3f", getTimeDuration(current)));
397 
398  int status = response.getStatus();
399  System.out.println(format("====> Northbound Get Flow Status = %s", status));
400 
401  return response;
402  }
403 
409  public static List<FlowPayload> getFlowDump() {
410  System.out.println("\n==> Northbound Get Flow Dump");
411 
412  long current = System.currentTimeMillis();
413  Client client = clientFactory();
414 
415  Response response = client
416  .target(northboundEndpoint)
417  .path("/api/v1/flows")
418  .request(MediaType.APPLICATION_JSON)
419  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
420  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
421  .get();
422 
423  System.out.println(format("===> Response = %s", response.toString()));
424  System.out.println(format("===> Northbound Get Flow Dump Time: %,.3f", getTimeDuration(current)));
425 
426  int responseCode = response.getStatus();
427  if (responseCode == 200) {
428  List<FlowPayload> flows = response.readEntity(new GenericType<List<FlowPayload>>() {
429  });
430  System.out.println(format("====> Northbound Get Flow Dump = %d", flows.size()));
431  return flows;
432  } else {
433  System.out.println(format("====> Error: Northbound Get Flow Dump = %s",
434  response.readEntity(MessageError.class)));
435  return Collections.emptyList();
436  }
437  }
438 
444  public static List<Flow> dumpFlows() {
445  System.out.println("\n==> Topology-Engine Dump Flows");
446 
447  long current = System.currentTimeMillis();
448  Client client = clientFactory();
449 
450  Response response = client
451  .target(topologyEndpoint)
452  .path("/api/v1/topology/flows")
453  .request()
454  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
455  .get();
456 
457  System.out.println(format("===> Response = %s", response.toString()));
458  System.out.println(format("===> Topology-Engine Dump Flows Time: %,.3f", getTimeDuration(current)));
459 
460  try {
461  List<Flow> flows = new ObjectMapper().readValue(response.readEntity(String.class),
462  new TypeReference<List<Flow>>() {
463  });
464  System.out.println(format("====> Topology-Engine Dump Flows = %d", flows.size()));
465 
466  return flows;
467 
468  } catch (IOException ex) {
469  throw new TopologyProcessingException(format("Unable to parse the flows '%s'.", response.toString()), ex);
470  }
471  }
472 
478  public static Integer getLinkBandwidth(final SwitchId srcSwitch, final String srcPort) {
479  System.out.println("\n==> Topology-Engine Link Bandwidth");
480 
481  long current = System.currentTimeMillis();
482  Client client = clientFactory();
483 
484  Response response = client
485  .target(topologyEndpoint)
486  .path("/api/v1/topology/links/bandwidth/")
487  .path("{src_switch}")
488  .path("{src_port}")
489  .resolveTemplate("src_switch", srcSwitch)
490  .resolveTemplate("src_port", srcPort)
491  .request()
492  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
493  .get();
494 
495  System.out.println(format("===> Response = %s", response.toString()));
496  System.out.println(format("===> Topology-Engine Link Bandwidth Time: %,.3f", getTimeDuration(current)));
497 
498  try {
499  Integer bandwidth = new ObjectMapper().readValue(response.readEntity(String.class), Integer.class);
500  System.out.println(format("====> Link switch=%s port=%s bandwidth=%d", srcSwitch, srcPort, bandwidth));
501 
502  return bandwidth;
503  } catch (IOException ex) {
504  throw new TopologyProcessingException(format("Unable to parse the links '%s'.", response.toString()), ex);
505  }
506  }
507 
511  public static void restoreFlows() {
512  System.out.println("\n==> Topology-Engine Restore Flows");
513 
514  long current = System.currentTimeMillis();
515  Client client = clientFactory();
516 
517  Response response = client
518  .target(topologyEndpoint)
519  .path("/api/v1/flows/restore")
520  .request()
521  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
522  .get();
523 
524  System.out.println(format("===> Response = %s", response.toString()));
525  System.out.println(format("===> Topology-Engine Restore Flows Time: %,.3f", getTimeDuration(current)));
526  }
527 
531  public static void cleanupFlows() {
532  try {
533  Set<String> flows = new HashSet<>();
534 
535  // TODO: This method started with getting counts and compariing, but that shouldn't be
536  // the responsibility of this method given its name - cleanupFlows.
537  // So, the TODO is to determine whether this code exists elsewhere in tests,
538  // and if not, move it somewhere after, or part of, create test.
539 
540  // Get the flows through the NB API
541  List<FlowPayload> nbFlows = getFlowDump();
542  System.out.println(format("=====> Cleanup Flows, nbflow count = %d",
543  nbFlows.size()));
544 
545  nbFlows.forEach(flow -> flows.add(flow.getId()));
546 
547  // Get the flows through the TE Rest API ... loop until the math works out.
548  List<Flow> tpeFlows = new ArrayList<>();
549  for (int i = 0; i < 10; ++i) {
550  tpeFlows = dumpFlows();
551  if (tpeFlows.size() == nbFlows.size() * 2) {
552  tpeFlows.forEach(flow -> flows.add(flow.getFlowId()));
553  break;
554  }
555  TimeUnit.SECONDS.sleep(2);
556  }
557  System.out.println(format("=====> Cleanup Flows, tpeFlows count = %d",
558  tpeFlows.size()));
559 
560  // Delete all the flows
561  flows.forEach(FlowUtils::deleteFlow);
562 
563  // Wait for them to become zero
564  int nbCount = -1;
565  int terCount = -1;
566  for (int i = 0; i < 10; ++i) {
567  TimeUnit.SECONDS.sleep(2);
568  nbCount = dumpFlows().size();
569  terCount = getFlowDump().size();
570  if (nbCount == 0 && terCount == 0) {
571  break;
572  }
573  }
574 
575  assertEquals(0, nbCount);
576  assertEquals(0, terCount);
577 
578  // (crimi) - it is unclear why we are doing a count validation here .. it makes sense to do this
579  // in the creation. But on cleanup, we just want things to be zero.
580  // assertEquals(nbFlows.size() * 2, tpeFlows.size());
581  // assertEquals(nbFlows.size(), flows.size());
582 
583  } catch (Exception exception) {
584  System.out.println(format("Error during flow deletion: %s", exception.getMessage()));
585  exception.printStackTrace();
586  }
587  }
588 
595  public static String getFlowName(final String flowId) {
596  return format("%s-%s", flowId, FEATURE_TIME);
597  }
598 
605  public static double getTimeDuration(final long current) {
606  return (System.currentTimeMillis() - current) / 1000.0;
607  }
608 
612  public static boolean isTrafficTestsEnabled() {
613  boolean isEnabled = Boolean.valueOf(System.getProperty("traffic", "true"));
614  System.out.println(format("\n=====> Traffic check is %s", isEnabled ? "enabled" : "disabled"));
615  return isEnabled;
616  }
617 
622  System.out.println("\n==> toggle features status");
623 
624  Client client = clientFactory();
625 
626  Response response;
627  response = client
628  .target(northboundEndpoint)
629  .path("/api/v1/features")
630  .request(MediaType.APPLICATION_JSON)
631  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
632  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
633  .post(Entity.json(desired));
634 
635  System.out.println(format("===> Response = %s", response.toString()));
636 
637  if (response.getStatus() != 200) {
638  System.out.println(format("====> Error: Northbound Create Flow = POST status: %s", response.getStatus()));
639  return null;
640  }
641 
642  response = client
643  .target(northboundEndpoint)
644  .path("/api/v1/features")
645  .request(MediaType.APPLICATION_JSON)
646  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
647  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
648  .get();
649  if (response.getStatus() != 200) {
650  System.out.println(format("====> Error: Northbound Create Flow = GET status: %s", response.getStatus()));
651  return null;
652  }
653 
654  return response.readEntity(FeatureTogglePayload.class);
655  }
656 
661  System.out.println("\n==> Northbound Sync Flow Cache");
662 
663  long current = System.currentTimeMillis();
664  Client client = clientFactory();
665  // Enable support of PATCH method
666  client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
667 
668  Response response = client
669  .target(northboundEndpoint)
670  .path("/api/v1/flows/cache")
671  .request(MediaType.APPLICATION_JSON)
672  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
673  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
674  .method("PATCH");
675 
676  System.out.println(format("===> Northbound Sync Flow Cache Time: %,.3f", getTimeDuration(current)));
677 
678  if (response.getStatus() != 200) {
679  System.out.println(format("====> Error: Northbound Sync Flow Cache = PATCH status: %s",
680  response.getStatus()));
681  return null;
682  }
683 
684  System.out.println(format("===> Response = %s", response.toString()));
685  return response.readEntity(FlowCacheSyncResults.class);
686  }
687 
692  System.out.println("\n==> Northbound Invalidate Flow Cache");
693 
694  long current = System.currentTimeMillis();
695  Client client = clientFactory();
696 
697  Response response = client
698  .target(northboundEndpoint)
699  .path("/api/v1/flows/cache")
700  .request(MediaType.APPLICATION_JSON)
701  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
702  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
703  .delete();
704 
705  System.out.println(format("===> Northbound Invalidate Flow Cache Time: %,.3f", getTimeDuration(current)));
706 
707  if (response.getStatus() != 200) {
708  System.out.println(
709  format("====> Error: Northbound Invalidate Flow Cache = PATCH status: %s", response.getStatus()));
710  return null;
711  }
712 
713  System.out.println(format("===> Response = %s", response.toString()));
714  return response.readEntity(FlowCacheSyncResults.class);
715  }
716 
720  public static boolean deleteFlowViaTe(final String flowId) {
721  System.out.println("\n==> TopologyEngine Delete Flow");
722 
723  long current = System.currentTimeMillis();
724  Client client = clientFactory();
725 
726  Response response = client
727  .target(topologyEndpoint)
728  .path("/api/v1/flow/{flowid}")
729  .resolveTemplate("flowid", flowId)
730  .request(MediaType.APPLICATION_JSON)
731  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
732  .delete();
733 
734  System.out.println(format("===> TopologyEngine Delete Flow Time: %,.3f", getTimeDuration(current)));
735 
736  int status = response.getStatus();
737  if (status != 200) {
738  System.out.println(String.format("====> Error: TopologyEngine Delete Flow = %s",
739  response.readEntity(String.class)));
740  return false;
741  }
742 
743  System.out.println(format("====> TopologyEngine Delete Flow = %s", response.readEntity(String.class)));
744  return true;
745  }
746 
750  public static List<FlowValidationDto> validateFlow(final String flowId) {
751  System.out.println("\n==> Northbound Validate Flow");
752 
753  Client client = ClientBuilder.newClient(new ClientConfig());
754 
755  Response response = client
756  .target(northboundEndpoint)
757  .path("/api/v1/flows/{flow-id}/validate")
758  .resolveTemplate("flow-id", flowId)
759  .request(MediaType.APPLICATION_JSON)
760  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
761  .header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis()))
762  .get();
763 
764  System.out.println(format("===> Response = %s", response.toString()));
765 
766  int responseCode = response.getStatus();
767  if (responseCode == 200) {
768  List<FlowValidationDto> flowDiscrepancy =
769  response.readEntity(new GenericType<List<FlowValidationDto>>() {});
770  System.out.println(format("====> Northbound Validate Flow = %s", flowDiscrepancy));
771  return flowDiscrepancy;
772  } else {
773  System.out.println(format("====> Error: Northbound Validate Flow = %s",
774  response.readEntity(MessageError.class)));
775  return null;
776  }
777  }
778 
786  public static BatchResults pushFlow(FlowInfoData flowInfo, boolean propagate)
787  throws JsonProcessingException {
788  System.out.println("\n==> Northbound Push Flow");
789 
790  long current = System.currentTimeMillis();
791  Client client = clientFactory();
792 
793  String correlationId = String.valueOf(current);
794  flowInfo.setCorrelationId(correlationId);
795 
796  String requestJson = new ObjectMapper().writerFor(new TypeReference<List<FlowInfoData>>() { })
797  .writeValueAsString(singletonList(flowInfo));
798 
799  Response response = client
800  .target(northboundEndpoint)
801  .path("/api/v1/push/flows")
802  .queryParam("propagate", propagate)
803  .request(MediaType.APPLICATION_JSON)
804  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
805  .header(Utils.CORRELATION_ID, correlationId)
806  .put(Entity.json(requestJson));
807 
808  System.out.println(format("===> Request Payload = %s", requestJson));
809  System.out.println(format("===> Response = %s", response.toString()));
810  System.out.println(format("===> Northbound Push Flow Time: %,.3f", getTimeDuration(current)));
811 
812  int responseCode = response.getStatus();
813  if (responseCode == 200) {
814  BatchResults result = response.readEntity(BatchResults.class);
815  System.out.println(format("====> Northbound Push Flow = %s", result));
816  return result;
817  } else {
818  System.out.println(format("====> Error: Northbound Push Flow = %s",
819  response.readEntity(MessageError.class)));
820  return null;
821  }
822  }
823 
827  public static VerificationOutput verifyFlow(String flowId, VerificationInput payload) {
828  long currentTime = System.currentTimeMillis();
829  String correlationId = String.valueOf(currentTime);
830 
831  System.out.println(String.format("\n==> Northbound verify Flow request (correlationId: %s)", correlationId));
832  Response response = client
833  .target(northboundEndpoint)
834  .path("/api/v1/flows/{id}/verify")
835  .resolveTemplate("id", flowId)
836  .request(MediaType.APPLICATION_JSON)
837  .header(HttpHeaders.AUTHORIZATION, authHeaderValue)
838  .header(Utils.CORRELATION_ID, correlationId)
839  .method("PUT", Entity.json(payload));
840 
841  System.out.println(format("===> Response = %s", response.toString()));
842  System.out.println(format("===> Northbound VERIFY Flow Time: %,.3f", getTimeDuration(currentTime)));
843 
844  int responseCode = response.getStatus();
845  if (responseCode == 200) {
846  VerificationOutput result = response.readEntity(VerificationOutput.class);
847  System.out.println(format("====> Northbound VERIFY Flow = %s", result));
848  return result;
849  } else {
850  System.out.println(format("====> Error: Northbound VERIFY Flow = %s",
851  response.readEntity(MessageError.class)));
852  return null;
853  }
854  }
855 
856  private FlowUtils() { }
857 }
static FlowPayload updateFlow(final String flowId, final FlowPayload payload)
Definition: FlowUtils.java:200
static VerificationOutput verifyFlow(String flowId, VerificationInput payload)
Definition: FlowUtils.java:827
static boolean deleteFlowViaTe(final String flowId)
Definition: FlowUtils.java:720
static List< FlowPayload > getFlowDump()
Definition: FlowUtils.java:409
static boolean isTrafficTestsEnabled()
Definition: FlowUtils.java:612
static Client clientFactory()
Definition: FlowUtils.java:84
static FlowPayload deleteFlow(final String flowId)
Definition: FlowUtils.java:238
ImmutablePair< PathInfoData, PathInfoData > getPath(Flow flow, AvailableNetwork network, Strategy strategy)
static List< Flow > dumpFlows()
Definition: FlowUtils.java:444
def status()
Definition: rest.py:593
static FlowPathPayload getFlowPath(final String flowId)
Definition: FlowUtils.java:275
static FlowIdStatusPayload getFlowStatus(final String flowId)
Definition: FlowUtils.java:367
static FlowCacheSyncResults invalidateFlowCache()
Definition: FlowUtils.java:691
list result
Definition: plan-d.py:72
static final String CORRELATION_ID
Definition: Utils.java:43
static FlowPayload getFlow(final String flowId)
Definition: FlowUtils.java:126
static List< FlowValidationDto > validateFlow(final String flowId)
Definition: FlowUtils.java:750
static void waitFlowDeletion(String flowId)
Definition: FlowUtils.java:341
static FeatureTogglePayload updateFeaturesStatus(FeatureTogglePayload desired)
Definition: FlowUtils.java:621
static FlowPayload putFlow(final FlowPayload payload)
Definition: FlowUtils.java:163
static BatchResults pushFlow(FlowInfoData flowInfo, boolean propagate)
Definition: FlowUtils.java:786
static FlowCacheSyncResults syncFlowCache()
Definition: FlowUtils.java:660
static FlowIdStatusPayload waitFlowStatus(String flowName, FlowState expected)
Definition: FlowUtils.java:325
static String getFlowName(final String flowId)
Definition: FlowUtils.java:595
static final String northboundEndpoint
static double getTimeDuration(final long current)
Definition: FlowUtils.java:605
static ImmutablePair< PathInfoData, PathInfoData > getFlowPath(Flow flow)
Definition: FlowUtils.java:311
static Integer getLinkBandwidth(final SwitchId srcSwitch, final String srcPort)
Definition: FlowUtils.java:478