HONEYCOMB-154: update revison of models that changed since 16.09
[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.vpp.util.ByteDataTranslator;
24 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import java.util.concurrent.TimeoutException;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.pitr.cfg.grouping.PitrCfg;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.VppBaseCallException;
32 import io.fd.vpp.jvpp.core.dto.LispPitrSetLocatorSet;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34
35
36 /**
37  * Customizer for {@code PitrCfg}
38  */
39 public class PitrCfgCustomizer extends FutureJVppCustomizer
40         implements WriterCustomizer<PitrCfg>, JvppReplyConsumer, ByteDataTranslator {
41
42     private static final String DEFAULT_LOCATOR_SET_NAME = "N/A";
43
44     public PitrCfgCustomizer(FutureJVppCore futureJvpp) {
45         super(futureJvpp);
46     }
47
48     @Override
49     public void writeCurrentAttributes(InstanceIdentifier<PitrCfg> id, PitrCfg dataAfter, WriteContext writeContext)
50             throws WriteFailedException {
51         checkNotNull(dataAfter, "PitrCfg is null");
52         checkNotNull(dataAfter.getLocatorSet(), "Locator set name is null");
53
54         try {
55             addDelPitrSetLocatorSetAndReply(true, dataAfter);
56         } catch (VppBaseCallException | TimeoutException e) {
57             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
58         }
59     }
60
61     @Override
62     public void updateCurrentAttributes(InstanceIdentifier<PitrCfg> id, PitrCfg dataBefore, PitrCfg dataAfter,
63                                         WriteContext writeContext) throws WriteFailedException {
64         checkNotNull(dataAfter, "PitrCfg is null");
65         checkNotNull(dataAfter.getLocatorSet(), "Locator set name is null");
66
67         try {
68             addDelPitrSetLocatorSetAndReply(true, dataAfter);
69         } catch (VppBaseCallException | TimeoutException e) {
70             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
71         }
72     }
73
74     @Override
75     public void deleteCurrentAttributes(InstanceIdentifier<PitrCfg> id, PitrCfg dataBefore, WriteContext writeContext)
76             throws WriteFailedException {
77         checkNotNull(dataBefore, "PitrCfg is null");
78         checkNotNull(dataBefore.getLocatorSet(), "Locator set name is null");
79
80         try {
81             addDelPitrSetLocatorSetAndReply(false, dataBefore);
82         } catch (VppBaseCallException | TimeoutException e) {
83             throw new WriteFailedException.CreateFailedException(id, dataBefore, e);
84         }
85     }
86
87     private void addDelPitrSetLocatorSetAndReply(boolean add, PitrCfg data)
88             throws VppBaseCallException, TimeoutException {
89
90         if (DEFAULT_LOCATOR_SET_NAME.equals(data.getLocatorSet())) {
91             // ignores attempts to write default locator set
92             // therefore even while its loaded to config data of honeycomb while starting
93             // you can still enable/disable Lisp without having to define N/A as default pitr-set
94             return;
95         }
96
97         LispPitrSetLocatorSet request = new LispPitrSetLocatorSet();
98         request.isAdd = booleanToByte(add);
99         request.lsName = data.getLocatorSet().getBytes(UTF_8);
100
101         getReply(getFutureJVpp().lispPitrSetLocatorSet(request).toCompletableFuture());
102     }
103
104 }