blob: 7d2604a6592a7dbbc2833307d37d5fd8e6fdec7e [file] [log] [blame]
Gamze Abaka1e5ccf52018-07-02 11:59:03 +00001/*
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.opencord.sadis.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import org.onosproject.codec.impl.CodecManager;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreServiceAdapter;
23import org.onosproject.net.config.ConfigApplyDelegate;
24import org.onosproject.net.config.NetworkConfigRegistryAdapter;
25import org.onosproject.net.config.NetworkConfigListener;
26import org.onosproject.net.config.Config;
27import org.onosproject.net.config.NetworkConfigEvent;
28import org.opencord.sadis.BaseConfig;
Daniele Moro82d9a362019-11-06 23:51:20 +000029import org.opencord.sadis.BaseInformationService;
Matteo Scandoloe4f4b632020-01-07 23:54:35 +000030import org.opencord.sadis.BaseInformation;
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000031
32import java.io.InputStream;
33import java.time.Duration;
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.List;
37import java.util.Map;
38
39import static org.junit.Assert.*;
40
41public abstract class BaseSadis {
42
43 protected SadisManager sadis;
44 protected ConfigApplyDelegate delegate;
45 protected ObjectMapper mapper;
46 protected ApplicationId subject;
47 protected BaseConfig config;
48 protected NetworkConfigEvent event;
49 protected static NetworkConfigListener configListener;
50
51 public void setUp(String localConfig, Class configClass) throws Exception {
52 sadis = new SadisManager();
53 sadis.coreService = new MockCoreService();
54 delegate = new MockConfigDelegate();
55 mapper = new ObjectMapper();
56 subject = sadis.coreService.registerApplication("org.opencord.sadis");
57
58 config.init(subject, "sadis-local-mode-test", node(localConfig), mapper, delegate);
59 sadis.cfgService = new MockNetworkConfigRegistry(subject, config);
60
61 event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, subject, configClass);
62
63 sadis.codecService = new CodecManager();
64 sadis.activate();
65 }
66
67 public void tearDown() {
68 this.sadis.deactivate();
69 }
70
71 protected JsonNode node(String jsonFile) throws Exception {
72 final InputStream jsonStream = BaseSadis.class.getResourceAsStream(jsonFile);
73 final JsonNode testConfig = mapper.readTree(jsonStream);
74 return testConfig;
75 }
76
77 protected void checkConfigInfo(int cacheMaxSize, String cacheTtl, BaseConfig config) {
78 assertEquals(cacheMaxSize, config.getCacheMaxSize());
79 assertEquals(Duration.parse(cacheTtl), config.getCacheTtl());
80 }
81
82 protected void invalidateId(String id, BaseInformationService service) {
83 service.invalidateId(id);
84 }
85
86 protected void invalidateAll(BaseInformationService service) {
87 service.invalidateAll();
88 }
89
90 protected void checkFromBoth(String id, BaseInformation localEntry, BaseInformationService service) {
91 BaseInformation entry = service.getfromCache(id);
92 assertNull(entry);
93 checkGetForExisting(id, localEntry, service);
94 }
95
96 abstract boolean checkEquality(BaseInformation localEntry, BaseInformation entry);
97
98 protected void checkGetForExisting(String id, BaseInformation localEntry, BaseInformationService service) {
99 BaseInformation entry = service.get(id);
100 assertNotNull(entry);
101 System.out.println(entry);
102 if (localEntry != null) {
103 assertTrue(checkEquality(localEntry, entry));
104 }
105 }
106
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000107
108 /**
109 * Mocks an ONOS configuration delegate to allow JSON based configuration to
110 * be tested.
111 */
112 private static final class MockConfigDelegate implements ConfigApplyDelegate {
113 @Override
114 public void onApply(@SuppressWarnings("rawtypes") Config config) {
115 config.apply();
116 }
117 }
118
119 /**
120 * Mocks an instance of {@link ApplicationId} so that the application
121 * component under test can query and use its application ID.
122 */
123 private static final class MockApplicationId implements ApplicationId {
124
125 private final short id;
126 private final String name;
127
128 public MockApplicationId(short id, String name) {
129 this.id = id;
130 this.name = name;
131 }
132
133 @Override
134 public short id() {
135 return id;
136 }
137
138 @Override
139 public String name() {
140 return name;
141 }
142 }
143
144 /**
145 * Mocks the core services of ONOS so that the application under test can
146 * register and query application IDs.
147 */
148 private static final class MockCoreService extends CoreServiceAdapter {
149
150 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
151 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
152
153 /*
154 * (non-Javadoc)
155 *
156 * @see
157 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
158 */
159 @Override
160 public ApplicationId getAppId(Short id) {
161 if (id >= idList.size()) {
162 return null;
163 }
164 return idList.get(id);
165 }
166
167 /*
168 * (non-Javadoc)
169 *
170 * @see
171 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
172 */
173 @Override
174 public ApplicationId getAppId(String name) {
175 return idMap.get(name);
176 }
177
178 /*
179 * (non-Javadoc)
180 *
181 * @see
182 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
183 * .String)
184 */
185 @Override
186 public ApplicationId registerApplication(String name) {
187 ApplicationId appId = idMap.get(name);
188 if (appId == null) {
189 appId = new MockApplicationId((short) idList.size(), name);
190 idList.add(appId);
191 idMap.put(name, appId);
192 }
193 return appId;
194 }
195
196 }
197
198
199 /**
200 * Mocks the ONOS network configuration registry so that the application
201 * under test can access a JSON defined configuration.
202 */
203 static final class MockNetworkConfigRegistry<S> extends NetworkConfigRegistryAdapter {
204 private final BaseConfig config;
205
206 public MockNetworkConfigRegistry(final S subject, final BaseConfig config) {
207 this.config = config;
208 }
209
210 @SuppressWarnings("unchecked")
211 @Override
212 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
213 return (C) config;
214 }
215
216 @Override
217 public void addListener(NetworkConfigListener listener) {
218 configListener = listener;
219 }
220 }
221}