blob: 73f2e5844567589316a3d7b4a1a07605dd6b07a4 [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";
Jian Li7e8f57e2019-01-24 18:31:03 +090031 private static final String NAME = "name";
Jian Li9ee9c8b2019-01-24 11:48:12 +090032 private static final String TYPE = "type";
33 private static final String MTU = "mtu";
34 private static final String SEGMENT_ID = "segmentId";
35 private static final String GATEWAY_IP = "gatewayIp";
36 private static final String CIDR = "cidr";
37
38 private K8sNetworkJsonMatcher(K8sNetwork network) {
39 this.network = network;
40 }
41
42 @Override
43 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
44
45 // check network ID
46 String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
47 String networkId = network.networkId();
48 if (!jsonNetworkId.equals(networkId)) {
49 description.appendText("network ID was " + jsonNetworkId);
50 return false;
51 }
52
Jian Li7e8f57e2019-01-24 18:31:03 +090053 // check network name
54 String jsonName = jsonNode.get(NAME).asText();
55 String name = network.name();
56 if (!jsonName.equals(name)) {
57 description.appendText("name was " + jsonName);
58 return false;
59 }
60
Jian Li9ee9c8b2019-01-24 11:48:12 +090061 // check type
62 String jsonType = jsonNode.get(TYPE).asText();
63 String type = network.type().name();
64 if (!jsonType.equals(type)) {
65 description.appendText("network type was " + jsonType);
66 return false;
67 }
68
69 // check MTU
70 int jsonMtu = jsonNode.get(MTU).asInt();
71 int mtu = network.mtu();
72 if (jsonMtu != mtu) {
73 description.appendText("MTU was " + jsonMtu);
74 return false;
75 }
76
77 // check segment ID
78 String jsonSegmentId = jsonNode.get(SEGMENT_ID).asText();
79 String segmentId = network.segmentId();
80 if (!jsonSegmentId.equals(segmentId)) {
81 description.appendText("segment ID was " + jsonSegmentId);
82 return false;
83 }
84
85 // check gateway IP
86 String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText();
87 String gatewayIp = network.gatewayIp().toString();
88 if (!jsonGatewayIp.equals(gatewayIp)) {
89 description.appendText("gateway IP was " + jsonGatewayIp);
90 return false;
91 }
92
93 // check CIDR
94 String jsonCidr = jsonNode.get(CIDR).asText();
95 String cidr = network.cidr();
96 if (!jsonCidr.equals(cidr)) {
97 description.appendText("CIDR was " + jsonCidr);
98 return false;
99 }
100
101 return true;
102 }
103
104 @Override
105 public void describeTo(Description description) {
106 description.appendText(network.toString());
107 }
108
109 /**
110 * Factory to allocate a kubernetes network matcher.
111 *
112 * @param network kubernetes network object we are looking for
113 * @return matcher
114 */
115 public static K8sNetworkJsonMatcher matchesK8sNetwork(K8sNetwork network) {
116 return new K8sNetworkJsonMatcher(network);
117 }
118}