blob: 3044335f3dad4efeff145d66bf9fee2987e5fcf2 [file] [log] [blame]
Jian Li5e2ad4a2018-07-16 13:40:53 +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.openstacknetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.openstacknetworking.api.ExternalPeerRouter;
22
23/**
24 * Hamcrest matcher for external peer router.
25 */
26public final class ExternalPeerRouterJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private static final String MAC_ADDRESS = "macAddress";
29 private static final String IP_ADDRESS = "ipAddress";
30 private static final String VLAN_ID = "vlanId";
31
32 private final ExternalPeerRouter router;
33
34 private ExternalPeerRouterJsonMatcher(ExternalPeerRouter router) {
35 this.router = router;
36 }
37
38 @Override
39 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
40
41 // check MAC address
42 String jsonMacAddress = jsonNode.get(MAC_ADDRESS).asText();
43 String macAddress = router.macAddress().toString();
44 if (!jsonMacAddress.equals(macAddress)) {
45 description.appendText("macAddress was " + jsonMacAddress);
46 return false;
47 }
48
49 // check IP address
50 String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
51 String ipAddress = router.ipAddress().toString();
52 if (!jsonIpAddress.equals(ipAddress)) {
53 description.appendText("ipAddress was " + jsonIpAddress);
54 return false;
55 }
56
57 // check VLAN ID
58 String jsonVlanId = jsonNode.get(VLAN_ID).asText();
59 String vlanId = router.vlanId().toString();
60 if (!jsonVlanId.equals(vlanId)) {
61 description.appendText("VLAN ID was " + jsonIpAddress);
62 return false;
63 }
64
65 return true;
66 }
67
68 @Override
69 public void describeTo(Description description) {
70 description.appendText(router.toString());
71 }
72
73 /**
74 * Factory to allocate an external peer router matcher.
75 *
76 * @param router openstack external peer router we are looking for
77 * @return matcher
78 */
79 public static ExternalPeerRouterJsonMatcher
80 matchesExternalPeerRouter(ExternalPeerRouter router) {
81 return new ExternalPeerRouterJsonMatcher(router);
82 }
83}