blob: 700325f9378c3edcfdbeb4b65a08bb951e300867 [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
debmaiti9553ed72019-03-18 14:27:42 +053018import java.util.HashMap;
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080019import java.util.Map;
debmaiti9553ed72019-03-18 14:27:42 +053020import java.util.Optional;
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080021
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.DefaultAnnotations.Builder;
debmaiti9553ed72019-03-18 14:27:42 +053025import org.onosproject.net.config.Config;
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080026import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.PortConfigOperator;
28import org.onosproject.net.config.basics.PortAnnotationConfig;
29import org.onosproject.net.device.DefaultPortDescription;
30import org.onosproject.net.device.PortDescription;
31
32/**
33 * Implementations of {@link PortConfigOperator} to weave
34 * annotations added via {@link PortAnnotationConfig}.
35 */
36public 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 HIGUCHI53e47962018-03-01 23:50:48 -080077 return DefaultPortDescription.builder(descr)
debmaiti9553ed72019-03-18 14:27:42 +053078 .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 HIGUCHI7438f5a2017-02-15 22:09:46 -0800105 }
106
107}