Seyeon Jeong | 8d3cad2 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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.t3.api; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.onosproject.net.config.Config; |
| 21 | import org.onosproject.net.config.basics.InterfaceConfig; |
| 22 | import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig; |
| 23 | import org.slf4j.Logger; |
| 24 | |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | |
| 29 | /** |
| 30 | * Represents Network Information Base (NIB) for network configurations |
| 31 | * and supports alternative functions to |
| 32 | * {@link org.onosproject.net.config.NetworkConfigService} for offline data. |
| 33 | */ |
| 34 | public class NetworkConfigNib { |
| 35 | |
| 36 | private static final Logger log = getLogger(NetworkConfigNib.class); |
| 37 | |
| 38 | // Map of str ConnectPoint : InterfaceConfig |
| 39 | private Map<String, Config> portConfigMap; |
| 40 | // Map of str DeviceId : SegmentRoutingDeviceConfig |
| 41 | private Map<String, Config> deviceConfigMap; |
| 42 | |
| 43 | // use the singleton helper to create the instance |
| 44 | protected NetworkConfigNib() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sets a map of port : configuration to the port. |
| 49 | * |
| 50 | * @param portConfigMap port-config map |
| 51 | */ |
| 52 | public void setPortConfigMap(Map<String, Config> portConfigMap) { |
| 53 | this.portConfigMap = portConfigMap; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets a map of device : configuration to the device. |
| 58 | * |
| 59 | * @param deviceConfigMap device-config map |
| 60 | */ |
| 61 | public void setDeviceConfigMap(Map<String, Config> deviceConfigMap) { |
| 62 | this.deviceConfigMap = deviceConfigMap; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the port-config map. |
| 67 | * |
| 68 | * @return port-config map |
| 69 | */ |
| 70 | public Map<Object, Object> getPortConfigMap() { |
| 71 | return ImmutableMap.copyOf(portConfigMap); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the device-config map. |
| 76 | * |
| 77 | * @return device-config map |
| 78 | */ |
| 79 | public Map<Object, Object> getDeviceConfigMap() { |
| 80 | return ImmutableMap.copyOf(deviceConfigMap); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the configuration for the specified subject and configuration |
| 85 | * class if one is available; null otherwise. |
| 86 | * |
| 87 | * @param subject configuration subject |
| 88 | * @param configClass configuration class |
| 89 | * @param <S> type of subject |
| 90 | * @param <C> type of configuration |
| 91 | * @return configuration or null if one is not available |
| 92 | */ |
| 93 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 94 | if (configClass.equals(InterfaceConfig.class)) { |
| 95 | return (C) portConfigMap.get(subject.toString()); |
| 96 | } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) { |
| 97 | return (C) deviceConfigMap.get(subject.toString()); |
| 98 | } else { |
| 99 | log.warn("Given configuration {} is not supported", configClass.toString()); |
| 100 | return null; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the singleton instance of multicast routes NIB. |
| 106 | * |
| 107 | * @return instance of multicast routes NIB |
| 108 | */ |
| 109 | public static NetworkConfigNib getInstance() { |
| 110 | return NetworkConfigNib.SingletonHelper.INSTANCE; |
| 111 | } |
| 112 | |
| 113 | private static class SingletonHelper { |
| 114 | private static final NetworkConfigNib INSTANCE = new NetworkConfigNib(); |
| 115 | } |
| 116 | |
| 117 | } |