Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPPathKeyObjectParser.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.parser.object;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.protocol.pcep.spi.EROSubobjectRegistry;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.Subobject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.explicit.route.object.ero.SubobjectBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.key.object.PathKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.key.object.PathKeyBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.key.object.path.key.PathKeys;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.path.key.object.path.key.PathKeysBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase;
27
28 /**
29  * Parser for {@link PathKey}.
30  */
31 public final class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser {
32
33     private static final int CLASS = 16;
34     private static final int TYPE = 1;
35
36     public PCEPPathKeyObjectParser(final EROSubobjectRegistry subReg) {
37         super(subReg, CLASS, TYPE);
38     }
39
40     @Override
41     public PathKey parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
42         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
43             "Array of bytes is mandatory. Can't be null or empty.");
44         final List<PathKeys> pk = new ArrayList<>();
45         final List<Subobject> subs = parseSubobjects(bytes);
46         for (final Subobject sub : subs) {
47             final PathKeyCase pkc = (PathKeyCase) sub.getSubobjectType();
48             pk.add(new PathKeysBuilder().setLoose(sub.isLoose()).setPceId(pkc.getPathKey().getPceId())
49                 .setPathKey(pkc.getPathKey().getPathKey()).build());
50         }
51         final PathKeyBuilder builder = new PathKeyBuilder()
52                 .setIgnore(header.isIgnore())
53                 .setProcessingRule(header.isProcessingRule())
54                 .setPathKeys(pk);
55         return builder.build();
56     }
57
58     @Override
59     public void serializeObject(final Object object, final ByteBuf buffer) {
60         Preconditions.checkArgument(object instanceof PathKey,
61             "Wrong instance of PCEPObject. Passed %s. Needed PathKeyObject.", object.getClass());
62         final PathKey pkey = (PathKey) object;
63         final ByteBuf body = Unpooled.buffer();
64         final List<PathKeys> pks = pkey.getPathKeys();
65         final List<Subobject> subs = new ArrayList<>();
66         for (final PathKeys pk : pks) {
67             subs.add(new SubobjectBuilder()
68                     .setLoose(pk.isLoose())
69                     .setSubobjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp
70                         .rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder()
71                         .setPathKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp
72                             .rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder()
73                             .setPathKey(pk.getPathKey())
74                             .setPceId(pk.getPceId())
75                             .build())
76                         .build())
77                     .build());
78         }
79         serializeSubobject(subs, body);
80         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
81     }
82 }