Merge "Code changes for OOF SON Use Case"
[policy/models.git] / models-interactions / model-actors / actor.sdnr / src / test / java / org / onap / policy / controlloop / actor / sdnr / SdnrActorServiceProviderTest.java
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.sdnr;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.time.Instant;
28 import java.util.HashMap;
29 import java.util.UUID;
30
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;
46
47 public class SdnrActorServiceProviderTest {
48
49     private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProviderTest.class);
50
51     private static final VirtualControlLoopEvent onsetEvent;
52     private static final ControlLoopOperation operation;
53     private static final Policy policy;
54
55     static {
56         /*
57          * Construct an onset. Using dummy AAI details since the code mandates AAI
58          * details.
59          */
60         onsetEvent = new VirtualControlLoopEvent();
61         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
62         onsetEvent.setRequestId(UUID.randomUUID());
63         onsetEvent.setClosedLoopEventClient("tca.instance00001");
64         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
65         onsetEvent.setTarget("generic-vnf.vnf-name");
66         onsetEvent.setFrom("DCAE");
67         onsetEvent.setClosedLoopAlarmStart(Instant.now());
68         onsetEvent.setAai(new HashMap<>());
69         onsetEvent.getAai().put("generic-vnf.vnf-name", "notused");
70         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
71         onsetEvent.setPayload("some payload");
72
73         /* Construct an operation with an SDNR actor and ModifyConfig operation. */
74         operation = new ControlLoopOperation();
75         operation.setActor("SDNR");
76         operation.setOperation("ModifyConfig");
77         operation.setTarget("VNF");
78         operation.setEnd(Instant.now());
79         operation.setSubRequestId("1");
80
81         /* Construct a policy specifying to modify configuration. */
82         policy = new Policy();
83         policy.setName("Modify PCI Config");
84         policy.setDescription("Upon getting the trigger event, modify pci config");
85         policy.setActor("SDNR");
86         policy.setTarget(new Target(TargetType.VNF));
87         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
88         policy.setRecipe("ModifyConfig");
89         policy.setPayload(null);
90         policy.setRetry(2);
91         policy.setTimeout(300);
92
93     }
94
95     @Test
96     public void getControlLoopResponseTest() {
97         PciRequest sdnrRequest;
98         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
99         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
100         sdnrResponse.getStatus().setCode(200);
101         sdnrResponse.getStatus().setValue("SDNR success");
102         sdnrResponse.setPayload("sdnr payload ");
103         /* Print out request as json to make sure serialization works */
104         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
105         logger.info(jsonResponse);
106         PciResponseWrapper pciResponseWrapper = new PciResponseWrapper();
107         pciResponseWrapper.setBody(sdnrResponse);
108
109         ControlLoopResponse clRsp = SdnrActorServiceProvider.getControlLoopResponse(pciResponseWrapper, onsetEvent);
110         assertEquals(clRsp.getClosedLoopControlName(), onsetEvent.getClosedLoopControlName());
111         assertEquals(clRsp.getRequestId(), onsetEvent.getRequestId());
112         assertEquals(clRsp.getPolicyName(), onsetEvent.getPolicyName());
113         assertEquals(clRsp.getPolicyVersion(), onsetEvent.getPolicyVersion());
114         assertEquals(clRsp.getVersion(), onsetEvent.getVersion());
115         assertEquals(clRsp.getFrom(), "SDNR");
116         assertEquals(clRsp.getTarget(), "DCAE");
117         assertEquals(clRsp.getPayload(), sdnrResponse.getPayload());
118     }
119
120     @Test
121     public void constructModifyConfigRequestTest() {
122
123         PciRequest sdnrRequest;
124         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
125
126         /* The service provider must return a non null SDNR request */
127         assertNotNull(sdnrRequest);
128
129         /* A common header is required and cannot be null */
130         assertNotNull(sdnrRequest.getCommonHeader());
131         assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
132
133         /* An action is required and cannot be null */
134         assertNotNull(sdnrRequest.getAction());
135         assertEquals("ModifyConfig", sdnrRequest.getAction());
136
137         /* A payload is required and cannot be null */
138         assertNotNull(sdnrRequest.getPayload());
139         assertEquals("some payload", sdnrRequest.getPayload());
140
141         logger.debug("SDNR Request: \n" + sdnrRequest.toString());
142
143         /* Print out request as json to make sure serialization works */
144         String jsonRequest = Serialization.gsonPretty.toJson(sdnrRequest);
145         logger.debug("JSON Output: \n" + jsonRequest);
146
147         /* The JSON string must contain the following fields */
148         assertTrue(jsonRequest.contains("CommonHeader"));
149         assertTrue(jsonRequest.contains("Action"));
150         assertTrue(jsonRequest.contains("ModifyConfig"));
151         assertTrue(jsonRequest.contains("payload"));
152
153         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
154         sdnrResponse.getStatus().setCode(200);
155         sdnrResponse.getStatus().setValue("SDNR success");
156         /* Print out request as json to make sure serialization works */
157         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
158         logger.debug("JSON Output: \n" + jsonResponse);
159     }
160
161     @Test
162     public void testMethods() {
163         SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
164
165         assertEquals("SDNR", sp.actor());
166         assertEquals(1, sp.recipes().size());
167         assertEquals("VNF", sp.recipeTargets("ModifyConfig").get(0));
168         assertEquals(2, sp.recipePayloads("ModifyConfig").size());
169     }
170 }