blob: ac9ea84fe7bb3df148b8f5b17a6993c166bf4fc8 [file] [log] [blame]
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -08003 *
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 */
16package org.onosproject.net.device.impl;
17
18import java.util.Map;
19
20import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.DefaultAnnotations.Builder;
23import org.onosproject.net.config.NetworkConfigService;
24import org.onosproject.net.config.PortConfigOperator;
25import org.onosproject.net.config.basics.PortAnnotationConfig;
26import org.onosproject.net.device.DefaultPortDescription;
27import org.onosproject.net.device.PortDescription;
28
29/**
30 * Implementations of {@link PortConfigOperator} to weave
31 * annotations added via {@link PortAnnotationConfig}.
32 */
33public 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
Yuta HIGUCHI53e47962018-03-01 23:50:48 -080074 return DefaultPortDescription.builder(descr)
75 .annotations(builder.build())
76 .build();
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080077 }
78
79}