2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.vppclassifier;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
22 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.booleanToByte;
24 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
27 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
28 import io.fd.honeycomb.translate.v3po.util.NamingContext;
29 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import java.util.concurrent.CompletionStage;
32 import javax.annotation.Nonnull;
33 import javax.xml.bind.DatatypeConverter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.OpaqueIndex;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySession;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.jvpp.VppBaseCallException;
41 import org.openvpp.jvpp.core.dto.ClassifyAddDelSession;
42 import org.openvpp.jvpp.core.dto.ClassifyAddDelSessionReply;
43 import org.openvpp.jvpp.core.future.FutureJVppCore;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Writer customizer responsible for classify session create/delete.<br> Sends {@code classify_add_del_session} message
49 * to VPP.<br> Equivalent to invoking {@code vppctl classify table} command.
51 public class ClassifySessionWriter extends FutureJVppCustomizer
52 implements ListWriterCustomizer<ClassifySession, ClassifySessionKey> {
54 private static final Logger LOG = LoggerFactory.getLogger(ClassifySessionWriter.class);
55 private final NamingContext classifyTableContext;
57 public ClassifySessionWriter(@Nonnull final FutureJVppCore futureJVppCore,
58 @Nonnull final NamingContext classifyTableContext) {
59 super(futureJVppCore);
60 this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
64 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifySession> id,
65 @Nonnull final ClassifySession dataAfter,
66 @Nonnull final WriteContext writeContext) throws WriteFailedException {
67 LOG.debug("Creating classify session: iid={} dataAfter={}", id, dataAfter);
69 classifyAddDelSession(true, id, dataAfter, writeContext);
70 LOG.debug("Successfully created classify session: iid={} dataAfter={}", id, dataAfter);
71 } catch (VppBaseCallException e) {
72 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
77 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifySession> id,
78 @Nonnull final ClassifySession dataBefore,
79 @Nonnull final ClassifySession dataAfter,
80 @Nonnull final WriteContext writeContext) throws WriteFailedException {
81 throw new UnsupportedOperationException("Classify session update is not supported");
85 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifySession> id,
86 @Nonnull final ClassifySession dataBefore,
87 @Nonnull final WriteContext writeContext) throws WriteFailedException {
88 LOG.debug("Removing classify session: iid={} dataBefore={}", id, dataBefore);
90 classifyAddDelSession(false, id, dataBefore, writeContext);
91 LOG.debug("Successfully removed classify session: iid={} dataBefore={}", id, dataBefore);
92 } catch (VppBaseCallException e) {
93 throw new WriteFailedException.DeleteFailedException(id, e);
97 private void classifyAddDelSession(final boolean isAdd, @Nonnull final InstanceIdentifier<ClassifySession> id,
98 @Nonnull final ClassifySession classifySession,
99 @Nonnull final WriteContext writeContext)
100 throws VppBaseCallException, WriteTimeoutException {
101 final ClassifyTableKey tableKey = id.firstKeyOf(ClassifyTable.class);
102 checkArgument(tableKey != null, "could not find classify table key in {}", id);
104 final String tableName = tableKey.getName();
105 checkState(classifyTableContext.containsIndex(tableName, writeContext.getMappingContext()),
106 "Could not find classify table index for {} in the classify table context", tableName);
107 final int tableIndex = classifyTableContext.getIndex(tableName, writeContext.getMappingContext());
109 final CompletionStage<ClassifyAddDelSessionReply> createClassifyTableReplyCompletionStage = getFutureJVpp()
110 .classifyAddDelSession(
111 getClassifyAddDelSessionRequest(isAdd, tableIndex, classifySession));
113 TranslateUtils.getReplyForWrite(createClassifyTableReplyCompletionStage.toCompletableFuture(), id);
116 private static ClassifyAddDelSession getClassifyAddDelSessionRequest(final boolean isAdd, final int tableIndex,
117 @Nonnull final ClassifySession classifySession) {
118 ClassifyAddDelSession request = new ClassifyAddDelSession();
119 request.isAdd = booleanToByte(isAdd);
120 request.tableIndex = tableIndex;
123 // TODO implement node name to index conversion after https://jira.fd.io/browse/VPP-203 is fixed
124 request.hitNextIndex = classifySession.getHitNext().getPacketHandlingAction().getIntValue();
126 if (classifySession.getOpaqueIndex() != null) {
127 request.opaqueIndex = getOpaqueIndexValue(classifySession.getOpaqueIndex());
129 request.opaqueIndex = ~0; // value not specified
133 request.advance = classifySession.getAdvance();
135 request.match = DatatypeConverter.parseHexBinary(classifySession.getMatch().getValue().replace(":", ""));
139 private static int getOpaqueIndexValue(@Nonnull final OpaqueIndex opaqueIndex) {
140 if (opaqueIndex.getUint32() != null) {
141 return opaqueIndex.getUint32().intValue();
143 // TODO: implement node name to index conversion after https://jira.fd.io/browse/VPP-203 is fixed
144 return opaqueIndex.getVppNode().getPacketHandlingAction().getIntValue();