blob: 801092f4b52d9df37408046994c8960d273a0a58 [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;
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070019import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibecc260d22015-08-04 17:13:38 -070020
21import java.time.Duration;
22
23import org.onosproject.net.AnnotationKeys;
Ray Milkeya4122362015-08-18 15:19:08 -070024import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070025import org.onosproject.net.config.basics.BasicLinkConfig;
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070026import org.onosproject.net.ConnectPoint;
Ayaka Koshibecc260d22015-08-04 17:13:38 -070027import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.Link;
29import org.onosproject.net.SparseAnnotations;
30import org.onosproject.net.link.DefaultLinkDescription;
31import org.onosproject.net.link.LinkDescription;
32import org.slf4j.Logger;
33
34/**
35 * Implementations of merge policies for various sources of link configuration
36 * information. This includes applications, provides, and network configurations.
37 */
Ayaka Koshibe5373e762015-08-06 12:31:44 -070038public final class BasicLinkOperator implements ConfigOperator {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070039
Ayaka Koshibe5373e762015-08-06 12:31:44 -070040 private static final long DEF_BANDWIDTH = -1L;
41 private static final Duration DEF_DURATION = Duration.ofNanos(-1L);
Ayaka Koshibecc260d22015-08-04 17:13:38 -070042 private static final Logger log = getLogger(BasicLinkOperator.class);
43
44 private BasicLinkOperator() {
45 }
46
47 /**
48 * Generates a LinkDescription containing fields from a LinkDescription and
49 * a LinkConfig.
50 *
51 * @param cfg the link config entity from network config
52 * @param descr a LinkDescription
53 * @return LinkDescription based on both sources
54 */
55 public static LinkDescription combine(BasicLinkConfig cfg, LinkDescription descr) {
56 if (cfg == null) {
57 return descr;
58 }
59
60 // cfg.type() defaults to DIRECT, so there is a risk of unwanted override.
61 // do we want this behavior?
62 Link.Type type = descr.type();
63 if (cfg.type() != type) {
64 type = cfg.type();
65 }
66
67 SparseAnnotations sa = combine(cfg, descr.annotations());
68 return new DefaultLinkDescription(descr.src(), descr.dst(), type, sa);
69 }
70
71 /**
72 * Generates an annotation from an existing annotation and LinkConfig.
73 *
74 * @param cfg the link config entity from network config
75 * @param an the annotation
76 * @return annotation combining both sources
77 */
78 public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) {
79 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
Ayaka Koshibe5373e762015-08-06 12:31:44 -070080 if (cfg.latency() != DEF_DURATION) {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070081 b.set(AnnotationKeys.LATENCY, cfg.latency().toString());
82 }
Ayaka Koshibe5373e762015-08-06 12:31:44 -070083 if (cfg.bandwidth() != DEF_BANDWIDTH) {
Ayaka Koshibecc260d22015-08-04 17:13:38 -070084 b.set(AnnotationKeys.BANDWIDTH, String.valueOf(cfg.bandwidth()));
85 }
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070086 if (cfg.isDurable() != null) {
87 b.set(AnnotationKeys.DURABLE, String.valueOf(cfg.isDurable()));
88 }
Ayaka Koshibecc260d22015-08-04 17:13:38 -070089 return DefaultAnnotations.union(an, b.build());
90 }
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070091
92 /**
93 * Generates a link description from a link description entity. The endpoints
94 * must be specified to indicate directionality.
95 *
96 * @param src the source ConnectPoint
97 * @param dst the destination ConnectPoint
98 * @param link the link config entity
99 * @return a linkDescription based on the config
100 */
101 public static LinkDescription descriptionOf(
102 ConnectPoint src, ConnectPoint dst, Link link) {
103 checkNotNull(src, "Must supply a source endpoint");
104 checkNotNull(dst, "Must supply a destination endpoint");
105 checkNotNull(link, "Must supply a link");
106 return new DefaultLinkDescription(
107 src, dst, link.type(), (SparseAnnotations) link.annotations());
108 }
109
110 /**
111 * Generates a link description from a link config entity. This is for
112 * links that cannot be discovered and has to be injected. The endpoints
113 * must be specified to indicate directionality.
114 *
115 * @param src the source ConnectPoint
116 * @param dst the destination ConnectPoint
117 * @param link the link config entity
118 * @return a linkDescription based on the config
119 */
120 public static LinkDescription descriptionOf(
121 ConnectPoint src, ConnectPoint dst, BasicLinkConfig link) {
122 checkNotNull(src, "Must supply a source endpoint");
123 checkNotNull(dst, "Must supply a destination endpoint");
124 checkNotNull(link, "Must supply a link config");
125 return new DefaultLinkDescription(
126 src, dst, link.type(), combine(link, DefaultAnnotations.EMPTY));
127 }
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700128}