Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [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 | package org.onosproject.net.device.impl; |
| 17 | |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 18 | import java.util.HashMap; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 19 | import java.util.Map; |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 20 | import java.util.Optional; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 21 | |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | import org.onosproject.net.DefaultAnnotations.Builder; |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 25 | import org.onosproject.net.config.Config; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.PortConfigOperator; |
| 28 | import org.onosproject.net.config.basics.PortAnnotationConfig; |
| 29 | import org.onosproject.net.device.DefaultPortDescription; |
| 30 | import org.onosproject.net.device.PortDescription; |
| 31 | |
| 32 | /** |
| 33 | * Implementations of {@link PortConfigOperator} to weave |
| 34 | * annotations added via {@link PortAnnotationConfig}. |
| 35 | */ |
| 36 | public class PortAnnotationOperator implements PortConfigOperator { |
| 37 | |
| 38 | private NetworkConfigService networkConfigService; |
| 39 | |
| 40 | /** |
| 41 | * Creates {@link PortAnnotationOperator} instance. |
| 42 | */ |
| 43 | public PortAnnotationOperator() { |
| 44 | } |
| 45 | |
| 46 | PortAnnotationOperator(NetworkConfigService networkConfigService) { |
| 47 | bindService(networkConfigService); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void bindService(NetworkConfigService networkConfigService) { |
| 52 | this.networkConfigService = networkConfigService; |
| 53 | } |
| 54 | |
| 55 | private PortAnnotationConfig lookupConfig(ConnectPoint cp) { |
| 56 | if (networkConfigService == null) { |
| 57 | return null; |
| 58 | } |
| 59 | return networkConfigService.getConfig(cp, PortAnnotationConfig.class); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public PortDescription combine(ConnectPoint cp, PortDescription descr) { |
| 64 | PortAnnotationConfig cfg = lookupConfig(cp); |
| 65 | if (cfg == null) { |
| 66 | return descr; |
| 67 | } |
| 68 | Map<String, String> annotations = cfg.annotations(); |
| 69 | if (annotations.isEmpty()) { |
| 70 | return descr; |
| 71 | } |
| 72 | |
| 73 | Builder builder = DefaultAnnotations.builder(); |
| 74 | builder.putAll(descr.annotations()); |
| 75 | builder.putAll(annotations); |
| 76 | |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 77 | return DefaultPortDescription.builder(descr) |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 78 | .annotations(builder.build()) |
| 79 | .build(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public PortDescription combine(ConnectPoint cp, PortDescription descr, |
| 84 | Optional<Config> prevConfig) { |
| 85 | PortAnnotationConfig cfg = lookupConfig(cp); |
| 86 | Map<String, String> annotations = new HashMap<>(); |
| 87 | if (cfg != null) { |
| 88 | annotations.putAll(cfg.annotations()); |
| 89 | } |
| 90 | |
| 91 | Builder builder = DefaultAnnotations.builder(); |
| 92 | builder.putAll(descr.annotations()); |
| 93 | if (prevConfig.isPresent()) { |
| 94 | PortAnnotationConfig prevDeviceAnnotationConfig = (PortAnnotationConfig) prevConfig.get(); |
| 95 | for (String key : prevDeviceAnnotationConfig.annotations().keySet()) { |
| 96 | if (!annotations.containsKey(key)) { |
| 97 | builder.remove(key); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | builder.putAll(annotations); |
| 102 | return DefaultPortDescription.builder(descr) |
| 103 | .annotations(builder.build()) |
| 104 | .build(); |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } |