blob: a6d83b148e3ae7aeced25b9e2b24ea1d7c6eccd3 [file] [log] [blame]
Lee Yongjae7c27bb42017-11-17 12:00:45 +09001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.simplefabric;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.google.common.collect.Sets;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.config.Config;
26import org.onosproject.net.EncapsulationType;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Set;
31
32/**
33 * Configuration object for prefix config.
34 */
35public class SimpleFabricConfig extends Config<ApplicationId> {
36 public static final String KEY = "simpleFabric";
37
38 private static final String L2NETWORKS = "l2Networks";
39 private static final String NAME = "name";
40 private static final String INTERFACES = "interfaces";
41 private static final String ENCAPSULATION = "encapsulation";
42 private static final String L2FORWARD = "l2Forward";
43 private static final String IPSUBNETS = "ipSubnets";
44 private static final String BORDERROUTES = "borderRoutes";
45 private static final String IPPREFIX = "ipPrefix";
46 private static final String GATEWAYIP = "gatewayIp";
47 private static final String GATEWAYMAC = "gatewayMac";
48 private static final String L2NETWORKNAME = "l2NetworkName";
49 private static final String NEXTHOP = "nextHop";
50
51 private final Logger log = LoggerFactory.getLogger(getClass());
52
53 /**
54 * Returns all l2Networks in this configuration.
55 *
56 * @return A set of L2Network.
57 */
58 public Set<L2Network> getL2Networks() {
59 Set<L2Network> l2Networks = Sets.newHashSet();
60 JsonNode l2NetworkNode = object.get(L2NETWORKS);
61 if (l2NetworkNode == null) {
62 return l2Networks;
63 }
64
65 l2NetworkNode.forEach(jsonNode -> {
66 Set<String> ifaces = Sets.newHashSet();
67 JsonNode l2NetworkIfaces = jsonNode.path(INTERFACES);
68 if (l2NetworkIfaces == null) {
69 log.warn("simple fabric network config cannot find {}; skip: jsonNode={}", INTERFACES, jsonNode);
70 } else if (!l2NetworkIfaces.toString().isEmpty()) {
71 l2NetworkIfaces.forEach(ifacesNode -> ifaces.add(new String(ifacesNode.asText())));
72 }
73 String encapsulation = "NONE"; // NONE or VLAN
74 if (jsonNode.hasNonNull(ENCAPSULATION)) {
75 encapsulation = jsonNode.get(ENCAPSULATION).asText();
76 }
77 boolean l2Forward = true;
78 if (jsonNode.hasNonNull(L2FORWARD)) {
79 l2Forward = jsonNode.get(L2FORWARD).asBoolean();
80 }
81 try {
82 l2Networks.add(new L2Network(
83 jsonNode.get(NAME).asText(),
84 ifaces,
85 EncapsulationType.enumFromString(encapsulation),
86 l2Forward));
87 } catch (Exception e) {
88 log.warn("simple fabric network config l2Network parse failed; skip: error={} jsonNode={}", jsonNode);
89 }
90 });
91 return l2Networks;
92 }
93
94 /**
95 * Gets the set of configured local IP subnets.
96 *
97 * @return IP Subnets
98 */
99 public Set<IpSubnet> ipSubnets() {
100 Set<IpSubnet> subnets = Sets.newHashSet();
101 JsonNode subnetsNode = object.get(IPSUBNETS);
102 if (subnetsNode == null) {
103 log.warn("simple fabric network config ipSubnets is null!");
104 return subnets;
105 }
106
107 subnetsNode.forEach(jsonNode -> {
108 String encapsulation = "NONE"; // NONE or VLAN
109 if (jsonNode.hasNonNull(ENCAPSULATION)) {
110 encapsulation = jsonNode.get(ENCAPSULATION).asText();
111 }
112 try {
113 subnets.add(new IpSubnet(
114 IpPrefix.valueOf(jsonNode.get(IPPREFIX).asText()),
115 IpAddress.valueOf(jsonNode.get(GATEWAYIP).asText()),
116 MacAddress.valueOf(jsonNode.get(GATEWAYMAC).asText()),
117 EncapsulationType.enumFromString(encapsulation),
118 jsonNode.get(L2NETWORKNAME).asText()));
119 } catch (Exception e) {
120 log.warn("simple fabric network config ipSubnet parse failed; skip: error={} jsonNode={}", jsonNode);
121 }
122 });
123
124 return subnets;
125 }
126
127 /**
128 * Returns all routes in this configuration.
129 *
130 * @return A set of route.
131 */
132 public Set<Route> borderRoutes() {
133 Set<Route> routes = Sets.newHashSet();
134
135 JsonNode routesNode = object.get(BORDERROUTES);
136 if (routesNode == null) {
137 //log.warn("simple fabric network config borderRoutes is null!");
138 return routes;
139 }
140
141 routesNode.forEach(jsonNode -> {
142 try {
143 routes.add(new Route(
144 Route.Source.STATIC,
145 IpPrefix.valueOf(jsonNode.path(IPPREFIX).asText()),
146 IpAddress.valueOf(jsonNode.path(NEXTHOP).asText())));
147 } catch (IllegalArgumentException e) {
148 log.warn("simple fabric network config parse error; skip: {}", jsonNode);
149 }
150 });
151
152 return routes;
153 }
154
155}