blob: 12d57ea7095dd0979e3fb2ddc800958ea728357d [file] [log] [blame]
Charles Chan6c624992017-08-18 17:11:34 -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 */
16
17package org.onosproject.segmentrouting;
18
Andrea Campanella09943842020-03-27 12:53:46 +010019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.JsonNodeFactory;
pier6a2052b2019-06-28 22:17:31 +020021import com.google.common.collect.ImmutableSet;
Charles Chan6c624992017-08-18 17:11:34 -070022import com.google.common.collect.Sets;
Andrea Campanella09943842020-03-27 12:53:46 +010023import org.onosproject.net.DeviceId;
Charles Chan6c624992017-08-18 17:11:34 -070024import org.onosproject.net.config.Config;
25import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Andrea Campanella09943842020-03-27 12:53:46 +010026import org.onosproject.net.config.basics.BasicDeviceConfig;
Charles Chan6c624992017-08-18 17:11:34 -070027
pier6a2052b2019-06-28 22:17:31 +020028import java.util.Objects;
Charles Chan6c624992017-08-18 17:11:34 -070029import java.util.Set;
30
31/**
32 * Mock Network Config Registry.
33 */
34class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
35 private Set<Config> configs = Sets.newHashSet();
36
pier6a2052b2019-06-28 22:17:31 +020037 void applyConfig(Config config) {
Charles Chan6c624992017-08-18 17:11:34 -070038 configs.add(config);
39 }
40
41 @Override
42 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
43 Config c = configs.stream()
44 .filter(config -> subject.equals(config.subject()))
45 .filter(config -> configClass.equals(config.getClass()))
46 .findFirst().orElse(null);
47 return (C) c;
48 }
pier6a2052b2019-06-28 22:17:31 +020049
50 @Override
Andrea Campanella09943842020-03-27 12:53:46 +010051 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
52 Config c = configs.stream()
53 .filter(config -> subject.equals(config.subject()))
54 .filter(config -> configClass.equals(config.getClass()))
55 .findFirst().orElseGet(() -> {
56 if (configClass.equals(BasicDeviceConfig.class)) {
57 BasicDeviceConfig deviceConfig = new BasicDeviceConfig();
58 ObjectMapper mapper = new ObjectMapper();
59 deviceConfig.init((DeviceId) subject, ((DeviceId) subject).toString(),
60 JsonNodeFactory.instance.objectNode(), mapper, config -> {
61 });
62 return deviceConfig;
63 }
64 return null;
65 });
66 return (C) c;
67 }
68
69 @Override
pier6a2052b2019-06-28 22:17:31 +020070 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subject, Class<C> configClass) {
71 ImmutableSet.Builder<S> builder = ImmutableSet.builder();
72 String cName = configClass.getName();
73 configs.forEach(k -> {
74 if (subject.isInstance(k.subject()) && Objects.equals(cName, k.getClass().getName())) {
75 builder.add((S) k.subject());
76 }
77 });
78 return builder.build();
79 }
Charles Chan6c624992017-08-18 17:11:34 -070080}