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