a9178961a6f9e290147e2b4fa17ddd5878fa26d2
[policy/models.git] /
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 String MODIFY_CONFIG = "ModifyConfig";
50
51     private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProviderTest.class);
52
53     private static final VirtualControlLoopEvent onsetEvent;
54     private static final ControlLoopOperation operation;
55     private static final Policy policy;
56
57     static {
58         /*
59          * Construct an onset. Using dummy AAI details since the code mandates AAI
60          * details.
61          */
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");
74
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");
82
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);
92         policy.setRetry(2);
93         policy.setTimeout(300);
94
95     }
96
97     @Test
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);
110
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());
120     }
121
122     @Test
123     public void constructModifyConfigRequestTest() {
124
125         PciRequest sdnrRequest;
126         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
127
128         /* The service provider must return a non null SDNR request */
129         assertNotNull(sdnrRequest);
130
131         /* A common header is required and cannot be null */
132         assertNotNull(sdnrRequest.getCommonHeader());
133         assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
134
135         /* An action is required and cannot be null */
136         assertNotNull(sdnrRequest.getAction());
137         assertEquals(MODIFY_CONFIG, sdnrRequest.getAction());
138
139         /* A payload is required and cannot be null */
140         assertNotNull(sdnrRequest.getPayload());
141         assertEquals("some payload", sdnrRequest.getPayload());
142
143         logger.debug("SDNR Request: \n" + sdnrRequest.toString());
144
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);
148
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"));
154
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);
161     }
162
163     @Test
164     public void testMethods() {
165         SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
166
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());
171     }
172 }