aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/common/src/test/java/org/onosproject/codec/impl/EthernetJsonMatcher.java
blob: c5827b9167e631ab00478563ca882c0204d36b5e (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
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * 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 org.onosproject.codec.impl;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.onlab.packet.Ethernet;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * Hamcrest matcher for ethernet objects.
 */
public final class EthernetJsonMatcher extends TypeSafeMatcher<JsonNode> {

    private final Ethernet ethernet;
    private String reason = "";

    private EthernetJsonMatcher(Ethernet ethernetValue) {
        ethernet = ethernetValue;
    }

    @Override
    public boolean matchesSafely(JsonNode jsonEthernet) {

        // check source MAC
        final JsonNode jsonSourceMacNode = jsonEthernet.get("srcMac");
        if (ethernet.getSourceMAC() != null) {
            final String jsonSourceMac = jsonSourceMacNode.textValue();
            final String sourceMac = ethernet.getSourceMAC().toString();
            if (!jsonSourceMac.equals(sourceMac)) {
                reason = "source MAC " + ethernet.getSourceMAC().toString();
                return false;
            }
        } else {
            //  source MAC not specified, JSON representation must be empty
            if (jsonSourceMacNode != null) {
                reason = "source mac should be null ";
                return false;
            }
        }

        // check destination MAC
        final JsonNode jsonDestinationMacNode = jsonEthernet.get("destMac");
        if (ethernet.getDestinationMAC() != null) {
            final String jsonDestinationMac = jsonDestinationMacNode.textValue();
            final String destinationMac = ethernet.getDestinationMAC().toString();
            if (!jsonDestinationMac.equals(destinationMac)) {
                reason = "destination MAC " + ethernet.getDestinationMAC().toString();
                return false;
            }
        } else {
            //  destination MAC not specified, JSON representation must be empty
            if (jsonDestinationMacNode != null) {
                reason = "destination mac should be null ";
                return false;
            }
        }

        // check priority code
        final short jsonPriorityCode = jsonEthernet.get("priorityCode").shortValue();
        final short priorityCode = ethernet.getPriorityCode();
        if (jsonPriorityCode != priorityCode) {
            reason = "priority code " + Short.toString(ethernet.getPriorityCode());
            return false;
        }

        // check vlanId
        final short jsonVlanId = jsonEthernet.get("vlanId").shortValue();
        final short vlanId = ethernet.getVlanID();
        if (jsonVlanId != vlanId) {
            reason = "vlan id " + Short.toString(ethernet.getVlanID());
            return false;
        }

        // check etherType
        final short jsonEtherType = jsonEthernet.get("etherType").shortValue();
        final short etherType = ethernet.getEtherType();
        if (jsonEtherType != etherType) {
            reason = "etherType " + Short.toString(ethernet.getEtherType());
            return false;
        }

        // check pad
        final boolean jsonPad = jsonEthernet.get("pad").asBoolean();
        final boolean pad = ethernet.isPad();
        if (jsonPad != pad) {
            reason = "pad " + Boolean.toString(ethernet.isPad());
            return false;
        }

        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(reason);
    }

    /**
     * Factory to allocate a ethernet matcher.
     *
     * @param ethernet ethernet object we are looking for
     * @return matcher
     */
    public static EthernetJsonMatcher matchesEthernet(Ethernet ethernet) {
        return new EthernetJsonMatcher(ethernet);
    }
}