blob: dd177b48498e9fe608a3b989f324b92b3a07cacd [file] [log] [blame]
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -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.pcepio.types;
18
19import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.pcepio.protocol.PcepVersion;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * Path Key SubObject: When a PCC needs to expand a path-key in order to expand a CPS, it
30 * issues a Path Computation Request (PCReq) to the PCE identified in
31 * the PKS in the RSVP-TE ERO that it is processing. The PCC supplies
32 * the PKS to be expanded in a PATH-KEY SubObject in the PCReq message.
33 */
34public class PathKeySubObject implements PcepValueType {
35
36 /*
37 Pathkey subobject(RFC 5520):
38 0 1 2 3
39 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 |L| Type | Length | Path-Key |
42 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 | PCE ID (4 bytes) |
44 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 */
46
Ray Milkey9c9cde42018-01-12 14:22:06 -080047 private static final Logger log = LoggerFactory.getLogger(PathKeySubObject.class);
Sho SHIMIZUe81e4db2015-09-03 09:44:38 -070048
49 public static final byte TYPE = 0x40;
50 public static final byte LENGTH = 8;
51 private final short pathKey;
52 private final int pceID;
53
54 /**
55 * Constructor for Path Key sub Object which initializes pathKey and pceId.
56 *
57 * @param pathKey path key provided by PCC
58 * @param pceID ID for the PCE
59 */
60 public PathKeySubObject(short pathKey, int pceID) {
61 this.pathKey = pathKey;
62 this.pceID = pceID;
63 }
64
65 /**
66 * Creates Path Key sub Object which initializes pathKey and pceId.
67 *
68 * @param pathKey path key provided by PCC
69 * @param pceID PCE id
70 * @return new object of type path key sub object
71 */
72 public static PathKeySubObject of(short pathKey, int pceID) {
73 return new PathKeySubObject(pathKey, pceID);
74 }
75
76 /**
77 * Returns Path Key.
78 *
79 * @return pathKey
80 */
81 public short getPathKey() {
82 return pathKey;
83 }
84
85 /**
86 * Returns pceID.
87 *
88 * @return pceID
89 */
90 public int getPceId() {
91 return pceID;
92 }
93
94 @Override
95 public PcepVersion getVersion() {
96 return PcepVersion.PCEP_1;
97 }
98
99 @Override
100 public short getType() {
101 return TYPE;
102 }
103
104 @Override
105 public short getLength() {
106 return LENGTH;
107 }
108
109 @Override
110 public int hashCode() {
111 return Objects.hash(pathKey, pceID);
112 }
113
114 @Override
115 public boolean equals(Object obj) {
116 if (this == obj) {
117 return true;
118 }
119 if (obj instanceof PathKeySubObject) {
120 PathKeySubObject other = (PathKeySubObject) obj;
121 return Objects.equals(this.pathKey, other.pathKey) && Objects.equals(this.pceID, other.pceID);
122 }
123 return false;
124 }
125
126 @Override
127 public int write(ChannelBuffer c) {
128 int iLenStartIndex = c.writerIndex();
129 c.writeShort(TYPE);
130 c.writeShort(LENGTH);
131
132 c.writeShort(pathKey);
133 c.writeInt(pceID);
134
135 return c.writerIndex() - iLenStartIndex;
136 }
137
138 /**
139 * Reads the channel buffer and returns new path key sub objects.
140 *
141 * @param c of type channel buffer
142 * @return object of type path key sub object
143 */
144 public static PcepValueType read(ChannelBuffer c) {
145 Short pathKey = c.readShort();
146 int pceID = c.readInt();
147 return new PathKeySubObject(pathKey, pceID);
148 }
149
150 @Override
151 public String toString() {
152 return MoreObjects.toStringHelper(getClass())
153 .add("Type", TYPE)
154 .add("Length", LENGTH)
155 .add("PathKey", pathKey)
156 .add("PceID", pceID)
157 .toString();
158 }
159}