SDNR Actor enhancements
[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.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import java.time.Instant;
29 import java.util.HashMap;
30 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.actorserviceprovider.Operator;
38 import org.onap.policy.controlloop.policy.Policy;
39 import org.onap.policy.controlloop.policy.Target;
40 import org.onap.policy.controlloop.policy.TargetType;
41 import org.onap.policy.sdnr.PciRequest;
42 import org.onap.policy.sdnr.PciResponse;
43 import org.onap.policy.sdnr.PciResponseWrapper;
44 import org.onap.policy.sdnr.util.Serialization;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class SdnrActorServiceProviderTest {
49
50     private static final String MODIFY_CONFIG = "ModifyConfig";
51
52     private static final Logger logger = LoggerFactory.getLogger(SdnrActorServiceProviderTest.class);
53
54     private static final VirtualControlLoopEvent onsetEvent;
55     private static final ControlLoopOperation operation;
56     private static final Policy policy;
57
58     static {
59         /*
60          * Construct an onset. Using dummy AAI details since the code mandates AAI 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 testGetOperator() {
99         SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
100
101         // should always return the same operator regardless of the name
102         Operator oper = sp.getOperator("unknown");
103         assertNotNull(oper);
104         assertSame(oper, sp.getOperator("another"));
105     }
106
107     @Test
108     public void testGetControlLoopResponse() {
109         PciRequest sdnrRequest;
110         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
111         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
112         sdnrResponse.getStatus().setCode(200);
113         sdnrResponse.getStatus().setValue("SDNR success");
114         sdnrResponse.setPayload("sdnr payload ");
115         /* Print out request as json to make sure serialization works */
116         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
117         logger.info(jsonResponse);
118         PciResponseWrapper pciResponseWrapper = new PciResponseWrapper();
119         pciResponseWrapper.setBody(sdnrResponse);
120
121         ControlLoopResponse clRsp = SdnrActorServiceProvider.getControlLoopResponse(pciResponseWrapper, onsetEvent);
122         assertEquals(clRsp.getClosedLoopControlName(), onsetEvent.getClosedLoopControlName());
123         assertEquals(clRsp.getRequestId(), onsetEvent.getRequestId());
124         assertEquals(clRsp.getPolicyName(), onsetEvent.getPolicyName());
125         assertEquals(clRsp.getPolicyVersion(), onsetEvent.getPolicyVersion());
126         assertEquals(clRsp.getVersion(), onsetEvent.getVersion());
127         assertEquals("SDNR", clRsp.getFrom());
128         assertEquals("DCAE", clRsp.getTarget());
129         assertEquals(clRsp.getPayload(), sdnrResponse.getPayload());
130     }
131
132     @Test
133     public void testConstructModifyConfigRequest() {
134
135         PciRequest sdnrRequest;
136         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
137
138         /* The service provider must return a non null SDNR request */
139         assertNotNull(sdnrRequest);
140
141         /* A common header is required and cannot be null */
142         assertNotNull(sdnrRequest.getCommonHeader());
143         assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
144
145         /* An action is required and cannot be null */
146         assertNotNull(sdnrRequest.getAction());
147         assertEquals(MODIFY_CONFIG, sdnrRequest.getAction());
148
149         /* A payload is required and cannot be null */
150         assertNotNull(sdnrRequest.getPayload());
151         assertEquals("some payload", sdnrRequest.getPayload());
152
153         logger.debug("SDNR Request: \n" + sdnrRequest.toString());
154
155         /* Print out request as json to make sure serialization works */
156         String jsonRequest = Serialization.gsonPretty.toJson(sdnrRequest);
157         logger.debug("JSON Output: \n" + jsonRequest);
158
159         /* The JSON string must contain the following fields */
160         assertTrue(jsonRequest.contains("CommonHeader"));
161         assertTrue(jsonRequest.contains("Action"));
162         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
163         assertTrue(jsonRequest.contains("payload"));
164
165         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
166         sdnrResponse.getStatus().setCode(200);
167         sdnrResponse.getStatus().setValue("SDNR success");
168         /* Print out request as json to make sure serialization works */
169         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
170         logger.debug("JSON Output: \n" + jsonResponse);
171     }
172
173     @Test
174     public void testMethods() {
175         SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
176
177         assertEquals("SDNR", sp.actor());
178         assertEquals(1, sp.recipes().size());
179         assertEquals("VNF", sp.recipeTargets(MODIFY_CONFIG).get(0));
180         assertEquals(2, sp.recipePayloads(MODIFY_CONFIG).size());
181     }
182 }