6d6c692278a3a066e7e9b7d6ac2d5403437c99b7
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2018 Bell Canada, Pantheon Technologies 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.hc2vpp.fib.management.request;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import io.fd.hc2vpp.common.translate.util.AddressTranslator;
23 import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
24 import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.vpp.jvpp.core.dto.IpTableAddDel;
27 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
28 import java.nio.charset.StandardCharsets;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 public class FibTableRequest implements AddressTranslator, JvppReplyConsumer {
32
33     private final FutureJVppCore api;
34     /**
35      * FIB table Name
36      */
37     private String fibName;
38
39     /**
40      * FIB table id to be installed
41      */
42     private int fibTable;
43
44     /**
45      * Whether to write IPv6 FIB table or IPv4
46      */
47     private boolean isIpv6;
48
49     public FibTableRequest(FutureJVppCore api) {
50         this.api = api;
51     }
52
53     public void checkValid() {
54         checkNotNull(getFibName(), "Fib table name not set");
55         checkArgument(!getFibName().isEmpty(), "Fib table name must not be empty");
56     }
57
58     public void write(InstanceIdentifier<?> identifier) throws WriteFailedException {
59         sendRequest(identifier, ByteDataTranslator.BYTE_TRUE);
60     }
61
62     public void delete(InstanceIdentifier<?> identifier) throws WriteFailedException {
63         sendRequest(identifier, ByteDataTranslator.BYTE_FALSE);
64     }
65
66     private void sendRequest(final InstanceIdentifier<?> identifier, final byte isAdd)
67             throws WriteFailedException {
68         IpTableAddDel tableAddDel = new IpTableAddDel();
69         tableAddDel.tableId = getFibTable();
70         tableAddDel.isIpv6 = booleanToByte(isIpv6());
71         tableAddDel.isAdd = isAdd;
72         if (getFibName() != null) {
73             // FIB table name is optional
74             tableAddDel.name = getFibName().getBytes(StandardCharsets.UTF_8);
75         }
76         getReplyForWrite(api.ipTableAddDel(tableAddDel).toCompletableFuture(), identifier);
77     }
78
79     public int getFibTable() {
80         return fibTable;
81     }
82
83     public void setFibTable(int fibTable) {
84         this.fibTable = fibTable;
85     }
86
87     public boolean isIpv6() {
88         return isIpv6;
89     }
90
91     public void setIpv6(boolean ipv6) {
92         isIpv6 = ipv6;
93     }
94
95     public String getFibName() {
96         return fibName;
97     }
98
99     public void setFibName(String fibName) {
100         this.fibName = fibName;
101     }
102 }