blob: 6a4cce7b4fbce9fad6971dc5ee9b726bf488a5ab [file] [log] [blame]
Jian Lie6312162018-03-21 21:41:00 +09001/*
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 */
16package org.onosproject.openstacknode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstacknode.api.OpenstackPhyInterface;
22
Jian Li789fadb2018-07-10 13:59:47 +090023/**
24 * Hamcrest matcher for openstack physical interface.
25 */
Jian Lie6312162018-03-21 21:41:00 +090026public final class OpenstackPhyInterfaceJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final OpenstackPhyInterface phyIntf;
29 private static final String NETWORK = "network";
30 private static final String INTERFACE = "intf";
31
32 private OpenstackPhyInterfaceJsonMatcher(OpenstackPhyInterface phyIntf) {
33 this.phyIntf = phyIntf;
34 }
35
36 @Override
37 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
38
39 // check network name
40 String jsonNetwork = jsonNode.get(NETWORK).asText();
41 String network = phyIntf.network();
42 if (!jsonNetwork.equals(network)) {
43 description.appendText("network name was " + jsonNetwork);
44 return false;
45 }
46
47 // check interface name
48 String jsonIntf = jsonNode.get(INTERFACE).asText();
49 String intf = phyIntf.intf();
50 if (!jsonIntf.equals(intf)) {
51 description.appendText("interface name was " + jsonIntf);
52 return false;
53 }
54
55 return true;
56 }
57
58 @Override
59 public void describeTo(Description description) {
60 description.appendText(phyIntf.toString());
61 }
62
63 /**
64 * Factory to allocate an openstack physical interface matcher.
65 *
66 * @param phyIntf openstack physical interface object we are looking for
67 * @return matcher
68 */
69 public static OpenstackPhyInterfaceJsonMatcher
70 matchesOpenstackPhyInterface(OpenstackPhyInterface phyIntf) {
71 return new OpenstackPhyInterfaceJsonMatcher(phyIntf);
72 }
73}