Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.net.device.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.Optional; |
| 20 | |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.DefaultAnnotations.Builder; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.config.Config; |
| 25 | import org.onosproject.net.config.DeviceConfigOperator; |
| 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.basics.DeviceAnnotationConfig; |
| 28 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 29 | import org.onosproject.net.device.DeviceDescription; |
| 30 | |
| 31 | /** |
| 32 | * Implementations of {@link DeviceConfigOperator} to weave |
| 33 | * annotations added via {@link DeviceAnnotationConfig}. |
| 34 | */ |
| 35 | public class DeviceAnnotationOperator implements DeviceConfigOperator { |
| 36 | |
| 37 | private NetworkConfigService networkConfigService; |
| 38 | |
| 39 | /** |
| 40 | * Creates {@link DeviceAnnotationOperator} instance. |
| 41 | */ |
| 42 | public DeviceAnnotationOperator() { |
| 43 | } |
| 44 | |
| 45 | DeviceAnnotationOperator(NetworkConfigService networkConfigService) { |
| 46 | bindService(networkConfigService); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void bindService(NetworkConfigService networkConfigService) { |
| 51 | this.networkConfigService = networkConfigService; |
| 52 | } |
| 53 | |
| 54 | private DeviceAnnotationConfig lookupConfig(DeviceId deviceId) { |
| 55 | if (networkConfigService == null) { |
| 56 | return null; |
| 57 | } |
| 58 | return networkConfigService.getConfig(deviceId, DeviceAnnotationConfig.class); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public DeviceDescription combine(DeviceId deviceId, DeviceDescription descr, |
| 63 | Optional<Config> prevConfig) { |
| 64 | DeviceAnnotationConfig cfg = lookupConfig(deviceId); |
| 65 | if (cfg == null) { |
| 66 | return descr; |
| 67 | } |
| 68 | Map<String, String> annotations = cfg.annotations(); |
| 69 | |
| 70 | Builder builder = DefaultAnnotations.builder(); |
| 71 | builder.putAll(descr.annotations()); |
| 72 | if (prevConfig.isPresent()) { |
| 73 | DeviceAnnotationConfig prevDeviceAnnotationConfig = (DeviceAnnotationConfig) prevConfig.get(); |
| 74 | for (String key : prevDeviceAnnotationConfig.annotations().keySet()) { |
| 75 | if (!annotations.containsKey(key)) { |
| 76 | builder.remove(key); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | builder.putAll(annotations); |
| 81 | |
| 82 | return DefaultDeviceDescription.copyReplacingAnnotation(descr, builder.build()); |
| 83 | } |
| 84 | |
| 85 | } |