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