blob: 5a497d61944563017d634c0a55004f10869deef2 [file] [log] [blame]
Yuta HIGUCHI24057822017-08-02 15:03:51 -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;
17
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070018import static org.hamcrest.Matchers.contains;
19import static org.hamcrest.Matchers.equalTo;
20import static org.hamcrest.Matchers.instanceOf;
21import static org.hamcrest.Matchers.is;
Yuta HIGUCHI24057822017-08-02 15:03:51 -070022import static org.junit.Assert.*;
23import static org.onosproject.d.config.DeviceResourceIds.DCS_NAMESPACE;
24import static org.onosproject.d.config.DeviceResourceIds.DEVICES_NAME;
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070025import static org.onosproject.d.config.DeviceResourceIds.DEVICE_ID_KL_NAME;
26import static org.onosproject.d.config.DeviceResourceIds.DEVICE_NAME;
Yuta HIGUCHI24057822017-08-02 15:03:51 -070027import static org.onosproject.d.config.DeviceResourceIds.ROOT_NAME;
28
29import org.junit.Before;
30import org.junit.Test;
Yuta HIGUCHI24057822017-08-02 15:03:51 -070031import org.onosproject.net.DeviceId;
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070032import org.onosproject.yang.model.KeyLeaf;
33import org.onosproject.yang.model.ListKey;
34import org.onosproject.yang.model.NodeKey;
Yuta HIGUCHI24057822017-08-02 15:03:51 -070035import org.onosproject.yang.model.ResourceId;
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070036import org.onosproject.yang.model.SchemaId;
Yuta HIGUCHI24057822017-08-02 15:03:51 -070037
38public class DeviceResourceIdsTest {
39
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070040 static final DeviceId DID_A = DeviceId.deviceId("test:a");
Yuta HIGUCHI24057822017-08-02 15:03:51 -070041
42 static final ResourceId DEVICES = ResourceId.builder()
43 .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
44 .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE)
45 .build();
46
47 ResourceId ridA;
48 ResourceId ridAcopy;
49
50 @Before
51 public void setUp() throws Exception {
52 ridA = DeviceResourceIds.toResourceId(DID_A);
53 ridAcopy = ridA.copyBuilder().build();
54 }
55
56 @Test
57 public void testToDeviceId() throws CloneNotSupportedException {
58 ResourceId ridAchild = ridA.copyBuilder()
59 .addBranchPointSchema("some", "ns")
60 .addBranchPointSchema("random", "ns")
61 .addBranchPointSchema("child", "ns")
62 .build();
63
64 assertEquals(DID_A, DeviceResourceIds.toDeviceId(ridAchild));
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070065
66 NodeKey<?> nodeKey = ridA.nodeKeys().get(2);
67 assertThat(nodeKey, is(instanceOf(ListKey.class)));
68 assertThat(nodeKey.schemaId(), is(equalTo(new SchemaId(DEVICE_NAME, DCS_NAMESPACE))));
69 ListKey listKey = (ListKey) nodeKey;
70 assertThat(listKey.keyLeafs(), is(contains(new KeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, DID_A.toString()))));
Yuta HIGUCHI24057822017-08-02 15:03:51 -070071 }
72
73 @Test
74 public void testDeviceRootNode() {
75 assertTrue(DeviceResourceIds.isDeviceRootNode(ridA));
76
77 assertFalse(DeviceResourceIds.isRootOrDevicesNode(ridA));
78 }
79
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070080 @Test
81 public void testDeviceSubtreeTest() {
82 ResourceId absDevice = ResourceId.builder()
83 .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE)
84 .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE)
85 .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE)
86 .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, DID_A.toString())
87 .build();
88
89 NodeKey<?> deviceKey = absDevice.nodeKeys().get(2);
90 assertThat(deviceKey, is(instanceOf(ListKey.class)));
91 assertThat(deviceKey.schemaId().namespace(), is(equalTo(DCS_NAMESPACE)));
92 assertThat(deviceKey.schemaId().name(), is(equalTo(DEVICE_NAME)));
93 assertTrue(DeviceResourceIds.isUnderDeviceRootNode(absDevice));
94 }
95
96 @Test
97 public void testDeviceSubtreeEventTest() {
98 // root relative ResourceId used by DynamicConfigEvent
99 ResourceId evtDevice = ResourceId.builder()
100 .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE)
101 .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE)
102 .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, DID_A.toString())
103 .build();
104
105 NodeKey<?> deviceKey = evtDevice.nodeKeys().get(1);
106 assertThat(deviceKey, is(instanceOf(ListKey.class)));
107 assertThat(deviceKey.schemaId().namespace(), is(equalTo(DCS_NAMESPACE)));
108 assertThat(deviceKey.schemaId().name(), is(equalTo(DEVICE_NAME)));
109 assertTrue(DeviceResourceIds.isUnderDeviceRootNode(evtDevice));
110 }
111
Yuta HIGUCHI24057822017-08-02 15:03:51 -0700112}