summaryrefslogtreecommitdiffstats
path: root/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerCustomizer.java
blob: 69eff100af70a37d0a79c0e6c615eb1438f3ce64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.policer.write;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.vpp.jvpp.core.dto.PolicerAddDel;
import io.fd.vpp.jvpp.core.dto.PolicerAddDelReply;
import io.fd.vpp.jvpp.core.future.FutureJVppCore;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.DscpType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionDrop;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionMarkDscp;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionParams;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionTransmit;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ConformAction;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ExceedAction;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ViolateAction;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policers.Policer;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policers.PolicerKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PolicerCustomizer extends FutureJVppCustomizer implements ListWriterCustomizer<Policer, PolicerKey>,
    JvppReplyConsumer, ByteDataTranslator {
    private static final Logger LOG = LoggerFactory.getLogger(PolicerCustomizer.class);
    private final NamingContext policerContext;

    public PolicerCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext policerContext) {
        super(futureJVppCore);
        this.policerContext = checkNotNull(policerContext, "policerContext should not be null");
    }

    @Override
    public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Policer> id, @Nonnull final Policer dataAfter,
                                       @Nonnull final WriteContext ctx) throws WriteFailedException {
        LOG.debug("Writing Policer {} dataAfter={}", id, dataAfter);
        final int policerIndex = policerAddDel(id, dataAfter, true);
        policerContext.addName(policerIndex, dataAfter.getName(), ctx.getMappingContext());
    }

    @Override
    public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Policer> id,
                                        @Nonnull final Policer dataBefore,
                                        @Nonnull final Policer dataAfter, @Nonnull final WriteContext ctx)
        throws WriteFailedException {
        LOG.debug("Updating Policer {} dataBefore={} dataAfter={}", id, dataBefore, dataAfter);
        policerAddDel(id, dataAfter, true);
    }

    @Override
    public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Policer> id,
                                        @Nonnull final Policer dataBefore,
                                        @Nonnull final WriteContext ctx)
        throws WriteFailedException {
        LOG.debug("Removing Policer {} dataBefore={}", id, dataBefore);
        policerAddDel(id, dataBefore, false);
        policerContext.removeName(dataBefore.getName(), ctx.getMappingContext());
    }

    private int policerAddDel(final InstanceIdentifier<Policer> id, final Policer policer, final boolean isAdd)
        throws WriteFailedException {
        final PolicerAddDel request = new PolicerAddDel();
        request.isAdd = booleanToByte(isAdd);
        request.name = policer.getName().getBytes(StandardCharsets.US_ASCII);

        // policer_add_del expects host order unlike most of the other VPP APIs
        // jvpp by default converts ordering to network order, so we need additional reverse
        if (policer.getCir() != null) {
            request.cir = Integer.reverseBytes(policer.getCir().intValue());
        }
        if (policer.getEir() != null) {
            request.eir = Integer.reverseBytes(policer.getEir().intValue());
        }
        if (policer.getCb() != null) {
            request.cb = Long.reverseBytes(policer.getCb().longValue());
        }
        if (policer.getEb() != null) {
            request.eb = Long.reverseBytes(policer.getEb().longValue());
        }
        if (policer.getRateType() != null) {
            request.rateType = (byte) policer.getRateType().getIntValue();
        }
        if (policer.getRoundType() != null) {
            request.roundType = (byte) policer.getRoundType().getIntValue();
        }
        if (policer.getType() != null) {
            request.type = (byte) policer.getType().getIntValue();
        }
        request.colorAware = booleanToByte(policer.isColorAware());
        final ConformAction conformAction = policer.getConformAction();
        if (conformAction != null) {
            request.conformActionType = parseActiontype(conformAction.getMeterActionType());
            request.conformDscp = parseDscp(conformAction);
        }
        final ExceedAction exceedAction = policer.getExceedAction();
        if (exceedAction != null) {
            request.exceedActionType = parseActiontype(exceedAction.getMeterActionType());
            request.exceedDscp = parseDscp(exceedAction);
        }
        final ViolateAction violateAction = policer.getViolateAction();
        if (violateAction != null) {
            request.violateActionType = parseActiontype(violateAction.getMeterActionType());
            request.violateDscp = parseDscp(violateAction);
        }
        LOG.debug("Policer config change id={} request={}", id, request);
        final PolicerAddDelReply reply =
            getReplyForWrite(getFutureJVpp().policerAddDel(request).toCompletableFuture(), id);
        return reply.policerIndex;
    }

    private byte parseDscp(@Nonnull MeterActionParams actionParams) {
        final DscpType dscp = actionParams.getDscp();
        if (dscp == null) {
            return 0;
        }
        final Class<? extends MeterActionType> meterActionType = actionParams.getMeterActionType();
        checkArgument(MeterActionMarkDscp.class == meterActionType,
            "dcsp is supported only for meter-action-mark-dscp, but %s defined", meterActionType);
        if (dscp.getVppDscpType() != null) {
            return (byte) dscp.getVppDscpType().getIntValue();
        }
        if (dscp.getDscp() != null) {
            return dscp.getDscp().getValue().byteValue();
        }
        return 0;
    }

    private byte parseActiontype(@Nonnull final Class<? extends MeterActionType> meterActionType) {
        if (MeterActionDrop.class == meterActionType) {
            return 0;
        } else if (MeterActionTransmit.class == meterActionType) {
            return 1;
        } else if (MeterActionMarkDscp.class == meterActionType) {
            return 2;
        } else {
            throw new IllegalArgumentException("Unsupported meter action type " + meterActionType);
        }
    }
}