92a1564c00b685aa30bbf4cb980258c555f5f74a
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / write / VniTableCustomizer.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.checkState;
20
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
23 import io.fd.honeycomb.translate.util.RWUtils;
24
25 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 /**
37  * This customizer serves only as a check if user is not trying to define VniTable <br>
38  * without mapping to vrf/bd
39  */
40 public class VniTableCustomizer extends FutureJVppCustomizer implements ListWriterCustomizer<VniTable, VniTableKey> {
41
42     private static final Logger LOG = LoggerFactory.getLogger(VniTableCustomizer.class);
43
44     public VniTableCustomizer(FutureJVppCore futureJvpp) {
45         super(futureJvpp);
46     }
47
48     @Override
49     public void writeCurrentAttributes(InstanceIdentifier<VniTable> id, VniTable dataAfter, WriteContext writeContext)
50             throws WriteFailedException {
51         checkAtLeastOnChildExists(id, writeContext, false);
52     }
53
54     @Override
55     public void updateCurrentAttributes(InstanceIdentifier<VniTable> id, VniTable dataBefore, VniTable dataAfter,
56                                         WriteContext writeContext) throws WriteFailedException {
57         throw new UnsupportedOperationException("Operation not supported");
58     }
59
60     @Override
61     public void deleteCurrentAttributes(InstanceIdentifier<VniTable> id, VniTable dataBefore, WriteContext writeContext)
62             throws WriteFailedException {
63         checkAtLeastOnChildExists(id, writeContext, true);
64     }
65
66     private void checkAtLeastOnChildExists(final InstanceIdentifier<VniTable> id, final WriteContext writeContext,
67                                            final boolean before) {
68
69         Optional<VniTable> optData;
70         final InstanceIdentifier<VniTable> trimmedId = RWUtils.cutId(id, InstanceIdentifier.create(VniTable.class));
71         if (before) {
72             optData = writeContext.readBefore(trimmedId);
73         } else {
74             optData = writeContext.readAfter(trimmedId);
75         }
76
77         checkState(optData.isPresent(), "Illegal after-write state");
78
79         final VniTable dataAfter = optData.get();
80         checkState(dataAfter.getVrfSubtable() != null || dataAfter.getBridgeDomainSubtable() != null,
81                 "At least one of VrfSubtable/BridgeDomainSubtable must be defined");
82     }
83 }