Hyunsun Moon | 5a84663d | 2016-05-11 14:01:20 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 17 | package org.onosproject.cordconfig.access; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
Hyunsun Moon | 5a84663d | 2016-05-11 14:01:20 -0700 | [diff] [blame^] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Iterators; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Hyunsun Moon | 5a84663d | 2016-05-11 14:01:20 -0700 | [diff] [blame^] | 23 | import org.apache.commons.lang.StringUtils; |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.config.Config; |
| 29 | |
| 30 | import java.util.Map; |
| 31 | import java.util.Optional; |
| 32 | |
Hyunsun Moon | 5a84663d | 2016-05-11 14:01:20 -0700 | [diff] [blame^] | 33 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 34 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 35 | |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Represents configuration for an OLT agent. |
| 38 | */ |
| 39 | public 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 Moon | 5a84663d | 2016-05-11 14:01:20 -0700 | [diff] [blame^] | 47 | @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 | |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 55 | /** |
| 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); |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 62 | 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 Moon | 5a84663d | 2016-05-11 14:01:20 -0700 | [diff] [blame^] | 80 | 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 | } |
alshabib | 1f36ab8 | 2016-04-29 15:38:19 -0700 | [diff] [blame] | 88 | } |