blob: 8216dde5f020584f453def6734128ca9840027a6 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cfm.impl;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.osgi.ServiceDirectory;
23import org.onlab.osgi.TestServiceDirectory;
24import org.onlab.packet.VlanId;
Sean Condon0e89bda2017-03-21 14:23:19 +000025import org.onosproject.cfm.CfmCodecContext;
26import org.onosproject.codec.CodecService;
27import org.onosproject.incubator.net.l2monitoring.cfm.Component;
28import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent;
29import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
30import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain;
31import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
32import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
34import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
35import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
36import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
37import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
38import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
39import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
40
41import javax.ws.rs.InternalServerErrorException;
42import javax.ws.rs.client.Entity;
43import javax.ws.rs.client.WebTarget;
44import javax.ws.rs.core.Response;
Sean Condon0e89bda2017-03-21 14:23:19 +000045import java.io.BufferedReader;
46import java.io.ByteArrayInputStream;
47import java.io.IOException;
48import java.io.InputStreamReader;
49import java.util.Optional;
50
51import static junit.framework.TestCase.fail;
52import static org.easymock.EasyMock.createMock;
53import static org.easymock.EasyMock.expect;
54import static org.easymock.EasyMock.replay;
55import static org.hamcrest.Matchers.is;
56import static org.junit.Assert.assertEquals;
57import static org.junit.Assert.assertThat;
58
59public class MaWebResourceTest extends CfmResourceTest {
60 private final CfmMdService mdService = createMock(CfmMdService.class);
61
62 private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1");
63 private static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1");
64
65 private MaintenanceAssociation ma1;
66
67 @Before
68 public void setUpTest() throws CfmConfigException {
69 CfmCodecContext context = new CfmCodecContext();
70 ServiceDirectory testDirectory = new TestServiceDirectory()
71 .add(CfmMdService.class, mdService)
72 .add(CodecService.class, context.codecManager());
Ray Milkey094a1352018-01-22 14:03:54 -080073 setServiceDirectory(testDirectory);
Sean Condon0e89bda2017-03-21 14:23:19 +000074
75 ma1 = DefaultMaintenanceAssociation
76 .builder(MANAME1, MDNAME1.getNameLength())
77 .addToRemoteMepIdList(MepId.valueOf((short) 101))
78 .addToRemoteMepIdList(MepId.valueOf((short) 102))
79 .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_3MS)
80 .maNumericId((short) 1)
81 .addToComponentList(
82 DefaultComponent.builder(1)
83 .tagType(Component.TagType.VLAN_STAG)
84 .mhfCreationType(Component.MhfCreationType.NONE)
85 .idPermission(Component.IdPermissionType.MANAGE)
86 .addToVidList(VlanId.vlanId((short) 1010))
87 .build())
88 .build();
89 }
90
91 @Test
92 public void testGetMa() {
93
94 expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
95 .andReturn(Optional.ofNullable(ma1)).anyTimes();
96 replay(mdService);
97
98 final WebTarget wt = target();
99 final String response = wt.path("md/" + MDNAME1.mdName()
100 + "/ma/" + MANAME1.maName()).request().get(String.class);
101
102 assertThat(response, is("{\"ma\":" +
103 "{\"maName\":\"ma-1-1\"," +
104 "\"maNameType\":\"CHARACTERSTRING\"," +
105 "\"maNumericId\":1," +
106 "\"ccm-interval\":\"INTERVAL_3MS\"," +
107 "\"component-list\":[{\"component\":" +
108 "{\"component-id\":1," +
109 "\"vid-list\":[{\"vid\":\"1010\"}]," +
110 "\"mhf-creation-type\":\"NONE\"," +
111 "\"id-permission\":\"MANAGE\"," +
112 "\"tag-type\":\"VLAN_STAG\"}}]," +
113 "\"rmep-list\":" +
114 "[{\"rmep\":101}," +
115 "{\"rmep\":102}]}}"));
116 }
117
118 @Test
119 public void testGetMaEmpty() throws IOException {
120 MaIdShort maId2 = MaIdCharStr.asMaId("ma-2");
121 expect(mdService
122 .getMaintenanceAssociation(MDNAME1, maId2))
123 .andReturn(Optional.empty()).anyTimes();
124 replay(mdService);
125
126 final WebTarget wt = target();
127 try {
128 final String response = wt.path("md/" + MDNAME1.mdName()
129 + "/ma/" + maId2.maName()).request().get(String.class);
130 fail("Expected InternalServerErrorException, as MA is unknown");
131 } catch (InternalServerErrorException e) {
132 ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity();
133 BufferedReader br = new BufferedReader(new InputStreamReader(is));
134 String line = null;
135 StringBuffer sb = new StringBuffer();
136 while ((line = br.readLine()) != null) {
137 sb.append(line);
138 }
139
140 assertThat(sb.toString(), is("{ \"failure\":" +
141 "\"java.lang.IllegalArgumentException: MA ma-2 not Found\" }"));
142 }
143 }
144
145 @Test
146 public void testDeleteMa() throws CfmConfigException {
147
148 expect(mdService.deleteMaintenanceAssociation(MDNAME1, MANAME1))
149 .andReturn(true).anyTimes();
150 replay(mdService);
151
152 final WebTarget wt = target();
153 final Response response = wt.path("md/" + MDNAME1.mdName()
154 + "/ma/" + MANAME1.maName()).request().delete();
155
156 assertEquals(200, response.getStatus());
157 }
158
159 @Test
160 public void testDeleteMaEmpty() throws CfmConfigException {
161 MaIdShort maId2 = MaIdCharStr.asMaId("ma-2");
162
163 expect(mdService.deleteMaintenanceAssociation(MDNAME1, maId2))
164 .andReturn(false).anyTimes();
165 replay(mdService);
166
167 final WebTarget wt = target();
168 final Response response = wt.path("md/" + MDNAME1.mdName()
169 + "/ma/" + maId2.maName()).request().delete();
170
171 assertEquals(304, response.getStatus());
172 }
173
174 @Test
175 public void testCreateMa() throws CfmConfigException {
176 MaintenanceDomain md1 = DefaultMaintenanceDomain
177 .builder(MDNAME1).mdLevel(MaintenanceDomain.MdLevel.LEVEL2).build();
178
179 expect(mdService.getMaintenanceDomain(MDNAME1))
180 .andReturn(Optional.ofNullable(md1)).anyTimes();
181 expect(mdService.createMaintenanceAssociation(MDNAME1, ma1))
182 .andReturn(false).anyTimes();
183 replay(mdService);
184
185 ObjectMapper mapper = new ObjectMapper();
186 CfmCodecContext context = new CfmCodecContext();
187 ObjectNode node = mapper.createObjectNode();
188 node.set("ma", context.codec(MaintenanceAssociation.class)
189 .encode(ma1, context));
190
191 final WebTarget wt = target();
192 final Response response = wt.path("md/" + MDNAME1.mdName()
193 + "/ma").request().post(Entity.json(node.toString()));
194
195 assertEquals(201, response.getStatus());
196 }
197}