Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Andrea Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Sets; |
Andrea Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.Config; |
| 25 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
Andrea Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 26 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 27 | |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 28 | import java.util.Objects; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 29 | import java.util.Set; |
| 30 | |
| 31 | /** |
| 32 | * Mock Network Config Registry. |
| 33 | */ |
| 34 | class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
| 35 | private Set<Config> configs = Sets.newHashSet(); |
| 36 | |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 37 | void applyConfig(Config config) { |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 38 | 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 | } |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 49 | |
| 50 | @Override |
Andrea Campanella | 0994384 | 2020-03-27 12:53:46 +0100 | [diff] [blame] | 51 | 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 |
pier | 6a2052b | 2019-06-28 22:17:31 +0200 | [diff] [blame] | 70 | 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 Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 80 | } |