blob: 3caa71c28d1d81ec5836d15cb7bd19187258e227 [file] [log] [blame]
Andrea Campanella432f7182017-07-14 18:43:27 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Andrea Campanella432f7182017-07-14 18:43:27 +02003 *
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 */
16
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Objects;
21import org.onlab.util.ImmutableByteSequence;
Carmelo Cascone87892e22017-11-13 16:01:29 -080022import org.onosproject.net.pi.model.PiControlMetadataId;
Andrea Campanella432f7182017-07-14 18:43:27 +020023
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
Carmelo Cascone87892e22017-11-13 16:01:29 -080027 * Instance of a control metadata for a protocol-independent pipeline.
Andrea Campanella432f7182017-07-14 18:43:27 +020028 */
29@Beta
Carmelo Cascone87892e22017-11-13 16:01:29 -080030public final class PiControlMetadata {
Andrea Campanella432f7182017-07-14 18:43:27 +020031
Carmelo Cascone87892e22017-11-13 16:01:29 -080032 private final PiControlMetadataId id;
Andrea Campanella432f7182017-07-14 18:43:27 +020033 private final ImmutableByteSequence value;
34
35 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -080036 * Creates a new control metadata instance for the given identifier and value.
Andrea Campanella432f7182017-07-14 18:43:27 +020037 *
Carmelo Cascone87892e22017-11-13 16:01:29 -080038 * @param id control metadata identifier
Andrea Campanella432f7182017-07-14 18:43:27 +020039 * @param value value for this metadata
40 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080041 private PiControlMetadata(PiControlMetadataId id, ImmutableByteSequence value) {
Andrea Campanella432f7182017-07-14 18:43:27 +020042 this.id = id;
43 this.value = value;
44 }
45
46 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -080047 * Return the identifier of this control metadata.
Andrea Campanella432f7182017-07-14 18:43:27 +020048 *
Carmelo Cascone87892e22017-11-13 16:01:29 -080049 * @return control metadata identifier
Andrea Campanella432f7182017-07-14 18:43:27 +020050 */
Carmelo Cascone87892e22017-11-13 16:01:29 -080051 public PiControlMetadataId id() {
Andrea Campanella432f7182017-07-14 18:43:27 +020052 return id;
53 }
54
55 /**
56 * Returns the value for the field in this metadata.
57 *
58 * @return value
59 */
60 public ImmutableByteSequence value() {
61 return value;
62 }
63
64 @Override
65 public boolean equals(Object o) {
66 if (this == o) {
67 return true;
68 }
69 if (o == null || getClass() != o.getClass()) {
70 return false;
71 }
Carmelo Cascone87892e22017-11-13 16:01:29 -080072 PiControlMetadata piPacket = (PiControlMetadata) o;
Andrea Campanella432f7182017-07-14 18:43:27 +020073 return Objects.equal(id, piPacket.id()) &&
74 Objects.equal(value, piPacket.value());
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hashCode(id, value);
80 }
81
82 @Override
83 public String toString() {
84 return this.id().toString() + " = " + value.toString();
85 }
86
87 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -080088 * Returns a control metadata builder.
Andrea Campanella432f7182017-07-14 18:43:27 +020089 *
90 * @return a new builder
91 */
92 public static Builder builder() {
93 return new Builder();
94 }
95
96 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -080097 * Builder of protocol-independent control metadatas.
Andrea Campanella432f7182017-07-14 18:43:27 +020098 */
99 public static final class Builder {
100
Carmelo Cascone87892e22017-11-13 16:01:29 -0800101 private PiControlMetadataId id;
Andrea Campanella432f7182017-07-14 18:43:27 +0200102 private ImmutableByteSequence value;
103
104 private Builder() {
105 // hides constructor.
106 }
107
108 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -0800109 * Sets the identifier of this control metadata.
Andrea Campanella432f7182017-07-14 18:43:27 +0200110 *
Carmelo Cascone87892e22017-11-13 16:01:29 -0800111 * @param id control metadata identifier
Andrea Campanella432f7182017-07-14 18:43:27 +0200112 * @return this
113 */
Carmelo Cascone87892e22017-11-13 16:01:29 -0800114 public Builder withId(PiControlMetadataId id) {
Andrea Campanella432f7182017-07-14 18:43:27 +0200115 this.id = id;
116 return this;
117 }
118
119 /**
120 * Sets the value of this metadata.
121 *
122 * @param value value of the metadata
123 * @return this
124 */
125 public Builder withValue(ImmutableByteSequence value) {
126 this.value = value;
127 return this;
128 }
129
130 /**
Carmelo Cascone87892e22017-11-13 16:01:29 -0800131 * Returns a new control metadata instance.
Andrea Campanella432f7182017-07-14 18:43:27 +0200132 *
Carmelo Cascone87892e22017-11-13 16:01:29 -0800133 * @return control metadata
Andrea Campanella432f7182017-07-14 18:43:27 +0200134 */
Carmelo Cascone87892e22017-11-13 16:01:29 -0800135 public PiControlMetadata build() {
Andrea Campanella432f7182017-07-14 18:43:27 +0200136 checkNotNull(id);
137 checkNotNull(value);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800138 return new PiControlMetadata(id, value);
Andrea Campanella432f7182017-07-14 18:43:27 +0200139 }
140 }
141}