Yuta HIGUCHI | 8810aa4 | 2017-08-02 15:05:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
| 16 | package org.onosproject.d.config.sync.impl; |
| 17 | |
| 18 | import static org.junit.Assert.*; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 19 | import static org.onosproject.d.config.ResourceIds.ROOT_ID; |
Yuta HIGUCHI | 8810aa4 | 2017-08-02 15:05:37 -0700 | [diff] [blame] | 20 | |
| 21 | import java.util.concurrent.CompletableFuture; |
| 22 | import java.util.concurrent.CountDownLatch; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | import java.util.function.BiFunction; |
| 25 | |
| 26 | import org.junit.After; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.onosproject.config.DynamicConfigEvent; |
| 30 | import org.onosproject.config.DynamicConfigServiceAdapter; |
| 31 | import org.onosproject.config.Filter; |
| 32 | import org.onosproject.d.config.DeviceResourceIds; |
| 33 | import org.onosproject.d.config.ResourceIds; |
| 34 | import org.onosproject.d.config.sync.DeviceConfigSynchronizationProvider; |
| 35 | import org.onosproject.d.config.sync.operation.SetRequest; |
| 36 | import org.onosproject.d.config.sync.operation.SetRequest.Change; |
| 37 | import org.onosproject.d.config.sync.operation.SetRequest.Change.Operation; |
| 38 | import org.onosproject.d.config.sync.operation.SetResponse; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
| 41 | import org.onosproject.net.provider.ProviderId; |
| 42 | import org.onosproject.yang.model.DataNode; |
| 43 | import org.onosproject.yang.model.InnerNode; |
| 44 | import org.onosproject.yang.model.LeafNode; |
| 45 | import org.onosproject.yang.model.ResourceId; |
| 46 | |
| 47 | import com.google.common.collect.Iterables; |
| 48 | |
| 49 | public class DynamicDeviceConfigSynchronizerTest { |
| 50 | |
| 51 | static final String TEST_NS = "testNS"; |
| 52 | |
| 53 | static final ResourceId REL_INTERFACES = ResourceId.builder() |
| 54 | .addBranchPointSchema("interfaces", TEST_NS) |
| 55 | .build(); |
| 56 | |
| 57 | static final DeviceId DID = DeviceId.deviceId("test:device1"); |
| 58 | |
| 59 | DynamicDeviceConfigSynchronizer sut; |
| 60 | |
| 61 | TestDynamicConfigService dyConService; |
| 62 | |
| 63 | CountDownLatch providerCalled = new CountDownLatch(1); |
| 64 | |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 65 | /** |
| 66 | * DynamicConfigService.readNode(ResourceId, Filter) stub. |
| 67 | */ |
Yuta HIGUCHI | 8810aa4 | 2017-08-02 15:05:37 -0700 | [diff] [blame] | 68 | BiFunction<ResourceId, Filter, DataNode> onDcsRead; |
| 69 | |
| 70 | BiFunction<DeviceId, SetRequest, CompletableFuture<SetResponse>> onSetConfiguration; |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | |
| 75 | sut = new DynamicDeviceConfigSynchronizer(); |
| 76 | dyConService = new TestDynamicConfigService(); |
| 77 | sut.dynConfigService = dyConService; |
| 78 | sut.netcfgService = new NetworkConfigServiceAdapter(); |
| 79 | |
| 80 | sut.activate(); |
| 81 | |
| 82 | sut.register(new MockDeviceConfigSynchronizerProvider()); |
| 83 | } |
| 84 | |
| 85 | @After |
| 86 | public void tearDown() throws Exception { |
| 87 | sut.deactivate(); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testDispatchRequest() throws Exception { |
| 92 | |
| 93 | ResourceId devicePath = DeviceResourceIds.toResourceId(DID); |
| 94 | ResourceId cfgPath = REL_INTERFACES; |
| 95 | ResourceId absPath = ResourceIds.concat(devicePath, cfgPath); |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 96 | ResourceId evtPath = ResourceIds.relativize(ROOT_ID, absPath); |
| 97 | DynamicConfigEvent event = new DynamicConfigEvent(DynamicConfigEvent.Type.NODE_REPLACED, evtPath); |
Yuta HIGUCHI | 8810aa4 | 2017-08-02 15:05:37 -0700 | [diff] [blame] | 98 | |
| 99 | // assertions |
| 100 | onDcsRead = (path, filter) -> { |
| 101 | assertTrue(filter.isEmptyFilter()); |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 102 | assertEquals("DCService get access by root relative RID", evtPath, path); |
Yuta HIGUCHI | 8810aa4 | 2017-08-02 15:05:37 -0700 | [diff] [blame] | 103 | return deviceConfigNode(); |
| 104 | }; |
| 105 | |
| 106 | onSetConfiguration = (deviceId, request) -> { |
| 107 | assertEquals(DID, deviceId); |
| 108 | assertEquals(1, request.changes().size()); |
| 109 | Change change = Iterables.get(request.changes(), 0); |
| 110 | assertEquals("Provider get access by rel RID", REL_INTERFACES, change.path()); |
| 111 | assertEquals(Operation.REPLACE, change.op()); |
| 112 | assertEquals("interfaces", change.val().key().schemaId().name()); |
| 113 | // walk and test children if it adds value |
| 114 | |
| 115 | providerCalled.countDown(); |
| 116 | return CompletableFuture.completedFuture(SetResponse.ok(request)); |
| 117 | }; |
| 118 | |
| 119 | // start test run |
| 120 | |
| 121 | // imitate event from DCS |
| 122 | dyConService.postEvent(event); |
| 123 | |
| 124 | // assert that it reached the provider |
| 125 | providerCalled.await(5, TimeUnit.HOURS); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * DataNode for testing. |
| 130 | * |
| 131 | * <pre> |
| 132 | * +-interfaces |
| 133 | * | |
| 134 | * +- interface{intf-name="en0"} |
| 135 | * | |
| 136 | * +- speed = "10G" |
| 137 | * +- state = "up" |
| 138 | * |
| 139 | * </pre> |
| 140 | * @return DataNode |
| 141 | */ |
| 142 | private DataNode deviceConfigNode() { |
| 143 | InnerNode.Builder intfs = InnerNode.builder("interfaces", TEST_NS); |
| 144 | intfs.type(DataNode.Type.SINGLE_INSTANCE_NODE); |
| 145 | InnerNode.Builder intf = intfs.createChildBuilder("interface", TEST_NS); |
| 146 | intf.type(DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE); |
| 147 | intf.addKeyLeaf("name", TEST_NS, "Ethernet0/0"); |
| 148 | LeafNode.Builder speed = intf.createChildBuilder("mtu", TEST_NS, "1500"); |
| 149 | speed.type(DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE); |
| 150 | |
| 151 | intf.addNode(speed.build()); |
| 152 | intfs.addNode(intf.build()); |
| 153 | return intfs.build(); |
| 154 | } |
| 155 | |
| 156 | private class TestDynamicConfigService extends DynamicConfigServiceAdapter { |
| 157 | |
| 158 | public void postEvent(DynamicConfigEvent event) { |
| 159 | listenerRegistry.process(event); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public DataNode readNode(ResourceId path, Filter filter) { |
| 164 | return onDcsRead.apply(path, filter); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private class MockDeviceConfigSynchronizerProvider |
| 169 | implements DeviceConfigSynchronizationProvider { |
| 170 | |
| 171 | @Override |
| 172 | public ProviderId id() { |
| 173 | return new ProviderId(DID.uri().getScheme(), "test-provider"); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public CompletableFuture<SetResponse> setConfiguration(DeviceId deviceId, |
| 178 | SetRequest request) { |
| 179 | return onSetConfiguration.apply(deviceId, request); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } |