Thomas Vachuska

ONOS-3387 Adding ability for network configurations to be validated before being…

… accepted into the system.

Change-Id: I26a7e2adb20318cf17a35081ff753b3448105e31
...@@ -21,6 +21,9 @@ import org.onosproject.core.ApplicationId; ...@@ -21,6 +21,9 @@ import org.onosproject.core.ApplicationId;
21 import org.onosproject.net.config.Config; 21 import org.onosproject.net.config.Config;
22 import org.onosproject.net.config.basics.BasicElementConfig; 22 import org.onosproject.net.config.basics.BasicElementConfig;
23 23
24 +import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
25 +import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
26 +
24 /** 27 /**
25 * DHCP Config class. 28 * DHCP Config class.
26 */ 29 */
...@@ -43,6 +46,20 @@ public class DhcpConfig extends Config<ApplicationId> { ...@@ -43,6 +46,20 @@ public class DhcpConfig extends Config<ApplicationId> {
43 46
44 public static final int DEFAULT = -1; 47 public static final int DEFAULT = -1;
45 48
49 + @Override
50 + public boolean isValid() {
51 + // FIXME: Sweep through and revisit the validation assertions
52 + // For now, this is just a demonstration of potential uses
53 + return hasOnlyFields(MY_IP, MY_MAC, SUBNET_MASK, BROADCAST_ADDRESS,
54 + ROUTER_ADDRESS, DOMAIN_SERVER, TTL, LEASE_TIME,
55 + RENEW_TIME, REBIND_TIME, TIMER_DELAY, DEFAULT_TIMEOUT,
56 + START_IP, END_IP) &&
57 + isIpAddress(MY_IP, MANDATORY) && isMacAddress(MY_MAC, MANDATORY) &&
58 + isIpAddress(START_IP, MANDATORY) && isIpAddress(END_IP, MANDATORY) &&
59 + isNumber(LEASE_TIME, OPTIONAL, 1) && isNumber(REBIND_TIME, OPTIONAL, 1) &&
60 + isNumber(DEFAULT_TIMEOUT, OPTIONAL, 1, 3600);
61 + }
62 +
46 /** 63 /**
47 * Returns the dhcp server ip. 64 * Returns the dhcp server ip.
48 * 65 *
......
...@@ -20,10 +20,15 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -20,10 +20,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.fasterxml.jackson.databind.node.ArrayNode; 20 import com.fasterxml.jackson.databind.node.ArrayNode;
21 import com.fasterxml.jackson.databind.node.ObjectNode; 21 import com.fasterxml.jackson.databind.node.ObjectNode;
22 import com.google.common.annotations.Beta; 22 import com.google.common.annotations.Beta;
23 +import com.google.common.collect.ImmutableSet;
24 +import com.google.common.collect.Iterators;
23 import com.google.common.collect.Lists; 25 import com.google.common.collect.Lists;
26 +import org.onlab.packet.IpAddress;
27 +import org.onlab.packet.MacAddress;
24 28
25 import java.util.Collection; 29 import java.util.Collection;
26 import java.util.List; 30 import java.util.List;
31 +import java.util.Set;
27 import java.util.function.Function; 32 import java.util.function.Function;
28 33
29 import static com.google.common.base.Preconditions.checkNotNull; 34 import static com.google.common.base.Preconditions.checkNotNull;
...@@ -51,6 +56,21 @@ public abstract class Config<S> { ...@@ -51,6 +56,21 @@ public abstract class Config<S> {
51 protected ConfigApplyDelegate delegate; 56 protected ConfigApplyDelegate delegate;
52 57
53 /** 58 /**
59 + * Indicator of whether a configuration JSON field is required.
60 + */
61 + public enum FieldPresence {
62 + /**
63 + * Signifies that config field is an optional one.
64 + */
65 + OPTIONAL,
66 +
67 + /**
68 + * Signifies that config field is mandatory.
69 + */
70 + MANDATORY
71 + }
72 +
73 + /**
54 * Initializes the configuration behaviour with necessary context. 74 * Initializes the configuration behaviour with necessary context.
55 * 75 *
56 * @param subject configuration subject 76 * @param subject configuration subject
...@@ -71,6 +91,29 @@ public abstract class Config<S> { ...@@ -71,6 +91,29 @@ public abstract class Config<S> {
71 } 91 }
72 92
73 /** 93 /**
94 + * Indicates whether or not the backing JSON node contains valid data.
95 + * <p>
96 + * Default implementation returns true.
97 + * Subclasses are expected to override this with their own validation.
98 + * </p>
99 + *
100 + * @return true if the data is valid; false otherwise
101 + */
102 + public boolean isValid() {
103 + // TODO: figure out what assertions could be made in the base class
104 + // NOTE: The thought is to have none, but instead to provide a set
105 + // of predicates to allow configs to test validity of present fields,
106 + // e.g.:
107 + // isString(path)
108 + // isBoolean(path)
109 + // isNumber(path, [min, max])
110 + // isDecimal(path, [min, max])
111 + // isMacAddress(path)
112 + // isIpAddress(path)
113 + return true;
114 + }
115 +
116 + /**
74 * Returns the specific subject to which this configuration pertains. 117 * Returns the specific subject to which this configuration pertains.
75 * 118 *
76 * @return configuration subject 119 * @return configuration subject
...@@ -309,4 +352,104 @@ public abstract class Config<S> { ...@@ -309,4 +352,104 @@ public abstract class Config<S> {
309 return this; 352 return this;
310 } 353 }
311 354
355 + /**
356 + * Indicates whether only the specified fields are present in the backing JSON.
357 + *
358 + * @param allowedFields allowed field names
359 + * @return true if all allowedFields are present; false otherwise
360 + */
361 + protected boolean hasOnlyFields(String... allowedFields) {
362 + Set<String> fields = ImmutableSet.copyOf(allowedFields);
363 + return !Iterators.any(object.fieldNames(), f -> !fields.contains(f));
364 + }
365 +
366 + /**
367 + * Indicates whether the specified field holds a valid MAC address.
368 + *
369 + * @param field JSON field name
370 + * @param presence specifies if field is optional or mandatory
371 + * @return true if valid; false otherwise
372 + * @throws IllegalArgumentException if field is present, but not valid MAC
373 + */
374 + protected boolean isMacAddress(String field, FieldPresence presence) {
375 + JsonNode node = object.path(field);
376 + return isValid(node, presence, node.isTextual() &&
377 + MacAddress.valueOf(node.asText()) != null);
378 + }
379 +
380 + /**
381 + * Indicates whether the specified field holds a valid IP address.
382 + *
383 + * @param field JSON field name
384 + * @param presence specifies if field is optional or mandatory
385 + * @return true if valid; false otherwise
386 + * @throws IllegalArgumentException if field is present, but not valid IP
387 + */
388 + protected boolean isIpAddress(String field, FieldPresence presence) {
389 + JsonNode node = object.path(field);
390 + return isValid(node, presence, node.isTextual() &&
391 + IpAddress.valueOf(node.asText()) != null);
392 + }
393 +
394 + /**
395 + * Indicates whether the specified field holds a valid string value.
396 + *
397 + * @param field JSON field name
398 + * @param presence specifies if field is optional or mandatory
399 + * @param pattern optional regex pattern
400 + * @return true if valid; false otherwise
401 + * @throws IllegalArgumentException if field is present, but not valid MAC
402 + */
403 + protected boolean isString(String field, FieldPresence presence, String... pattern) {
404 + JsonNode node = object.path(field);
405 + return isValid(node, presence, node.isTextual() &&
406 + (pattern.length > 0 && node.asText().matches(pattern[0]) || pattern.length < 1));
407 + }
408 +
409 + /**
410 + * Indicates whether the specified field holds a valid number.
411 + *
412 + * @param field JSON field name
413 + * @param presence specifies if field is optional or mandatory
414 + * @param minMax optional min/max values
415 + * @return true if valid; false otherwise
416 + * @throws IllegalArgumentException if field is present, but not valid
417 + */
418 + protected boolean isNumber(String field, FieldPresence presence, long... minMax) {
419 + JsonNode node = object.path(field);
420 + return isValid(node, presence, (node.isLong() || node.isInt()) &&
421 + (minMax.length > 0 && minMax[0] <= node.asLong() || minMax.length < 1) &&
422 + (minMax.length > 1 && minMax[1] > node.asLong() || minMax.length < 2));
423 + }
424 +
425 + /**
426 + * Indicates whether the specified field holds a valid decimal number.
427 + *
428 + * @param field JSON field name
429 + * @param presence specifies if field is optional or mandatory
430 + * @param minMax optional min/max values
431 + * @return true if valid; false otherwise
432 + * @throws IllegalArgumentException if field is present, but not valid
433 + */
434 + protected boolean isDecimal(String field, FieldPresence presence, double... minMax) {
435 + JsonNode node = object.path(field);
436 + return isValid(node, presence, (node.isDouble() || node.isFloat()) &&
437 + (minMax.length > 0 && minMax[0] <= node.asDouble() || minMax.length < 1) &&
438 + (minMax.length > 1 && minMax[1] > node.asDouble() || minMax.length < 2));
439 + }
440 +
441 + /**
442 + * Indicates whether the node is present and of correct value or not
443 + * mandatory and absent.
444 + *
445 + * @param node JSON node
446 + * @param presence specifies if field is optional or mandatory
447 + * @param correctValue true if the value is correct
448 + * @return true if the field is as expected
449 + */
450 + private boolean isValid(JsonNode node, FieldPresence presence, boolean correctValue) {
451 + boolean isMandatory = presence == FieldPresence.MANDATORY;
452 + return isMandatory && correctValue || !isMandatory && !node.isNull() || correctValue;
453 + }
454 +
312 } 455 }
......
...@@ -119,7 +119,7 @@ public interface NetworkConfigService ...@@ -119,7 +119,7 @@ public interface NetworkConfigService
119 119
120 /** 120 /**
121 * Applies configuration for the specified subject and configuration 121 * Applies configuration for the specified subject and configuration
122 - * class using the raw JSON object. If configuration already exists, it 122 + * class using the raw JSON node. If configuration already exists, it
123 * will be updated. 123 * will be updated.
124 * 124 *
125 * @param subject configuration subject 125 * @param subject configuration subject
...@@ -128,6 +128,8 @@ public interface NetworkConfigService ...@@ -128,6 +128,8 @@ public interface NetworkConfigService
128 * @param <S> type of subject 128 * @param <S> type of subject
129 * @param <C> type of configuration 129 * @param <C> type of configuration
130 * @return configuration or null if one is not available 130 * @return configuration or null if one is not available
131 + * @throws IllegalArgumentException if the supplied JSON node contains
132 + * invalid data
131 */ 133 */
132 <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, 134 <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass,
133 JsonNode json); 135 JsonNode json);
......
...@@ -113,6 +113,8 @@ public interface NetworkConfigStore extends Store<NetworkConfigEvent, NetworkCon ...@@ -113,6 +113,8 @@ public interface NetworkConfigStore extends Store<NetworkConfigEvent, NetworkCon
113 * @param <S> type of subject 113 * @param <S> type of subject
114 * @param <C> type of configuration 114 * @param <C> type of configuration
115 * @return configuration object 115 * @return configuration object
116 + * @throws IllegalArgumentException if the supplied JSON node contains
117 + * invalid data
116 */ 118 */
117 <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, 119 <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass,
118 JsonNode json); 120 JsonNode json);
......
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
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 + */
16 +
17 +package org.onosproject.net.config;
18 +
19 +import com.fasterxml.jackson.databind.JsonNode;
20 +import com.fasterxml.jackson.databind.ObjectMapper;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +
24 +import static org.junit.Assert.assertFalse;
25 +import static org.junit.Assert.assertTrue;
26 +import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
27 +import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
28 +
29 +/**
30 + * Test of the base network config class.
31 + */
32 +public class ConfigTest {
33 +
34 + private static final String SUBJECT = "subject";
35 + private static final String KEY = "key";
36 +
37 + private static final String TEXT = "text";
38 + private static final String LONG = "long";
39 + private static final String DOUBLE = "double";
40 + private static final String MAC = "mac";
41 + private static final String IP = "ip";
42 +
43 + private final ObjectMapper mapper = new ObjectMapper();
44 + private final ConfigApplyDelegate delegate = new TestDelegate();
45 +
46 + private Config<String> cfg;
47 + private JsonNode json;
48 +
49 + @Before
50 + public void setUp() {
51 + json = new ObjectMapper().createObjectNode()
52 + .put(TEXT, "foo").put(LONG, 5).put(DOUBLE, 0.5)
53 + .put(MAC, "ab:cd:ef:ca:fe:ed").put(IP, "12.34.56.78");
54 + cfg = new TestConfig();
55 + cfg.init(SUBJECT, KEY, json, mapper, delegate);
56 + }
57 +
58 + @Test
59 + public void hasOnlyFields() {
60 + assertTrue("has unexpected fields", cfg.hasOnlyFields(TEXT, LONG, DOUBLE, MAC, IP));
61 + assertFalse("did not detect unexpected fields", cfg.hasOnlyFields(TEXT, LONG, DOUBLE, MAC));
62 + assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY));
63 + }
64 +
65 + @Test
66 + public void isString() {
67 + assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY));
68 + assertTrue("is not proper text", cfg.isString(TEXT, MANDATORY, "^f.*"));
69 + assertTrue("is not proper text", cfg.isString(TEXT, OPTIONAL, "^f.*"));
70 + assertTrue("is not proper text", cfg.isString(TEXT, OPTIONAL));
71 + assertTrue("is not proper text", cfg.isString("none", OPTIONAL));
72 + assertFalse("did not detect missing field", cfg.isString("none", MANDATORY));
73 + }
74 +
75 + @Test
76 + public void isNumber() {
77 + assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY));
78 + assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 0));
79 + assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 0, 10));
80 + assertTrue("is not proper number", cfg.isNumber(LONG, MANDATORY, 5, 6));
81 + assertFalse("is not in range", cfg.isNumber(LONG, MANDATORY, 6, 10));
82 + assertFalse("is not in range", cfg.isNumber(LONG, MANDATORY, 4, 5));
83 + assertTrue("is not proper number", cfg.isNumber(LONG, OPTIONAL, 0, 10));
84 + assertTrue("is not proper number", cfg.isNumber(LONG, OPTIONAL));
85 + assertTrue("is not proper number", cfg.isNumber("none", OPTIONAL));
86 + assertFalse("did not detect missing field", cfg.isNumber("none", MANDATORY));
87 + }
88 +
89 + @Test
90 + public void isDecimal() {
91 + assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY));
92 + assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.0));
93 + assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.0, 1.0));
94 + assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, MANDATORY, 0.5, 0.6));
95 + assertFalse("is not in range", cfg.isDecimal(DOUBLE, MANDATORY, 0.6, 1.0));
96 + assertFalse("is not in range", cfg.isDecimal(DOUBLE, MANDATORY, 0.4, 0.5));
97 + assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, OPTIONAL, 0.0, 1.0));
98 + assertTrue("is not proper decimal", cfg.isDecimal(DOUBLE, OPTIONAL));
99 + assertTrue("is not proper decimal", cfg.isDecimal("none", OPTIONAL));
100 + assertFalse("did not detect missing field", cfg.isDecimal("none", MANDATORY));
101 + }
102 +
103 + @Test
104 + public void isMacAddress() {
105 + assertTrue("is not proper mac", cfg.isMacAddress(MAC, MANDATORY));
106 + assertTrue("is not proper mac", cfg.isMacAddress(MAC, OPTIONAL));
107 + assertTrue("is not proper mac", cfg.isMacAddress("none", OPTIONAL));
108 + assertFalse("did not detect missing field", cfg.isMacAddress("none", MANDATORY));
109 + }
110 +
111 + @Test(expected = IllegalArgumentException.class)
112 + public void badMacAddress() {
113 + assertTrue("is not proper mac", cfg.isMacAddress(TEXT, MANDATORY));
114 + }
115 +
116 +
117 + @Test
118 + public void isIpAddress() {
119 + assertTrue("is not proper ip", cfg.isIpAddress(IP, MANDATORY));
120 + assertTrue("is not proper ip", cfg.isIpAddress(IP, OPTIONAL));
121 + assertTrue("is not proper ip", cfg.isIpAddress("none", OPTIONAL));
122 + assertFalse("did not detect missing field", cfg.isMacAddress("none", MANDATORY));
123 + }
124 +
125 + @Test(expected = IllegalArgumentException.class)
126 + public void badIpAddress() {
127 + assertTrue("is not proper ip", cfg.isIpAddress(TEXT, MANDATORY));
128 + }
129 +
130 +
131 + // TODO: Add tests for other helper methods
132 +
133 + private class TestConfig extends Config<String> {
134 + }
135 +
136 + private class TestDelegate implements ConfigApplyDelegate {
137 + @Override
138 + public void onApply(Config config) {
139 + }
140 + }
141 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -60,6 +60,7 @@ import java.util.Map; ...@@ -60,6 +60,7 @@ import java.util.Map;
60 import java.util.Objects; 60 import java.util.Objects;
61 import java.util.Set; 61 import java.util.Set;
62 62
63 +import static com.google.common.base.Preconditions.checkArgument;
63 import static org.onosproject.net.config.NetworkConfigEvent.Type.*; 64 import static org.onosproject.net.config.NetworkConfigEvent.Type.*;
64 65
65 /** 66 /**
...@@ -71,10 +72,12 @@ public class DistributedNetworkConfigStore ...@@ -71,10 +72,12 @@ public class DistributedNetworkConfigStore
71 extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate> 72 extends AbstractStore<NetworkConfigEvent, NetworkConfigStoreDelegate>
72 implements NetworkConfigStore { 73 implements NetworkConfigStore {
73 74
74 - private static final int MAX_BACKOFF = 10;
75 -
76 private final Logger log = LoggerFactory.getLogger(getClass()); 75 private final Logger log = LoggerFactory.getLogger(getClass());
77 76
77 + private static final int MAX_BACKOFF = 10;
78 + private static final String INVALID_CONFIG_JSON =
79 + "JSON node does not contain valid configuration";
80 +
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected StorageService storageService; 82 protected StorageService storageService;
80 83
...@@ -187,8 +190,17 @@ public class DistributedNetworkConfigStore ...@@ -187,8 +190,17 @@ public class DistributedNetworkConfigStore
187 190
188 @Override 191 @Override
189 public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) { 192 public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) {
190 - return createConfig(subject, configClass, 193 + // Create the configuration and validate it.
191 - configs.putAndGet(key(subject, configClass), json).value()); 194 + C config = createConfig(subject, configClass, json);
195 + checkArgument(config.isValid(), INVALID_CONFIG_JSON);
196 +
197 + // Insert the validated configuration and get it back.
198 + Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json);
199 +
200 + // Re-create the config if for some reason what we attempted to put
201 + // was supplanted by someone else already.
202 + return versioned.value() == json ? config :
203 + createConfig(subject, configClass, versioned.value());
192 } 204 }
193 205
194 @Override 206 @Override
......