8101b12fdae69b10a0da69e81af777a75586bf80
[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 details.
60          */
61         onsetEvent = new VirtualControlLoopEvent();
62         onsetEvent.setClosedLoopControlName("closedLoopControlName-Test");
63         onsetEvent.setRequestId(UUID.randomUUID());
64         onsetEvent.setClosedLoopEventClient("tca.instance00001");
65         onsetEvent.setTargetType(ControlLoopTargetType.VNF);
66         onsetEvent.setTarget("generic-vnf.vnf-name");
67         onsetEvent.setFrom("DCAE");
68         onsetEvent.setClosedLoopAlarmStart(Instant.now());
69         onsetEvent.setAai(new HashMap<>());
70         onsetEvent.getAai().put("generic-vnf.vnf-name", "notused");
71         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
72         onsetEvent.setPayload("some payload");
73
74         /* Construct an operation with an SDNR actor and ModifyConfig operation. */
75         operation = new ControlLoopOperation();
76         operation.setActor("SDNR");
77         operation.setOperation(MODIFY_CONFIG);
78         operation.setTarget("VNF");
79         operation.setEnd(Instant.now());
80         operation.setSubRequestId("1");
81
82         /* Construct a policy specifying to modify configuration. */
83         policy = new Policy();
84         policy.setName("Modify PCI Config");
85         policy.setDescription("Upon getting the trigger event, modify pci config");
86         policy.setActor("SDNR");
87         policy.setTarget(new Target(TargetType.VNF));
88         policy.getTarget().setResourceID("Eace933104d443b496b8.nodes.heat.vpg");
89         policy.setRecipe(MODIFY_CONFIG);
90         policy.setPayload(null);
91         policy.setRetry(2);
92         policy.setTimeout(300);
93
94     }
95
96     @Test
97     public void testGetControlLoopResponse() {
98         PciRequest sdnrRequest;
99         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
100         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
101         sdnrResponse.getStatus().setCode(200);
102         sdnrResponse.getStatus().setValue("SDNR success");
103         sdnrResponse.setPayload("sdnr payload ");
104         /* Print out request as json to make sure serialization works */
105         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
106         logger.info(jsonResponse);
107         PciResponseWrapper pciResponseWrapper = new PciResponseWrapper();
108         pciResponseWrapper.setBody(sdnrResponse);
109
110         ControlLoopResponse clRsp = SdnrActorServiceProvider.getControlLoopResponse(pciResponseWrapper, onsetEvent);
111         assertEquals(clRsp.getClosedLoopControlName(), onsetEvent.getClosedLoopControlName());
112         assertEquals(clRsp.getRequestId(), onsetEvent.getRequestId());
113         assertEquals(clRsp.getPolicyName(), onsetEvent.getPolicyName());
114         assertEquals(clRsp.getPolicyVersion(), onsetEvent.getPolicyVersion());
115         assertEquals(clRsp.getVersion(), onsetEvent.getVersion());
116         assertEquals("SDNR", clRsp.getFrom());
117         assertEquals("DCAE", clRsp.getTarget());
118         assertEquals(clRsp.getPayload(), sdnrResponse.getPayload());
119     }
120
121     @Test
122     public void testConstructModifyConfigRequest() {
123
124         PciRequest sdnrRequest;
125         sdnrRequest = SdnrActorServiceProvider.constructRequest(onsetEvent, operation, policy).getBody();
126
127         /* The service provider must return a non null SDNR request */
128         assertNotNull(sdnrRequest);
129
130         /* A common header is required and cannot be null */
131         assertNotNull(sdnrRequest.getCommonHeader());
132         assertEquals(sdnrRequest.getCommonHeader().getRequestId(), onsetEvent.getRequestId());
133
134         /* An action is required and cannot be null */
135         assertNotNull(sdnrRequest.getAction());
136         assertEquals(MODIFY_CONFIG, sdnrRequest.getAction());
137
138         /* A payload is required and cannot be null */
139         assertNotNull(sdnrRequest.getPayload());
140         assertEquals("some payload", sdnrRequest.getPayload());
141
142         logger.debug("SDNR Request: \n" + sdnrRequest.toString());
143
144         /* Print out request as json to make sure serialization works */
145         String jsonRequest = Serialization.gsonPretty.toJson(sdnrRequest);
146         logger.debug("JSON Output: \n" + jsonRequest);
147
148         /* The JSON string must contain the following fields */
149         assertTrue(jsonRequest.contains("CommonHeader"));
150         assertTrue(jsonRequest.contains("Action"));
151         assertTrue(jsonRequest.contains(MODIFY_CONFIG));
152         assertTrue(jsonRequest.contains("payload"));
153
154         PciResponse sdnrResponse = new PciResponse(sdnrRequest);
155         sdnrResponse.getStatus().setCode(200);
156         sdnrResponse.getStatus().setValue("SDNR success");
157         /* Print out request as json to make sure serialization works */
158         String jsonResponse = Serialization.gsonPretty.toJson(sdnrResponse);
159         logger.debug("JSON Output: \n" + jsonResponse);
160     }
161
162     @Test
163     public void testMethods() {
164         SdnrActorServiceProvider sp = new SdnrActorServiceProvider();
165
166         assertEquals("SDNR", sp.actor());
167         assertEquals(1, sp.recipes().size());
168         assertEquals("VNF", sp.recipeTargets(MODIFY_CONFIG).get(0));
169         assertEquals(2, sp.recipePayloads(MODIFY_CONFIG).size());
170     }
171 }