2 * SdnrActorServiceProviderTest
3 * ================================================================================
4 * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
5 * Modifications Copyright (C) 2019 Nordix Foundation.
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;
26 import static org.junit.Assert.fail;
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
36 import org.onap.policy.controlloop.ControlLoopEventStatus;
37 import org.onap.policy.controlloop.ControlLoopOperation;
38 import org.onap.policy.controlloop.ControlLoopTargetType;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.policy.Policy;
41 import org.onap.policy.controlloop.policy.Target;
42 import org.onap.policy.controlloop.policy.TargetType;
43 import org.onap.policy.drools.system.PolicyEngine;
44 import org.onap.policy.sdnr.PciRequest;
45 import org.onap.policy.sdnr.PciResponse;
46 import org.onap.policy.sdnr.util.Serialization;
47 import org.onap.policy.simulators.Util;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class SdnrActorServiceProviderTest {
53 private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProviderTest.class);
55 private static final VirtualControlLoopEvent onsetEvent;
56 private static final ControlLoopOperation operation;
57 private static final Policy policy;
61 * Construct an onset. Using dummy AAI details since the code mandates AAI
64 onsetEvent = new VirtualControlLoopEvent();
65 onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
66 onsetEvent.setRequestId(UUID.randomUUID());
67 onsetEvent.setClosedLoopEventClient("tca.instance00001");
68 onsetEvent.setTargetType(ControlLoopTargetType.VNF);
69 onsetEvent.setTarget("generic-vnf.vnf-name");
70 onsetEvent.setFrom("DCAE");
71 onsetEvent.setClosedLoopAlarmStart(Instant.now());
72 onsetEvent.setAai(new HashMap<>());
73 onsetEvent.getAai().put("generic-vnf.vnf-name", "notused");
74 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
75 onsetEvent.setPayload("some payload");
77 /* Construct an operation with an SDNR actor and ModifyConfig operation. */
78 operation = new ControlLoopOperation();
79 operation.setActor("SDNR");
80 operation.setOperation("ModifyConfig");
81 operation.setTarget("VNF");
82 operation.setEnd(Instant.now());
83 operation.setSubRequestId("1");
85 /* Construct a policy specifying to modify configuration. */
86 policy = new Policy();
87 policy.setName("Modify PCI Config");
88 policy.setDescription("Upon getting the trigger event, modify pci config");
89 policy.setActor("SDNR");
90 policy.setTarget(new Target(TargetType.VNF));
91 policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
92 policy.setRecipe("ModifyConfig");
93 policy.setPayload(null);
95 policy.setTimeout(300);
99 public void constructModifyConfigRequestTest() {
101 PciRequest sdnrRequest;
102 sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
104 /* The service provider must return a non null SDNR request */
105 assertNotNull(sdnrRequest);
107 /* A common header is required and cannot be null */
108 assertNotNull(sdnrRequest.getCommonHeader());
109 assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
111 /* An action is required and cannot be null */
112 assertNotNull(sdnrRequest.getAction());
113 assertEquals("ModifyConfig", sdnrRequest.getAction());
115 /* A payload is required and cannot be null */
116 assertNotNull(sdnrRequest.getPayload());
117 assertEquals("some payload", sdnrRequest.getPayload());
119 logger.debug("SDNR Request: \n" + sdnrRequest.toString());
121 /* Print out request as json to make sure serialization works */
122 String jsonRequest = Serialization.gsonPretty.toJson(sdnrRequest);
123 logger.debug("JSON Output: \n" + jsonRequest);
125 /* The JSON string must contain the following fields */
126 assertTrue(jsonRequest.contains("CommonHeader"));
127 assertTrue(jsonRequest.contains("Action"));
128 assertTrue(jsonRequest.contains("ModifyConfig"));
129 assertTrue(jsonRequest.contains("payload"));
131 PciResponse sdnrResponse = new PciResponse(sdnrRequest);
132 sdnrResponse.getStatus().setCode(200);
133 sdnrResponse.getStatus().setValue("SDNR success");
134 /* Print out request as json to make sure serialization works */
135 String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
136 logger.debug("JSON Output: \n" + jsonResponse);
140 public void testMethods() {
141 SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
143 assertEquals("SDNR", sp.actor());
144 assertEquals(1, sp.recipes().size());
145 assertEquals("VNF", sp.recipeTargets("ModifyConfig").get(0));
146 assertEquals(2, sp.recipePayloads("ModifyConfig").size());