blob: 6378ea93b254b3ba1e19d72b3dd2c75a57266078 [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;
25import org.onlab.rest.BaseResource;
26import org.onosproject.cfm.CfmCodecContext;
27import org.onosproject.codec.CodecService;
28import org.onosproject.incubator.net.l2monitoring.cfm.Component;
29import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent;
30import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
31import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain;
32import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
33import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
34import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
35import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
36import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
37import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
38import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
39import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
40import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
41
42import javax.ws.rs.InternalServerErrorException;
43import javax.ws.rs.client.Entity;
44import javax.ws.rs.client.WebTarget;
45import javax.ws.rs.core.Response;
46
47import java.io.BufferedReader;
48import java.io.ByteArrayInputStream;
49import java.io.IOException;
50import java.io.InputStreamReader;
51import java.util.Optional;
52
53import static junit.framework.TestCase.fail;
54import static org.easymock.EasyMock.createMock;
55import static org.easymock.EasyMock.expect;
56import static org.easymock.EasyMock.replay;
57import static org.hamcrest.Matchers.is;
58import static org.junit.Assert.assertEquals;
59import static org.junit.Assert.assertThat;
60
61public class MaWebResourceTest extends CfmResourceTest {
62 private final CfmMdService mdService = createMock(CfmMdService.class);
63
64 private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1");
65 private static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1");
66
67 private MaintenanceAssociation ma1;
68
69 @Before
70 public void setUpTest() throws CfmConfigException {
71 CfmCodecContext context = new CfmCodecContext();
72 ServiceDirectory testDirectory = new TestServiceDirectory()
73 .add(CfmMdService.class, mdService)
74 .add(CodecService.class, context.codecManager());
75 BaseResource.setServiceDirectory(testDirectory);
76
77 ma1 = DefaultMaintenanceAssociation
78 .builder(MANAME1, MDNAME1.getNameLength())
79 .addToRemoteMepIdList(MepId.valueOf((short) 101))
80 .addToRemoteMepIdList(MepId.valueOf((short) 102))
81 .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_3MS)
82 .maNumericId((short) 1)
83 .addToComponentList(
84 DefaultComponent.builder(1)
85 .tagType(Component.TagType.VLAN_STAG)
86 .mhfCreationType(Component.MhfCreationType.NONE)
87 .idPermission(Component.IdPermissionType.MANAGE)
88 .addToVidList(VlanId.vlanId((short) 1010))
89 .build())
90 .build();
91 }
92
93 @Test
94 public void testGetMa() {
95
96 expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
97 .andReturn(Optional.ofNullable(ma1)).anyTimes();
98 replay(mdService);
99
100 final WebTarget wt = target();
101 final String response = wt.path("md/" + MDNAME1.mdName()
102 + "/ma/" + MANAME1.maName()).request().get(String.class);
103
104 assertThat(response, is("{\"ma\":" +
105 "{\"maName\":\"ma-1-1\"," +
106 "\"maNameType\":\"CHARACTERSTRING\"," +
107 "\"maNumericId\":1," +
108 "\"ccm-interval\":\"INTERVAL_3MS\"," +
109 "\"component-list\":[{\"component\":" +
110 "{\"component-id\":1," +
111 "\"vid-list\":[{\"vid\":\"1010\"}]," +
112 "\"mhf-creation-type\":\"NONE\"," +
113 "\"id-permission\":\"MANAGE\"," +
114 "\"tag-type\":\"VLAN_STAG\"}}]," +
115 "\"rmep-list\":" +
116 "[{\"rmep\":101}," +
117 "{\"rmep\":102}]}}"));
118 }
119
120 @Test
121 public void testGetMaEmpty() throws IOException {
122 MaIdShort maId2 = MaIdCharStr.asMaId("ma-2");
123 expect(mdService
124 .getMaintenanceAssociation(MDNAME1, maId2))
125 .andReturn(Optional.empty()).anyTimes();
126 replay(mdService);
127
128 final WebTarget wt = target();
129 try {
130 final String response = wt.path("md/" + MDNAME1.mdName()
131 + "/ma/" + maId2.maName()).request().get(String.class);
132 fail("Expected InternalServerErrorException, as MA is unknown");
133 } catch (InternalServerErrorException e) {
134 ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity();
135 BufferedReader br = new BufferedReader(new InputStreamReader(is));
136 String line = null;
137 StringBuffer sb = new StringBuffer();
138 while ((line = br.readLine()) != null) {
139 sb.append(line);
140 }
141
142 assertThat(sb.toString(), is("{ \"failure\":" +
143 "\"java.lang.IllegalArgumentException: MA ma-2 not Found\" }"));
144 }
145 }
146
147 @Test
148 public void testDeleteMa() throws CfmConfigException {
149
150 expect(mdService.deleteMaintenanceAssociation(MDNAME1, MANAME1))
151 .andReturn(true).anyTimes();
152 replay(mdService);
153
154 final WebTarget wt = target();
155 final Response response = wt.path("md/" + MDNAME1.mdName()
156 + "/ma/" + MANAME1.maName()).request().delete();
157
158 assertEquals(200, response.getStatus());
159 }
160
161 @Test
162 public void testDeleteMaEmpty() throws CfmConfigException {
163 MaIdShort maId2 = MaIdCharStr.asMaId("ma-2");
164
165 expect(mdService.deleteMaintenanceAssociation(MDNAME1, maId2))
166 .andReturn(false).anyTimes();
167 replay(mdService);
168
169 final WebTarget wt = target();
170 final Response response = wt.path("md/" + MDNAME1.mdName()
171 + "/ma/" + maId2.maName()).request().delete();
172
173 assertEquals(304, response.getStatus());
174 }
175
176 @Test
177 public void testCreateMa() throws CfmConfigException {
178 MaintenanceDomain md1 = DefaultMaintenanceDomain
179 .builder(MDNAME1).mdLevel(MaintenanceDomain.MdLevel.LEVEL2).build();
180
181 expect(mdService.getMaintenanceDomain(MDNAME1))
182 .andReturn(Optional.ofNullable(md1)).anyTimes();
183 expect(mdService.createMaintenanceAssociation(MDNAME1, ma1))
184 .andReturn(false).anyTimes();
185 replay(mdService);
186
187 ObjectMapper mapper = new ObjectMapper();
188 CfmCodecContext context = new CfmCodecContext();
189 ObjectNode node = mapper.createObjectNode();
190 node.set("ma", context.codec(MaintenanceAssociation.class)
191 .encode(ma1, context));
192
193 final WebTarget wt = target();
194 final Response response = wt.path("md/" + MDNAME1.mdName()
195 + "/ma").request().post(Entity.json(node.toString()));
196
197 assertEquals(201, response.getStatus());
198 }
199}