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