blob: 435de0fa522d44acfe972b0651c0610219ad95f2 [file] [log] [blame]
Jian Li9ee9c8b2019-01-24 11:48:12 +09001/*
2 * Copyright 2019-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.k8snetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.hamcrest.Description;
20import org.hamcrest.TypeSafeDiagnosingMatcher;
21import org.onosproject.k8snetworking.api.K8sNetwork;
22
23/**
24 * Hamcrest matcher for kubernetes network.
25 */
26public final class K8sNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
27
28 private final K8sNetwork network;
29
30 private static final String NETWORK_ID = "networkId";
31 private static final String TYPE = "type";
32 private static final String MTU = "mtu";
33 private static final String SEGMENT_ID = "segmentId";
34 private static final String GATEWAY_IP = "gatewayIp";
35 private static final String CIDR = "cidr";
36
37 private K8sNetworkJsonMatcher(K8sNetwork network) {
38 this.network = network;
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 = network.networkId();
47 if (!jsonNetworkId.equals(networkId)) {
48 description.appendText("network ID was " + jsonNetworkId);
49 return false;
50 }
51
52 // check type
53 String jsonType = jsonNode.get(TYPE).asText();
54 String type = network.type().name();
55 if (!jsonType.equals(type)) {
56 description.appendText("network type was " + jsonType);
57 return false;
58 }
59
60 // check MTU
61 int jsonMtu = jsonNode.get(MTU).asInt();
62 int mtu = network.mtu();
63 if (jsonMtu != mtu) {
64 description.appendText("MTU was " + jsonMtu);
65 return false;
66 }
67
68 // check segment ID
69 String jsonSegmentId = jsonNode.get(SEGMENT_ID).asText();
70 String segmentId = network.segmentId();
71 if (!jsonSegmentId.equals(segmentId)) {
72 description.appendText("segment ID was " + jsonSegmentId);
73 return false;
74 }
75
76 // check gateway IP
77 String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText();
78 String gatewayIp = network.gatewayIp().toString();
79 if (!jsonGatewayIp.equals(gatewayIp)) {
80 description.appendText("gateway IP was " + jsonGatewayIp);
81 return false;
82 }
83
84 // check CIDR
85 String jsonCidr = jsonNode.get(CIDR).asText();
86 String cidr = network.cidr();
87 if (!jsonCidr.equals(cidr)) {
88 description.appendText("CIDR was " + jsonCidr);
89 return false;
90 }
91
92 return true;
93 }
94
95 @Override
96 public void describeTo(Description description) {
97 description.appendText(network.toString());
98 }
99
100 /**
101 * Factory to allocate a kubernetes network matcher.
102 *
103 * @param network kubernetes network object we are looking for
104 * @return matcher
105 */
106 public static K8sNetworkJsonMatcher matchesK8sNetwork(K8sNetwork network) {
107 return new K8sNetworkJsonMatcher(network);
108 }
109}