2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.segmentrouting.config;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentMap;
29 import org.onosproject.net.DeviceId;
30 import org.onosproject.net.Link;
31 import org.onosproject.segmentrouting.config.NetworkConfig.LinkConfig;
32 import org.onosproject.segmentrouting.config.NetworkConfig.SwitchConfig;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.fasterxml.jackson.core.JsonParseException;
37 import com.fasterxml.jackson.databind.JsonMappingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
41 * NetworkConfigManager manages all network configuration for switches, links
42 * and any other state that needs to be configured for correct network
46 public class NetworkConfigManager implements NetworkConfigService {
47 protected static final Logger log = LoggerFactory
48 .getLogger(NetworkConfigManager.class);
49 private static final String CONFIG_DIR = "../config";
50 private static final String DEFAULT_CONFIG_FILE = "segmentrouting.conf";
51 private final String configFileName = DEFAULT_CONFIG_FILE;
53 * JSON Config file needs to use one of the following types for defining the
54 * kind of switch or link it wishes to configure.
56 public static final String SEGMENT_ROUTER = "Router_SR";
58 public static final String PKT_LINK = "pktLink";
60 NetworkConfig networkConfig;
61 private ConcurrentMap<DeviceId, SwitchConfig> configuredSwitches;
62 private ConcurrentMap<Link, LinkConfig> configuredLinks;
63 private Map<String, DeviceId> nameToDpid;
66 public SwitchConfigStatus checkSwitchConfig(DeviceId dpid) {
67 SwitchConfig swc = configuredSwitches.get(dpid);
68 if (networkConfig.getRestrictSwitches()) {
69 // default deny behavior
71 // switch is not configured - we deny this switch
72 return new SwitchConfigStatus(NetworkConfigState.DENY, null,
73 "Switch not configured, in network denying switches by default.");
75 if (swc.isAllowed()) {
76 // switch is allowed in config, return configured attributes
77 return new SwitchConfigStatus(NetworkConfigState.ACCEPT_ADD, swc);
79 // switch has been configured off (administratively down)
80 return new SwitchConfigStatus(NetworkConfigState.DENY, null,
81 "Switch configured down (allowed=false).");
84 // default allow behavior
87 return new SwitchConfigStatus(NetworkConfigState.ACCEPT, null);
89 if (swc.isAllowed()) {
90 // switch is allowed in config, return configured attributes
91 return new SwitchConfigStatus(NetworkConfigState.ACCEPT_ADD, swc);
93 // switch has been configured off (administratively down)
94 return new SwitchConfigStatus(NetworkConfigState.DENY, null,
95 "Switch configured down (allowed=false).");
102 public LinkConfigStatus checkLinkConfig(Link linkTuple) {
103 LinkConfig lkc = getConfiguredLink(linkTuple);
104 // links are always disallowed if any one of the nodes that make up the
105 // link are disallowed
106 DeviceId linkNode1 = linkTuple.src().deviceId();
107 SwitchConfigStatus scs1 = checkSwitchConfig(linkNode1);
108 if (scs1.getConfigState() == NetworkConfigState.DENY) {
109 return new LinkConfigStatus(NetworkConfigState.DENY, null,
110 "Link-node: " + linkNode1 + " denied by config: " + scs1.getMsg());
112 DeviceId linkNode2 = linkTuple.dst().deviceId();
113 SwitchConfigStatus scs2 = checkSwitchConfig(linkNode2);
114 if (scs2.getConfigState() == NetworkConfigState.DENY) {
115 return new LinkConfigStatus(NetworkConfigState.DENY, null,
116 "Link-node: " + linkNode2 + " denied by config: " + scs2.getMsg());
118 if (networkConfig.getRestrictLinks()) {
119 // default deny behavior
121 // link is not configured - we deny this link
122 return new LinkConfigStatus(NetworkConfigState.DENY, null,
123 "Link not configured, in network denying links by default.");
125 if (lkc.isAllowed()) {
126 // link is allowed in config, return configured attributes
127 return new LinkConfigStatus(NetworkConfigState.ACCEPT_ADD, lkc);
129 // link has been configured off (administratively down)
130 return new LinkConfigStatus(NetworkConfigState.DENY, null,
131 "Link configured down (allowed=false).");
134 // default allow behavior
137 return new LinkConfigStatus(NetworkConfigState.ACCEPT, null);
139 if (lkc.isAllowed()) {
140 // link is allowed in config, return configured attributes
141 return new LinkConfigStatus(NetworkConfigState.ACCEPT_ADD, lkc);
143 // link has been configured off (administratively down)
144 return new LinkConfigStatus(NetworkConfigState.DENY, null,
145 "Link configured down (allowed=false).");
152 public List<SwitchConfig> getConfiguredAllowedSwitches() {
153 List<SwitchConfig> allowed = new ArrayList<>();
154 for (SwitchConfig swc : configuredSwitches.values()) {
155 if (swc.isAllowed()) {
163 public List<LinkConfig> getConfiguredAllowedLinks() {
164 List<LinkConfig> allowed = new ArrayList<>();
165 for (LinkConfig lkc : configuredLinks.values()) {
166 if (lkc.isAllowed()) {
174 public DeviceId getDpidForName(String name) {
175 if (nameToDpid.get(name) != null) {
176 return nameToDpid.get(name);
185 private void loadNetworkConfig() {
186 File configFile = new File(CONFIG_DIR, configFileName);
187 ObjectMapper mapper = new ObjectMapper();
188 networkConfig = new NetworkConfig();
191 networkConfig = mapper.readValue(configFile,
192 NetworkConfig.class);
193 } catch (JsonParseException e) {
194 String err = String.format("JsonParseException while loading network "
195 + "config from file: %s: %s", configFileName,
197 throw new NetworkConfigException.ErrorConfig(err);
198 } catch (JsonMappingException e) {
199 String err = String.format(
200 "JsonMappingException while loading network config "
201 + "from file: %s: %s",
204 throw new NetworkConfigException.ErrorConfig(err);
205 } catch (IOException e) {
206 String err = String.format("IOException while loading network config "
207 + "from file: %s %s", configFileName, e.getMessage());
208 throw new NetworkConfigException.ErrorConfig(err);
211 log.info("Network config specifies: {} switches and {} links",
212 (networkConfig.getRestrictSwitches())
213 ? networkConfig.getSwitchConfig().size() : "default allow",
214 (networkConfig.getRestrictLinks())
215 ? networkConfig.getLinkConfig().size() : "default allow");
218 private void parseNetworkConfig() {
219 List<SwitchConfig> swConfList = networkConfig.getSwitchConfig();
220 List<LinkConfig> lkConfList = networkConfig.getLinkConfig();
221 validateSwitchConfig(swConfList);
222 createTypeSpecificSwitchConfig(swConfList);
223 validateLinkConfig(lkConfList);
224 createTypeSpecificLinkConfig(lkConfList);
225 // TODO validate reachability matrix 'names' for configured dpids
228 private void createTypeSpecificSwitchConfig(List<SwitchConfig> swConfList) {
229 for (SwitchConfig swc : swConfList) {
230 nameToDpid.put(swc.getName(), swc.getDpid());
231 String swtype = swc.getType();
234 SwitchConfig sr = new SegmentRouterConfig(swc);
235 configuredSwitches.put(sr.getDpid(), sr);
238 throw new NetworkConfigException.UnknownSwitchType(swtype,
244 private void createTypeSpecificLinkConfig(List<LinkConfig> lkConfList) {
245 for (LinkConfig lkc : lkConfList) {
246 String lktype = lkc.getType();
249 PktLinkConfig pk = new PktLinkConfig(lkc);
250 for (Link lt : pk.getLinkTupleList()) {
251 configuredLinks.put(lt, pk);
255 throw new NetworkConfigException.UnknownLinkType(lktype,
256 lkc.getNodeDpid1(), lkc.getNodeDpid2());
261 private void validateSwitchConfig(List<SwitchConfig> swConfList) {
262 Set<DeviceId> swDpids = new HashSet<>();
263 Set<String> swNames = new HashSet<>();
264 for (SwitchConfig swc : swConfList) {
265 if (swc.getNodeDpid() == null || swc.getDpid() == null) {
266 throw new NetworkConfigException.DpidNotSpecified(swc.getName());
268 // ensure both String and DeviceId values of dpid are set
269 if (!swc.getDpid().equals(DeviceId.deviceId(swc.getNodeDpid()))) {
270 throw new NetworkConfigException.SwitchDpidNotConverted(
273 if (swc.getName() == null) {
274 throw new NetworkConfigException.NameNotSpecified(swc.getDpid());
276 if (swc.getType() == null) {
277 throw new NetworkConfigException.SwitchTypeNotSpecified(
280 if (!swDpids.add(swc.getDpid())) {
281 throw new NetworkConfigException.DuplicateDpid(swc.getDpid());
283 if (!swNames.add(swc.getName())) {
284 throw new NetworkConfigException.DuplicateName(swc.getName());
286 // TODO Add more validations
290 private void validateLinkConfig(List<LinkConfig> lkConfList) {
291 for (LinkConfig lkc : lkConfList) {
292 if (lkc.getNodeDpid1() == null || lkc.getNodeDpid2() == null) {
293 throw new NetworkConfigException.LinkDpidNotSpecified(
294 lkc.getNodeDpid1(), lkc.getNodeDpid2());
296 // ensure both String and Long values are set
297 if (!lkc.getDpid1().equals(DeviceId.deviceId(lkc.getNodeDpid1())) ||
298 !lkc.getDpid2().equals(DeviceId.deviceId(lkc.getNodeDpid2()))) {
299 throw new NetworkConfigException.LinkDpidNotConverted(
300 lkc.getNodeDpid1(), lkc.getNodeDpid2());
302 if (lkc.getType() == null) {
303 throw new NetworkConfigException.LinkTypeNotSpecified(
304 lkc.getNodeDpid1(), lkc.getNodeDpid2());
306 if (configuredSwitches.get(lkc.getDpid1()) == null) {
307 throw new NetworkConfigException.LinkForUnknownSwitchConfig(
310 if (configuredSwitches.get(lkc.getDpid2()) == null) {
311 throw new NetworkConfigException.LinkForUnknownSwitchConfig(
314 // TODO add more validations
319 private LinkConfig getConfiguredLink(Link linkTuple) {
320 LinkConfig lkc = null;
321 // first try the unidirectional link with the ports assigned
322 lkc = configuredLinks.get(linkTuple);
328 * Initializes the network configuration manager module by
329 * loading and parsing the network configuration file.
333 configuredSwitches = new ConcurrentHashMap<>();
334 configuredLinks = new ConcurrentHashMap<>();
335 nameToDpid = new HashMap<>();
336 parseNetworkConfig();