701475936c7fca27a6fc5cf4feb719b761283019
[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.Pop;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.Push;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.Swap;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.VxlanGpe;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.maps.NshMap;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.maps.NshMapKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Writer customizer responsible for NshMap create/delete.
47  */
48 public class NshMapWriterCustomizer extends FutureJVppNshCustomizer
49         implements ListWriterCustomizer<NshMap, NshMapKey>, ByteDataTranslator, JvppReplyConsumer {
50
51     private static final Logger LOG = LoggerFactory.getLogger(NshMapWriterCustomizer.class);
52     private final NamingContext nshMapContext;
53     private final NamingContext interfaceContext;
54
55     public NshMapWriterCustomizer(@Nonnull final FutureJVppNsh futureJVppNsh,
56                                   @Nonnull final NamingContext nshMapContext,
57                                   @Nonnull final NamingContext interfaceContext) {
58         super(futureJVppNsh);
59         this.nshMapContext = checkNotNull(nshMapContext, "nshMapContext should not be null");
60         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
61     }
62
63     @Override
64     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<NshMap> id,
65                                        @Nonnull final NshMap dataAfter, @Nonnull final WriteContext writeContext)
66             throws WriteFailedException {
67         LOG.debug("Creating nsh map: iid={} dataAfter={}", id, dataAfter);
68         final int newMapIndex =
69                 nshAddDelMap(true, id, dataAfter, ~0 /* value not present */, writeContext.getMappingContext());
70
71         // Add nsh map name <-> vpp index mapping to the naming context:
72         nshMapContext.addName(newMapIndex, dataAfter.getName(), writeContext.getMappingContext());
73         LOG.debug("Successfully created nsh map(id={]): iid={} dataAfter={}", newMapIndex, id, dataAfter);
74     }
75
76     @Override
77     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<NshMap> id,
78                                         @Nonnull final NshMap dataBefore, @Nonnull final NshMap dataAfter,
79                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
80         throw new UnsupportedOperationException("Nsh map update is not supported");
81     }
82
83     @Override
84     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<NshMap> id,
85                                         @Nonnull final NshMap dataBefore,
86                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
87         LOG.debug("Removing nsh map: iid={} dataBefore={}", id, dataBefore);
88         final String mapName = dataBefore.getName();
89         checkState(nshMapContext.containsIndex(mapName, writeContext.getMappingContext()),
90                 "Removing nsh map {}, but index could not be found in the nsh map context", mapName);
91
92         final int mapIndex = nshMapContext.getIndex(mapName, writeContext.getMappingContext());
93         nshAddDelMap(false, id, dataBefore, mapIndex, writeContext.getMappingContext());
94
95         // Remove deleted interface from interface context:
96         nshMapContext.removeName(dataBefore.getName(), writeContext.getMappingContext());
97         LOG.debug("Successfully removed nsh map(id={]): iid={} dataAfter={}", mapIndex, id, dataBefore);
98     }
99
100     private int nshAddDelMap(final boolean isAdd, @Nonnull final InstanceIdentifier<NshMap> id,
101                              @Nonnull final NshMap map, final int mapId, final MappingContext ctx)
102             throws WriteFailedException {
103         final CompletionStage<NshAddDelMapReply> createNshMapReplyCompletionStage =
104                 getFutureJVppNsh().nshAddDelMap(getNshAddDelMapRequest(isAdd, mapId, map, ctx));
105
106         final NshAddDelMapReply reply = getReplyForWrite(createNshMapReplyCompletionStage.toCompletableFuture(), id);
107         return reply.mapIndex;
108
109     }
110
111     private NshAddDelMap getNshAddDelMapRequest(final boolean isAdd, final int mapIndex,
112                                                 @Nonnull final NshMap map,
113                                                 @Nonnull final MappingContext ctx) {
114         final NshAddDelMap request = new NshAddDelMap();
115         request.isAdd = booleanToByte(isAdd);
116
117         request.nspNsi = (map.getNsp().intValue() << 8) | map.getNsi();
118         request.mappedNspNsi = (map.getMappedNsp().intValue() << 8) | map.getMappedNsi();
119
120         if (map.getNshAction() == Swap.class) {
121             request.nshAction = 0;
122         } else if (map.getNshAction() == Push.class) {
123             request.nshAction = 1;
124         } else if (map.getNshAction() == Pop.class) {
125             request.nshAction = 2;
126         }
127
128         if (map.getEncapType() == VxlanGpe.class) {
129             request.nextNode = 2;
130         }
131
132         checkState(interfaceContext.containsIndex(map.getEncapIfName(), ctx),
133                 "Mapping does not contains mapping for provider interface Name ".concat(map.getEncapIfName()));
134         request.swIfIndex = interfaceContext.getIndex(map.getEncapIfName(), ctx);
135
136         return request;
137     }
138 }