Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.impl; |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | ed4fdcd | 2015-09-08 14:13:37 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | c43fd1c | 2016-01-18 20:02:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.config.BasicNetworkConfigService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 28 | import org.onosproject.net.config.Config; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 29 | import org.onosproject.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.net.config.NetworkConfigService; |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.io.File; |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 36 | import java.util.Iterator; |
| 37 | import java.util.Map; |
| 38 | import java.util.Objects; |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Component for loading the initial network configuration. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public class NetworkConfigLoader { |
| 45 | |
| 46 | private static final File CFG_FILE = new File("../config/network-cfg.json"); |
| 47 | |
| 48 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 49 | |
Jonathan Hart | c43fd1c | 2016-01-18 20:02:20 -0800 | [diff] [blame] | 50 | // Dependency to ensure the basic subject factories are properly initialized |
| 51 | // before we start loading configs from file |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected BasicNetworkConfigService basicConfigs; |
| 54 | |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected NetworkConfigService networkConfigService; |
| 57 | |
| 58 | // FIXME: Add mutual exclusion to make sure this happens only once per startup. |
| 59 | |
Jonathan Hart | ed4fdcd | 2015-09-08 14:13:37 -0700 | [diff] [blame] | 60 | private final Map<InnerConfigPosition, JsonNode> jsons = Maps.newConcurrentMap(); |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 61 | |
| 62 | private final NetworkConfigListener configListener = new InnerConfigListener(); |
| 63 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 64 | private ObjectNode root; |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 65 | |
| 66 | @Activate |
| 67 | public void activate() { |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 68 | //TODO Maybe this should be at the bottom to avoid a potential race |
| 69 | networkConfigService.addListener(configListener); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 70 | try { |
| 71 | if (CFG_FILE.exists()) { |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 72 | root = (ObjectNode) new ObjectMapper().readTree(CFG_FILE); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 73 | |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 74 | populateConfigurations(); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 75 | |
Deepa Vaddireddy | 7fae189 | 2016-06-15 17:13:15 +0530 | [diff] [blame] | 76 | if (applyConfigurations()) { |
| 77 | log.info("Loaded initial network configuration from {}", CFG_FILE); |
| 78 | } else { |
| 79 | log.error("Partially loaded initial network configuration from {}", CFG_FILE); |
| 80 | } |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 81 | } |
| 82 | } catch (Exception e) { |
Deepa Vaddireddy | 7fae189 | 2016-06-15 17:13:15 +0530 | [diff] [blame] | 83 | log.warn("Unable to load initial network configuration from {}", CFG_FILE, e); |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 87 | @Deactivate |
| 88 | public void deactivate() { |
| 89 | networkConfigService.removeListener(configListener); |
| 90 | } |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 91 | // sweep through pending config jsons and try to add them |
| 92 | |
| 93 | /** |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 94 | * Inner class that allows for handling of newly added NetConfig types. |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 95 | */ |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 96 | private final class InnerConfigListener implements NetworkConfigListener { |
| 97 | |
| 98 | @Override |
| 99 | public void event(NetworkConfigEvent event) { |
| 100 | //TODO should this be done for other types of NetworkConfigEvents? |
| 101 | if (event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED || |
| 102 | event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) { |
| 103 | applyConfigurations(); |
| 104 | } |
| 105 | |
| 106 | } |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Inner class that allows for tracking of JSON class configurations. |
| 111 | */ |
| 112 | private final class InnerConfigPosition { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 113 | private final String subjectKey, subject, configKey; |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 114 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 115 | private String subjectKey() { |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 116 | return subjectKey; |
| 117 | } |
| 118 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 119 | private String subject() { |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 120 | return subject; |
| 121 | } |
| 122 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 123 | private String configKey() { |
| 124 | return configKey; |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 127 | private InnerConfigPosition(String subjectKey, String subject, String configKey) { |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 128 | this.subjectKey = subjectKey; |
| 129 | this.subject = subject; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 130 | this.configKey = configKey; |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean equals(Object obj) { |
| 135 | if (this == obj) { |
| 136 | return true; |
| 137 | } |
| 138 | if (obj instanceof InnerConfigPosition) { |
| 139 | final InnerConfigPosition that = (InnerConfigPosition) obj; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 140 | return Objects.equals(this.subjectKey, that.subjectKey) |
| 141 | && Objects.equals(this.subject, that.subject) |
| 142 | && Objects.equals(this.configKey, that.configKey); |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public int hashCode() { |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 149 | return Objects.hash(subjectKey, subject, configKey); |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 153 | /** |
| 154 | * Save the JSON leaves associated with a specific subject key. |
| 155 | * |
| 156 | * @param sk the subject key string. |
| 157 | * @param node the node associated with the subject key. |
| 158 | */ |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 159 | private void saveJson(String sk, ObjectNode node) { |
| 160 | node.fieldNames().forEachRemaining(s -> |
| 161 | saveSubjectJson(sk, s, (ObjectNode) node.path(s))); |
| 162 | } |
| 163 | |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Save the JSON leaves of the tree rooted as the node 'node' with subject key 'sk'. |
| 166 | * |
| 167 | * @param sk the string of the subject key. |
| 168 | * @param s the subject name. |
| 169 | * @param node the node rooting this subtree. |
| 170 | */ |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 171 | private void saveSubjectJson(String sk, |
| 172 | String s, ObjectNode node) { |
| 173 | node.fieldNames().forEachRemaining(c -> |
Jonathan Hart | ed4fdcd | 2015-09-08 14:13:37 -0700 | [diff] [blame] | 174 | this.jsons.put(new InnerConfigPosition(sk, s, c), node.path(c))); |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Iterate through the JSON and populate a list of the leaf nodes of the structure. |
| 179 | */ |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 180 | private void populateConfigurations() { |
| 181 | root.fieldNames().forEachRemaining(sk -> |
| 182 | saveJson(sk, (ObjectNode) root.path(sk))); |
| 183 | |
| 184 | } |
| 185 | |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 186 | /** |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 187 | * Apply the configurations associated with all of the config classes that |
| 188 | * are imported and have not yet been applied. |
Deepa Vaddireddy | 7fae189 | 2016-06-15 17:13:15 +0530 | [diff] [blame] | 189 | * |
| 190 | * @return false if any of the configuration parsing fails |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 191 | */ |
Deepa Vaddireddy | 7fae189 | 2016-06-15 17:13:15 +0530 | [diff] [blame] | 192 | private boolean applyConfigurations() { |
Jonathan Hart | ed4fdcd | 2015-09-08 14:13:37 -0700 | [diff] [blame] | 193 | Iterator<Map.Entry<InnerConfigPosition, JsonNode>> iter = jsons.entrySet().iterator(); |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 194 | |
Jonathan Hart | ed4fdcd | 2015-09-08 14:13:37 -0700 | [diff] [blame] | 195 | Map.Entry<InnerConfigPosition, JsonNode> entry; |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 196 | InnerConfigPosition key; |
Jonathan Hart | ed4fdcd | 2015-09-08 14:13:37 -0700 | [diff] [blame] | 197 | JsonNode node; |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 198 | String subjectKey; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 199 | String subjectString; |
| 200 | String configKey; |
Deepa Vaddireddy | 7fae189 | 2016-06-15 17:13:15 +0530 | [diff] [blame] | 201 | boolean isSuccess = true; |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 202 | while (iter.hasNext()) { |
| 203 | entry = iter.next(); |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 204 | node = entry.getValue(); |
| 205 | key = entry.getKey(); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 206 | subjectKey = key.subjectKey(); |
| 207 | subjectString = key.subject(); |
| 208 | configKey = key.configKey(); |
| 209 | |
| 210 | Class<? extends Config> configClass = |
| 211 | networkConfigService.getConfigClass(subjectKey, configKey); |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 212 | //Check that the config class has been imported |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 213 | if (configClass != null) { |
| 214 | |
| 215 | Object subject = networkConfigService.getSubjectFactory(subjectKey). |
| 216 | createSubject(subjectString); |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 217 | |
Deepa Vaddireddy | 7fae189 | 2016-06-15 17:13:15 +0530 | [diff] [blame] | 218 | try { |
| 219 | //Apply the configuration |
| 220 | networkConfigService.applyConfig(subject, configClass, node); |
| 221 | } catch (IllegalArgumentException e) { |
| 222 | log.warn("Error parsing config " + subjectKey + "/" + subject + "/" + configKey); |
| 223 | isSuccess = false; |
| 224 | } |
Aaron Kruglikov | a598c9e | 2015-07-23 16:56:27 -0700 | [diff] [blame] | 225 | |
| 226 | //Now that it has been applied the corresponding JSON entry is no longer needed |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 227 | iter.remove(); |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 228 | } |
Aaron Kruglikov | bd1eb3f | 2015-07-21 10:39:06 -0700 | [diff] [blame] | 229 | } |
Deepa Vaddireddy | 7fae189 | 2016-06-15 17:13:15 +0530 | [diff] [blame] | 230 | return isSuccess; |
| 231 | } |
Thomas Vachuska | e636022 | 2015-07-21 10:10:36 -0700 | [diff] [blame] | 232 | |
| 233 | } |