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; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.incubator.net.config.NetworkConfigService; |
| 25 | import org.onosproject.incubator.net.config.SubjectFactory; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.io.File; |
| 30 | |
| 31 | /** |
| 32 | * Component for loading the initial network configuration. |
| 33 | */ |
| 34 | @Component(immediate = true) |
| 35 | public class NetworkConfigLoader { |
| 36 | |
| 37 | private static final File CFG_FILE = new File("../config/network-cfg.json"); |
| 38 | |
| 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 42 | protected NetworkConfigService networkConfigService; |
| 43 | |
| 44 | // FIXME: Add mutual exclusion to make sure this happens only once per startup. |
| 45 | |
| 46 | // TODO: add a field to track the collection of pending JSONS |
| 47 | |
| 48 | @Activate |
| 49 | public void activate() { |
| 50 | // Add listener to net config events |
| 51 | try { |
| 52 | if (CFG_FILE.exists()) { |
| 53 | ObjectNode root = (ObjectNode) new ObjectMapper().readTree(CFG_FILE); |
| 54 | // Parse this JSON structure and accumulate a collection of all leaf config JSONs |
| 55 | |
| 56 | // Perform initial iteration over all leaf configs and attempt to apply them, |
| 57 | // but do this only if they are valid. |
| 58 | // networkConfigService.getConfigClass("foo"); |
| 59 | |
| 60 | // This code can be used for building the collection of jsons |
| 61 | root.fieldNames().forEachRemaining(sk -> |
| 62 | consumeJson(networkConfigService, (ObjectNode) root.path(sk), |
| 63 | networkConfigService.getSubjectFactory(sk))); |
| 64 | log.info("Loaded initial network configuration from {}", CFG_FILE); |
| 65 | } |
| 66 | } catch (Exception e) { |
| 67 | log.warn("Unable to load initial network configuration from {}", |
| 68 | CFG_FILE, e); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | // TODO: add deactivate which will remove listener |
| 74 | |
| 75 | // TODO: implement event listener and as each config is registered, |
| 76 | // sweep through pending config jsons and try to add them |
| 77 | |
| 78 | /** |
| 79 | * Consumes configuration JSON for the specified subject factory. |
| 80 | * |
| 81 | * @param service network configuration service |
| 82 | * @param classNode subject class JSON node |
| 83 | * @param subjectFactory subject factory |
| 84 | */ |
| 85 | static void consumeJson(NetworkConfigService service, ObjectNode classNode, |
| 86 | SubjectFactory subjectFactory) { |
| 87 | classNode.fieldNames().forEachRemaining(s -> |
| 88 | consumeSubjectJson(service, (ObjectNode) classNode.path(s), |
| 89 | subjectFactory.createSubject(s))); |
| 90 | } |
| 91 | |
| 92 | private static void consumeSubjectJson(NetworkConfigService service, |
| 93 | ObjectNode subjectNode, Object subject) { |
| 94 | subjectNode.fieldNames().forEachRemaining(c -> |
| 95 | service.applyConfig(subject, service.getConfigClass(c), |
| 96 | (ObjectNode) subjectNode.path(c))); |
| 97 | } |
| 98 | |
| 99 | } |