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