Jonathan Hart | 2b5ceec | 2017-12-04 13:57:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | |
| 17 | package org.opencord.cordconfig.access; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Iterators; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.config.Config; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | import java.util.Optional; |
| 30 | |
| 31 | import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; |
| 32 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 33 | |
| 34 | /** |
| 35 | * Represents configuration for an OLT agent. |
| 36 | */ |
| 37 | public class AccessAgentConfig extends Config<DeviceId> { |
| 38 | |
| 39 | private static final String OLTS = "olts"; |
| 40 | private static final String AGENT_MAC = "mac"; |
| 41 | |
| 42 | // TODO: Remove this, it is only useful as long as XOS doesn't manage this. |
| 43 | private static final String VTN_LOCATION = "vtn-location"; |
| 44 | |
| 45 | @Override |
| 46 | public boolean isValid() { |
| 47 | return hasOnlyFields(OLTS, AGENT_MAC, VTN_LOCATION) && |
| 48 | isMacAddress(AGENT_MAC, MANDATORY) && |
| 49 | isConnectPoint(VTN_LOCATION, OPTIONAL) && |
| 50 | areOltsValid(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Gets the access agent configuration for this device. |
| 55 | * |
| 56 | * @return access agent configuration |
| 57 | */ |
| 58 | public AccessAgentData getAgent() { |
| 59 | JsonNode olts = node.get(OLTS); |
| 60 | Map<ConnectPoint, MacAddress> oltMacInfo = Maps.newHashMap(); |
| 61 | olts.fields().forEachRemaining(item -> oltMacInfo.put( |
| 62 | ConnectPoint.deviceConnectPoint(item.getKey()), |
| 63 | MacAddress.valueOf(item.getValue().asText()))); |
| 64 | |
| 65 | MacAddress agentMac = MacAddress.valueOf(node.path(AGENT_MAC).asText()); |
| 66 | |
| 67 | JsonNode vtn = node.path(VTN_LOCATION); |
| 68 | Optional<ConnectPoint> vtnLocation; |
| 69 | if (vtn.isMissingNode()) { |
| 70 | vtnLocation = Optional.empty(); |
| 71 | } else { |
| 72 | vtnLocation = Optional.of(ConnectPoint.deviceConnectPoint(vtn.asText())); |
| 73 | } |
| 74 | |
| 75 | return new AccessAgentData(subject(), oltMacInfo, agentMac, vtnLocation); |
| 76 | } |
| 77 | |
| 78 | private boolean areOltsValid() { |
| 79 | JsonNode olts = node.get(OLTS); |
| 80 | if (!olts.isObject()) { |
| 81 | return false; |
| 82 | } |
| 83 | return Iterators.all(olts.fields(), |
| 84 | item -> ConnectPoint.deviceConnectPoint(item.getKey()) != null && |
| 85 | isMacAddress((ObjectNode) olts, item.getKey(), MANDATORY)); |
| 86 | } |
| 87 | } |