blob: 4ab87b45a45c4e40bee04d8fbbe6987406cad729 [file] [log] [blame]
Kyuhwi Choidc2973b2016-05-13 14:54:31 +09001/*
2 * Copyright 2016-present 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.scalablegateway.api;
17
18
19import com.fasterxml.jackson.databind.JsonNode;
Kyuhwi Choi92d9ea42016-06-13 17:28:00 +090020import com.fasterxml.jackson.databind.node.ObjectNode;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090021import com.google.common.collect.Lists;
22import com.google.common.collect.Sets;
23import org.onlab.packet.Ip4Address;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.config.Config;
27import org.slf4j.Logger;
28
29import java.util.Collections;
30import java.util.List;
31import java.util.Set;
Kyuhwi Choi92d9ea42016-06-13 17:28:00 +090032import java.util.stream.StreamSupport;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090033
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090034import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
Kyuhwi Choi92d9ea42016-06-13 17:28:00 +090035import static org.slf4j.LoggerFactory.getLogger;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090036
37/**
38 * Configuration object for OpensatckNode service.
39 */
40public class GatewayNodeConfig extends Config<ApplicationId> {
41
42 protected final Logger log = getLogger(getClass());
43
44 public static final String NODES = "nodes";
45 public static final String BRIDGE_ID = "bridgeId";
46 public static final String DATAPLANE_IP = "dataPlaneIp";
47 public static final String EXTERNAL_INTERFACE_NAME = "gatewayExternalInterfaceName";
48
49 /**
50 * Returns the set of nodes read from network config.
51 *
52 * @return set of OpensatckNodeConfig or null
53 */
54 public Set<GatewayNode> gatewayNodes() {
55
56 Set<GatewayNode> nodes = Sets.newHashSet();
57
58 JsonNode jsonNodes = object.get(NODES);
59 if (jsonNodes == null) {
60 return null;
61 }
62
63 jsonNodes.forEach(jsonNode -> {
64 try {
65 nodes.add(new GatewayNode.Builder()
66 .gatewayDeviceId(DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText()))
67 .gatewayExternalInterfaceNames(
68 getExternalInterfaceName(jsonNode.path(EXTERNAL_INTERFACE_NAME).asText()))
69 .dataIpAddress(Ip4Address.valueOf(jsonNode.path(DATAPLANE_IP).asText())).build());
70 } catch (IllegalArgumentException | NullPointerException e) {
71 log.error("Failed to read {}", e.toString());
72 }
73 });
74 return nodes;
75 }
76
77 private List<String> getExternalInterfaceName(String s) {
78 List<String> list = Lists.newArrayList();
79 return Collections.addAll(list, s.split(",")) ? list : null;
80 }
81
82 @Override
83 public boolean isValid() {
Kyuhwi Choi92d9ea42016-06-13 17:28:00 +090084 JsonNode jsonNodes = object.get(NODES);
85
86 if (jsonNodes == null) {
87 return false;
88 }
89
90 return hasOnlyFields(NODES)
91 && StreamSupport.stream(jsonNodes.spliterator(), false).allMatch(this::checkValid);
92 }
93
94 private boolean checkValid(JsonNode jsonNode) {
95 ObjectNode objectNode = (ObjectNode) jsonNode;
96 return isString(objectNode, BRIDGE_ID, MANDATORY)
97 && isIpAddress(objectNode, DATAPLANE_IP, MANDATORY)
98 && isString(objectNode, EXTERNAL_INTERFACE_NAME, MANDATORY);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090099 }
100
101}