blob: 6c569d0c8e6b6aeffe5735963fc7cdd889a54d63 [file] [log] [blame]
Thomas Vachuskae6360222015-07-21 10:10:36 -07001/*
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 */
16package org.onosproject.incubator.net.config.impl;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070020import com.google.common.collect.Maps;
Thomas Vachuskae6360222015-07-21 10:10:36 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070023import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuskae6360222015-07-21 10:10:36 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070026import org.onosproject.incubator.net.config.NetworkConfigEvent;
27import org.onosproject.incubator.net.config.NetworkConfigListener;
Thomas Vachuskae6360222015-07-21 10:10:36 -070028import org.onosproject.incubator.net.config.NetworkConfigService;
Thomas Vachuskae6360222015-07-21 10:10:36 -070029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.io.File;
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070033import java.util.Iterator;
34import java.util.Map;
35import java.util.Objects;
Thomas Vachuskae6360222015-07-21 10:10:36 -070036
37/**
38 * Component for loading the initial network configuration.
39 */
40@Component(immediate = true)
41public 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 Kruglikovbd1eb3f2015-07-21 10:39:06 -070052 private Map<InnerConfigPosition, ObjectNode> jsons = Maps.newHashMap();
53
54 private final NetworkConfigListener configListener = new InnerConfigListener();
55
56 ObjectNode root;
Thomas Vachuskae6360222015-07-21 10:10:36 -070057
58 @Activate
59 public void activate() {
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070060 //TODO Maybe this should be at the bottom to avoid a potential race
61 networkConfigService.addListener(configListener);
Thomas Vachuskae6360222015-07-21 10:10:36 -070062 try {
63 if (CFG_FILE.exists()) {
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070064 root = (ObjectNode) new ObjectMapper().readTree(CFG_FILE);
Thomas Vachuskae6360222015-07-21 10:10:36 -070065
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070066 populateConfigurations();
Thomas Vachuskae6360222015-07-21 10:10:36 -070067
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070068 applyConfigurations();
69
Thomas Vachuskae6360222015-07-21 10:10:36 -070070 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 Kruglikovbd1eb3f2015-07-21 10:39:06 -070074 CFG_FILE, e);
Thomas Vachuskae6360222015-07-21 10:10:36 -070075 }
76 }
77
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070078 @Deactivate
79 public void deactivate() {
80 networkConfigService.removeListener(configListener);
81 }
Thomas Vachuskae6360222015-07-21 10:10:36 -070082 // sweep through pending config jsons and try to add them
83
84 /**
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070085 * Inner class that allows for handling of newly added NetConfig types.
Thomas Vachuskae6360222015-07-21 10:10:36 -070086 */
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -070087 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 Vachuskae6360222015-07-21 10:10:36 -070098 }
99
Aaron Kruglikovbd1eb3f2015-07-21 10:39:06 -0700100 /**
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
143 private void saveJson(String sk, ObjectNode node) {
144 node.fieldNames().forEachRemaining(s ->
145 saveSubjectJson(sk, s, (ObjectNode) node.path(s)));
146 }
147
148 private void saveSubjectJson(String sk,
149 String s, ObjectNode node) {
150 node.fieldNames().forEachRemaining(c ->
151 this.jsons.put(new InnerConfigPosition(sk, s, c), (ObjectNode) node.path(c)));
152 }
153
154 private void populateConfigurations() {
155 root.fieldNames().forEachRemaining(sk ->
156 saveJson(sk, (ObjectNode) root.path(sk)));
157
158 }
159
160 protected void applyConfigurations() {
161 Iterator<Map.Entry<InnerConfigPosition, ObjectNode>> iter = jsons.entrySet().iterator();
162 Map.Entry<InnerConfigPosition, ObjectNode> entry;
163 while (iter.hasNext()) {
164 entry = iter.next();
165 if (networkConfigService.getConfigClass(networkConfigService.getSubjectFactory(entry.getKey().
166 getSubjectKey()).subjectKey(), entry.getKey().getSubject()) != null) {
167 networkConfigService.applyConfig(networkConfigService.getSubjectFactory(
168 entry.getKey().getSubjectKey()).createSubject(entry.getKey().getSubject()),
169 networkConfigService.getConfigClass(networkConfigService.getSubjectFactory(entry.getKey().
170 getSubjectKey()).subjectKey(), entry.getKey().getClassKey()),
171 (ObjectNode) root.path(entry.getKey().getSubjectKey()).
172 path(entry.getKey().getSubject()).
173 path(entry.getKey().getClassKey()));
174 jsons.remove(entry.getKey());
175 }
176
177 }
Thomas Vachuskae6360222015-07-21 10:10:36 -0700178 }
179
180}