HONEYCOMB-75 - Lisp implemetation
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / PitrCfgCustomizer.java
1 /*
2  * Copyright (c) 2015 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.honeycomb.lisp.translate.write;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static java.nio.charset.StandardCharsets.UTF_8;
21
22 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import java.nio.charset.StandardCharsets;
28 import java.util.concurrent.TimeoutException;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.pitr.cfg.grouping.PitrCfg;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.openvpp.jvpp.VppBaseCallException;
32 import org.openvpp.jvpp.core.dto.LispPitrSetLocatorSet;
33 import org.openvpp.jvpp.core.future.FutureJVppCore;
34
35
36 /**
37  * Customizer for {@code PitrCfg}
38  */
39 public class PitrCfgCustomizer extends FutureJVppCustomizer implements WriterCustomizer<PitrCfg> {
40
41     public PitrCfgCustomizer(FutureJVppCore futureJvpp) {
42         super(futureJvpp);
43     }
44
45     @Override
46     public void writeCurrentAttributes(InstanceIdentifier<PitrCfg> id, PitrCfg dataAfter, WriteContext writeContext)
47             throws WriteFailedException {
48         checkNotNull(dataAfter, "PitrCfg is null");
49         checkNotNull(dataAfter.getLocatorSet(), "Locator set name is null");
50
51         try {
52             addDelPitrSetLocatorSetAndReply(true, dataAfter);
53         } catch (VppBaseCallException | TimeoutException e) {
54             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
55         }
56     }
57
58     @Override
59     public void updateCurrentAttributes(InstanceIdentifier<PitrCfg> id, PitrCfg dataBefore, PitrCfg dataAfter,
60                                         WriteContext writeContext) throws WriteFailedException {
61         checkNotNull(dataAfter, "PitrCfg is null");
62         checkNotNull(dataAfter.getLocatorSet(), "Locator set name is null");
63
64         try {
65             addDelPitrSetLocatorSetAndReply(true, dataAfter);
66         } catch (VppBaseCallException | TimeoutException e) {
67             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
68         }
69     }
70
71     @Override
72     public void deleteCurrentAttributes(InstanceIdentifier<PitrCfg> id, PitrCfg dataBefore, WriteContext writeContext)
73             throws WriteFailedException {
74         checkNotNull(dataBefore, "PitrCfg is null");
75         checkNotNull(dataBefore.getLocatorSet(), "Locator set name is null");
76
77         try {
78             addDelPitrSetLocatorSetAndReply(false, dataBefore);
79         } catch (VppBaseCallException | TimeoutException e) {
80             throw new WriteFailedException.CreateFailedException(id, dataBefore, e);
81         }
82     }
83
84     private void addDelPitrSetLocatorSetAndReply(boolean add, PitrCfg data)
85             throws VppBaseCallException, TimeoutException {
86
87         LispPitrSetLocatorSet request = new LispPitrSetLocatorSet();
88         request.isAdd = TranslateUtils.booleanToByte(add);
89         request.lsName = data.getLocatorSet().getBytes(UTF_8);
90
91         TranslateUtils.getReply(getFutureJVpp().lispPitrSetLocatorSet(request).toCompletableFuture());
92     }
93
94 }