490da5723f7c4d46fee57dde940e089426fdabda
[bgpcep.git] / pcep / segment-routing / src / test / java / org / opendaylight / protocol / pcep / segment / routing / TopologyProviderTest.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.routing;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.opendaylight.protocol.util.CheckTestUtil.readDataOperational;
14
15 import com.google.common.collect.Lists;
16 import java.nio.charset.StandardCharsets;
17 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.bgpcep.pcep.topology.provider.AbstractPCEPSessionTest;
22 import org.opendaylight.bgpcep.pcep.topology.provider.AbstractTopologySessionListener;
23 import org.opendaylight.bgpcep.pcep.topology.provider.Stateful07TopologySessionListenerFactory;
24 import org.opendaylight.protocol.pcep.PCEPSession;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Path1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Pcrpt;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.PcrptBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.PlspId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.SrpIdNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.SymbolicPathName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.identifiers.tlv.LspIdentifiersBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.object.LspBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.object.lsp.TlvsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.PcrptMessageBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.pcrpt.message.ReportsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.pcrpt.message.reports.PathBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.srp.object.SrpBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.symbolic.path.name.tlv.SymbolicPathNameBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.SidType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.network.topology.topology.node.path.computation.client.reported.lsp.path.ero.subobject.subobject.type.SrEroType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.pcrpt.pcrpt.message.reports.path.ero.subobject.subobject.type.SrEroTypeBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.sr.subobject.nai.IpNodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.segment.routing.rev171025.sr.subobject.nai.IpNodeIdBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.Ero;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.SubobjectBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.path.setup.type.tlv.PathSetupTypeBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.path.computation.client.ReportedLsp;
53
54 public class TopologyProviderTest extends AbstractPCEPSessionTest<Stateful07TopologySessionListenerFactory> {
55
56     private AbstractTopologySessionListener<SrpIdNumber, PlspId> listener;
57     private PCEPSession session;
58
59     @Override
60     @SuppressWarnings("unchecked")
61     @Before
62     public void setUp() throws Exception {
63         super.setUp();
64         this.listener = (AbstractTopologySessionListener<SrpIdNumber, PlspId>) getSessionListener();
65         this.session = getPCEPSession(getLocalPref(), getRemotePref());
66     }
67
68     @Test
69     public void testOnReportMessage() throws ExecutionException, InterruptedException {
70         this.listener.onSessionUp(this.session);
71
72         Pcrpt pcRptMsg = createSrPcRpt("1.1.1.1", "sr-path1", 1L, true);
73         this.listener.onMessage(this.session, pcRptMsg);
74         readDataOperational(getDataBroker(), this.pathComputationClientIId, pcc -> {
75             //check sr-path
76             final List<ReportedLsp> reportedLsps = pcc.getReportedLsp();
77             assertNotNull(reportedLsps);
78             assertEquals(1, reportedLsps.size());
79             final ReportedLsp lsp = reportedLsps.get(0);
80             assertEquals("sr-path1", lsp.getName());
81             assertEquals(1, lsp.getPath().get(0).augmentation(Path1.class).getPathSetupType()
82                 .getPst().intValue());
83             final List<Subobject> subobjects = lsp.getPath().get(0).getEro().getSubobject();
84             assertEquals(1, subobjects.size());
85             assertEquals("1.1.1.1", ((IpNodeId)((SrEroType)subobjects.get(0).getSubobjectType())
86                 .getNai()).getIpAddress().getIpv4AddressNoZone().getValue());
87             return pcc;
88         });
89
90         pcRptMsg = createSrPcRpt("1.1.1.3", "sr-path2", 2L, false);
91         this.listener.onMessage(this.session, pcRptMsg);
92         readDataOperational(getDataBroker(), this.pathComputationClientIId, pcc -> {
93             //check second lsp sr-path
94             final List<ReportedLsp> reportedLsps = pcc.getReportedLsp();
95             assertNotNull(reportedLsps);
96             assertEquals(2, reportedLsps.size());
97             return pcc;
98         });
99
100
101         pcRptMsg = createSrPcRpt("1.1.1.2", "sr-path1", 1L, true);
102         this.listener.onMessage(this.session, pcRptMsg);
103         readDataOperational(getDataBroker(), this.pathComputationClientIId, pcc -> {
104             //check updated sr-path
105             final List<ReportedLsp> reportedLsps = pcc.getReportedLsp();
106             assertNotNull(reportedLsps);
107             assertEquals(2, reportedLsps.size());
108             for (final ReportedLsp rlsp : reportedLsps) {
109                 if (rlsp.getName().equals("sr-path1")) {
110                     final List<Subobject> subobjects = rlsp.getPath().get(0).getEro().getSubobject();
111                     assertEquals(1, subobjects.size());
112                     assertEquals("1.1.1.2", ((IpNodeId)((SrEroType)subobjects.get(0)
113                         .getSubobjectType()).getNai()).getIpAddress().getIpv4AddressNoZone().getValue());
114                 }
115             }
116             return pcc;
117         });
118     }
119
120     private static Pcrpt createSrPcRpt(final String nai, final String pathName, final long plspId, final boolean hasLspIdTlv) {
121         final TlvsBuilder lspTlvBuilder = new TlvsBuilder();
122         if (hasLspIdTlv) {
123             lspTlvBuilder.setLspIdentifiers(new LspIdentifiersBuilder().setLspId(new LspId(plspId)).build());
124         }
125         return new PcrptBuilder().setPcrptMessage(new PcrptMessageBuilder().setReports(Lists.newArrayList(new ReportsBuilder()
126             .setLsp(new LspBuilder().setPlspId(new PlspId(plspId)).setRemove(false).setSync(true).setAdministrative(true).setDelegate(true)
127                     .setTlvs(lspTlvBuilder
128                         .setSymbolicPathName(new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(pathName.getBytes(StandardCharsets.UTF_8))).build()).build()).build())
129             .setSrp(new SrpBuilder().setOperationId(new SrpIdNumber(0L)).setTlvs(
130                     new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.srp.object.srp.TlvsBuilder()
131                         .setPathSetupType(new PathSetupTypeBuilder().setPst((short) 1).build()).build()).build())
132             .setPath(new PathBuilder().setEro(createSrEroObject(nai)).build())
133             .build())).build()).build();
134     }
135
136     private static Ero createSrEroObject(final String nai) {
137         final SrEroTypeBuilder srEroBuilder = new SrEroTypeBuilder();
138         srEroBuilder.setCFlag(false);
139         srEroBuilder.setMFlag(false);
140         srEroBuilder.setSidType(SidType.Ipv4NodeId);
141         srEroBuilder.setSid(123456L);
142         srEroBuilder.setNai(new IpNodeIdBuilder().setIpAddress(new IpAddressNoZone(new Ipv4AddressNoZone(nai))).build());
143         final SubobjectBuilder subobjBuilder = new SubobjectBuilder().setSubobjectType(srEroBuilder.build()).setLoose(false);
144
145         final List<Subobject> subobjects = Lists.newArrayList(subobjBuilder.build());
146         return new EroBuilder().setProcessingRule(false).setIgnore(false).setSubobject(subobjects).build();
147     }
148
149 }