Add new revision for pcep types model
[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
27 /**
28  * Parser for {@link PathKey}
29  */
30 public final class PCEPPathKeyObjectParser extends AbstractEROWithSubobjectsParser {
31
32     private static final int CLASS = 16;
33     private static final int TYPE = 1;
34
35     public PCEPPathKeyObjectParser(final EROSubobjectRegistry subReg) {
36         super(subReg, CLASS, TYPE);
37     }
38
39     @Override
40     public PathKey parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
41         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
42             "Array of bytes is mandatory. Can't be null or empty.");
43         final PathKeyBuilder builder = new PathKeyBuilder();
44         builder.setIgnore(header.isIgnore());
45         builder.setProcessingRule(header.isProcessingRule());
46         final List<PathKeys> pk = new ArrayList<>();
47         final List<Subobject> subs = parseSubobjects(bytes);
48         for (final Subobject s : subs) {
49             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit.route
50                 .subobjects.subobject.type.PathKeyCase k = (org.opendaylight.yang.gen.v1.urn.opendaylight.params
51                 .xml.ns.yang.rsvp.rev150820.explicit.route.subobjects.subobject.type.PathKeyCase) s.getSubobjectType();
52             pk.add(new PathKeysBuilder().setLoose(s.isLoose()).setPceId(k.getPathKey().getPceId())
53                 .setPathKey(k.getPathKey().getPathKey()).build());
54         }
55         builder.setPathKeys(pk);
56         return builder.build();
57     }
58
59     @Override
60     public void serializeObject(final Object object, final ByteBuf buffer) {
61         Preconditions.checkArgument(object instanceof PathKey,
62             "Wrong instance of PCEPObject. Passed %s. Needed PathKeyObject.", object.getClass());
63         final PathKey pkey = (PathKey) object;
64         final ByteBuf body = Unpooled.buffer();
65         final List<PathKeys> pk = pkey.getPathKeys();
66         final List<Subobject> subs = new ArrayList<>();
67         for (final PathKeys p : pk) {
68             subs.add(new SubobjectBuilder().setLoose(p.isLoose()).setSubobjectType(
69                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.explicit
70                     .route.subobjects.subobject.type.PathKeyCaseBuilder().setPathKey(
71                     new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820
72                         .explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder().setPathKey(p
73                         .getPathKey()).setPceId(p.getPceId()).build()).build()).build());
74         }
75         serializeSubobject(subs, body);
76         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
77     }
78 }