Jian Li | 171582e | 2020-12-21 01:44:05 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.kubevirtnode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.kubevirtnode.api.KubevirtPhyInterface; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for kubevirt physical interface. |
| 25 | */ |
| 26 | public final class KubevirtPhyInterfaceJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | private final KubevirtPhyInterface phyIntf; |
| 29 | private static final String NETWORK = "network"; |
| 30 | private static final String INTERFACE = "intf"; |
| 31 | |
| 32 | private KubevirtPhyInterfaceJsonMatcher(KubevirtPhyInterface phyIntf) { |
| 33 | this.phyIntf = phyIntf; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 38 | // check network name |
| 39 | String jsonNetwork = jsonNode.get(NETWORK).asText(); |
| 40 | String network = phyIntf.network(); |
| 41 | if (!jsonNetwork.equals(network)) { |
| 42 | description.appendText("network name was " + jsonNetwork); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // check interface name |
| 47 | String jsonIntf = jsonNode.get(INTERFACE).asText(); |
| 48 | String intf = phyIntf.intf(); |
| 49 | if (!jsonIntf.equals(intf)) { |
| 50 | description.appendText("interface name was " + jsonIntf); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void describeTo(Description description) { |
| 59 | description.appendText(phyIntf.toString()); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Factory to allocate an openstack physical interface matcher. |
| 64 | * |
| 65 | * @param phyIntf kubevirt physical interface object we are looking for |
| 66 | * @return matcher |
| 67 | */ |
| 68 | public static KubevirtPhyInterfaceJsonMatcher |
| 69 | matchesKubevirtPhyInterface(KubevirtPhyInterface phyIntf) { |
| 70 | return new KubevirtPhyInterfaceJsonMatcher(phyIntf); |
| 71 | } |
| 72 | } |