Bump MDSAL to 4.0.0
[bgpcep.git] / rsvp / spi / src / main / java / org / opendaylight / protocol / rsvp / parser / spi / pojo / SimpleRSVPObjectRegistry.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.rsvp.parser.spi.pojo;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.HashBasedTable;
13 import com.google.common.collect.Table;
14 import io.netty.buffer.ByteBuf;
15 import java.util.HashMap;
16 import java.util.Map;
17 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
18 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectParser;
19 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectRegistry;
20 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectSerializer;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
23
24 public final class SimpleRSVPObjectRegistry implements RSVPTeObjectRegistry {
25
26     private final Table<Integer, Integer, RSVPTeObjectParser> parserHandler = HashBasedTable.create();
27     private final Map<Class<? extends RsvpTeObject>, RSVPTeObjectSerializer> serializerHandler = new HashMap<>();
28
29     public void registerRsvpObjectParser(final int classNum, final int ctype, final RSVPTeObjectParser parser) {
30         Preconditions.checkArgument(classNum >= 0 && classNum <= Values.UNSIGNED_BYTE_MAX_VALUE);
31         Preconditions.checkArgument(ctype >= 0 && ctype <= Values.UNSIGNED_BYTE_MAX_VALUE);
32         this.parserHandler.put(classNum, ctype, parser);
33     }
34
35     public void registerRsvpObjectSerializer(final Class<? extends RsvpTeObject> objectClass,
36         final RSVPTeObjectSerializer serializer) {
37         this.serializerHandler.put(objectClass, serializer);
38     }
39
40     @Override
41     public RsvpTeObject parseRSPVTe(final int classNum, final int ctype, final ByteBuf buffer)
42         throws RSVPParsingException {
43         final RSVPTeObjectParser parser = this.parserHandler.get(classNum, ctype);
44         if (parser == null) {
45             return null;
46         }
47         return parser.parseObject(buffer);
48     }
49
50     @Override
51     public void serializeRSPVTe(final RsvpTeObject parameter, final ByteBuf bytes) {
52         if (parameter == null) {
53             return;
54         }
55         final RSVPTeObjectSerializer serializer = this.serializerHandler.get(parameter.implementedInterface());
56         if (serializer == null) {
57             return;
58         }
59         serializer.serializeObject(parameter, bytes);
60     }
61 }