Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 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.onosproject.provider.general.device.api; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.google.common.annotations.Beta; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.config.Config; |
| 23 | |
| 24 | import java.util.HashMap; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
| 28 | * Configuration for General device provider. |
| 29 | */ |
| 30 | @Beta |
| 31 | public class GeneralProviderDeviceConfig extends Config<DeviceId> { |
| 32 | |
| 33 | private static final String DEVICEKEYID = "deviceKeyId"; |
| 34 | |
| 35 | |
| 36 | @Override |
| 37 | public boolean isValid() { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Gets the information of all protocols associated to the device. |
| 43 | * |
| 44 | * @return map of protocol name and relative information |
| 45 | */ |
| 46 | public Map<String, DeviceInfoConfig> protocolsInfo() { |
| 47 | return getProtocolInfoMap(); |
| 48 | } |
| 49 | |
| 50 | private Map<String, DeviceInfoConfig> getProtocolInfoMap() { |
| 51 | Map<String, DeviceInfoConfig> deviceMap = new HashMap<>(); |
| 52 | node.fieldNames().forEachRemaining(name -> { |
| 53 | |
| 54 | Map<String, String> configMap = new HashMap<>(); |
| 55 | JsonNode protocol = node.get(name); |
| 56 | protocol.fieldNames().forEachRemaining(info -> configMap.put(info, protocol.get(info).asText())); |
| 57 | |
| 58 | String deviceKeyId = ""; |
| 59 | if (protocol.has(DEVICEKEYID)) { |
| 60 | deviceKeyId = protocol.get(DEVICEKEYID).asText(""); |
| 61 | } |
| 62 | |
| 63 | deviceMap.put(name, new DeviceInfoConfig(configMap, deviceKeyId)); |
| 64 | }); |
| 65 | return deviceMap; |
| 66 | } |
| 67 | |
| 68 | } |