blob: a6b08f62e4a186fb8f73e0341e480bf2ebda8519 [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;
Ray Milkeya4122362015-08-18 15:19:08 -070023import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070024import org.onosproject.net.config.basics.BasicLinkConfig;
Ayaka Koshibecc260d22015-08-04 17:13:38 -070025import org.onosproject.net.DefaultAnnotations;
26import org.onosproject.net.Link;
27import org.onosproject.net.SparseAnnotations;
28import org.onosproject.net.link.DefaultLinkDescription;
29import org.onosproject.net.link.LinkDescription;
30import org.slf4j.Logger;
31
32/**
33 * Implementations of merge policies for various sources of link configuration
34 * information. This includes applications, provides, and network configurations.
35 */
Ayaka Koshibe5373e762015-08-06 12:31:44 -070036public final class BasicLinkOperator implements ConfigOperator {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070037
Ayaka Koshibe5373e762015-08-06 12:31:44 -070038 private static final long DEF_BANDWIDTH = -1L;
39 private static final Duration DEF_DURATION = Duration.ofNanos(-1L);
Ayaka Koshibecc260d22015-08-04 17:13:38 -070040 private static final Logger log = getLogger(BasicLinkOperator.class);
41
42 private BasicLinkOperator() {
43 }
44
45 /**
46 * Generates a LinkDescription containing fields from a LinkDescription and
47 * a LinkConfig.
48 *
49 * @param cfg the link config entity from network config
50 * @param descr a LinkDescription
51 * @return LinkDescription based on both sources
52 */
53 public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) {
54 if (cfg == null) {
55 return descr;
56 }
57
58 // cfg.type() defaults to DIRECT, so there is a risk of unwanted override.
59 // do we want this behavior?
60 Link.Type type = descr.type();
61 if (cfg.type() != type) {
62 type = cfg.type();
63 }
64
65 SparseAnnotations sa = combine(cfg, descr.annotations());
66 return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa);
67 }
68
69 /**
70 * Generates an annotation from an existing annotation and LinkConfig.
71 *
72 * @param cfg the link config entity from network config
73 * @param an the annotation
74 * @return annotation combining both sources
75 */
76 public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) {
77 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
Ayaka Koshibe5373e762015-08-06 12:31:44 -070078 if (cfg.latency() != DEF_DURATION) {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070079 b.set(AnnotationKeys.LATENCY, cfg.latency().toString());
80 }
Ayaka Koshibe5373e762015-08-06 12:31:44 -070081 if (cfg.bandwidth() != DEF_BANDWIDTH) {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070082 b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth()));
83 }
84 return DefaultAnnotations.union(an, b.build());
85 }
86}