Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import org.hamcrest.Description; |
| 19 | import org.hamcrest.TypeSafeMatcher; |
| 20 | import org.onlab.packet.Ethernet; |
| 21 | |
| 22 | import com.fasterxml.jackson.databind.JsonNode; |
| 23 | |
| 24 | /** |
| 25 | * Hamcrest matcher for ethernet objects. |
| 26 | */ |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 27 | public final class EthernetJsonMatcher extends TypeSafeMatcher<JsonNode> { |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 28 | |
| 29 | private final Ethernet ethernet; |
| 30 | private String reason = ""; |
| 31 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 32 | private EthernetJsonMatcher(Ethernet ethernetValue) { |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 33 | ethernet = ethernetValue; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public boolean matchesSafely(JsonNode jsonEthernet) { |
| 38 | |
| 39 | // check source MAC |
| 40 | final JsonNode jsonSourceMacNode = jsonEthernet.get("srcMac"); |
| 41 | if (ethernet.getSourceMAC() != null) { |
| 42 | final String jsonSourceMac = jsonSourceMacNode.textValue(); |
| 43 | final String sourceMac = ethernet.getSourceMAC().toString(); |
| 44 | if (!jsonSourceMac.equals(sourceMac)) { |
| 45 | reason = "source MAC " + ethernet.getSourceMAC().toString(); |
| 46 | return false; |
| 47 | } |
| 48 | } else { |
| 49 | // source MAC not specified, JSON representation must be empty |
| 50 | if (jsonSourceMacNode != null) { |
| 51 | reason = "source mac should be null "; |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // check destination MAC |
| 57 | final JsonNode jsonDestinationMacNode = jsonEthernet.get("destMac"); |
| 58 | if (ethernet.getDestinationMAC() != null) { |
| 59 | final String jsonDestinationMac = jsonDestinationMacNode.textValue(); |
| 60 | final String destinationMac = ethernet.getDestinationMAC().toString(); |
| 61 | if (!jsonDestinationMac.equals(destinationMac)) { |
| 62 | reason = "destination MAC " + ethernet.getDestinationMAC().toString(); |
| 63 | return false; |
| 64 | } |
| 65 | } else { |
| 66 | // destination MAC not specified, JSON representation must be empty |
| 67 | if (jsonDestinationMacNode != null) { |
| 68 | reason = "destination mac should be null "; |
| 69 | return false; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // check priority code |
| 74 | final short jsonPriorityCode = jsonEthernet.get("priorityCode").shortValue(); |
| 75 | final short priorityCode = ethernet.getPriorityCode(); |
| 76 | if (jsonPriorityCode != priorityCode) { |
| 77 | reason = "priority code " + Short.toString(ethernet.getPriorityCode()); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // check vlanId |
| 82 | final short jsonVlanId = jsonEthernet.get("vlanId").shortValue(); |
| 83 | final short vlanId = ethernet.getVlanID(); |
| 84 | if (jsonVlanId != vlanId) { |
| 85 | reason = "vlan id " + Short.toString(ethernet.getVlanID()); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // check etherType |
| 90 | final short jsonEtherType = jsonEthernet.get("etherType").shortValue(); |
| 91 | final short etherType = ethernet.getEtherType(); |
| 92 | if (jsonEtherType != etherType) { |
| 93 | reason = "etherType " + Short.toString(ethernet.getEtherType()); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | // check pad |
| 98 | final boolean jsonPad = jsonEthernet.get("pad").asBoolean(); |
| 99 | final boolean pad = ethernet.isPad(); |
| 100 | if (jsonPad != pad) { |
| 101 | reason = "pad " + Boolean.toString(ethernet.isPad()); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void describeTo(Description description) { |
| 110 | description.appendText(reason); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Factory to allocate a ethernet matcher. |
| 115 | * |
| 116 | * @param ethernet ethernet object we are looking for |
| 117 | * @return matcher |
| 118 | */ |
| 119 | public static EthernetJsonMatcher matchesEthernet(Ethernet ethernet) { |
| 120 | return new EthernetJsonMatcher(ethernet); |
| 121 | } |
| 122 | } |