Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstacknode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.openstacknode.api.DpdkInterface; |
| 22 | import org.onosproject.openstacknode.api.DpdkInterface.Type; |
| 23 | import org.slf4j.Logger; |
| 24 | |
| 25 | |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | /** |
| 29 | * Hamcrest matcher for dpdk interface. |
| 30 | */ |
| 31 | public final class DpdkInterfaceJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 32 | private final Logger log = getLogger(getClass()); |
| 33 | |
| 34 | private static final String DEVICE_NAME = "deviceName"; |
| 35 | private static final String INTF = "intf"; |
| 36 | private static final String PCI_ADDRESS = "pciAddress"; |
| 37 | private static final String TYPE = "type"; |
| 38 | private static final String MTU = "mtu"; |
| 39 | |
| 40 | private final DpdkInterface dpdkIntf; |
| 41 | |
| 42 | private DpdkInterfaceJsonMatcher(DpdkInterface dpdkIntf) { |
| 43 | this.dpdkIntf = dpdkIntf; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 48 | |
| 49 | // check device name |
| 50 | String jsonDeviceName = jsonNode.get(DEVICE_NAME).asText(); |
| 51 | String deviceName = dpdkIntf.deviceName(); |
| 52 | if (!jsonDeviceName.equals(deviceName)) { |
| 53 | description.appendText("device name was " + jsonDeviceName); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | String jsonIntf = jsonNode.get(INTF).asText(); |
| 58 | String intf = dpdkIntf.intf(); |
| 59 | if (!jsonIntf.equals(intf)) { |
| 60 | description.appendText("interface name was " + jsonIntf); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | String jsonPciAddress = jsonNode.get(PCI_ADDRESS).asText(); |
| 65 | String pciAddress = dpdkIntf.pciAddress(); |
| 66 | if (!jsonPciAddress.equals(pciAddress)) { |
| 67 | description.appendText("pci address was " + jsonPciAddress); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | Type jsonType = Type.valueOf(jsonNode.get(TYPE).asText()); |
| 72 | Type type = dpdkIntf.type(); |
| 73 | if (!jsonType.equals(type)) { |
| 74 | description.appendText("type was " + jsonType.name()); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | JsonNode jsonMtu = jsonNode.get(MTU); |
| 79 | if (jsonMtu != null) { |
| 80 | Long mtu = dpdkIntf.mtu(); |
| 81 | if (!jsonMtu.asText().equals(mtu.toString())) { |
| 82 | description.appendText("mtu was " + jsonMtu.asText()); |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | @Override |
| 92 | public void describeTo(Description description) { |
| 93 | description.appendText(dpdkIntf.toString()); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Factory to allocate an dpdk interface matcher. |
| 98 | * |
| 99 | * @param dpdkIntf dpdk interface object we are looking for |
| 100 | * @return matcher |
| 101 | */ |
| 102 | public static DpdkInterfaceJsonMatcher matchesDpdkInterface(DpdkInterface dpdkIntf) { |
| 103 | return new DpdkInterfaceJsonMatcher(dpdkIntf); |
| 104 | } |
| 105 | } |