blob: 0d4f561291bb2f5f76302517d8d67b15c6e1ca5a [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.Sets;
22import org.onlab.packet.Ip4Address;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.config.Config;
26import org.slf4j.Logger;
27
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090028import java.util.Set;
Kyuhwi Choi92d9ea42016-06-13 17:28:00 +090029import java.util.stream.StreamSupport;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090030
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090031import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
Kyuhwi Choi92d9ea42016-06-13 17:28:00 +090032import static org.slf4j.LoggerFactory.getLogger;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090033
34/**
35 * Configuration object for OpensatckNode service.
36 */
37public class GatewayNodeConfig extends Config<ApplicationId> {
38
39 protected final Logger log = getLogger(getClass());
40
41 public static final String NODES = "nodes";
42 public static final String BRIDGE_ID = "bridgeId";
43 public static final String DATAPLANE_IP = "dataPlaneIp";
44 public static final String EXTERNAL_INTERFACE_NAME = "gatewayExternalInterfaceName";
45
46 /**
47 * Returns the set of nodes read from network config.
48 *
49 * @return set of OpensatckNodeConfig or null
50 */
51 public Set<GatewayNode> gatewayNodes() {
52
53 Set<GatewayNode> nodes = Sets.newHashSet();
54
55 JsonNode jsonNodes = object.get(NODES);
56 if (jsonNodes == null) {
57 return null;
58 }
59
60 jsonNodes.forEach(jsonNode -> {
61 try {
62 nodes.add(new GatewayNode.Builder()
63 .gatewayDeviceId(DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText()))
Kyuhwi Choi176c83d2016-07-14 11:39:37 +090064 .gatewayExternalInterfaceName(jsonNode.path(EXTERNAL_INTERFACE_NAME).asText())
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090065 .dataIpAddress(Ip4Address.valueOf(jsonNode.path(DATAPLANE_IP).asText())).build());
66 } catch (IllegalArgumentException | NullPointerException e) {
67 log.error("Failed to read {}", e.toString());
68 }
69 });
70 return nodes;
71 }
72
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090073 @Override
74 public boolean isValid() {
Kyuhwi Choi92d9ea42016-06-13 17:28:00 +090075 JsonNode jsonNodes = object.get(NODES);
76
77 if (jsonNodes == null) {
78 return false;
79 }
80
81 return hasOnlyFields(NODES)
82 && StreamSupport.stream(jsonNodes.spliterator(), false).allMatch(this::checkValid);
83 }
84
85 private boolean checkValid(JsonNode jsonNode) {
86 ObjectNode objectNode = (ObjectNode) jsonNode;
87 return isString(objectNode, BRIDGE_ID, MANDATORY)
88 && isIpAddress(objectNode, DATAPLANE_IP, MANDATORY)
89 && isString(objectNode, EXTERNAL_INTERFACE_NAME, MANDATORY);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090090 }
91
92}