blob: 7a4dc1a55f13edb0cdb965b435879674da380ce3 [file] [log] [blame]
Ray Milkeyd29bc1c2018-03-13 11:57:11 -07001/*
2 * Copyright 2018-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
17package org.onosproject.net.config.basics;
18
19import org.junit.Before;
20import org.junit.Test;
21import org.onosproject.TestApplicationId;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreServiceAdapter;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.HostId;
27import org.onosproject.net.LinkKey;
28import org.onosproject.net.NetTestTools;
29import org.onosproject.net.config.SubjectFactory;
30import org.onosproject.net.region.RegionId;
31import org.onosproject.ui.model.topo.UiTopoLayoutId;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.equalTo;
35import static org.hamcrest.Matchers.is;
36import static org.hamcrest.Matchers.notNullValue;
37
38public class SubjectFactoriesTest {
39
40 class TestCoreService extends CoreServiceAdapter {
41
42 @Override
43 public ApplicationId registerApplication(String key) {
44 return new TestApplicationId(key);
45 }
46
47 }
48
49 @Before
50 public void setUp() {
51 SubjectFactories.setCoreService(new TestCoreService());
52 }
53
54 @Test
55 public void testAppIdFactory() {
56 SubjectFactory<ApplicationId> appIdFactory = SubjectFactories.APP_SUBJECT_FACTORY;
57 assertThat(appIdFactory, notNullValue());
58
59 ApplicationId id = NetTestTools.APP_ID;
60 ApplicationId createdAppId = appIdFactory.createSubject(id.name());
61 assertThat(createdAppId.id(), equalTo(id.id()));
62 assertThat(appIdFactory.subjectKey(id), is(id.name()));
63 }
64
65 @Test
66 public void testDeviceIdFactory() {
67 SubjectFactory<DeviceId> deviceIdFactory = SubjectFactories.DEVICE_SUBJECT_FACTORY;
68 assertThat(deviceIdFactory, notNullValue());
69
70 String deviceName = "d1";
71 String ofDeviceName = "of:" + deviceName;
72 DeviceId id = NetTestTools.did(deviceName);
73
74 DeviceId createdDeviceId = deviceIdFactory.createSubject(ofDeviceName);
75 assertThat(createdDeviceId, equalTo(id));
76 assertThat(deviceIdFactory.subjectKey(id), is(ofDeviceName));
77 }
78
79 @Test
80 public void testConnectPointFactory() {
81 SubjectFactory<ConnectPoint> connectPointFactory = SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
82 assertThat(connectPointFactory, notNullValue());
83
84 String deviceName = "d1";
85 String ofDeviceName = "of:" + deviceName;
86 int devicePort = 2;
87 String cpString = ofDeviceName + "/" + Integer.toString(devicePort);
88 ConnectPoint cp = NetTestTools.connectPoint(deviceName, devicePort);
89
90 ConnectPoint createdConnectPoint = connectPointFactory.createSubject(cpString);
91 assertThat(createdConnectPoint, equalTo(cp));
92 assertThat(connectPointFactory.subjectKey(cp), is(cpString));
93 }
94
95 @Test
96 public void testHostFactory() {
97 SubjectFactory<HostId> hostFactory = SubjectFactories.HOST_SUBJECT_FACTORY;
98 assertThat(hostFactory, notNullValue());
99
100 String hostName = "11:11:11:11:11:11/3";
101 HostId hostId = NetTestTools.hid(hostName);
102
103 HostId createdHostId = hostFactory.createSubject(hostName);
104 assertThat(createdHostId, equalTo(hostId));
105 assertThat(hostFactory.subjectKey(hostId), is(hostId.toString()));
106 }
107
108 @Test
109 public void testLinkFactory() {
110 SubjectFactory<LinkKey> linkFactory = SubjectFactories.LINK_SUBJECT_FACTORY;
111 assertThat(linkFactory, notNullValue());
112
113 String deviceName1 = "d1";
114 String deviceName2 = "d2";
115 String ofDeviceName1 = "of:" + deviceName1;
116 String ofDeviceName2 = "of:" + deviceName2;
117 int devicePort1 = 2;
118 int devicePort2 = 3;
119 String cpString1 = ofDeviceName1 + "/" + Integer.toString(devicePort1);
120 String cpString2 = ofDeviceName2 + "/" + Integer.toString(devicePort2);
121 ConnectPoint cp1 = NetTestTools.connectPoint(deviceName1, devicePort1);
122 ConnectPoint cp2 = NetTestTools.connectPoint(deviceName2, devicePort2);
123 String linkString1 = cpString1 + '-' + cpString2;
124 LinkKey key1 = LinkKey.linkKey(cp1, cp2);
125
126 LinkKey createdLink1 = linkFactory.createSubject(linkString1);
127 assertThat(createdLink1.asId(), is(linkString1));
128 assertThat(linkFactory.subjectKey(key1), is(linkString1));
129 }
130
131 @Test
132 public void testRegionIdFactory() {
133 SubjectFactory<RegionId> regionIdFactory = SubjectFactories.REGION_SUBJECT_FACTORY;
134 assertThat(regionIdFactory, notNullValue());
135
136 String region1 = "region1";
137 RegionId id = RegionId.regionId(region1);
138
139 RegionId createdRegionId = regionIdFactory.createSubject(region1);
140 assertThat(createdRegionId.id(), equalTo(region1));
141 assertThat(regionIdFactory.subjectKey(id), is(region1));
142 }
143
144 @Test
145 public void testUITopoLayoutIdFactory() {
146 SubjectFactory<UiTopoLayoutId> uiTopoLayoutIdFactory = SubjectFactories.LAYOUT_SUBJECT_FACTORY;
147 assertThat(uiTopoLayoutIdFactory, notNullValue());
148
149 String layout1 = "layout1";
150 UiTopoLayoutId id = UiTopoLayoutId.layoutId(layout1);
151
152 UiTopoLayoutId createdLayouId = uiTopoLayoutIdFactory.createSubject(layout1);
153 assertThat(createdLayouId.id(), equalTo(layout1));
154 assertThat(uiTopoLayoutIdFactory.subjectKey(id), is(layout1));
155 }
156}