blob: 575d590ab3b478352edcf593d01d35ffd6c84db4 [file] [log] [blame]
Yi Tseng0b809722017-11-03 10:23:26 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16
17package org.onosproject.pipelines.fabric.pipeliner;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.ImmutableList;
21import org.onosproject.net.flow.FlowRule;
22import org.onosproject.net.flowobjective.ObjectiveError;
23import org.onosproject.net.group.GroupDescription;
24
25import java.util.Collection;
26import java.util.Objects;
27import java.util.Optional;
28
29/**
30 * Translation results from fabric pipeliner.
31 */
32public final class PipelinerTranslationResult {
33 private Collection<FlowRule> flowRules;
34 private Collection<GroupDescription> groups;
35 private ObjectiveError error;
36
37 private PipelinerTranslationResult(Collection<FlowRule> flowRules,
38 Collection<GroupDescription> groups,
39 ObjectiveError error) {
40 this.flowRules = flowRules;
41 this.groups = groups;
42 this.error = error;
43 }
44
45 /**
46 * Gets flow rules from result.
47 *
48 * @return flow rules
49 */
50 public Collection<FlowRule> flowRules() {
51 return flowRules;
52 }
53
54 /**
55 * Gets groups from result.
56 *
57 * @return groups
58 */
59 public Collection<GroupDescription> groups() {
60 return groups;
61 }
62
63 /**
64 * Gets error from result.
65 *
66 * @return error of the result; empty if there is no error
67 */
68 public Optional<ObjectiveError> error() {
69 return Optional.ofNullable(error);
70 }
71
72 /**
73 * Creates a new builder.
74 *
75 * @return the builder
76 */
77 public static Builder builder() {
78 return new Builder();
79 }
80
81 @Override
82 public String toString() {
83 return MoreObjects.toStringHelper(this)
84 .add("flowRules", flowRules)
85 .add("groups", groups)
86 .add("error", error)
87 .toString();
88 }
89
90 @Override
91 public int hashCode() {
92 return Objects.hash(flowRules, groups, error);
93 }
94
95 @Override
96 public boolean equals(Object obj) {
97 if (this == obj) {
98 return true;
99 }
100 if (obj == null || getClass() != obj.getClass()) {
101 return false;
102 }
103 final PipelinerTranslationResult other = (PipelinerTranslationResult) obj;
104 return Objects.equals(this.flowRules, other.flowRules)
105 && Objects.equals(this.groups, other.groups)
106 && Objects.equals(this.error, other.error);
107 }
108
109 /**
110 * Builder for PipelinerTranslationResult.
111 */
112 public static final class Builder {
113 private ImmutableList.Builder<FlowRule> flowRules = ImmutableList.builder();
114 private ImmutableList.Builder<GroupDescription> groups = ImmutableList.builder();
115 private ObjectiveError error = null;
116
117 // Hide default constructor
118 private Builder() {
119 }
120
121 /**
122 * Adds flow rule to the result.
123 *
124 * @param flowRule the flow rule
125 */
126 public void addFlowRule(FlowRule flowRule) {
127 flowRules.add(flowRule);
128 }
129
130 /**
131 * Adds group to the result.
132 *
133 * @param group the group
134 */
135 public void addGroup(GroupDescription group) {
136 groups.add(group);
137 }
138
139 /**
140 * Sets objective error to the result.
141 *
142 * @param error the error
143 */
144 public void setError(ObjectiveError error) {
145 this.error = error;
146 }
147
148 public PipelinerTranslationResult build() {
149 return new PipelinerTranslationResult(flowRules.build(),
150 groups.build(),
151 error);
152 }
153 }
154}