blob: 37ac0661b7e35a257156733f7cfae31027284ad5 [file] [log] [blame]
Ayaka Koshibecc260d22015-08-04 17:13:38 -07001/*
2 * Copyright 2014-2015 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 */
16package org.onosproject.net.link.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.time.Duration;
21
22import org.onosproject.net.AnnotationKeys;
23import org.onosproject.incubator.net.config.basics.BasicLinkConfig;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.Link;
26import org.onosproject.net.SparseAnnotations;
27import org.onosproject.net.link.DefaultLinkDescription;
28import org.onosproject.net.link.LinkDescription;
29import org.slf4j.Logger;
30
31/**
32 * Implementations of merge policies for various sources of link configuration
33 * information. This includes applications, provides, and network configurations.
34 */
35public final class BasicLinkOperator {
36
37 private static final Logger log = getLogger(BasicLinkOperator.class);
38
39 private BasicLinkOperator() {
40 }
41
42 /**
43 * Generates a LinkDescription containing fields from a LinkDescription and
44 * a LinkConfig.
45 *
46 * @param cfg the link config entity from network config
47 * @param descr a LinkDescription
48 * @return LinkDescription based on both sources
49 */
50 public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) {
51 if (cfg == null) {
52 return descr;
53 }
54
55 // cfg.type() defaults to DIRECT, so there is a risk of unwanted override.
56 // do we want this behavior?
57 Link.Type type = descr.type();
58 if (cfg.type() != type) {
59 type = cfg.type();
60 }
61
62 SparseAnnotations sa = combine(cfg, descr.annotations());
63 return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa);
64 }
65
66 /**
67 * Generates an annotation from an existing annotation and LinkConfig.
68 *
69 * @param cfg the link config entity from network config
70 * @param an the annotation
71 * @return annotation combining both sources
72 */
73 public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) {
74 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
75 if (cfg.latency() != Duration.ofNanos(-1)) {
76 b.set(AnnotationKeys.LATENCY, cfg.latency().toString());
77 }
78 if (cfg.bandwidth() != -1) {
79 b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth()));
80 }
81 return DefaultAnnotations.union(an, b.build());
82 }
83}