1 package org.onosproject.segmentrouting.config;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.HashSet;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
14 import org.onosproject.net.DeviceId;
15 import org.onosproject.net.Link;
16 import org.onosproject.segmentrouting.config.NetworkConfig.LinkConfig;
17 import org.onosproject.segmentrouting.config.NetworkConfig.SwitchConfig;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.fasterxml.jackson.core.JsonParseException;
22 import com.fasterxml.jackson.databind.JsonMappingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
26 * NetworkConfigManager manages all network configuration for switches, links
27 * and any other state that needs to be configured for correct network
31 public class NetworkConfigManager implements NetworkConfigService {
32 protected static final Logger log = LoggerFactory
33 .getLogger(NetworkConfigManager.class);
34 private static final String CONFIG_DIR = "../config";
35 private static final String DEFAULT_CONFIG_FILE = "segmentrouting.conf";
36 private final String configFileName = DEFAULT_CONFIG_FILE;
38 * JSON Config file needs to use one of the following types for defining the
39 * kind of switch or link it wishes to configure.
41 public static final String SEGMENT_ROUTER = "Router_SR";
43 public static final String PKT_LINK = "pktLink";
45 NetworkConfig networkConfig;
46 private ConcurrentMap<DeviceId, SwitchConfig> configuredSwitches;
47 private ConcurrentMap<Link, LinkConfig> configuredLinks;
48 private Map<String, DeviceId> nameToDpid;
51 public SwitchConfigStatus checkSwitchConfig(DeviceId dpid) {
52 SwitchConfig swc = configuredSwitches.get(dpid);
53 if (networkConfig.getRestrictSwitches()) {
54 // default deny behavior
56 // switch is not configured - we deny this switch
57 return new SwitchConfigStatus(NetworkConfigState.DENY, null,
58 "Switch not configured, in network denying switches by default.");
60 if (swc.isAllowed()) {
61 // switch is allowed in config, return configured attributes
62 return new SwitchConfigStatus(NetworkConfigState.ACCEPT_ADD, swc);
64 // switch has been configured off (administratively down)
65 return new SwitchConfigStatus(NetworkConfigState.DENY, null,
66 "Switch configured down (allowed=false).");
69 // default allow behavior
72 return new SwitchConfigStatus(NetworkConfigState.ACCEPT, null);
74 if (swc.isAllowed()) {
75 // switch is allowed in config, return configured attributes
76 return new SwitchConfigStatus(NetworkConfigState.ACCEPT_ADD, swc);
78 // switch has been configured off (administratively down)
79 return new SwitchConfigStatus(NetworkConfigState.DENY, null,
80 "Switch configured down (allowed=false).");
87 public LinkConfigStatus checkLinkConfig(Link linkTuple) {
88 LinkConfig lkc = getConfiguredLink(linkTuple);
89 // links are always disallowed if any one of the nodes that make up the
90 // link are disallowed
91 DeviceId linkNode1 = linkTuple.src().deviceId();
92 SwitchConfigStatus scs1 = checkSwitchConfig(linkNode1);
93 if (scs1.getConfigState() == NetworkConfigState.DENY) {
94 return new LinkConfigStatus(NetworkConfigState.DENY, null,
95 "Link-node: " + linkNode1 + " denied by config: " + scs1.getMsg());
97 DeviceId linkNode2 = linkTuple.dst().deviceId();
98 SwitchConfigStatus scs2 = checkSwitchConfig(linkNode2);
99 if (scs2.getConfigState() == NetworkConfigState.DENY) {
100 return new LinkConfigStatus(NetworkConfigState.DENY, null,
101 "Link-node: " + linkNode2 + " denied by config: " + scs2.getMsg());
103 if (networkConfig.getRestrictLinks()) {
104 // default deny behavior
106 // link is not configured - we deny this link
107 return new LinkConfigStatus(NetworkConfigState.DENY, null,
108 "Link not configured, in network denying links by default.");
110 if (lkc.isAllowed()) {
111 // link is allowed in config, return configured attributes
112 return new LinkConfigStatus(NetworkConfigState.ACCEPT_ADD, lkc);
114 // link has been configured off (administratively down)
115 return new LinkConfigStatus(NetworkConfigState.DENY, null,
116 "Link configured down (allowed=false).");
119 // default allow behavior
122 return new LinkConfigStatus(NetworkConfigState.ACCEPT, null);
124 if (lkc.isAllowed()) {
125 // link is allowed in config, return configured attributes
126 return new LinkConfigStatus(NetworkConfigState.ACCEPT_ADD, lkc);
128 // link has been configured off (administratively down)
129 return new LinkConfigStatus(NetworkConfigState.DENY, null,
130 "Link configured down (allowed=false).");
137 public List<SwitchConfig> getConfiguredAllowedSwitches() {
138 List<SwitchConfig> allowed = new ArrayList<SwitchConfig>();
139 for (SwitchConfig swc : configuredSwitches.values()) {
140 if (swc.isAllowed()) {
148 public List<LinkConfig> getConfiguredAllowedLinks() {
149 List<LinkConfig> allowed = new ArrayList<LinkConfig>();
150 for (LinkConfig lkc : configuredLinks.values()) {
151 if (lkc.isAllowed()) {
159 public DeviceId getDpidForName(String name) {
160 if (nameToDpid.get(name) != null) {
161 return nameToDpid.get(name);
170 private void loadNetworkConfig() {
171 File configFile = new File(CONFIG_DIR, configFileName);
172 ObjectMapper mapper = new ObjectMapper();
173 networkConfig = new NetworkConfig();
176 networkConfig = mapper.readValue(configFile,
177 NetworkConfig.class);
178 } catch (JsonParseException e) {
179 String err = String.format("JsonParseException while loading network "
180 + "config from file: %s: %s", configFileName,
182 throw new NetworkConfigException.ErrorConfig(err);
183 } catch (JsonMappingException e) {
184 String err = String.format(
185 "JsonMappingException while loading network config "
186 + "from file: %s: %s",
189 throw new NetworkConfigException.ErrorConfig(err);
190 } catch (IOException e) {
191 String err = String.format("IOException while loading network config "
192 + "from file: %s %s", configFileName, e.getMessage());
193 throw new NetworkConfigException.ErrorConfig(err);
196 log.info("Network config specifies: {} switches and {} links",
197 (networkConfig.getRestrictSwitches())
198 ? networkConfig.getSwitchConfig().size() : "default allow",
199 (networkConfig.getRestrictLinks())
200 ? networkConfig.getLinkConfig().size() : "default allow");
203 private void parseNetworkConfig() {
204 List<SwitchConfig> swConfList = networkConfig.getSwitchConfig();
205 List<LinkConfig> lkConfList = networkConfig.getLinkConfig();
206 validateSwitchConfig(swConfList);
207 createTypeSpecificSwitchConfig(swConfList);
208 validateLinkConfig(lkConfList);
209 createTypeSpecificLinkConfig(lkConfList);
210 // TODO validate reachability matrix 'names' for configured dpids
213 private void createTypeSpecificSwitchConfig(List<SwitchConfig> swConfList) {
214 for (SwitchConfig swc : swConfList) {
215 nameToDpid.put(swc.getName(), swc.getDpid());
216 String swtype = swc.getType();
219 SwitchConfig sr = new SegmentRouterConfig(swc);
220 configuredSwitches.put(sr.getDpid(), sr);
223 throw new NetworkConfigException.UnknownSwitchType(swtype,
229 private void createTypeSpecificLinkConfig(List<LinkConfig> lkConfList) {
230 for (LinkConfig lkc : lkConfList) {
231 String lktype = lkc.getType();
234 PktLinkConfig pk = new PktLinkConfig(lkc);
235 for (Link lt : pk.getLinkTupleList()) {
236 configuredLinks.put(lt, pk);
240 throw new NetworkConfigException.UnknownLinkType(lktype,
241 lkc.getNodeDpid1(), lkc.getNodeDpid2());
246 private void validateSwitchConfig(List<SwitchConfig> swConfList) {
247 Set<DeviceId> swDpids = new HashSet<DeviceId>();
248 Set<String> swNames = new HashSet<String>();
249 for (SwitchConfig swc : swConfList) {
250 if (swc.getNodeDpid() == null || swc.getDpid() == null) {
251 throw new NetworkConfigException.DpidNotSpecified(swc.getName());
253 // ensure both String and DeviceId values of dpid are set
254 if (!swc.getDpid().equals(DeviceId.deviceId(swc.getNodeDpid()))) {
255 throw new NetworkConfigException.SwitchDpidNotConverted(
258 if (swc.getName() == null) {
259 throw new NetworkConfigException.NameNotSpecified(swc.getDpid());
261 if (swc.getType() == null) {
262 throw new NetworkConfigException.SwitchTypeNotSpecified(
265 if (!swDpids.add(swc.getDpid())) {
266 throw new NetworkConfigException.DuplicateDpid(swc.getDpid());
268 if (!swNames.add(swc.getName())) {
269 throw new NetworkConfigException.DuplicateName(swc.getName());
271 // TODO Add more validations
275 private void validateLinkConfig(List<LinkConfig> lkConfList) {
276 for (LinkConfig lkc : lkConfList) {
277 if (lkc.getNodeDpid1() == null || lkc.getNodeDpid2() == null) {
278 throw new NetworkConfigException.LinkDpidNotSpecified(
279 lkc.getNodeDpid1(), lkc.getNodeDpid2());
281 // ensure both String and Long values are set
282 if (!lkc.getDpid1().equals(DeviceId.deviceId(lkc.getNodeDpid1())) ||
283 !lkc.getDpid2().equals(DeviceId.deviceId(lkc.getNodeDpid2()))) {
284 throw new NetworkConfigException.LinkDpidNotConverted(
285 lkc.getNodeDpid1(), lkc.getNodeDpid2());
287 if (lkc.getType() == null) {
288 throw new NetworkConfigException.LinkTypeNotSpecified(
289 lkc.getNodeDpid1(), lkc.getNodeDpid2());
291 if (configuredSwitches.get(lkc.getDpid1()) == null) {
292 throw new NetworkConfigException.LinkForUnknownSwitchConfig(
295 if (configuredSwitches.get(lkc.getDpid2()) == null) {
296 throw new NetworkConfigException.LinkForUnknownSwitchConfig(
299 // TODO add more validations
304 private LinkConfig getConfiguredLink(Link linkTuple) {
305 LinkConfig lkc = null;
306 // first try the unidirectional link with the ports assigned
307 lkc = configuredLinks.get(linkTuple);
313 * Initializes the network configuration manager module by
314 * loading and parsing the network configuration file.
318 configuredSwitches = new ConcurrentHashMap<DeviceId, SwitchConfig>();
319 configuredLinks = new ConcurrentHashMap<Link, LinkConfig>();
320 nameToDpid = new HashMap<String, DeviceId>();
321 parseNetworkConfig();