Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.net.device.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.DefaultAnnotations.Builder; |
| 23 | import org.onosproject.net.config.NetworkConfigService; |
| 24 | import org.onosproject.net.config.PortConfigOperator; |
| 25 | import org.onosproject.net.config.basics.PortAnnotationConfig; |
| 26 | import org.onosproject.net.device.DefaultPortDescription; |
| 27 | import org.onosproject.net.device.PortDescription; |
| 28 | |
| 29 | /** |
| 30 | * Implementations of {@link PortConfigOperator} to weave |
| 31 | * annotations added via {@link PortAnnotationConfig}. |
| 32 | */ |
| 33 | public class PortAnnotationOperator implements PortConfigOperator { |
| 34 | |
| 35 | private NetworkConfigService networkConfigService; |
| 36 | |
| 37 | /** |
| 38 | * Creates {@link PortAnnotationOperator} instance. |
| 39 | */ |
| 40 | public PortAnnotationOperator() { |
| 41 | } |
| 42 | |
| 43 | PortAnnotationOperator(NetworkConfigService networkConfigService) { |
| 44 | bindService(networkConfigService); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public void bindService(NetworkConfigService networkConfigService) { |
| 49 | this.networkConfigService = networkConfigService; |
| 50 | } |
| 51 | |
| 52 | private PortAnnotationConfig lookupConfig(ConnectPoint cp) { |
| 53 | if (networkConfigService == null) { |
| 54 | return null; |
| 55 | } |
| 56 | return networkConfigService.getConfig(cp, PortAnnotationConfig.class); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public PortDescription combine(ConnectPoint cp, PortDescription descr) { |
| 61 | PortAnnotationConfig cfg = lookupConfig(cp); |
| 62 | if (cfg == null) { |
| 63 | return descr; |
| 64 | } |
| 65 | Map<String, String> annotations = cfg.annotations(); |
| 66 | if (annotations.isEmpty()) { |
| 67 | return descr; |
| 68 | } |
| 69 | |
| 70 | Builder builder = DefaultAnnotations.builder(); |
| 71 | builder.putAll(descr.annotations()); |
| 72 | builder.putAll(annotations); |
| 73 | |
| 74 | return DefaultPortDescription.copyReplacingAnnotation(descr, builder.build()); |
| 75 | } |
| 76 | |
| 77 | } |