blob: 44ec8e4e5f130b9751c539e7b057066f9dafe26c [file] [log] [blame]
yjimmyycfcb0532016-07-11 16:03:48 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
yjimmyycfcb0532016-07-11 16:03:48 -07003 *
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.driver.extensions;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.Maps;
21import org.onlab.util.KryoNamespace;
22import org.onosproject.net.flow.AbstractExtension;
23import org.onosproject.net.flow.instructions.ExtensionTreatment;
24import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
25
26import java.util.Map;
27import java.util.Objects;
28
29/**
30 * Instruction for Oplink channel attenuation.
31 */
32public class OplinkAttenuation extends AbstractExtension implements ExtensionTreatment {
33 private static final String KEY_ATT = "attenuation";
34
35 private int attenuation;
36
37 private final KryoNamespace appKryo = new KryoNamespace.Builder()
38 .register(Map.class)
39 .build("OplinkAttenuation");
40
41 /**
42 * Creates new attenuation instruction.
43 * @param attenuation attenuation value
44 */
45 public OplinkAttenuation(int attenuation) {
46 this.attenuation = attenuation;
47 }
48
49 /**
50 * Gets the attenuation value.
51 * @return attenuation
52 */
53 public int getAttenuation() {
54 return attenuation;
55 }
56
57 /**
58 * Modify the attenuation value.
59 * @param attenuation new attenuation value
60 */
61 public void setAttenuation(int attenuation) {
62 this.attenuation = attenuation;
63 }
64
65 @Override
66 public ExtensionTreatmentType type() {
67 return ExtensionTreatmentType.ExtensionTreatmentTypes.OPLINK_ATTENUATION.type();
68 }
69
70 @Override
71 public byte[] serialize() {
72 Map<String, Object> values = Maps.newHashMap();
73 values.put(KEY_ATT, attenuation);
74 return appKryo.serialize(values);
75 }
76
77 @Override
78 public void deserialize(byte[] data) {
79 Map<String, Object> values = appKryo.deserialize(data);
80 attenuation = (int) values.get(KEY_ATT);
81 }
82
83 @Override
84 public int hashCode() {
85 return Objects.hash(attenuation);
86 }
87
88 @Override
89 public boolean equals(Object obj) {
90 if (this == obj) {
91 return true;
92 }
93 if (obj instanceof OplinkAttenuation) {
94 OplinkAttenuation that = (OplinkAttenuation) obj;
95 return Objects.equals(attenuation, that.attenuation);
96 }
97 return false;
98 }
99
100 @Override
101 public String toString() {
102 return MoreObjects.toStringHelper(getClass())
103 .add(KEY_ATT, attenuation)
104 .toString();
105 }
106}