blob: 1f3526fd107e77be39b121268d5cd5bae8a34df5 [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";
Jian Li9ee9c8b2019-01-24 11:48:12 +090035 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
Jian Li7e8f57e2019-01-24 18:31:03 +090052 // check network name
53 String jsonName = jsonNode.get(NAME).asText();
54 String name = network.name();
55 if (!jsonName.equals(name)) {
56 description.appendText("name was " + jsonName);
57 return false;
58 }
59
Jian Li9ee9c8b2019-01-24 11:48:12 +090060 // check type
Jian Li5a9c2bb2019-05-07 13:21:52 +090061 JsonNode jsonType = jsonNode.get(TYPE);
62 if (jsonType != null) {
63 String type = network.type().name();
64 if (!jsonType.asText().equals(type)) {
65 description.appendText("network type was " + jsonType);
66 return false;
67 }
Jian Li9ee9c8b2019-01-24 11:48:12 +090068 }
69
70 // check MTU
71 int jsonMtu = jsonNode.get(MTU).asInt();
72 int mtu = network.mtu();
73 if (jsonMtu != mtu) {
74 description.appendText("MTU was " + jsonMtu);
75 return false;
76 }
77
78 // check segment ID
Jian Li5a9c2bb2019-05-07 13:21:52 +090079 JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID);
80 if (jsonSegmentId != null) {
81 String segmentId = network.segmentId();
82 if (!jsonSegmentId.asText().equals(segmentId)) {
83 description.appendText("segment ID was " + jsonSegmentId);
84 return false;
85 }
Jian Li9ee9c8b2019-01-24 11:48:12 +090086 }
87
88 // check CIDR
89 String jsonCidr = jsonNode.get(CIDR).asText();
90 String cidr = network.cidr();
91 if (!jsonCidr.equals(cidr)) {
92 description.appendText("CIDR was " + jsonCidr);
93 return false;
94 }
95
96 return true;
97 }
98
99 @Override
100 public void describeTo(Description description) {
101 description.appendText(network.toString());
102 }
103
104 /**
105 * Factory to allocate a kubernetes network matcher.
106 *
107 * @param network kubernetes network object we are looking for
108 * @return matcher
109 */
110 public static K8sNetworkJsonMatcher matchesK8sNetwork(K8sNetwork network) {
111 return new K8sNetworkJsonMatcher(network);
112 }
113}