b7561fdef9f5593791ffb352829014e26095ec3a
[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.getLoose()).setPceId(pkc.getPathKey().getPceId())
49                 .setPathKey(pkc.getPathKey().getPathKey()).build());
50         }
51         return new PathKeyBuilder()
52                 .setIgnore(header.getIgnore())
53                 .setProcessingRule(header.getProcessingRule())
54                 .setPathKeys(pk)
55                 .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<Subobject> subs = new ArrayList<>();
65         for (final PathKeys pk : pkey.nonnullPathKeys()) {
66             subs.add(new SubobjectBuilder()
67                     .setLoose(pk.getLoose())
68                     .setSubobjectType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp
69                         .rev150820.explicit.route.subobjects.subobject.type.PathKeyCaseBuilder()
70                         .setPathKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp
71                             .rev150820.explicit.route.subobjects.subobject.type.path.key._case.PathKeyBuilder()
72                             .setPathKey(pk.getPathKey())
73                             .setPceId(pk.getPceId())
74                             .build())
75                         .build())
76                     .build());
77         }
78         serializeSubobject(subs, body);
79         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
80     }
81 }