HONEYCOMB-236 - Unified read/write exceptions
[honeycomb.git] / nsh / impl / src / main / java / io / fd / honeycomb / vppnsh / impl / config / NshMapWriterCustomizer.java
1 /*
2  * Copyright (c) 2016 Intel 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.vppnsh.impl.config;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static com.google.common.base.Preconditions.checkState;
21
22 import io.fd.honeycomb.translate.MappingContext;
23 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
25 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.honeycomb.vppnsh.impl.util.FutureJVppNshCustomizer;
30 import io.fd.vpp.jvpp.nsh.dto.NshAddDelMap;
31 import io.fd.vpp.jvpp.nsh.dto.NshAddDelMapReply;
32 import io.fd.vpp.jvpp.nsh.future.FutureJVppNsh;
33 import java.util.concurrent.CompletionStage;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.VxlanGpe;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.maps.NshMap;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.maps.NshMapKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Writer customizer responsible for NshMap create/delete.
44  */
45 public class NshMapWriterCustomizer extends FutureJVppNshCustomizer
46         implements ListWriterCustomizer<NshMap, NshMapKey>, ByteDataTranslator, JvppReplyConsumer {
47
48     private static final Logger LOG = LoggerFactory.getLogger(NshMapWriterCustomizer.class);
49     private final NamingContext nshMapContext;
50     private final NamingContext interfaceContext;
51
52     public NshMapWriterCustomizer(@Nonnull final FutureJVppNsh futureJVppNsh,
53                                   @Nonnull final NamingContext nshMapContext,
54                                   @Nonnull final NamingContext interfaceContext) {
55         super(futureJVppNsh);
56         this.nshMapContext = checkNotNull(nshMapContext, "nshMapContext should not be null");
57         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
58     }
59
60     @Override
61     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<NshMap> id,
62                                        @Nonnull final NshMap dataAfter, @Nonnull final WriteContext writeContext)
63             throws WriteFailedException {
64         LOG.debug("Creating nsh map: iid={} dataAfter={}", id, dataAfter);
65         final int newMapIndex =
66                 nshAddDelMap(true, id, dataAfter, ~0 /* value not present */, writeContext.getMappingContext());
67
68         // Add nsh map name <-> vpp index mapping to the naming context:
69         nshMapContext.addName(newMapIndex, dataAfter.getName(), writeContext.getMappingContext());
70         LOG.debug("Successfully created nsh map(id={]): iid={} dataAfter={}", newMapIndex, id, dataAfter);
71     }
72
73     @Override
74     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<NshMap> id,
75                                         @Nonnull final NshMap dataBefore, @Nonnull final NshMap dataAfter,
76                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
77         throw new UnsupportedOperationException("Nsh map update is not supported");
78     }
79
80     @Override
81     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<NshMap> id,
82                                         @Nonnull final NshMap dataBefore,
83                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
84         LOG.debug("Removing nsh map: iid={} dataBefore={}", id, dataBefore);
85         final String mapName = dataBefore.getName();
86         checkState(nshMapContext.containsIndex(mapName, writeContext.getMappingContext()),
87                 "Removing nsh map {}, but index could not be found in the nsh map context", mapName);
88
89         final int mapIndex = nshMapContext.getIndex(mapName, writeContext.getMappingContext());
90         nshAddDelMap(false, id, dataBefore, mapIndex, writeContext.getMappingContext());
91
92         // Remove deleted interface from interface context:
93         nshMapContext.removeName(dataBefore.getName(), writeContext.getMappingContext());
94         LOG.debug("Successfully removed nsh map(id={]): iid={} dataAfter={}", mapIndex, id, dataBefore);
95     }
96
97     private int nshAddDelMap(final boolean isAdd, @Nonnull final InstanceIdentifier<NshMap> id,
98                              @Nonnull final NshMap map, final int mapId, final MappingContext ctx)
99             throws WriteFailedException {
100         final CompletionStage<NshAddDelMapReply> createNshMapReplyCompletionStage =
101                 getFutureJVppNsh().nshAddDelMap(getNshAddDelMapRequest(isAdd, mapId, map, ctx));
102
103         final NshAddDelMapReply reply = getReplyForWrite(createNshMapReplyCompletionStage.toCompletableFuture(), id);
104         return reply.mapIndex;
105
106     }
107
108     private NshAddDelMap getNshAddDelMapRequest(final boolean isAdd, final int mapIndex,
109                                                 @Nonnull final NshMap map,
110                                                 @Nonnull final MappingContext ctx) {
111         final NshAddDelMap request = new NshAddDelMap();
112         request.isAdd = booleanToByte(isAdd);
113
114         request.nspNsi = (map.getNsp().intValue() << 8) | map.getNsi();
115         request.mappedNspNsi = (map.getMappedNsp().intValue() << 8) | map.getMappedNsi();
116
117         if (map.getEncapType() == VxlanGpe.class) {
118             request.nextNode = 2;
119         }
120
121         checkState(interfaceContext.containsIndex(map.getEncapIfName(), ctx),
122                 "Mapping does not contains mapping for provider interface Name ".concat(map.getEncapIfName()));
123         request.swIfIndex = interfaceContext.getIndex(map.getEncapIfName(), ctx);
124
125         return request;
126     }
127 }