blob: bfdead6ad5c34f47ac387da700340496a408ae23 [file] [log] [blame]
Yuta HIGUCHI8810aa42017-08-02 15:05:37 -07001/*
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 */
16package org.onosproject.d.config.sync.impl;
17
18import static org.junit.Assert.*;
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070019import static org.onosproject.d.config.ResourceIds.ROOT_ID;
Yuta HIGUCHI8810aa42017-08-02 15:05:37 -070020
21import java.util.concurrent.CompletableFuture;
22import java.util.concurrent.CountDownLatch;
23import java.util.concurrent.TimeUnit;
24import java.util.function.BiFunction;
25
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29import org.onosproject.config.DynamicConfigEvent;
30import org.onosproject.config.DynamicConfigServiceAdapter;
31import org.onosproject.config.Filter;
32import org.onosproject.d.config.DeviceResourceIds;
33import org.onosproject.d.config.ResourceIds;
34import org.onosproject.d.config.sync.DeviceConfigSynchronizationProvider;
35import org.onosproject.d.config.sync.operation.SetRequest;
36import org.onosproject.d.config.sync.operation.SetRequest.Change;
37import org.onosproject.d.config.sync.operation.SetRequest.Change.Operation;
38import org.onosproject.d.config.sync.operation.SetResponse;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.config.NetworkConfigServiceAdapter;
41import org.onosproject.net.provider.ProviderId;
42import org.onosproject.yang.model.DataNode;
43import org.onosproject.yang.model.InnerNode;
44import org.onosproject.yang.model.LeafNode;
45import org.onosproject.yang.model.ResourceId;
46
47import com.google.common.collect.Iterables;
48
49public 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 HIGUCHIe057dee2017-09-15 13:56:10 -070065 /**
66 * DynamicConfigService.readNode(ResourceId, Filter) stub.
67 */
Yuta HIGUCHI8810aa42017-08-02 15:05:37 -070068 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 HIGUCHIe057dee2017-09-15 13:56:10 -070096 ResourceId evtPath = ResourceIds.relativize(ROOT_ID, absPath);
97 DynamicConfigEvent event = new DynamicConfigEvent(DynamicConfigEvent.Type.NODE_REPLACED, evtPath);
Yuta HIGUCHI8810aa42017-08-02 15:05:37 -070098
99 // assertions
100 onDcsRead = (path, filter) -> {
101 assertTrue(filter.isEmptyFilter());
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -0700102 assertEquals("DCService get access by root relative RID", evtPath, path);
Yuta HIGUCHI8810aa42017-08-02 15:05:37 -0700103 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}