Enforce pcep-spi checkstyle
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / AbstractMessageParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.pcep.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
18 import javax.annotation.Nullable;
19 import org.opendaylight.protocol.util.BitArray;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.PcerrBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Message;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObjectBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.PcerrMessageBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.ErrorsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.RequestCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.request._case.RequestBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.error.type.request._case.request.RpsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.rp.object.Rp;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
34
35 public abstract class AbstractMessageParser implements MessageParser, MessageSerializer {
36
37     private static final int COMMON_OBJECT_HEADER_LENGTH = 4;
38
39     private static final int OT_SF_LENGTH = 4;
40     /*
41      * offsets of fields inside of multi-field in bits
42      */
43     private static final int OT_SF_OFFSET = 0;
44     /*
45      * flags offsets inside multi-filed
46      */
47     private static final int PROCESSED = 6;
48     private static final int IGNORED = 7;
49
50     private final ObjectRegistry registry;
51
52     protected AbstractMessageParser(final ObjectRegistry registry) {
53         this.registry = requireNonNull(registry);
54     }
55
56     /**
57      * Calls registry to pick up specific object serializer for given object.
58      * Checks if the object is not null.
59      * @param object Object to be serialized, may be null
60      * @param buffer ByteBuf where the object should be serialized
61      */
62     protected void serializeObject(@Nullable final Object object, final ByteBuf buffer) {
63         if (object == null) {
64             return;
65         }
66         this.registry.serializeObject(object, buffer);
67     }
68
69     private List<Object> parseObjects(final ByteBuf bytes) throws PCEPDeserializerException {
70         final List<Object> objs = new ArrayList<>();
71         while (bytes.isReadable()) {
72             if (bytes.readableBytes() < COMMON_OBJECT_HEADER_LENGTH) {
73                 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + bytes.readableBytes()
74                     + " Expected: >= " + COMMON_OBJECT_HEADER_LENGTH + ".");
75             }
76             final int objClass = bytes.readUnsignedByte();
77
78             final byte flagsByte = bytes.readByte();
79             final BitArray flags = BitArray.valueOf(flagsByte);
80             final int objType = UnsignedBytes.toInt(ByteArray.copyBitsRange(flagsByte, OT_SF_OFFSET, OT_SF_LENGTH));
81             final int objLength = bytes.readUnsignedShort();
82
83             if (bytes.readableBytes() < objLength - COMMON_OBJECT_HEADER_LENGTH) {
84                 throw new PCEPDeserializerException("Too few bytes in passed array. Passed: " + bytes.readableBytes()
85                     + " Expected: >= " + objLength + ".");
86             }
87             // copy bytes for deeper parsing
88             final ByteBuf bytesToPass = bytes.readSlice(objLength - COMMON_OBJECT_HEADER_LENGTH);
89
90             final ObjectHeader header = new ObjectHeaderImpl(flags.get(PROCESSED), flags.get(IGNORED));
91
92             if (VendorInformationUtil.isVendorInformationObject(objClass, objType)) {
93                 final EnterpriseNumber enterpriseNumber = new EnterpriseNumber(bytesToPass.readUnsignedInt());
94                 final Optional<? extends Object> obj = this.registry.parseVendorInformationObject(enterpriseNumber,
95                     header, bytesToPass);
96                 if (obj.isPresent()) {
97                     objs.add(obj.get());
98                 }
99             } else {
100                 // parseObject is required to return null for P=0 errored objects
101                 final Object o = this.registry.parseObject(objClass, objType, header, bytesToPass);
102                 if (o != null) {
103                     objs.add(o);
104                 }
105             }
106         }
107
108         return objs;
109     }
110
111     public static Message createErrorMsg(final PCEPErrors err, final Optional<Rp> rp) {
112         final PcerrMessageBuilder msgBuilder = new PcerrMessageBuilder();
113         if (rp.isPresent()) {
114             msgBuilder.setErrorType(new RequestCaseBuilder().setRequest(new RequestBuilder().setRps(
115                 Collections.singletonList(new RpsBuilder().setRp(rp.get()).build())).build()).build());
116         }
117         return new PcerrBuilder().setPcerrMessage(
118                 msgBuilder.setErrors(Collections.singletonList(new ErrorsBuilder().setErrorObject(
119                     new ErrorObjectBuilder().setType(err.getErrorType()).setValue(
120                         err.getErrorValue()).build()).build())).build()).build();
121     }
122
123     protected abstract Message validate(List<Object> objects, List<Message> errors) throws PCEPDeserializerException;
124
125     @Override
126     public final Message parseMessage(final ByteBuf buffer, final List<Message> errors)
127             throws PCEPDeserializerException {
128         requireNonNull(buffer, "Buffer may not be null");
129
130         // Parse objects first
131         final List<Object> objs = parseObjects(buffer);
132
133         // Run validation
134         return validate(objs, errors);
135     }
136
137     protected final void serializeVendorInformationObjects(final List<VendorInformationObject> viObjects,
138             final ByteBuf buffer) {
139         if (viObjects != null) {
140             for (final VendorInformationObject viObject : viObjects) {
141                 this.registry.serializeVendorInformationObject(viObject, buffer);
142             }
143         }
144     }
145
146     protected static List<VendorInformationObject> addVendorInformationObjects(final List<Object> objects) {
147         final List<VendorInformationObject> vendorInfo = new ArrayList<>();
148         while (!objects.isEmpty() && objects.get(0) instanceof VendorInformationObject) {
149             final VendorInformationObject viObject = (VendorInformationObject) objects.get(0);
150             vendorInfo.add(viObject);
151             objects.remove(0);
152         }
153         return vendorInfo;
154     }
155 }