summaryrefslogtreecommitdiffstats
path: root/vpp-classifier/impl/src/test/java/io/fd/hc2vpp/policer/write/PolicerValidatorTest.java
blob: 15122bbb0250e1e9b5df57c791bf7c2ff7e32a4b (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
/*
 * Copyright (c) 2019 PANTHEON.tech.
 *
 * 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 org.mockito.MockitoAnnotations.initMocks;

import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.write.DataValidationFailedException.CreateValidationFailedException;
import io.fd.honeycomb.translate.write.DataValidationFailedException.DeleteValidationFailedException;
import io.fd.honeycomb.translate.write.WriteContext;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.DscpType;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.MeterActionDrop;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.MeterActionMarkDscp;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.MeterActionTransmit;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.MeterActionType;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policer.base.attributes.ConformAction;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policer.base.attributes.ConformActionBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policer.base.attributes.ExceedAction;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policer.base.attributes.ExceedActionBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policer.base.attributes.ViolateAction;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policer.base.attributes.ViolateActionBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policers.Policer;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.policer.rev170315.policers.PolicerBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class PolicerValidatorTest {

    @Mock
    private WriteContext writeContext;

    private static final InstanceIdentifier<Policer> POLICER_IID = InstanceIdentifier.create(Policer.class);
    private static short DSCP = 10;
    private PolicerValidator validator;

    @Before
    public void setUp() {
        initMocks(this);
        validator = new PolicerValidator(new NamingContext("testPolicerValidator", "testPolicerValidator"));
    }

    @Test
    public void testWriteSuccessfull()
            throws CreateValidationFailedException {
        PolicerBuilder builder = generatePrePopulatedPolicerBuilder();
        validator.validateWrite(POLICER_IID, builder.build(), writeContext);
    }

    @Test(expected = CreateValidationFailedException.class)
    public void testWriteFailedDscp()
            throws CreateValidationFailedException {
        PolicerBuilder builder = generatePrePopulatedPolicerBuilder();
        builder.setConformAction(generateConformAction(true, MeterActionDrop.class));
        validator.validateWrite(POLICER_IID, builder.build(), writeContext);
    }

    @Test(expected = DeleteValidationFailedException.class)
    public void testDeleteFailedDscp()
            throws DeleteValidationFailedException {
        PolicerBuilder builder = generatePrePopulatedPolicerBuilder();
        builder.setExceedAction(generateExceedAction(true, MeterActionTransmit.class));
        validator.validateDelete(POLICER_IID, builder.build(), writeContext);
    }

    private PolicerBuilder generatePrePopulatedPolicerBuilder() {
        PolicerBuilder builder = new PolicerBuilder();
        builder.setConformAction(generateConformAction(true, MeterActionMarkDscp.class))
                .setExceedAction(generateExceedAction(false, MeterActionTransmit.class))
                .setViolateAction(generateViolateAction(false, MeterActionDrop.class));
        return builder;
    }

    private ExceedAction generateExceedAction(final boolean hasDscp,
                                              final Class<? extends MeterActionType> actionType) {
        ExceedActionBuilder builder = new ExceedActionBuilder();
        if (hasDscp) {
            builder.setDscp(new DscpType(new Dscp(DSCP)));
        }
        builder.setMeterActionType(actionType);
        return builder.build();
    }

    private ConformAction generateConformAction(final boolean hasDscp,
                                                final Class<? extends MeterActionType> actionType) {
        ConformActionBuilder builder = new ConformActionBuilder();
        if (hasDscp) {
            builder.setDscp(new DscpType(new Dscp(DSCP)));
        }
        builder.setMeterActionType(actionType);
        return builder.build();
    }

    private ViolateAction generateViolateAction(final boolean hasDscp,
                                                final Class<? extends MeterActionType> actionType) {
        ViolateActionBuilder builder = new ViolateActionBuilder();
        if (hasDscp) {
            builder.setDscp(new DscpType(new Dscp(DSCP)));
        }
        builder.setMeterActionType(actionType);
        return builder.build();
    }
}