Jimo Jung | 1bf5435 | 2018-11-08 14:16:08 +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.openstackvtap.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.openstackvtap.api.OpenstackVtapNetwork; |
| 22 | |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | /** |
| 26 | * Hamcrest matcher for openstack vtap network. |
| 27 | */ |
| 28 | public final class OpenstackVtapNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 29 | |
| 30 | private final OpenstackVtapNetwork vtapNetwork; |
| 31 | |
| 32 | private static final String MODE = "mode"; |
| 33 | private static final String NETWORK_ID = "networkId"; |
| 34 | private static final String SERVER_IP = "serverIp"; |
| 35 | |
| 36 | private OpenstackVtapNetworkJsonMatcher(OpenstackVtapNetwork vtapNetwork) { |
| 37 | this.vtapNetwork = vtapNetwork; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 42 | |
| 43 | // check mode |
| 44 | String jsonMode = jsonNode.get(MODE).asText(); |
| 45 | String mode = String.valueOf(vtapNetwork.mode()); |
| 46 | if (!Objects.equals(jsonMode, mode)) { |
| 47 | description.appendText("mode was " + jsonMode); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | // check network id |
| 52 | int jsonNetworkId = jsonNode.get(NETWORK_ID).asInt(); |
| 53 | int networkId = vtapNetwork.networkId(); |
| 54 | if (jsonNetworkId != networkId) { |
| 55 | description.appendText("network id was " + jsonNetworkId); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | // check server IP |
| 60 | String jsonServerIp = jsonNode.get(SERVER_IP).asText(); |
| 61 | String serverIp = vtapNetwork.serverIp().toString(); |
| 62 | if (!jsonServerIp.equals(serverIp)) { |
| 63 | description.appendText("Server IP was " + jsonServerIp); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void describeTo(Description description) { |
| 72 | description.appendText(vtapNetwork.toString()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Factory to allocate an openstack vtap network matcher. |
| 77 | * |
| 78 | * @param vtapNetwork openstack vtap network object we are looking for |
| 79 | * @return matcher |
| 80 | */ |
| 81 | public static OpenstackVtapNetworkJsonMatcher matchesVtapNetwork(OpenstackVtapNetwork vtapNetwork) { |
| 82 | return new OpenstackVtapNetworkJsonMatcher(vtapNetwork); |
| 83 | } |
| 84 | } |