docs: change code blocks from "shell" to "console"
[vpp.git] / src / vpp-api / java / jvpp-core / io / fd / vpp / jvpp / core / examples / LispAdjacencyExample.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.vpp.jvpp.core.examples;
18
19 import io.fd.vpp.jvpp.JVppRegistry;
20 import io.fd.vpp.jvpp.JVppRegistryImpl;
21 import io.fd.vpp.jvpp.core.JVppCoreImpl;
22 import io.fd.vpp.jvpp.core.dto.LispAddDelAdjacency;
23 import io.fd.vpp.jvpp.core.dto.LispAddDelLocalEid;
24 import io.fd.vpp.jvpp.core.dto.LispAddDelLocatorSet;
25 import io.fd.vpp.jvpp.core.dto.LispAddDelRemoteMapping;
26 import io.fd.vpp.jvpp.core.dto.LispAdjacenciesGet;
27 import io.fd.vpp.jvpp.core.dto.LispAdjacenciesGetReply;
28 import io.fd.vpp.jvpp.core.dto.LispEnableDisable;
29 import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade;
30 import java.nio.charset.StandardCharsets;
31 import java.util.concurrent.ExecutionException;
32 import java.util.logging.Logger;
33
34 /**
35  * Tests lisp adjacency creation and read (custom vpe.api type support showcase).
36  */
37 public class LispAdjacencyExample {
38
39     private static final Logger LOG = Logger.getLogger(LispAdjacencyExample.class.getName());
40
41     private static void enableLisp(final FutureJVppCoreFacade jvpp) throws ExecutionException, InterruptedException {
42         final LispEnableDisable request = new LispEnableDisable();
43         request.isEn = 1;
44         jvpp.lispEnableDisable(request).toCompletableFuture().get();
45         LOG.info("Lisp enabled successfully");
46     }
47
48     private static void addLocatorSet(final FutureJVppCoreFacade jvpp) throws ExecutionException, InterruptedException {
49         final LispAddDelLocatorSet request = new LispAddDelLocatorSet();
50         request.isAdd = 1;
51         request.locatorSetName = "ls1".getBytes(StandardCharsets.UTF_8);
52         jvpp.lispAddDelLocatorSet(request).toCompletableFuture().get();
53         LOG.info("Locator set created successfully:" + request.toString());
54     }
55
56     private static void addLocalEid(final FutureJVppCoreFacade jvpp) throws ExecutionException, InterruptedException {
57         final LispAddDelLocalEid request = new LispAddDelLocalEid();
58         request.isAdd = 1;
59         request.locatorSetName = "ls1".getBytes(StandardCharsets.UTF_8);
60         request.eid = new byte[] {1, 2, 1, 10};
61         request.eidType = 0; // ip4
62         request.vni = 0;
63         request.prefixLen = 32;
64         jvpp.lispAddDelLocalEid(request).toCompletableFuture().get();
65         LOG.info("Local EID created successfully:" + request.toString());
66     }
67
68     private static void addRemoteMapping(final FutureJVppCoreFacade jvpp)
69         throws ExecutionException, InterruptedException {
70         final LispAddDelRemoteMapping request = new LispAddDelRemoteMapping();
71         request.isAdd = 1;
72         request.vni = 0;
73         request.eid = new byte[] {1, 2, 1, 20};
74         request.eidLen = 32;
75         request.rlocNum = 1;
76         // FIXME!!!!
77         //request.rlocs = new byte[] {1, 1, 1, 1, 2, 1, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
78         jvpp.lispAddDelRemoteMapping(request).toCompletableFuture().get();
79         LOG.info("Remote mapping created successfully:" + request.toString());
80     }
81
82     private static void addAdjacency(final FutureJVppCoreFacade jvpp) throws ExecutionException, InterruptedException {
83         final LispAddDelAdjacency request = new LispAddDelAdjacency();
84         request.isAdd = 1;
85         request.leid = new byte[] {1, 2, 1, 10};
86         request.leidLen = 32;
87         request.reid = new byte[] {1, 2, 1, 20};
88         request.reidLen = 32;
89         request.eidType = 0; // ip4
90         request.vni = 0;
91         jvpp.lispAddDelAdjacency(request).toCompletableFuture().get();
92         LOG.info("Lisp adjacency created successfully:" + request.toString());
93     }
94
95     private static void showAdjacencies(final FutureJVppCoreFacade jvpp)
96         throws ExecutionException, InterruptedException {
97         final LispAdjacenciesGetReply reply =
98             jvpp.lispAdjacenciesGet(new LispAdjacenciesGet()).toCompletableFuture().get();
99         LOG.info("Lisp adjacency received successfully:" + reply.toString());
100     }
101
102     private static void testAdjacency(final FutureJVppCoreFacade jvpp) throws Exception {
103         enableLisp(jvpp);
104         addLocatorSet(jvpp);
105         addLocalEid(jvpp);
106         addRemoteMapping(jvpp);
107         addAdjacency(jvpp);
108         showAdjacencies(jvpp);
109     }
110
111     private static void testFutureApi() throws Exception {
112         LOG.info("Create lisp adjacency test");
113         try (final JVppRegistry registry = new JVppRegistryImpl("LispAdjacencyExample");
114              final FutureJVppCoreFacade jvppFacade = new FutureJVppCoreFacade(registry, new JVppCoreImpl())) {
115             LOG.info("Successfully connected to VPP");
116
117             testAdjacency(jvppFacade);
118             LOG.info("Disconnecting...");
119         }
120     }
121
122     public static void main(String[] args) throws Exception {
123         testFutureApi();
124     }
125 }