blob: 704c6a14904f1fcf2da072f4bc54f7c038fdb252 [file] [log] [blame]
Hyunsun Moon5a84663d2016-05-11 14:01:20 -07001/*
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 */
16
alshabib1f36ab82016-04-29 15:38:19 -070017package org.onosproject.cordconfig.access;
18
19import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Moon5a84663d2016-05-11 14:01:20 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.Iterators;
alshabib1f36ab82016-04-29 15:38:19 -070022import com.google.common.collect.Maps;
Hyunsun Moon5a84663d2016-05-11 14:01:20 -070023import org.apache.commons.lang.StringUtils;
alshabib1f36ab82016-04-29 15:38:19 -070024import org.onlab.packet.MacAddress;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.config.Config;
29
30import java.util.Map;
31import java.util.Optional;
32
Hyunsun Moon5a84663d2016-05-11 14:01:20 -070033import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
34import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
35
alshabib1f36ab82016-04-29 15:38:19 -070036/**
37 * Represents configuration for an OLT agent.
38 */
39public class AccessAgentConfig extends Config<DeviceId> {
40
41 private static final String OLTS = "olts";
42 private static final String AGENT_MAC = "mac";
43
44 // TODO: Remove this, it is only useful as long as XOS doesn't manage this.
45 private static final String VTN_LOCATION = "vtn-location";
46
Hyunsun Moon5a84663d2016-05-11 14:01:20 -070047 @Override
48 public boolean isValid() {
49 return hasOnlyFields(OLTS, AGENT_MAC, VTN_LOCATION) &&
50 isMacAddress(AGENT_MAC, MANDATORY) &&
51 isConnectPoint(VTN_LOCATION, OPTIONAL) &&
52 isValidOlts();
53 }
54
alshabib1f36ab82016-04-29 15:38:19 -070055 /**
56 * Gets the access agent configuration for this device.
57 *
58 * @return access agent configuration
59 */
60 public AccessAgentData getAgent() {
61 JsonNode olts = node.get(OLTS);
alshabib1f36ab82016-04-29 15:38:19 -070062 Map<ConnectPoint, MacAddress> oltMacInfo = Maps.newHashMap();
63 olts.fields().forEachRemaining(item -> oltMacInfo.put(
64 new ConnectPoint(subject(), PortNumber.fromString(item.getKey())),
65 MacAddress.valueOf(item.getValue().asText())));
66
67 MacAddress agentMac = MacAddress.valueOf(node.path(AGENT_MAC).asText());
68
69 JsonNode vtn = node.path(VTN_LOCATION);
70 Optional<ConnectPoint> vtnLocation;
71 if (vtn.isMissingNode()) {
72 vtnLocation = Optional.empty();
73 } else {
74 vtnLocation = Optional.of(ConnectPoint.deviceConnectPoint(vtn.asText()));
75 }
76
77 return new AccessAgentData(subject(), oltMacInfo, agentMac, vtnLocation);
78 }
79
Hyunsun Moon5a84663d2016-05-11 14:01:20 -070080 private boolean isValidOlts() {
81 JsonNode olts = node.get(OLTS);
82 if (!olts.isObject()) {
83 return false;
84 }
85 return !Iterators.any(olts.fields(), item -> !StringUtils.isNumeric(item.getKey()) ||
86 !isMacAddress((ObjectNode) olts, item.getKey(), MANDATORY));
87 }
alshabib1f36ab82016-04-29 15:38:19 -070088}