blob: b816339cc610ef625f630a452b7d37e5e85d1515 [file] [log] [blame]
Jian Li2c63bd22018-07-15 23:35:34 +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.InstancePort;
22
23/**
24 * Hamcrest matcher for instance port.
25 */
26public final class InstancePortJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final InstancePort port;
29 private static final String NETWORK_ID = "networkId";
30 private static final String PORT_ID = "portId";
31 private static final String MAC_ADDRESS = "macAddress";
32 private static final String IP_ADDRESS = "ipAddress";
33 private static final String DEVICE_ID = "deviceId";
34 private static final String PORT_NUMBER = "portNumber";
35 private static final String STATE = "state";
36
37 private InstancePortJsonMatcher(InstancePort port) {
38 this.port = port;
39 }
40
41 @Override
42 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
43
44 // check network ID
45 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
46 String networkId = port.networkId();
47 if (!jsonNetworkId.equals(networkId)) {
48 description.appendText("networkId was " + jsonNetworkId);
49 return false;
50 }
51
52 // check port ID
53 String jsonPortId = jsonNode.get(PORT_ID).asText();
54 String portId = port.portId();
55 if (!jsonPortId.equals(portId)) {
56 description.appendText("portId was " + jsonPortId);
57 return false;
58 }
59
60 // check MAC address
61 String jsonMacAddress = jsonNode.get(MAC_ADDRESS).asText();
62 String macAddress = port.macAddress().toString();
63 if (!jsonMacAddress.equals(macAddress)) {
64 description.appendText("macAddress was " + jsonMacAddress);
65 return false;
66 }
67
68 // check IP address
69 String jsonIpAddress = jsonNode.get(IP_ADDRESS).asText();
70 String ipAddress = port.ipAddress().toString();
71 if (!jsonIpAddress.equals(ipAddress)) {
72 description.appendText("ipAddress was " + jsonIpAddress);
73 return false;
74 }
75
76 // check device ID
77 String jsonDeviceId = jsonNode.get(DEVICE_ID).asText();
78 String deviceId = port.deviceId().toString();
79 if (!jsonDeviceId.equals(deviceId)) {
80 description.appendText("deviceId was " + jsonDeviceId);
81 return false;
82 }
83
84 // check port number
85 String jsonPortNumber = jsonNode.get(PORT_NUMBER).asText();
86 String portNumber = port.portNumber().toString();
87 if (!jsonPortNumber.equals(portNumber)) {
88 description.appendText("portNumber was " + jsonPortNumber);
89 return false;
90 }
91
92 // check state
93 String jsonState = jsonNode.get(STATE).asText();
94 String state = port.state().name();
95 if (!jsonState.equals(state)) {
96 description.appendText("state was " + jsonState);
97 return false;
98 }
99
100 return true;
101 }
102
103 @Override
104 public void describeTo(Description description) {
105 description.appendText(port.toString());
106 }
107
108 /**
109 * Factory to allocate an instance port matcher.
110 *
111 * @param port openstack instance port we are looking for
112 * @return matcher
113 */
114 public static InstancePortJsonMatcher matchesInstancePort(InstancePort port) {
115 return new InstancePortJsonMatcher(port);
116 }
117}