2 * SdnrActorServiceProviderTest
3 * ================================================================================
4 * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.actor.sdnr;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.time.Instant;
28 import java.util.HashMap;
29 import java.util.UUID;
31 import org.junit.Test;
32 import org.onap.policy.controlloop.ControlLoopEventStatus;
33 import org.onap.policy.controlloop.ControlLoopOperation;
34 import org.onap.policy.controlloop.ControlLoopResponse;
35 import org.onap.policy.controlloop.ControlLoopTargetType;
36 import org.onap.policy.controlloop.VirtualControlLoopEvent;
37 import org.onap.policy.controlloop.policy.Policy;
38 import org.onap.policy.controlloop.policy.Target;
39 import org.onap.policy.controlloop.policy.TargetType;
40 import org.onap.policy.sdnr.PciRequest;
41 import org.onap.policy.sdnr.PciResponse;
42 import org.onap.policy.sdnr.PciResponseWrapper;
43 import org.onap.policy.sdnr.util.Serialization;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class SdnrActorServiceProviderTest {
49 private static final String MODIFY_CONFIG = "ModifyConfig";
51 private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProviderTest.class);
53 private static final VirtualControlLoopEvent onsetEvent;
54 private static final ControlLoopOperation operation;
55 private static final Policy policy;
59 * Construct an onset. Using dummy AAI details since the code mandates AAI
62 onsetEvent = new VirtualControlLoopEvent();
63 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
64 onsetEvent.setRequestId(UUID.randomUUID());
65 onsetEvent.setClosedLoopEventClient("tca.instance00001");
66 onsetEvent.setTargetType(ControlLoopTargetType.VNF);
67 onsetEvent.setTarget("generic-vnf.vnf-name");
68 onsetEvent.setFrom("DCAE");
69 onsetEvent.setClosedLoopAlarmStart(Instant.now());
70 onsetEvent.setAai(new HashMap<>());
71 onsetEvent.getAai().put("generic-vnf.vnf-name", "notused");
72 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
73 onsetEvent.setPayload("some payload");
75 /* Construct an operation with an SDNR actor and ModifyConfig operation. */
76 operation = new ControlLoopOperation();
77 operation.setActor("SDNR");
78 operation.setOperation(MODIFY_CONFIG);
79 operation.setTarget("VNF");
80 operation.setEnd(Instant.now());
81 operation.setSubRequestId("1");
83 /* Construct a policy specifying to modify configuration. */
84 policy = new Policy();
85 policy.setName("Modify PCI Config");
86 policy.setDescription("Upon getting the trigger event, modify pci config");
87 policy.setActor("SDNR");
88 policy.setTarget(new Target(TargetType.VNF));
89 policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
90 policy.setRecipe(MODIFY_CONFIG);
91 policy.setPayload(null);
93 policy.setTimeout(300);
98 public void getControlLoopResponseTest() {
99 PciRequest sdnrRequest;
100 sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
101 PciResponse sdnrResponse = new PciResponse(sdnrRequest);
102 sdnrResponse.getStatus().setCode(200);
103 sdnrResponse.getStatus().setValue("SDNR success");
104 sdnrResponse.setPayload("sdnr payload ");
105 /* Print out request as json to make sure serialization works */
106 String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
107 logger.info(jsonResponse);
108 PciResponseWrapper pciResponseWrapper = new PciResponseWrapper();
109 pciResponseWrapper.setBody(sdnrResponse);
111 ControlLoopResponse clRsp = SdnrActorServiceProvider.getControlLoopResponse(pciResponseWrapper, onsetEvent);
112 assertEquals(clRsp.getClosedLoopControlName(), onsetEvent.getClosedLoopControlName());
113 assertEquals(clRsp.getRequestId(), onsetEvent.getRequestId());
114 assertEquals(clRsp.getPolicyName(), onsetEvent.getPolicyName());
115 assertEquals(clRsp.getPolicyVersion(), onsetEvent.getPolicyVersion());
116 assertEquals(clRsp.getVersion(), onsetEvent.getVersion());
117 assertEquals(clRsp.getFrom(), "SDNR");
118 assertEquals(clRsp.getTarget(), "DCAE");
119 assertEquals(clRsp.getPayload(), sdnrResponse.getPayload());
123 public void constructModifyConfigRequestTest() {
125 PciRequest sdnrRequest;
126 sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
128 /* The service provider must return a non null SDNR request */
129 assertNotNull(sdnrRequest);
131 /* A common header is required and cannot be null */
132 assertNotNull(sdnrRequest.getCommonHeader());
133 assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
135 /* An action is required and cannot be null */
136 assertNotNull(sdnrRequest.getAction());
137 assertEquals(MODIFY_CONFIG, sdnrRequest.getAction());
139 /* A payload is required and cannot be null */
140 assertNotNull(sdnrRequest.getPayload());
141 assertEquals("some payload", sdnrRequest.getPayload());
143 logger.debug("SDNR Request: \n" + sdnrRequest.toString());
145 /* Print out request as json to make sure serialization works */
146 String jsonRequest = Serialization.gsonPretty.toJson(sdnrRequest);
147 logger.debug("JSON Output: \n" + jsonRequest);
149 /* The JSON string must contain the following fields */
150 assertTrue(jsonRequest.contains("CommonHeader"));
151 assertTrue(jsonRequest.contains("Action"));
152 assertTrue(jsonRequest.contains(MODIFY_CONFIG));
153 assertTrue(jsonRequest.contains("payload"));
155 PciResponse sdnrResponse = new PciResponse(sdnrRequest);
156 sdnrResponse.getStatus().setCode(200);
157 sdnrResponse.getStatus().setValue("SDNR success");
158 /* Print out request as json to make sure serialization works */
159 String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
160 logger.debug("JSON Output: \n" + jsonResponse);
164 public void testMethods() {
165 SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
167 assertEquals("SDNR", sp.actor());
168 assertEquals(1, sp.recipes().size());
169 assertEquals("VNF", sp.recipeTargets(MODIFY_CONFIG).get(0));
170 assertEquals(2, sp.recipePayloads(MODIFY_CONFIG).size());