8967dcf1e94edfd9bcb2510d3e5679ede60ce0a2
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vppclassifier / ClassifySessionWriter.java
1 /*
2  * Copyright (c) 2016 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.translate.v3po.vppclassifier;
18
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;
23
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;
46
47 /**
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.
50  */
51 public class ClassifySessionWriter extends FutureJVppCustomizer
52     implements ListWriterCustomizer<ClassifySession, ClassifySessionKey> {
53
54     private static final Logger LOG = LoggerFactory.getLogger(ClassifySessionWriter.class);
55     private final NamingContext classifyTableContext;
56
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");
61     }
62
63     @Override
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);
68         try {
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);
73         }
74     }
75
76     @Override
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");
82     }
83
84     @Override
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);
89         try {
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);
94         }
95     }
96
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);
103
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());
108
109         final CompletionStage<ClassifyAddDelSessionReply> createClassifyTableReplyCompletionStage = getFutureJVpp()
110             .classifyAddDelSession(
111                 getClassifyAddDelSessionRequest(isAdd, tableIndex, classifySession));
112
113         TranslateUtils.getReplyForWrite(createClassifyTableReplyCompletionStage.toCompletableFuture(), id);
114     }
115
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;
121
122         // mandatory:
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();
125
126         if (classifySession.getOpaqueIndex() != null) {
127             request.opaqueIndex = getOpaqueIndexValue(classifySession.getOpaqueIndex());
128         } else {
129             request.opaqueIndex = ~0; // value not specified
130         }
131
132         // default 0:
133         request.advance = classifySession.getAdvance();
134
135         request.match = DatatypeConverter.parseHexBinary(classifySession.getMatch().getValue().replace(":", ""));
136         return request;
137     }
138
139     private static int getOpaqueIndexValue(@Nonnull final OpaqueIndex opaqueIndex) {
140         if (opaqueIndex.getUint32() != null) {
141             return opaqueIndex.getUint32().intValue();
142         } else {
143             // TODO: implement node name to index conversion after https://jira.fd.io/browse/VPP-203 is fixed
144             return opaqueIndex.getVppNode().getPacketHandlingAction().getIntValue();
145         }
146     }
147 }