48c16d05ad19d7a256eb0f23c650ba87be5c50f7
[policy/models.git] / models-interactions / model-actors / actor.sdnr / src / test / java / org / onap / policy / controlloop / actor / sdnr / SdnrActorTest.java
1 /*-
2  * ONAP
3  * ================================================================================
4  * Copyright (C) 2018 Wipro Limited Intellectual Property. All rights reserved.
5  * Modifications Copyright (C) 2019-2020 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.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import java.time.Instant;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.UUID;
32 import java.util.stream.Collectors;
33 import org.junit.Test;
34 import org.onap.policy.controlloop.ControlLoopEventStatus;
35 import org.onap.policy.controlloop.ControlLoopOperation;
36 import org.onap.policy.controlloop.ControlLoopResponse;
37 import org.onap.policy.controlloop.ControlLoopTargetType;
38 import org.onap.policy.controlloop.VirtualControlLoopEvent;
39 import org.onap.policy.controlloop.actor.test.BasicActor;
40 import org.onap.policy.controlloop.actorserviceprovider.Operator;
41 import org.onap.policy.controlloop.policy.Policy;
42 import org.onap.policy.controlloop.policy.Target;
43 import org.onap.policy.controlloop.policy.TargetType;
44 import org.onap.policy.sdnr.PciRequest;
45 import org.onap.policy.sdnr.PciResponse;
46 import org.onap.policy.sdnr.PciResponseWrapper;
47 import org.onap.policy.sdnr.util.Serialization;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class SdnrActorTest extends BasicActor {
52
53     private static final String MODIFY_CONFIG = "ModifyConfig";
54
55     private static final Logger logger = LoggerFactory.getLogger(SdnrActorTest.class);
56
57     private static final VirtualControlLoopEvent onsetEvent;
58     private static final ControlLoopOperation operation;
59     private static final Policy policy;
60
61     static {
62         /*
63          * Construct an onset. Using dummy AAI details since the code mandates AAI details.
64          */
65         onsetEvent = new VirtualControlLoopEvent();
66         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
67         onsetEvent.setRequestId(UUID.randomUUID());
68         onsetEvent.setClosedLoopEventClient("tca.instance00001");
69         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
70         onsetEvent.setTarget("generic-vnf.vnf-name");
71         onsetEvent.setFrom("DCAE");
72         onsetEvent.setClosedLoopAlarmStart(Instant.now());
73         onsetEvent.setAai(new HashMap<>());
74         onsetEvent.getAai().put("generic-vnf.vnf-name", "notused");
75         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
76         onsetEvent.setPayload("some payload");
77
78         /* Construct an operation with an SDNR actor and ModifyConfig operation. */
79         operation = new ControlLoopOperation();
80         operation.setActor("SDNR");
81         operation.setOperation(MODIFY_CONFIG);
82         operation.setTarget("VNF");
83         operation.setEnd(Instant.now());
84         operation.setSubRequestId("1");
85
86         /* Construct a policy specifying to modify configuration. */
87         policy = new Policy();
88         policy.setName("Modify PCI Config");
89         policy.setDescription("Upon getting the trigger event, modify pci config");
90         policy.setActor("SDNR");
91         policy.setTarget(new Target(TargetType.VNF));
92         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
93         policy.setRecipe(MODIFY_CONFIG);
94         policy.setPayload(null);
95         policy.setRetry(2);
96         policy.setTimeout(300);
97
98     }
99
100     @Test
101     public void testConstructor() {
102         SdnrActor prov = new SdnrActor();
103         assertEquals(0, prov.getSequenceNumber());
104
105         // verify that it has the operators we expect
106         var expected = Arrays.asList(SdnrOperation.NAME).stream().sorted().collect(Collectors.toList());
107         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
108
109         assertEquals(expected.toString(), actual.toString());
110     }
111
112     @Test
113     public void testActorService() {
114         // verify that it all plugs into the ActorService
115         verifyActorService(SdnrActor.NAME, "service.yaml");
116     }
117
118     @Test
119     public void testGetOperator() {
120         SdnrActor sp = new SdnrActor();
121
122         // should always return the same operator regardless of the name
123         Operator oper = sp.getOperator("unknown");
124         assertNotNull(oper);
125         assertSame(oper, sp.getOperator("another"));
126     }
127
128     @Test
129     public void testGetControlLoopResponse() {
130         PciRequest sdnrRequest;
131         sdnrRequest = SdnrActor.constructRequest(onsetEvent, operation, policy).getBody();
132         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
133         sdnrResponse.getStatus().setCode(200);
134         sdnrResponse.getStatus().setValue("SDNR success");
135         sdnrResponse.setPayload("sdnr payload ");
136         /* Print out request as json to make sure serialization works */
137         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
138         logger.info(jsonResponse);
139         PciResponseWrapper pciResponseWrapper = new PciResponseWrapper();
140         pciResponseWrapper.setBody(sdnrResponse);
141
142         ControlLoopResponse clRsp = SdnrActor.getControlLoopResponse(pciResponseWrapper, onsetEvent);
143         assertEquals(clRsp.getClosedLoopControlName(), onsetEvent.getClosedLoopControlName());
144         assertEquals(clRsp.getRequestId(), onsetEvent.getRequestId());
145         assertEquals(clRsp.getPolicyName(), onsetEvent.getPolicyName());
146         assertEquals(clRsp.getPolicyVersion(), onsetEvent.getPolicyVersion());
147         assertEquals(clRsp.getVersion(), onsetEvent.getVersion());
148         assertEquals("SDNR", clRsp.getFrom());
149         assertEquals("DCAE", clRsp.getTarget());
150         assertEquals(clRsp.getPayload(), sdnrResponse.getPayload());
151     }
152
153     @Test
154     public void testConstructModifyConfigRequest() {
155
156         PciRequest sdnrRequest;
157         sdnrRequest = SdnrActor.constructRequest(onsetEvent, operation, policy).getBody();
158
159         /* The service provider must return a non null SDNR request */
160         assertNotNull(sdnrRequest);
161
162         /* A common header is required and cannot be null */
163         assertNotNull(sdnrRequest.getCommonHeader());
164         assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
165
166         /* An action is required and cannot be null */
167         assertNotNull(sdnrRequest.getAction());
168         assertEquals(MODIFY_CONFIG, sdnrRequest.getAction());
169
170         /* A payload is required and cannot be null */
171         assertNotNull(sdnrRequest.getPayload());
172         assertEquals("some payload", sdnrRequest.getPayload());
173
174         logger.debug("SDNR Request: \n" + sdnrRequest.toString());
175
176         /* Print out request as json to make sure serialization works */
177         String jsonRequest = Serialization.gsonPretty.toJson(sdnrRequest);
178         logger.debug("JSON Output: \n" + jsonRequest);
179
180         /* The JSON string must contain the following fields */
181         assertTrue(jsonRequest.contains("CommonHeader"));
182         assertTrue(jsonRequest.contains("Action"));
183         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
184         assertTrue(jsonRequest.contains("payload"));
185
186         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
187         sdnrResponse.getStatus().setCode(200);
188         sdnrResponse.getStatus().setValue("SDNR success");
189         /* Print out request as json to make sure serialization works */
190         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
191         logger.debug("JSON Output: \n" + jsonResponse);
192     }
193
194     @Test
195     public void testMethods() {
196         SdnrActor sp = new SdnrActor();
197
198         assertEquals("SDNR", sp.actor());
199         assertEquals(1, sp.recipes().size());
200         assertEquals("VNF", sp.recipeTargets(MODIFY_CONFIG).get(0));
201         assertEquals(2, sp.recipePayloads(MODIFY_CONFIG).size());
202     }
203 }