blob: 7f794a7decd091ceb97ff74165f23d42b8c6e38e [file] [log] [blame]
Jian Lia0bf4592017-02-03 17:43:21 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lia0bf4592017-02-03 17:43:21 +09003 *
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.mapping;
17
Jian Li44155b02017-02-15 17:03:38 +090018import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import org.onosproject.mapping.actions.MappingAction;
22import org.onosproject.mapping.actions.MappingActions;
Jian Li136fd6c2017-02-10 14:54:59 +090023
24import java.util.List;
Jian Li44155b02017-02-15 17:03:38 +090025import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkNotNull;
Jian Li136fd6c2017-02-10 14:54:59 +090028
Jian Lia0bf4592017-02-03 17:43:21 +090029/**
30 * Default mapping value implementation.
31 */
Jian Li44155b02017-02-15 17:03:38 +090032public final class DefaultMappingValue implements MappingValue {
Jian Li136fd6c2017-02-10 14:54:59 +090033
Jian Li44155b02017-02-15 17:03:38 +090034 private final MappingAction action;
35 private final List<MappingTreatment> treatments;
36
37 /**
38 * Creates a new mapping value from the specified mapping action and a
39 * list of mapping treatments.
40 *
41 * @param action mapping action
42 * @param treatments a collection of mapping treatment
43 */
44 private DefaultMappingValue(MappingAction action,
45 List<MappingTreatment> treatments) {
46 this.action = action;
47 this.treatments = ImmutableList.copyOf(checkNotNull(treatments));
Jian Li136fd6c2017-02-10 14:54:59 +090048 }
49
50 @Override
Jian Li44155b02017-02-15 17:03:38 +090051 public MappingAction action() {
52 return action;
53 }
54
55 @Override
56 public List<MappingTreatment> treatments() {
57 return ImmutableList.copyOf(treatments);
58 }
59
60 @Override
61 public int hashCode() {
62 return Objects.hash(action, treatments);
63 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (this == obj) {
68 return true;
69 }
70 if (obj instanceof DefaultMappingValue) {
71 DefaultMappingValue that = (DefaultMappingValue) obj;
72 return Objects.equals(action, that.action) &&
73 Objects.equals(treatments, that.treatments);
74 }
75 return false;
76 }
77
78 @Override
79 public String toString() {
80 return MoreObjects.toStringHelper(getClass())
81 .add("action", action)
82 .add("treatments", treatments)
83 .toString();
84 }
85
86 /**
87 * Returns a new mapping value builder.
88 *
89 * @return mapping value builder
90 */
91 public static MappingValue.Builder builder() {
92 return new DefaultMappingValue.Builder();
93 }
94
95 /**
96 * Returns a new mapping value builder primed to produce entities
97 * patterned after the supplied mapping value.
98 *
99 * @param value base mapping value
100 * @return mapping value builder
101 */
102 public static MappingValue.Builder builder(MappingValue value) {
103 return new Builder(value);
104 }
105
106 /**
107 * Builds a mapping value.
108 */
109 public static final class Builder implements MappingValue.Builder {
110
111 private MappingAction action;
112 private List<MappingTreatment> treatments = Lists.newArrayList();
113
114 // creates a new builder
115 private Builder() {
116 }
117
118 // creates a new builder based off an existing mapping value
119 private Builder(MappingValue value) {
120 value.treatments().forEach(t -> treatments.add(t));
121 action = value.action();
122 }
123
124 @Override
125 public Builder withAction(MappingAction action) {
126 this.action = action;
127 return this;
128 }
129
130 @Override
131 public Builder add(MappingTreatment treatment) {
132 treatments.add(treatment);
133 return this;
134 }
135
136 @Override
137 public MappingValue build() {
138
139 // if no action has been specified, we simply assign noAction
140 if (action == null) {
141 action = MappingActions.noAction();
142 }
143
144 // FIXME: we will check the number of treatment later
145 // checkArgument(treatments.size() >= 1,
146 // "Must specify more than one mapping treatment");
147
148 return new DefaultMappingValue(action, treatments);
149 }
Jian Li136fd6c2017-02-10 14:54:59 +0900150 }
Jian Lia0bf4592017-02-03 17:43:21 +0900151}