Optimize AsPath lists
[bgpcep.git] / pcep / segment-routing / src / test / java / org / opendaylight / protocol / pcep / segment / routing02 / SrEroUtilTest.java
1 /*
2  * Copyright (c) 2014 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.pcep.segment.routing02;
10
11 import com.google.common.collect.Lists;
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.Collections;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.SrEroSubobject.Flags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing._02.rev140506.pcinitiate.pcinitiate.message.requests.ero.subobject.subobject.type.SrEroTypeBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.basic.explicit.route.subobjects.subobject.type.IpPrefixCaseBuilder;
26
27 public class SrEroUtilTest {
28
29     @Test(expected=UnsupportedOperationException.class)
30     public void testPrivateConstructor() throws Throwable {
31         final Constructor<SrEroUtil> c = SrEroUtil.class.getDeclaredConstructor();
32         c.setAccessible(true);
33         try {
34             c.newInstance();
35         } catch (InvocationTargetException e) {
36             throw e.getCause();
37         }
38     }
39
40     @Test
41     public void testIsSegmentRoutingPath() {
42         Assert.assertTrue(SrEroUtil.isSegmentRoutingPath(createEro(Lists.newArrayList(createSRSubobject()))));
43         Assert.assertFalse(SrEroUtil.isSegmentRoutingPath(createEro(Collections.<Subobject>emptyList())));
44         Assert.assertFalse(SrEroUtil.isSegmentRoutingPath(createEro(null)));
45         Assert.assertFalse(SrEroUtil.isSegmentRoutingPath(createEro(Lists.newArrayList(createIpPrefixSubobject()))));
46     }
47
48     @Test
49     public void testValidateSrEroSubobjects() {
50         Assert.assertNull(SrEroUtil.validateSrEroSubobjects(createEro(Lists.newArrayList(createSRSubobject()))));
51         Assert.assertNull(SrEroUtil.validateSrEroSubobjects(createEro(Collections.<Subobject>emptyList())));
52         Assert.assertNull(SrEroUtil.validateSrEroSubobjects(createEro(null)));
53         Assert.assertNull(SrEroUtil.validateSrEroSubobjects(createEro(Lists.newArrayList(createSRSubobject(20L, true)))));
54         Assert.assertNull(SrEroUtil.validateSrEroSubobjects(createEro(Lists.newArrayList(createSRSubobject(20L, false)))));
55         Assert.assertNull(SrEroUtil.validateSrEroSubobjects(createEro(Lists.newArrayList(createSRSubobject(10L, false)))));
56         Assert.assertEquals(PCEPErrors.BAD_LABEL_VALUE, SrEroUtil.validateSrEroSubobjects(createEro(Lists.newArrayList(createSRSubobject(10L, true)))));
57         Assert.assertEquals(PCEPErrors.NON_IDENTICAL_ERO_SUBOBJECTS,
58                 SrEroUtil.validateSrEroSubobjects(createEro(Lists.newArrayList(createIpPrefixSubobject()))));
59     }
60
61     private Ero createEro(final List<Subobject> subobejcts) {
62         return new EroBuilder().setSubobject(subobejcts).build();
63     }
64
65     private Subobject createSRSubobject() {
66         final SubobjectBuilder builder = new SubobjectBuilder();
67         builder.setSubobjectType(new SrEroTypeBuilder().build());
68         return builder.build();
69     }
70
71     private Subobject createSRSubobject(final long sid, final boolean isM) {
72         final SubobjectBuilder builder = new SubobjectBuilder();
73         builder.setSubobjectType(new SrEroTypeBuilder().setFlags(new Flags(false, false, isM, false)).setSid(sid).build());
74         return builder.build();
75     }
76
77     private Subobject createIpPrefixSubobject() {
78         final SubobjectBuilder builder = new SubobjectBuilder();
79         builder.setSubobjectType(new IpPrefixCaseBuilder().build());
80         return builder.build();
81     }
82 }