diff options
-rw-r--r-- | ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java | 11 | ||||
-rw-r--r-- | ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java | 1 | ||||
-rw-r--r-- | ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/PolicyElasticSearchControllerTest.java | 55 | ||||
-rw-r--r-- | ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/client/ElkConnectorImplTest.java (renamed from ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/ElkConnectorImplTest.java) | 35 | ||||
-rw-r--r-- | ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/MetricServiceTest.java | 21 | ||||
-rw-r--r-- | ONAP-PDP-REST/src/test/resources/test.drl | 1113 | ||||
-rw-r--r-- | PolicyEngineAPI/src/main/java/org/onap/policy/api/ImportParameters.java | 295 | ||||
-rw-r--r-- | PolicyEngineClient/src/test/java/org/onap/policyengine/ImportBRMSTemplate.java | 5 | ||||
-rw-r--r-- | PolicyEngineClient/src/test/resources/test.drl | 34 | ||||
-rw-r--r-- | PolicyEngineClient/testResources/test.drl | 1121 |
10 files changed, 295 insertions, 2396 deletions
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java index 594112636..36dd5fdee 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java @@ -21,13 +21,11 @@ package org.onap.policy.pap.xacml.rest.service; import com.att.research.xacml.api.pap.PDPPolicy; - +import com.google.common.annotations.VisibleForTesting; import java.util.HashSet; import java.util.List; import java.util.Set; - import javax.servlet.http.HttpServletResponse; - import org.json.JSONObject; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.eelf.PolicyLogger; @@ -51,8 +49,9 @@ public class MetricService { } + @VisibleForTesting @Autowired - private MetricService(CommonClassDao commonClassDao) { + protected MetricService(CommonClassDao commonClassDao) { MetricService.commonClassDao = commonClassDao; } @@ -79,7 +78,7 @@ public class MetricService { json.put("totalCount", totalCount); if (pdpCount > 0 && papCount > 0 && totalCount > 0) { PolicyLogger.info( - "Metrics have been found on the Policy Engine for the number of policies on the PAP and PDP."); + "Metrics have been found on the Policy Engine for the number of policies on the PAP and PDP."); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", "success"); response.addHeader("operation", "getMetrics"); @@ -87,7 +86,7 @@ public class MetricService { return; } else { String message = - "The policy count on the PAP and PDP is 0. Please check the database and file system to correct this error."; + "The policy count on the PAP and PDP is 0. Please check the database and file system to correct this error."; response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(errorMsg, message); return; diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java index d990b9002..f109dbdda 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/controller/OptimizationDictionaryControllerTest.java @@ -194,7 +194,6 @@ public class OptimizationDictionaryControllerTest { req.setBodyContent("{\n\"modelType\": \"type.yml\", \"dataOrderInfo\": \"info\", \"userid\": \"id\", " + "\"optimizationModelsDictionaryData\": {\"description\": \"desc\", \"modelName\": \"name\", \"version\": \"1.0\"}, " + "\"classMap\": \"{\\\"dep\\\":\\\"{\\\"dependency\\\":\\\"depval\\\"}\\\"}\" }\n"); - // + "\"classMap\": \"{\\\"dep\\\":\\\"dependency\\\"}\" }\n"); assertThatThrownBy(() -> controller.saveOptimizationModelsDictionary(req, response)) .isInstanceOf(NullPointerException.class); diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/PolicyElasticSearchControllerTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/PolicyElasticSearchControllerTest.java index 9456dd4e4..38102afb0 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/PolicyElasticSearchControllerTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/PolicyElasticSearchControllerTest.java @@ -20,37 +20,45 @@ package org.onap.policy.pap.xacml.rest.elk; +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.when; import java.io.BufferedReader; +import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; - +import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType; import org.onap.policy.pap.xacml.rest.elk.client.PolicyElasticSearchController; +import org.onap.policy.rest.adapter.PolicyRestAdapter; +import org.onap.policy.rest.dao.CommonClassDao; +import org.springframework.mock.web.MockHttpServletResponse; public class PolicyElasticSearchControllerTest { - private PolicyElasticSearchController conroller; + private PolicyElasticSearchController controller; private HttpServletRequest request = null; private HttpServletResponse response = null; @Before public void setup() { - conroller = new PolicyElasticSearchController(); + controller = new PolicyElasticSearchController(); request = Mockito.mock(HttpServletRequest.class); - response = Mockito.mock(HttpServletResponse.class); + response = new MockHttpServletResponse(); } @Test - public void testSearchDictionary() { + public void testSearchDictionary() throws IOException { List<String> jsonString = new ArrayList<>(); jsonString.add("{\"type\":\"attribute\",\"data\":{\"xacmlId\":\"Test\"}}"); jsonString.add("{\"type\":\"onapName\",\"data\":{\"onapName\":\"Test\"}}"); @@ -71,12 +79,35 @@ public class PolicyElasticSearchControllerTest { jsonString.add("{\"type\":\"safeRisk\",\"data\":{\"name\":\"Test\"}}"); jsonString.add("{\"type\":\"safePolicyWarning\",\"data\":{\"name\":\"Test\"}}"); for (int i = 0; i < jsonString.size(); i++) { - try (BufferedReader br = new BufferedReader(new StringReader(jsonString.get(i)))) { - when(request.getReader()).thenReturn(br); - conroller.searchDictionary(request, response); - } catch (Exception e) { - assertEquals(NullPointerException.class, e.getClass()); - } + BufferedReader br = new BufferedReader(new StringReader(jsonString.get(i))); + when(request.getReader()).thenReturn(br); + assertThatCode(() -> controller.searchDictionary(request, response)).doesNotThrowAnyException(); } } + + @Test + public void testController() throws IOException { + CommonClassDao dao = Mockito.mock(CommonClassDao.class); + PolicyElasticSearchController controller = new PolicyElasticSearchController(dao); + assertEquals(PolicyIndexType.all, controller.toPolicyIndexType(null)); + assertEquals(PolicyIndexType.config, controller.toPolicyIndexType("config")); + + Map<String, String> searchKeys = new HashMap<String, String>(); + searchKeys.put("key", "value"); + assertThatThrownBy(() -> controller.search(PolicyIndexType.config, "text", searchKeys)) + .isInstanceOf(Exception.class); + + when(request.getParameter("policyName")).thenReturn("policyName"); + when(request.getParameter("action")).thenReturn("search"); + when(request.getReader()) + .thenReturn(new BufferedReader(new StringReader("{\"searchdata\": { \"query\": \"value space\", " + + "\"policyType\": \"all\", " + "\"closedLooppolicyType\": \"type\", " + "\"onapName\": \"pef\", " + + "\"vnfType\": \"vnf\", " + "\"policyStatus\": \"active\", " + "\"vproAction\": \"reboot\", " + + "\"serviceType\": \"type\", " + "\"bindTextSearch\": \"pef\", " + "\"d2Service\": \"vDNS\"} }"))); + controller.searchPolicy(request, response); + assertEquals(HttpServletResponse.SC_OK, response.getStatus()); + + PolicyRestAdapter policyData = new PolicyRestAdapter(); + assertFalse(controller.deleteElk(policyData)); + } } diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/ElkConnectorImplTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/client/ElkConnectorImplTest.java index 87f56e284..8eb003ca8 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/ElkConnectorImplTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/elk/client/ElkConnectorImplTest.java @@ -18,8 +18,9 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.pap.xacml.rest.elk; +package org.onap.policy.pap.xacml.rest.elk.client; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -27,15 +28,14 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import io.searchbox.client.JestResult; - import java.io.IOException; import java.lang.reflect.Method; - +import java.util.HashMap; +import java.util.Map; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.onap.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType; -import org.onap.policy.pap.xacml.rest.elk.client.ElkConnectorImpl; import org.onap.policy.rest.adapter.PolicyRestAdapter; public class ElkConnectorImplTest { @@ -143,4 +143,31 @@ public class ElkConnectorImplTest { impl.search(PolicyIndexType.config, "search", null); fail("Expected exception to be thrown"); } + + @Test + public void testImplNegCases() throws IOException { + ElkConnectorImpl impl = new ElkConnectorImpl(); + Map<String, String> filter = new HashMap<String, String>(); + assertThatThrownBy(() -> impl.isType(PolicyIndexType.config)).isInstanceOf(IOException.class); + assertThatThrownBy(() -> impl.isIndex()).isInstanceOf(IOException.class); + assertThatThrownBy(() -> impl.search(null, null)).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> impl.search(null, "")).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> impl.search(null, ";;;")).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> impl.search(null, "foo")).isInstanceOf(IllegalStateException.class); + assertThatThrownBy(() -> impl.search(PolicyIndexType.all, "foo")).isInstanceOf(IllegalStateException.class); + + assertThatThrownBy(() -> impl.search(null, null, null)).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> impl.search(null, null, filter)).isInstanceOf(IllegalArgumentException.class); + filter.put("key", "value"); + assertThatThrownBy(() -> impl.search(null, ";;;", filter)).isInstanceOf(IllegalArgumentException.class); + assertThatThrownBy(() -> impl.search(null, "foo", filter)).isInstanceOf(IllegalStateException.class); + assertThatThrownBy(() -> impl.search(PolicyIndexType.config, "foo", filter)) + .isInstanceOf(IllegalStateException.class); + + PolicyRestAdapter adapter = new PolicyRestAdapter(); + adapter.setNewFileName("scope.Decision_newFile"); + adapter.setConfigPolicyType("Config"); + assertThatThrownBy(() -> impl.put(adapter)).isInstanceOf(IOException.class); + assertThatThrownBy(() -> impl.delete(adapter)).isInstanceOf(IllegalStateException.class); + } } diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/MetricServiceTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/MetricServiceTest.java index a4ee0ca77..ebd3292aa 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/MetricServiceTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/xacml/rest/service/MetricServiceTest.java @@ -21,15 +21,19 @@ package org.onap.policy.pap.xacml.rest.service; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.when; import com.mockrunner.mock.web.MockHttpServletResponse; - +import java.util.ArrayList; +import java.util.List; import javax.servlet.http.HttpServletResponse; - import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.onap.policy.pap.xacml.rest.XACMLPapServlet; +import org.onap.policy.rest.dao.CommonClassDao; +import org.onap.policy.rest.jpa.PolicyVersion; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -47,4 +51,17 @@ public class MetricServiceTest { MetricService.doGetPolicyMetrics(response); assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatusCode()); } + + @Test + public void testService() { + CommonClassDao dao = Mockito.mock(CommonClassDao.class); + List<Object> value = new ArrayList<Object>(); + when(dao.getData(PolicyVersion.class)).thenReturn(value); + MetricService service = new MetricService(dao); + assertNotNull(service); + + MockHttpServletResponse response = new MockHttpServletResponse(); + MetricService.doGetPolicyMetrics(response); + assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatusCode()); + } } diff --git a/ONAP-PDP-REST/src/test/resources/test.drl b/ONAP-PDP-REST/src/test/resources/test.drl index 7accc63ae..bb709575d 100644 --- a/ONAP-PDP-REST/src/test/resources/test.drl +++ b/ONAP-PDP-REST/src/test/resources/test.drl @@ -1,15 +1,14 @@ /*- * ============LICENSE_START======================================================= - * archetype-closed-loop-demo-rules - * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,1102 +19,16 @@ package org.onap.policy.controlloop; -import java.util.List; -import java.util.LinkedList; -import java.util.Map; -import java.util.HashMap; -import java.util.UUID; - -import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.ControlLoopEventStatus; -import org.onap.policy.controlloop.VirtualControlLoopNotification; -import org.onap.policy.controlloop.ControlLoopNotificationType; -import org.onap.policy.controlloop.ControlLoopOperation; -import org.onap.policy.controlloop.ControlLoopOperationWrapper; -import org.onap.policy.template.demo.ControlLoopException; - -import org.onap.policy.aai.AAINQF199.AAINQF199CloudRegion; -import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperties; -import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperty; -import org.onap.policy.aai.AAINQF199.AAINQF199GenericVNF; -import org.onap.policy.aai.AAINQF199.AAINQF199InstanceFilters; -import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItem; -import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItems; -import org.onap.policy.aai.AAINQF199.AAINQF199Manager; -import org.onap.policy.aai.AAINQF199.AAINQF199NamedQuery; -import org.onap.policy.aai.AAINQF199.AAINQF199QueryParameters; -import org.onap.policy.aai.AAINQF199.AAINQF199Request; -import org.onap.policy.aai.AAINQF199.AAINQF199RequestWrapper; -import org.onap.policy.aai.AAINQF199.AAINQF199Response; -import org.onap.policy.aai.AAINQF199.AAINQF199ResponseWrapper; -import org.onap.policy.aai.AAINQF199.AAINQF199ServiceInstance; -import org.onap.policy.aai.AAINQF199.AAINQF199Tenant; -import org.onap.policy.aai.AAINQF199.AAINQF199VfModule; -import org.onap.policy.aai.AAINQF199.AAINQF199VServer; -import org.onap.policy.aai.util.Serialization; - -import org.onap.policy.appc.CommonHeader; -import org.onap.policy.appc.Request; -import org.onap.policy.appc.Response; -import org.onap.policy.appc.ResponseCode; -import org.onap.policy.appc.ResponseStatus; -import org.onap.policy.appc.ResponseValue; - -import org.onap.policy.template.demo.EventManager; -import org.onap.policy.vnf.trafficgenerator.PGRequest; -import org.onap.policy.vnf.trafficgenerator.PGStream; -import org.onap.policy.vnf.trafficgenerator.PGStreams; - -import org.onap.policy.mso.MSOManager; -import org.onap.policy.mso.MSORequest; -import org.onap.policy.mso.MSORequestStatus; -import org.onap.policy.mso.MSORequestDetails; -import org.onap.policy.mso.MSOModelInfo; -import org.onap.policy.mso.MSOCloudConfiguration; -import org.onap.policy.mso.MSORequestInfo; -import org.onap.policy.mso.MSORequestParameters; -import org.onap.policy.mso.MSORelatedInstanceListElement; -import org.onap.policy.mso.MSORelatedInstance; -import org.onap.policy.mso.MSOResponse; - -import org.onap.policy.drools.system.PolicyEngine; - -// -// These parameters are required to build the runtime policy -// -declare Params - closedLoopControlName : String - actor : String - aaiURL : String - aaiUsername : String - aaiPassword : String - msoURL : String - msoUsername : String - msoPassword : String - aaiNamedQueryUUID : String - aaiPatternMatch : int - notificationTopic : String - appcTopic : String -end - -/* -* -* Called once and only once to insert the parameters into working memory for this Closed Loop policy. -* (Comment SETUP rule out for the first ONAP opensource release since policy BRMS_GW already puts a Params fact in there) -* -* -*rule "${policyName}.SETUP" -* when -* then -* System.out.println("rule SETUP is triggered."); -* Params params = new Params(); -* params.setClosedLoopControlName("${closedLoopControlName}"); -* params.setActor("${actor}"); -* params.setAaiURL("${aaiURL}"); -* params.setAaiUsername("${aaiUsername}"); -* params.setAaiPassword("${aaiPassword}"); -* params.setMsoURL("${msoURL}"); -* params.setMsoUsername("${msoUsername}"); -* params.setMsoPassword("${msoPassword}"); -* params.setAaiNamedQueryUUID("${aaiNamedQueryUUID}"); -* params.setAaiPatternMatch(${aaiPatternMatch}); -* params.setNotificationTopic("${notificationTopic}"); -* params.setAppcTopic("${appcTopic}"); -* // -* // This stays in memory as long as the rule is alive and running -* // -* insert(params); -*end -*/ -/* -* -* This rule responds to DCAE Events -* -*/ -rule "${policyName}.EVENT" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - not ( EventManager( closedLoopControlName == $event.closedLoopControlName )) - then - System.out.println("rule EVENT is triggered."); - try { - // - // Check the requestID in the event to make sure it is not null before we create the EventManager. - // The EventManager will do extra syntax checking as well check if the closed loop is disabled/ - // - if ($event.requestID == null) { - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.REJECTED; - notification.from = "policy"; - notification.message = "Missing requestID from DCAE event"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver notification: " + notification); - } - // - // Retract it from memory - // - retract($event); - System.out.println("Event with requestID=null has been retracted."); - } else { - // - // Create an EventManager - // - EventManager manager = new EventManager($params.getClosedLoopControlName(), $event.requestID, $event.target); - // - // Determine if EventManager can actively process the event (i.e. syntax) - // - VirtualControlLoopNotification notification = manager.activate($event); - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - // Are we actively pursuing this event? - // - if (notification.notification == ControlLoopNotificationType.ACTIVE) { - // - // Insert Event Manager into memory, this will now kick off processing. - // - insert(manager); - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver notification: " + notification); - } - } else { - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver notification: " + notification); - } - // - // Retract it from memory - // - retract($event); - } - // - // Now that the manager is inserted into Drools working memory, we'll wait for - // another rule to fire in order to continue processing. This way we can also - // then screen for additional ONSET and ABATED events for this same RequestIDs - // and for different RequestIDs but with the same closedLoopControlName and target. - // - } - // - } catch (Exception e) { - e.printStackTrace(); - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.REJECTED; - notification.message = "Exception occurred " + e.getMessage(); - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - // - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e1) { - System.out.println("Can't deliver notification: " + notification); - e1.printStackTrace(); - } - // - // Retract the event - // - retract($event); - } -end - -/* -* -* This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager -* is created. We can start the operations for this closed loop. -* -*/ -rule "${policyName}.EVENT.MANAGER" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName, controlLoopResult == null) - then - System.out.println("rule EVENT.MANAGER is triggered."); - // - // Check which event this is. - // - EventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event); - // - // We only want the initial ONSET event in memory, - // all the other events need to be retracted to support - // cleanup and avoid the other rules being fired for this event. - // - if (eventStatus != EventManager.NEW_EVENT_STATUS.FIRST_ONSET) { - System.out.println("Retracting "+eventStatus+" Event."); - retract($event); - return; - } - // - // Now the event in memory is first onset event - // - try { - // - // Pull the known AAI field from the Event - // - // generic-vnf is needed for vFirewall case - // vserver-name is needed for vLoadBalancer case - // - String genericVNF = $event.AAI.get("generic-vnf.vnf-id"); - String vserver = $event.AAI.get("vserver.vserver-name"); - // - // Check if we are implementing a simple pattern match. - // - if ($params.getAaiPatternMatch() == 1) { - // - // Yes - // - //Basic naming characteristics: - //VF Name (9 char)+VM name (13 char total)+VFC (19 char total) - //Example: - //VF Name (9 characters): cscf0001v - //VM Name(13 characters): cscf0001vm001 - //VFC name(19 characters): cscf0001vm001cfg001 - // - // zdfw1fwl01fwl02 or zdfw1fwl01fwl01 - // replaced with - // zdfw1fwl01pgn02 or zdfw1fwl01pgn01 - // - int index = genericVNF.lastIndexOf("fwl"); - if (index == -1) { - System.err.println("The generic-vnf.vnf-id from DCAE Event is not valid."); - } else { - genericVNF = genericVNF.substring(0, index) + "pgn" + genericVNF.substring(index+"fwl".length()); - } - // - // Construct an APPC request - // - ControlLoopOperation operation = new ControlLoopOperation(); - operation.actor = $params.getActor(); - operation.operation = "ModifyConfig"; - operation.target = $event.target; - // - // Create operationWrapper - // - ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); - // - // insert operationWrapper into memory - // - insert(operationWrapper); - // - Request request = new Request(); - request.CommonHeader = new CommonHeader(); - request.CommonHeader.RequestID = $event.requestID; - request.Action = operation.operation; - request.Payload = new HashMap<String, Object>(); - // - // Fill in the payload - // - request.Payload.put("generic-vnf.vnf-id", genericVNF); - // - PGRequest pgRequest = new PGRequest(); - pgRequest.pgStreams = new PGStreams(); - - PGStream pgStream; - for(int i = 0; i < 5; i++){ - pgStream = new PGStream(); - pgStream.streamId = "fw_udp"+(i+1); - pgStream.isEnabled = "true"; - pgRequest.pgStreams.pgStream.add(pgStream); - } - request.Payload.put("pg-streams", pgRequest.pgStreams); - - if (request != null) { - // - // Insert request into memory - // - insert(request); - // - // Tell interested parties we are performing this Operation - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.OPERATION; - // message and history ?? - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Now send the operation request - // - if (request instanceof Request) { - try { - System.out.println("APPC request sent:"); - System.out.println(Serialization.gsonPretty.toJson(request)); - PolicyEngine.manager.deliver($params.getAppcTopic(), request); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver request: " + request); - } - } - } else { - // - // what happens if it is null - // - } - // - } else { - // - // create AAI named-query request with UUID started with "F199" - // - AAINQF199Request aainqf199request = new AAINQF199Request(); - AAINQF199QueryParameters aainqf199queryparam = new AAINQF199QueryParameters(); - AAINQF199NamedQuery aainqf199namedquery = new AAINQF199NamedQuery(); - AAINQF199InstanceFilters aainqf199instancefilter = new AAINQF199InstanceFilters(); - // - // queryParameters - // - aainqf199namedquery.namedQueryUUID = UUID.fromString($params.getAaiNamedQueryUUID()); - aainqf199queryparam.namedQuery = aainqf199namedquery; - aainqf199request.queryParameters = aainqf199queryparam; - // - // instanceFilters - // - Map aainqf199instancefiltermap = new HashMap(); - Map aainqf199instancefiltermapitem = new HashMap(); - aainqf199instancefiltermapitem.put("vserver-name", vserver); - aainqf199instancefiltermap.put("vserver", aainqf199instancefiltermapitem); - aainqf199instancefilter.instanceFilter.add(aainqf199instancefiltermap); - aainqf199request.instanceFilters = aainqf199instancefilter; - // - // print aainqf199request for debug - // - System.out.println("AAI Request sent:"); - System.out.println(Serialization.gsonPretty.toJson(aainqf199request)); - // - // Create AAINQF199RequestWrapper - // - AAINQF199RequestWrapper aainqf199RequestWrapper = new AAINQF199RequestWrapper($event.requestID, aainqf199request); - // - // insert aainqf199request into memory - // - insert(aainqf199RequestWrapper); - } - // - } catch (Exception e) { - e.printStackTrace(); - } -end - -/* -* -* This rule happens when we got a valid ONSET, closed loop is enabled, an Event Manager -* is created, AAI Manager and AAI Request are ready in memory. We can start sending query to AAI and then wait for response. -* -*/ -rule "${policyName}.EVENT.MANAGER.AAINQF199REQUEST" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID) - then - System.out.println("rule EVENT.MANAGER.AAINQF199REQUEST is triggered."); - // - // send the request - // - AAINQF199Response aainqf199response = AAINQF199Manager.postQuery($params.getAaiURL(), $params.getAaiUsername(), $params.getAaiPassword(), - $aainqf199RequestWrapper.aainqf199request, $event.requestID); - // - // Check AAI response - // - if (aainqf199response == null) { - System.err.println("Failed to get AAI response"); - // - // Fail and retract everything - // - retract($event); - retract($manager); - retract($aainqf199RequestWrapper); - } else { - // - // Create AAINQF199ResponseWrapper - // - AAINQF199ResponseWrapper aainqf199ResponseWrapper = new AAINQF199ResponseWrapper($event.requestID, aainqf199response); - // - // insert aainqf199ResponseWrapper to memeory - // - insert(aainqf199ResponseWrapper); - } +rule "INIT" +when +then + insert("This is a test"); end -/* -* -* This rule happens when we got a valid AAI response. We can start sending request to APPC or MSO now. -* -*/ -rule "${policyName}.EVENT.MANAGER.AAINQF199RESPONSE" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID) - $aainqf199ResponseWrapper : AAINQF199ResponseWrapper(requestID == $event.requestID) - then - System.out.println("rule EVENT.MANAGER.AAINQF199RESPONSE is triggered."); - // - // Extract related fields out of AAINQF199RESPONSE - // - String vnfItemVnfId, vnfItemVnfType, vnfItemPersonaModelId, vnfItemPersonaModelVersion, vnfItemModelName, - vnfItemModelVersion, vnfItemModelNameVersionId, serviceItemServiceInstanceId, serviceItemPersonaModelId, - serviceItemModelName, serviceItemModelType, serviceItemModelVersion, serviceItemModelNameVersionId, - vfModuleItemVfModuleName, vfModuleItemPersonaModelId, vfModuleItemPersonaModelVersion, vfModuleItemModelName, - vfModuleItemModelNameVersionId, tenantItemTenantId, cloudRegionItemCloudRegionId; - try { - // - // vnfItem - // - vnfItemVnfId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfID; - vnfItemVnfType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfType; - vnfItemVnfType = vnfItemVnfType.substring(vnfItemVnfType.lastIndexOf("/")+1); - vnfItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelId; - vnfItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelVersion; - vnfItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue; - vnfItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(2).propertyValue; - vnfItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue; - // - // serviceItem - // - serviceItemServiceInstanceId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.serviceInstanceID; - serviceItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelId; - serviceItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue; - serviceItemModelType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(1).propertyValue; - serviceItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelVersion; - serviceItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue; - // - // Find the index for base vf module and non-base vf module - // - int baseIndex = -1; - int nonBaseIndex = -1; - List<AAINQF199InventoryResponseItem> inventoryItems = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems; - for (AAINQF199InventoryResponseItem m : inventoryItems) { - if (m.vfModule != null && m.vfModule.isBaseVfModule == true) { - baseIndex = inventoryItems.indexOf(m); - } else if (m.vfModule != null && m.vfModule.isBaseVfModule == false && m.vfModule.orchestrationStatus == null) { - nonBaseIndex = inventoryItems.indexOf(m); - } - // - if (baseIndex != -1 && nonBaseIndex != -1) { - break; - } - } - // - // Report the error if either base vf module or non-base vf module is not found - // - if (baseIndex == -1 || nonBaseIndex == -1) { - System.err.println("Either base or non-base vf module is not found from AAI response."); - retract($aainqf199RequestWrapper); - retract($aainqf199ResponseWrapper); - retract($manager); - retract($event); - return; - } - // - // This comes from the base module - // - vfModuleItemVfModuleName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(baseIndex).vfModule.vfModuleName; - vfModuleItemVfModuleName = vfModuleItemVfModuleName.replace("Vfmodule", "vDNS"); - // - // vfModuleItem - NOT the base module - // - vfModuleItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelId; - vfModuleItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelVersion; - vfModuleItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(0).propertyValue; - vfModuleItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(4).propertyValue; - // - // tenantItem - // - tenantItemTenantId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).tenant.tenantId; - // - // cloudRegionItem - // - cloudRegionItemCloudRegionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).items.inventoryResponseItems.get(0).cloudRegion.cloudRegionId; - // - } catch (Exception e) { - e.printStackTrace(); - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.REJECTED; - notification.message = "Exception occurred " + e.getMessage(); - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e1) { - System.out.println("Can't deliver notification: " + notification); - e1.printStackTrace(); - } - // - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - notification.message = "Invalid named-query response from AAI"; - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e1) { - System.out.println("Can't deliver notification: " + notification); - e1.printStackTrace(); - } - // - // Retract everything - // - retract($aainqf199RequestWrapper); - retract($aainqf199ResponseWrapper); - retract($manager); - retract($event); - return; - } - // - // Extracted fields should not be null - // - if ((vnfItemVnfId == null) || (vnfItemVnfType == null) || - (vnfItemPersonaModelId == null) || (vnfItemModelName == null) || - (vnfItemModelVersion == null) || (vnfItemModelNameVersionId == null) || - (serviceItemServiceInstanceId == null) || (serviceItemModelName == null) || - (serviceItemModelType == null) || (serviceItemModelVersion == null) || - (serviceItemModelNameVersionId == null) || (vfModuleItemVfModuleName == null) || - (vfModuleItemPersonaModelId == null) || (vfModuleItemPersonaModelVersion == null) || - (vfModuleItemModelName == null) || (vfModuleItemModelNameVersionId == null) || - (tenantItemTenantId == null) || (cloudRegionItemCloudRegionId == null)) { - // - System.err.println("some fields are missing from AAI response."); - // - // Fail and retract everything - // - retract($aainqf199RequestWrapper); - retract($aainqf199ResponseWrapper); - retract($manager); - retract($event); - return; - } - // - // We don't need them any more - // - retract($aainqf199ResponseWrapper); - retract($aainqf199RequestWrapper); - // - // check the actor of this closed loop - // - switch ($params.getActor()) { - case "APPC": - { - // - // Construct an APPC request - // - ControlLoopOperation operation = new ControlLoopOperation(); - operation.actor = $params.getActor(); - operation.operation = "ModifyConfig"; - operation.target = $event.target; - // - // Create operationWrapper - // - ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); - // - // insert operationWrapper into memory - // - insert(operationWrapper); - // - Request request = new Request(); - request.CommonHeader = new CommonHeader(); - request.CommonHeader.RequestID = $event.requestID; - request.Action = operation.operation; - request.Payload = new HashMap<String, Object>(); - // - // Fill in the payload - // Hardcode genericVNF for now since AAI has not been ready for vFirewall demo case - // - String genericVNF = "zdfw1fwl01pgn02"; - request.Payload.put("generic-vnf.vnf-id", genericVNF); - // - PGRequest pgRequest = new PGRequest(); - pgRequest.pgStreams = new PGStreams(); - - PGStream pgStream; - for(int i = 0; i < 5; i++){ - pgStream = new PGStream(); - pgStream.streamId = "fw_udp"+(i+1); - pgStream.isEnabled = "true"; - pgRequest.pgStreams.pgStream.add(pgStream); - } - request.Payload.put("pg-streams", pgRequest.pgStreams); - - if (request != null) { - // - // Insert request into memory - // - insert(request); - // - // Tell interested parties we are performing this Operation - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.OPERATION; - // message and history ?? - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Now send the operation request - // - if (request instanceof Request) { - try { - System.out.println("APPC request sent:"); - System.out.println(Serialization.gsonPretty.toJson(request)); - PolicyEngine.manager.deliver($params.getAppcTopic(), request); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver request: " + request); - } - } - } else { - // - // what happens if it is null - // - } - } - break; - case "MSO": - { - // - // Construct an operation - // - ControlLoopOperation operation = new ControlLoopOperation(); - operation.actor = $params.getActor(); - operation.operation = "createModuleInstance"; - operation.target = $event.target; - // - // Create operationWrapper - // - ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); - // - // Construct an MSO request - // - MSORequest request = new MSORequest(); - request.requestDetails = new MSORequestDetails(); - request.requestDetails.modelInfo = new MSOModelInfo(); - request.requestDetails.cloudConfiguration = new MSOCloudConfiguration(); - request.requestDetails.requestInfo = new MSORequestInfo(); - request.requestDetails.requestParameters = new MSORequestParameters(); - request.requestDetails.requestParameters.userParams = null; - // - // cloudConfiguration - // - request.requestDetails.cloudConfiguration.lcpCloudRegionId = cloudRegionItemCloudRegionId; - request.requestDetails.cloudConfiguration.tenantId = tenantItemTenantId; - // - // modelInfo - // - request.requestDetails.modelInfo.modelType = "vfModule"; - request.requestDetails.modelInfo.modelInvariantId = vfModuleItemPersonaModelId; - request.requestDetails.modelInfo.modelNameVersionId = vfModuleItemModelNameVersionId; - request.requestDetails.modelInfo.modelName = vfModuleItemModelName; - request.requestDetails.modelInfo.modelVersion = vfModuleItemPersonaModelVersion; - // - // requestInfo - // - request.requestDetails.requestInfo.instanceName = vfModuleItemVfModuleName; - request.requestDetails.requestInfo.source = "POLICY"; - request.requestDetails.requestInfo.suppressRollback = false; - // - // relatedInstanceList - // - MSORelatedInstanceListElement relatedInstanceListElement1 = new MSORelatedInstanceListElement(); - MSORelatedInstanceListElement relatedInstanceListElement2 = new MSORelatedInstanceListElement(); - relatedInstanceListElement1.relatedInstance = new MSORelatedInstance(); - relatedInstanceListElement2.relatedInstance = new MSORelatedInstance(); - // - relatedInstanceListElement1.relatedInstance.instanceId = serviceItemServiceInstanceId; - relatedInstanceListElement1.relatedInstance.modelInfo = new MSOModelInfo(); - relatedInstanceListElement1.relatedInstance.modelInfo.modelType = "service"; - relatedInstanceListElement1.relatedInstance.modelInfo.modelInvariantId = serviceItemPersonaModelId; - relatedInstanceListElement1.relatedInstance.modelInfo.modelNameVersionId = serviceItemModelNameVersionId; - relatedInstanceListElement1.relatedInstance.modelInfo.modelName = serviceItemModelName; - relatedInstanceListElement1.relatedInstance.modelInfo.modelVersion = serviceItemModelVersion; - // - relatedInstanceListElement2.relatedInstance.instanceId = vnfItemVnfId; - relatedInstanceListElement2.relatedInstance.modelInfo = new MSOModelInfo(); - relatedInstanceListElement2.relatedInstance.modelInfo.modelType = "vnf"; - relatedInstanceListElement2.relatedInstance.modelInfo.modelInvariantId = vnfItemPersonaModelId; - relatedInstanceListElement2.relatedInstance.modelInfo.modelNameVersionId = vnfItemModelNameVersionId; - relatedInstanceListElement2.relatedInstance.modelInfo.modelName = vnfItemModelName; - relatedInstanceListElement2.relatedInstance.modelInfo.modelVersion = vnfItemModelVersion; - relatedInstanceListElement2.relatedInstance.modelInfo.modelCustomizationName = vnfItemVnfType; - // - request.requestDetails.relatedInstanceList.add(relatedInstanceListElement1); - request.requestDetails.relatedInstanceList.add(relatedInstanceListElement2); - // - // print MSO request for debug - // - System.out.println("MSO request sent:"); - System.out.println(Serialization.gsonPretty.toJson(request)); - // - // - // - if (request != null) { - // - // Tell interested parties we are performing this Operation - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.OPERATION; - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Concatenate serviceItemServiceInstanceId and vnfItemVnfId to msoURL - // - String MSOUrl = $params.getMsoURL() + "/serviceInstances/v2/" + serviceItemServiceInstanceId + "/vnfs/" + vnfItemVnfId + "/vfModules"; - // - // Call MSO - // - MSOResponse response = MSOManager.createModuleInstance(MSOUrl, $params.getMsoURL(), $params.getMsoUsername(), $params.getMsoPassword(), request); - // - if (response != null) { - // - // Assign requestId - // - request.requestId = $event.requestID.toString(); - response.request.requestId = $event.requestID.toString(); - // - // Insert facts - // - insert(operationWrapper); - insert(request); - insert(response); - } else { - // - // MSO request not even accepted - // - notification.message = operationWrapper.operation.toMessage(); - operationWrapper.operation.message = operationWrapper.operation.toMessage(); - operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - notification.history.add(operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Retract everything - // - retract($event); - retract($manager); - } - } else { - System.err.println("constructed MSO request is invalid."); - } - } - break; - } +rule "PRINT_MSG" +when + $o : Object(); +then + System.out.println("MSG: " + $o); + retract($o); end - -/* -* -* This rule responds to APPC Response Events -* -*/ -rule "${policyName}.APPC.RESPONSE" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID ) - $request : Request( getCommonHeader().RequestID == $event.requestID ) - $response : Response( getCommonHeader().RequestID == $event.requestID ) - then - System.out.println("rule APPC.RESPONSE is triggered."); - if ($response.Status == null) { - $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - } - // - // Get the Response Code - // - ResponseCode code = ResponseCode.toResponseCode($response.Status.Code); - if (code == null) { - $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - } - // - // Construct notification - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - notification.message = $operationWrapper.operation.toMessage(); - $operationWrapper.operation.message = $operationWrapper.operation.toMessage(); - // - // Ok, let's figure out what APP-C's response is - // - switch (code) { - case ACCEPT: - $operationWrapper.operation.outcome = "PROCESSING"; - break; - case ERROR: - case REJECT: - $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - break; - case SUCCESS: - $operationWrapper.operation.outcome = "SUCCESS"; - $manager.setControlLoopResult("SUCCESS"); - break; - case FAILURE: - $operationWrapper.operation.outcome = "FAILURE"; - $manager.setControlLoopResult("FAILURE"); - break; - } - if ($operationWrapper.operation.outcome.equals("SUCCESS")) { - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_SUCCESS; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } else if ($operationWrapper.operation.outcome.equals("PROCESSING")) { - retract($response); - } else { - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } - -end - -/* -* -* This rule is used to clean up APPC response -* -*/ -rule "${policyName}.APPC.RESPONSE.CLEANUP" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $response : Response($id : getCommonHeader().RequestID ) - not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) ) - then - System.out.println("rule APPC.RESPONSE.CLEANUP is triggered."); - retract($response); -end - -/* -* -* This rule responds to MSO Response Events -* -*/ -rule "${policyName}.MSO.RESPONSE" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID ) - $request : MSORequest( requestId == $event.requestID.toString() ) - $response : MSOResponse( request.requestId == $event.requestID.toString() ) - then - System.out.println("rule MSO.RESPONSE is triggered."); - // - // Construct notification - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - notification.message = $operationWrapper.operation.toMessage(); - $operationWrapper.operation.message = $operationWrapper.operation.toMessage(); - // - // The operation can either be succeeded or failed - // - if($response.request.requestStatus.requestState.equals("Completed")) { - $operationWrapper.operation.outcome = "SUCCESS"; - $manager.setControlLoopResult("SUCCESS"); - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_SUCCESS; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } else { - $operationWrapper.operation.outcome = "FAILURE"; - $manager.setControlLoopResult("FAILURE"); - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } - -end diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/ImportParameters.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/ImportParameters.java index 51398f32d..8cf57e1a1 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/ImportParameters.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/ImportParameters.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -25,152 +25,153 @@ import java.util.UUID; /** * <code>ImportParameters</code> defines the Policy Engine Import Parameters - * which are required to import a new Policy Service or Value. - * + * which are required to import a new Policy Service or Value. + * * @version 0.1 */ public class ImportParameters { - private String serviceName; - private String description; - private UUID requestID; - private String filePath; - private String version; - private IMPORT_TYPE importType; - - public enum IMPORT_TYPE { - MICROSERVICE, - BRMSPARAM, - OPTIMIZATION - } - - /** - * Sets Import Policy Parameters. - * - * @param serviceName the <code>String</code> format of the Service Name - * @param description the <code>String</code> format of the i Description - * @param requestID unique request ID which will be passed throughout the ONAP components to correlate logging messages. - * @param filePath the <code>List</code> format of the file paths for the service files - * @param importType the {@link IMPORT_TYPE} format of the Policy Service List - * @param version the <code>String</code> format of the Policy Import Version - * A different request ID should be passed for each request. - */ - public void setImportParameters(String serviceName, String description, UUID requestID, String filePath, IMPORT_TYPE importType, String version){ - - this.setServiceName(serviceName); - this.setDescription(description); - this.setRequestID(requestID); - this.setFilePath(filePath); - this.setServiceType(importType); - this.setVersion(version); - - } - - /** - * Gets the Policy Service of the Policy Service Import Parameters. - * - * @return serviceName the <code>String</code> format of the Policy Service Name - */ - public String getServiceName() { - return serviceName; - } - - /** - * Sets the serviceName of the Policy Service Parameters. - * - * @param serviceName the <code>String</code> format of the Policy Service Name - */ - public void setServiceName(String serviceName) { - this.serviceName = serviceName; - } - - /** - * Gets the Policy Import Description. - * - * @return description the <code>String</code> format of the Policy Import Description - */ - public String getDescription() { - return description; - } - - /** - * Sets the Description of the new Policy Import Description. - * - * @param description the <code>String</code> format of the Policy Import Description - */ - public void setDescription(String description) { - this.description = description; - } - - /** - * Gets the requestID of the Policy Parameters. - * - * @return unique request ID which will be passed throughout the ONAP components to correlate logging messages. - */ - public UUID getRequestID() { - return requestID; - } - - /** - * Sets the requestID of the Policy Parameters. - * - * @param requestID unique request ID which will be passed throughout the ONAP components to correlate logging messages. - */ - public void setRequestID(UUID requestID) { - this.requestID = requestID; - } - - /** - * Gets the List of File Paths of the new import. - * - * @return filePath the <code>List</code> format of the Policy Import File - */ - public String getFilePath() { - return filePath; - } - - /** - * Sets the policy Import File List of the new Policy Import. - * - * @param filePath the <code>List</code> format of the Policy Import File - */ - public void setFilePath(String filePath) { - this.filePath = filePath; - } - - /** - * Gets the Service Type of the new policy import. - * - * @return ImportType {@link IMPORT_TYPE} format of the Policy Service List - */ - public IMPORT_TYPE getServiceType() { - return importType; - } - - /** - * Sets the policy Service Type of the new Policy Service. - * - * @param enumImportType the <code>enumServiceType</code> format of the Policy Service List - */ - public void setServiceType(IMPORT_TYPE enumImportType) { - this.importType = enumImportType; - } - - /** - * - * Gets the Import Version of the new policy import. - * - * @return version the <code>String</code> format of the Policy Import Version - */ - public String getVersion() { - return version; - } - - /** - * Sets the policy Import Version of the new Policy Import. - * - * @param version the <code>String</code> format of the Policy Import Version - */ - public void setVersion(String version) { - this.version = version; - } + + private String serviceName; + private String description; + private UUID requestID; + private String filePath; + private String version; + private IMPORT_TYPE importType; + + public enum IMPORT_TYPE { + MICROSERVICE, BRMSPARAM, OPTIMIZATION + } + + /** + * Sets Import Policy Parameters. + * + * @param serviceName the <code>String</code> format of the Service Name + * @param description the <code>String</code> format of the i Description + * @param requestID unique request ID which will be passed throughout the ONAP components to correlate logging + * messages. + * @param filePath the <code>List</code> format of the file paths for the service files + * @param importType the {@link IMPORT_TYPE} format of the Policy Service List + * @param version the <code>String</code> format of the Policy Import Version + * A different request ID should be passed for each request. + */ + public void setImportParameters(String serviceName, String description, UUID requestID, String filePath, + IMPORT_TYPE importType, String version) { + + this.setServiceName(serviceName); + this.setDescription(description); + this.setRequestID(requestID); + this.setFilePath(filePath); + this.setServiceType(importType); + this.setVersion(version); + + } + + /** + * Gets the Policy Service of the Policy Service Import Parameters. + * + * @return serviceName the <code>String</code> format of the Policy Service Name + */ + public String getServiceName() { + return serviceName; + } + + /** + * Sets the serviceName of the Policy Service Parameters. + * + * @param serviceName the <code>String</code> format of the Policy Service Name + */ + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + /** + * Gets the Policy Import Description. + * + * @return description the <code>String</code> format of the Policy Import Description + */ + public String getDescription() { + return description; + } + + /** + * Sets the Description of the new Policy Import Description. + * + * @param description the <code>String</code> format of the Policy Import Description + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Gets the requestID of the Policy Parameters. + * + * @return unique request ID which will be passed throughout the ONAP components to correlate logging messages. + */ + public UUID getRequestID() { + return requestID; + } + + /** + * Sets the requestID of the Policy Parameters. + * + * @param requestID unique request ID which will be passed throughout the ONAP components to correlate logging + * messages. + */ + public void setRequestID(UUID requestID) { + this.requestID = requestID; + } + + /** + * Gets the List of File Paths of the new import. + * + * @return filePath the <code>List</code> format of the Policy Import File + */ + public String getFilePath() { + return filePath; + } + + /** + * Sets the policy Import File List of the new Policy Import. + * + * @param filePath the <code>List</code> format of the Policy Import File + */ + public void setFilePath(String filePath) { + this.filePath = filePath; + } + + /** + * Gets the Service Type of the new policy import. + * + * @return ImportType {@link IMPORT_TYPE} format of the Policy Service List + */ + public IMPORT_TYPE getServiceType() { + return importType; + } + + /** + * Sets the policy Service Type of the new Policy Service. + * + * @param enumImportType the <code>enumServiceType</code> format of the Policy Service List + */ + public void setServiceType(IMPORT_TYPE enumImportType) { + this.importType = enumImportType; + } + + /** + * Gets the Import Version of the new policy import. + * + * @return version the <code>String</code> format of the Policy Import Version + */ + public String getVersion() { + return version; + } + + /** + * Sets the policy Import Version of the new Policy Import. + * + * @param version the <code>String</code> format of the Policy Import Version + */ + public void setVersion(String version) { + this.version = version; + } } diff --git a/PolicyEngineClient/src/test/java/org/onap/policyengine/ImportBRMSTemplate.java b/PolicyEngineClient/src/test/java/org/onap/policyengine/ImportBRMSTemplate.java index 2e5f0e169..9f132d36b 100644 --- a/PolicyEngineClient/src/test/java/org/onap/policyengine/ImportBRMSTemplate.java +++ b/PolicyEngineClient/src/test/java/org/onap/policyengine/ImportBRMSTemplate.java @@ -38,15 +38,14 @@ public class ImportBRMSTemplate { public static void main(String[] args) { try { ImportParameters importParameters = new ImportParameters(); - importParameters.setFilePath("testResources" + File.separator + "test.drl"); + importParameters.setFilePath("resources" + File.separator + "test.drl"); importParameters.setServiceName("testTemplate"); importParameters.setRequestID(UUID.randomUUID()); importParameters.setServiceType(IMPORT_TYPE.BRMSPARAM); // API method to create Policy or update policy - PolicyChangeResponse response = null; PolicyEngine policyEngine = new PolicyEngine("config.properties"); - response = policyEngine.policyEngineImport(importParameters); + PolicyChangeResponse response = policyEngine.policyEngineImport(importParameters); System.out.println(response.getResponseMessage()); } catch (Exception e) { diff --git a/PolicyEngineClient/src/test/resources/test.drl b/PolicyEngineClient/src/test/resources/test.drl new file mode 100644 index 000000000..f46a7fe50 --- /dev/null +++ b/PolicyEngineClient/src/test/resources/test.drl @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policyengine; + +rule "INIT" +when +then + insert("This is a test"); +end + +rule "PRINT_MSG" +when + $o : Object(); +then + System.out.println("MSG: " + $o); + retract($o); +end diff --git a/PolicyEngineClient/testResources/test.drl b/PolicyEngineClient/testResources/test.drl deleted file mode 100644 index 5253cf8fb..000000000 --- a/PolicyEngineClient/testResources/test.drl +++ /dev/null @@ -1,1121 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * archetype-closed-loop-demo-rules - * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.controlloop; - -import java.util.List; -import java.util.LinkedList; -import java.util.Map; -import java.util.HashMap; -import java.util.UUID; - -import org.onap.policy.controlloop.VirtualControlLoopEvent; -import org.onap.policy.controlloop.ControlLoopEventStatus; -import org.onap.policy.controlloop.VirtualControlLoopNotification; -import org.onap.policy.controlloop.ControlLoopNotificationType; -import org.onap.policy.controlloop.ControlLoopOperation; -import org.onap.policy.controlloop.ControlLoopOperationWrapper; -import org.onap.policy.template.demo.ControlLoopException; - -import org.onap.policy.aai.AAINQF199.AAINQF199CloudRegion; -import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperties; -import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperty; -import org.onap.policy.aai.AAINQF199.AAINQF199GenericVNF; -import org.onap.policy.aai.AAINQF199.AAINQF199InstanceFilters; -import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItem; -import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItems; -import org.onap.policy.aai.AAINQF199.AAINQF199Manager; -import org.onap.policy.aai.AAINQF199.AAINQF199NamedQuery; -import org.onap.policy.aai.AAINQF199.AAINQF199QueryParameters; -import org.onap.policy.aai.AAINQF199.AAINQF199Request; -import org.onap.policy.aai.AAINQF199.AAINQF199RequestWrapper; -import org.onap.policy.aai.AAINQF199.AAINQF199Response; -import org.onap.policy.aai.AAINQF199.AAINQF199ResponseWrapper; -import org.onap.policy.aai.AAINQF199.AAINQF199ServiceInstance; -import org.onap.policy.aai.AAINQF199.AAINQF199Tenant; -import org.onap.policy.aai.AAINQF199.AAINQF199VfModule; -import org.onap.policy.aai.AAINQF199.AAINQF199VServer; -import org.onap.policy.aai.util.Serialization; - -import org.onap.policy.appc.CommonHeader; -import org.onap.policy.appc.Request; -import org.onap.policy.appc.Response; -import org.onap.policy.appc.ResponseCode; -import org.onap.policy.appc.ResponseStatus; -import org.onap.policy.appc.ResponseValue; - -import org.onap.policy.template.demo.EventManager; -import org.onap.policy.vnf.trafficgenerator.PGRequest; -import org.onap.policy.vnf.trafficgenerator.PGStream; -import org.onap.policy.vnf.trafficgenerator.PGStreams; - -import org.onap.policy.mso.MSOManager; -import org.onap.policy.mso.MSORequest; -import org.onap.policy.mso.MSORequestStatus; -import org.onap.policy.mso.MSORequestDetails; -import org.onap.policy.mso.MSOModelInfo; -import org.onap.policy.mso.MSOCloudConfiguration; -import org.onap.policy.mso.MSORequestInfo; -import org.onap.policy.mso.MSORequestParameters; -import org.onap.policy.mso.MSORelatedInstanceListElement; -import org.onap.policy.mso.MSORelatedInstance; -import org.onap.policy.mso.MSOResponse; - -import org.onap.policy.drools.system.PolicyEngine; - -// -// These parameters are required to build the runtime policy -// -declare Params - closedLoopControlName : String - actor : String - aaiURL : String - aaiUsername : String - aaiPassword : String - msoURL : String - msoUsername : String - msoPassword : String - aaiNamedQueryUUID : String - aaiPatternMatch : int - notificationTopic : String - appcTopic : String -end - -/* -* -* Called once and only once to insert the parameters into working memory for this Closed Loop policy. -* (Comment SETUP rule out for the first ONAP opensource release since policy BRMS_GW already puts a Params fact in there) -* -* -*rule "${policyName}.SETUP" -* when -* then -* System.out.println("rule SETUP is triggered."); -* Params params = new Params(); -* params.setClosedLoopControlName("${closedLoopControlName}"); -* params.setActor("${actor}"); -* params.setAaiURL("${aaiURL}"); -* params.setAaiUsername("${aaiUsername}"); -* params.setAaiPassword("${aaiPassword}"); -* params.setMsoURL("${msoURL}"); -* params.setMsoUsername("${msoUsername}"); -* params.setMsoPassword("${msoPassword}"); -* params.setAaiNamedQueryUUID("${aaiNamedQueryUUID}"); -* params.setAaiPatternMatch(${aaiPatternMatch}); -* params.setNotificationTopic("${notificationTopic}"); -* params.setAppcTopic("${appcTopic}"); -* // -* // This stays in memory as long as the rule is alive and running -* // -* insert(params); -*end -*/ -/* -* -* This rule responds to DCAE Events -* -*/ -rule "${policyName}.EVENT" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - not ( EventManager( closedLoopControlName == $event.closedLoopControlName )) - then - System.out.println("rule EVENT is triggered."); - try { - // - // Check the requestID in the event to make sure it is not null before we create the EventManager. - // The EventManager will do extra syntax checking as well check if the closed loop is disabled/ - // - if ($event.requestID == null) { - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.REJECTED; - notification.from = "policy"; - notification.message = "Missing requestID from DCAE event"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver notification: " + notification); - } - // - // Retract it from memory - // - retract($event); - System.out.println("Event with requestID=null has been retracted."); - } else { - // - // Create an EventManager - // - EventManager manager = new EventManager($params.getClosedLoopControlName(), $event.requestID, $event.target); - // - // Determine if EventManager can actively process the event (i.e. syntax) - // - VirtualControlLoopNotification notification = manager.activate($event); - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - // Are we actively pursuing this event? - // - if (notification.notification == ControlLoopNotificationType.ACTIVE) { - // - // Insert Event Manager into memory, this will now kick off processing. - // - insert(manager); - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver notification: " + notification); - } - } else { - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver notification: " + notification); - } - // - // Retract it from memory - // - retract($event); - } - // - // Now that the manager is inserted into Drools working memory, we'll wait for - // another rule to fire in order to continue processing. This way we can also - // then screen for additional ONSET and ABATED events for this same RequestIDs - // and for different RequestIDs but with the same closedLoopControlName and target. - // - } - // - } catch (Exception e) { - e.printStackTrace(); - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.REJECTED; - notification.message = "Exception occurred " + e.getMessage(); - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - // - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e1) { - System.out.println("Can't deliver notification: " + notification); - e1.printStackTrace(); - } - // - // Retract the event - // - retract($event); - } -end - -/* -* -* This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager -* is created. We can start the operations for this closed loop. -* -*/ -rule "${policyName}.EVENT.MANAGER" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName, controlLoopResult == null) - then - System.out.println("rule EVENT.MANAGER is triggered."); - // - // Check which event this is. - // - EventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event); - // - // We only want the initial ONSET event in memory, - // all the other events need to be retracted to support - // cleanup and avoid the other rules being fired for this event. - // - if (eventStatus != EventManager.NEW_EVENT_STATUS.FIRST_ONSET) { - System.out.println("Retracting "+eventStatus+" Event."); - retract($event); - return; - } - // - // Now the event in memory is first onset event - // - try { - // - // Pull the known AAI field from the Event - // - // generic-vnf is needed for vFirewall case - // vserver-name is needed for vLoadBalancer case - // - String genericVNF = $event.AAI.get("generic-vnf.vnf-id"); - String vserver = $event.AAI.get("vserver.vserver-name"); - // - // Check if we are implementing a simple pattern match. - // - if ($params.getAaiPatternMatch() == 1) { - // - // Yes - // - //Basic naming characteristics: - //VF Name (9 char)+VM name (13 char total)+VFC (19 char total) - //Example: - //VF Name (9 characters): cscf0001v - //VM Name(13 characters): cscf0001vm001 - //VFC name(19 characters): cscf0001vm001cfg001 - // - // zdfw1fwl01fwl02 or zdfw1fwl01fwl01 - // replaced with - // zdfw1fwl01pgn02 or zdfw1fwl01pgn01 - // - int index = genericVNF.lastIndexOf("fwl"); - if (index == -1) { - System.err.println("The generic-vnf.vnf-id from DCAE Event is not valid."); - } else { - genericVNF = genericVNF.substring(0, index) + "pgn" + genericVNF.substring(index+"fwl".length()); - } - // - // Construct an APPC request - // - ControlLoopOperation operation = new ControlLoopOperation(); - operation.actor = $params.getActor(); - operation.operation = "ModifyConfig"; - operation.target = $event.target; - // - // Create operationWrapper - // - ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); - // - // insert operationWrapper into memory - // - insert(operationWrapper); - // - Request request = new Request(); - request.CommonHeader = new CommonHeader(); - request.CommonHeader.RequestID = $event.requestID; - request.Action = operation.operation; - request.Payload = new HashMap<String, Object>(); - // - // Fill in the payload - // - request.Payload.put("generic-vnf.vnf-id", genericVNF); - // - PGRequest pgRequest = new PGRequest(); - pgRequest.pgStreams = new PGStreams(); - - PGStream pgStream; - for(int i = 0; i < 5; i++){ - pgStream = new PGStream(); - pgStream.streamId = "fw_udp"+(i+1); - pgStream.isEnabled = "true"; - pgRequest.pgStreams.pgStream.add(pgStream); - } - request.Payload.put("pg-streams", pgRequest.pgStreams); - - if (request != null) { - // - // Insert request into memory - // - insert(request); - // - // Tell interested parties we are performing this Operation - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.OPERATION; - // message and history ?? - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Now send the operation request - // - if (request instanceof Request) { - try { - System.out.println("APPC request sent:"); - System.out.println(Serialization.gsonPretty.toJson(request)); - PolicyEngine.manager.deliver($params.getAppcTopic(), request); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver request: " + request); - } - } - } else { - // - // what happens if it is null - // - } - // - } else { - // - // create AAI named-query request with UUID started with "F199" - // - AAINQF199Request aainqf199request = new AAINQF199Request(); - AAINQF199QueryParameters aainqf199queryparam = new AAINQF199QueryParameters(); - AAINQF199NamedQuery aainqf199namedquery = new AAINQF199NamedQuery(); - AAINQF199InstanceFilters aainqf199instancefilter = new AAINQF199InstanceFilters(); - // - // queryParameters - // - aainqf199namedquery.namedQueryUUID = UUID.fromString($params.getAaiNamedQueryUUID()); - aainqf199queryparam.namedQuery = aainqf199namedquery; - aainqf199request.queryParameters = aainqf199queryparam; - // - // instanceFilters - // - Map aainqf199instancefiltermap = new HashMap(); - Map aainqf199instancefiltermapitem = new HashMap(); - aainqf199instancefiltermapitem.put("vserver-name", vserver); - aainqf199instancefiltermap.put("vserver", aainqf199instancefiltermapitem); - aainqf199instancefilter.instanceFilter.add(aainqf199instancefiltermap); - aainqf199request.instanceFilters = aainqf199instancefilter; - // - // print aainqf199request for debug - // - System.out.println("AAI Request sent:"); - System.out.println(Serialization.gsonPretty.toJson(aainqf199request)); - // - // Create AAINQF199RequestWrapper - // - AAINQF199RequestWrapper aainqf199RequestWrapper = new AAINQF199RequestWrapper($event.requestID, aainqf199request); - // - // insert aainqf199request into memory - // - insert(aainqf199RequestWrapper); - } - // - } catch (Exception e) { - e.printStackTrace(); - } -end - -/* -* -* This rule happens when we got a valid ONSET, closed loop is enabled, an Event Manager -* is created, AAI Manager and AAI Request are ready in memory. We can start sending query to AAI and then wait for response. -* -*/ -rule "${policyName}.EVENT.MANAGER.AAINQF199REQUEST" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID) - then - System.out.println("rule EVENT.MANAGER.AAINQF199REQUEST is triggered."); - // - // send the request - // - AAINQF199Response aainqf199response = AAINQF199Manager.postQuery($params.getAaiURL(), $params.getAaiUsername(), $params.getAaiPassword(), - $aainqf199RequestWrapper.aainqf199request, $event.requestID); - // - // Check AAI response - // - if (aainqf199response == null) { - System.err.println("Failed to get AAI response"); - // - // Fail and retract everything - // - retract($event); - retract($manager); - retract($aainqf199RequestWrapper); - } else { - // - // Create AAINQF199ResponseWrapper - // - AAINQF199ResponseWrapper aainqf199ResponseWrapper = new AAINQF199ResponseWrapper($event.requestID, aainqf199response); - // - // insert aainqf199ResponseWrapper to memeory - // - insert(aainqf199ResponseWrapper); - } -end - -/* -* -* This rule happens when we got a valid AAI response. We can start sending request to APPC or MSO now. -* -*/ -rule "${policyName}.EVENT.MANAGER.AAINQF199RESPONSE" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID) - $aainqf199ResponseWrapper : AAINQF199ResponseWrapper(requestID == $event.requestID) - then - System.out.println("rule EVENT.MANAGER.AAINQF199RESPONSE is triggered."); - // - // Extract related fields out of AAINQF199RESPONSE - // - String vnfItemVnfId, vnfItemVnfType, vnfItemPersonaModelId, vnfItemPersonaModelVersion, vnfItemModelName, - vnfItemModelVersion, vnfItemModelNameVersionId, serviceItemServiceInstanceId, serviceItemPersonaModelId, - serviceItemModelName, serviceItemModelType, serviceItemModelVersion, serviceItemModelNameVersionId, - vfModuleItemVfModuleName, vfModuleItemPersonaModelId, vfModuleItemPersonaModelVersion, vfModuleItemModelName, - vfModuleItemModelNameVersionId, tenantItemTenantId, cloudRegionItemCloudRegionId; - try { - // - // vnfItem - // - vnfItemVnfId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfID; - vnfItemVnfType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfType; - vnfItemVnfType = vnfItemVnfType.substring(vnfItemVnfType.lastIndexOf("/")+1); - vnfItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelId; - vnfItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelVersion; - vnfItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue; - vnfItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(2).propertyValue; - vnfItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue; - // - // serviceItem - // - serviceItemServiceInstanceId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.serviceInstanceID; - serviceItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelId; - serviceItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue; - serviceItemModelType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(1).propertyValue; - serviceItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelVersion; - serviceItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue; - // - // Find the index for base vf module and non-base vf module - // - int baseIndex = -1; - int nonBaseIndex = -1; - List<AAINQF199InventoryResponseItem> inventoryItems = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems; - for (AAINQF199InventoryResponseItem m : inventoryItems) { - if (m.vfModule != null && m.vfModule.isBaseVfModule == true) { - baseIndex = inventoryItems.indexOf(m); - } else if (m.vfModule != null && m.vfModule.isBaseVfModule == false && m.vfModule.orchestrationStatus == null) { - nonBaseIndex = inventoryItems.indexOf(m); - } - // - if (baseIndex != -1 && nonBaseIndex != -1) { - break; - } - } - // - // Report the error if either base vf module or non-base vf module is not found - // - if (baseIndex == -1 || nonBaseIndex == -1) { - System.err.println("Either base or non-base vf module is not found from AAI response."); - retract($aainqf199RequestWrapper); - retract($aainqf199ResponseWrapper); - retract($manager); - retract($event); - return; - } - // - // This comes from the base module - // - vfModuleItemVfModuleName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(baseIndex).vfModule.vfModuleName; - vfModuleItemVfModuleName = vfModuleItemVfModuleName.replace("Vfmodule", "vDNS"); - // - // vfModuleItem - NOT the base module - // - vfModuleItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelId; - vfModuleItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelVersion; - vfModuleItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(0).propertyValue; - vfModuleItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(4).propertyValue; - // - // tenantItem - // - tenantItemTenantId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).tenant.tenantId; - // - // cloudRegionItem - // - cloudRegionItemCloudRegionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).items.inventoryResponseItems.get(0).cloudRegion.cloudRegionId; - // - } catch (Exception e) { - e.printStackTrace(); - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.REJECTED; - notification.message = "Exception occurred " + e.getMessage(); - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e1) { - System.out.println("Can't deliver notification: " + notification); - e1.printStackTrace(); - } - // - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - notification.message = "Invalid named-query response from AAI"; - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e1) { - System.out.println("Can't deliver notification: " + notification); - e1.printStackTrace(); - } - // - // Retract everything - // - retract($aainqf199RequestWrapper); - retract($aainqf199ResponseWrapper); - retract($manager); - retract($event); - return; - } - // - // Extracted fields should not be null - // - if ((vnfItemVnfId == null) || (vnfItemVnfType == null) || - (vnfItemPersonaModelId == null) || (vnfItemModelName == null) || - (vnfItemModelVersion == null) || (vnfItemModelNameVersionId == null) || - (serviceItemServiceInstanceId == null) || (serviceItemModelName == null) || - (serviceItemModelType == null) || (serviceItemModelVersion == null) || - (serviceItemModelNameVersionId == null) || (vfModuleItemVfModuleName == null) || - (vfModuleItemPersonaModelId == null) || (vfModuleItemPersonaModelVersion == null) || - (vfModuleItemModelName == null) || (vfModuleItemModelNameVersionId == null) || - (tenantItemTenantId == null) || (cloudRegionItemCloudRegionId == null)) { - // - System.err.println("some fields are missing from AAI response."); - // - // Fail and retract everything - // - retract($aainqf199RequestWrapper); - retract($aainqf199ResponseWrapper); - retract($manager); - retract($event); - return; - } - // - // We don't need them any more - // - retract($aainqf199ResponseWrapper); - retract($aainqf199RequestWrapper); - // - // check the actor of this closed loop - // - switch ($params.getActor()) { - case "APPC": - { - // - // Construct an APPC request - // - ControlLoopOperation operation = new ControlLoopOperation(); - operation.actor = $params.getActor(); - operation.operation = "ModifyConfig"; - operation.target = $event.target; - // - // Create operationWrapper - // - ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); - // - // insert operationWrapper into memory - // - insert(operationWrapper); - // - Request request = new Request(); - request.CommonHeader = new CommonHeader(); - request.CommonHeader.RequestID = $event.requestID; - request.Action = operation.operation; - request.Payload = new HashMap<String, Object>(); - // - // Fill in the payload - // Hardcode genericVNF for now since AAI has not been ready for vFirewall demo case - // - String genericVNF = "zdfw1fwl01pgn02"; - request.Payload.put("generic-vnf.vnf-id", genericVNF); - // - PGRequest pgRequest = new PGRequest(); - pgRequest.pgStreams = new PGStreams(); - - PGStream pgStream; - for(int i = 0; i < 5; i++){ - pgStream = new PGStream(); - pgStream.streamId = "fw_udp"+(i+1); - pgStream.isEnabled = "true"; - pgRequest.pgStreams.pgStream.add(pgStream); - } - request.Payload.put("pg-streams", pgRequest.pgStreams); - - if (request != null) { - // - // Insert request into memory - // - insert(request); - // - // Tell interested parties we are performing this Operation - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.OPERATION; - // message and history ?? - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Now send the operation request - // - if (request instanceof Request) { - try { - System.out.println("APPC request sent:"); - System.out.println(Serialization.gsonPretty.toJson(request)); - PolicyEngine.manager.deliver($params.getAppcTopic(), request); - } catch (Exception e) { - e.printStackTrace(); - System.out.println("Can't deliver request: " + request); - } - } - } else { - // - // what happens if it is null - // - } - } - break; - case "MSO": - { - // - // Construct an operation - // - ControlLoopOperation operation = new ControlLoopOperation(); - operation.actor = $params.getActor(); - operation.operation = "createModuleInstance"; - operation.target = $event.target; - // - // Create operationWrapper - // - ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); - // - // Construct an MSO request - // - MSORequest request = new MSORequest(); - request.requestDetails = new MSORequestDetails(); - request.requestDetails.modelInfo = new MSOModelInfo(); - request.requestDetails.cloudConfiguration = new MSOCloudConfiguration(); - request.requestDetails.requestInfo = new MSORequestInfo(); - request.requestDetails.requestParameters = new MSORequestParameters(); - request.requestDetails.requestParameters.userParams = null; - // - // cloudConfiguration - // - request.requestDetails.cloudConfiguration.lcpCloudRegionId = cloudRegionItemCloudRegionId; - request.requestDetails.cloudConfiguration.tenantId = tenantItemTenantId; - // - // modelInfo - // - request.requestDetails.modelInfo.modelType = "vfModule"; - request.requestDetails.modelInfo.modelInvariantId = vfModuleItemPersonaModelId; - request.requestDetails.modelInfo.modelNameVersionId = vfModuleItemModelNameVersionId; - request.requestDetails.modelInfo.modelName = vfModuleItemModelName; - request.requestDetails.modelInfo.modelVersion = vfModuleItemPersonaModelVersion; - // - // requestInfo - // - request.requestDetails.requestInfo.instanceName = vfModuleItemVfModuleName; - request.requestDetails.requestInfo.source = "POLICY"; - request.requestDetails.requestInfo.suppressRollback = false; - // - // relatedInstanceList - // - MSORelatedInstanceListElement relatedInstanceListElement1 = new MSORelatedInstanceListElement(); - MSORelatedInstanceListElement relatedInstanceListElement2 = new MSORelatedInstanceListElement(); - relatedInstanceListElement1.relatedInstance = new MSORelatedInstance(); - relatedInstanceListElement2.relatedInstance = new MSORelatedInstance(); - // - relatedInstanceListElement1.relatedInstance.instanceId = serviceItemServiceInstanceId; - relatedInstanceListElement1.relatedInstance.modelInfo = new MSOModelInfo(); - relatedInstanceListElement1.relatedInstance.modelInfo.modelType = "service"; - relatedInstanceListElement1.relatedInstance.modelInfo.modelInvariantId = serviceItemPersonaModelId; - relatedInstanceListElement1.relatedInstance.modelInfo.modelNameVersionId = serviceItemModelNameVersionId; - relatedInstanceListElement1.relatedInstance.modelInfo.modelName = serviceItemModelName; - relatedInstanceListElement1.relatedInstance.modelInfo.modelVersion = serviceItemModelVersion; - // - relatedInstanceListElement2.relatedInstance.instanceId = vnfItemVnfId; - relatedInstanceListElement2.relatedInstance.modelInfo = new MSOModelInfo(); - relatedInstanceListElement2.relatedInstance.modelInfo.modelType = "vnf"; - relatedInstanceListElement2.relatedInstance.modelInfo.modelInvariantId = vnfItemPersonaModelId; - relatedInstanceListElement2.relatedInstance.modelInfo.modelNameVersionId = vnfItemModelNameVersionId; - relatedInstanceListElement2.relatedInstance.modelInfo.modelName = vnfItemModelName; - relatedInstanceListElement2.relatedInstance.modelInfo.modelVersion = vnfItemModelVersion; - relatedInstanceListElement2.relatedInstance.modelInfo.modelCustomizationName = vnfItemVnfType; - // - request.requestDetails.relatedInstanceList.add(relatedInstanceListElement1); - request.requestDetails.relatedInstanceList.add(relatedInstanceListElement2); - // - // print MSO request for debug - // - System.out.println("MSO request sent:"); - System.out.println(Serialization.gsonPretty.toJson(request)); - // - // - // - if (request != null) { - // - // Tell interested parties we are performing this Operation - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.notification = ControlLoopNotificationType.OPERATION; - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Concatenate serviceItemServiceInstanceId and vnfItemVnfId to msoURL - // - String MSOUrl = $params.getMsoURL() + "/serviceInstances/v2/" + serviceItemServiceInstanceId + "/vnfs/" + vnfItemVnfId + "/vfModules"; - // - // Call MSO - // - MSOResponse response = MSOManager.createModuleInstance(MSOUrl, $params.getMsoURL(), $params.getMsoUsername(), $params.getMsoPassword(), request); - // - if (response != null) { - // - // Assign requestId - // - request.requestId = $event.requestID.toString(); - response.request.requestId = $event.requestID.toString(); - // - // Insert facts - // - insert(operationWrapper); - insert(request); - insert(response); - } else { - // - // MSO request not even accepted - // - notification.message = operationWrapper.operation.toMessage(); - operationWrapper.operation.message = operationWrapper.operation.toMessage(); - operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - notification.history.add(operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // Retract everything - // - retract($event); - retract($manager); - } - } else { - System.err.println("constructed MSO request is invalid."); - } - } - break; - } -end - -/* -* -* This rule responds to APPC Response Events -* -*/ -rule "${policyName}.APPC.RESPONSE" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID ) - $request : Request( getCommonHeader().RequestID == $event.requestID ) - $response : Response( getCommonHeader().RequestID == $event.requestID ) - then - System.out.println("rule APPC.RESPONSE is triggered."); - if ($response.Status == null) { - $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - } - // - // Get the Response Code - // - ResponseCode code = ResponseCode.toResponseCode($response.Status.Code); - if (code == null) { - $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - } - // - // Construct notification - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - notification.message = $operationWrapper.operation.toMessage(); - $operationWrapper.operation.message = $operationWrapper.operation.toMessage(); - // - // Ok, let's figure out what APP-C's response is - // - switch (code) { - case ACCEPT: - $operationWrapper.operation.outcome = "PROCESSING"; - break; - case ERROR: - case REJECT: - $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; - $manager.setControlLoopResult("FAILURE_EXCEPTION"); - break; - case SUCCESS: - $operationWrapper.operation.outcome = "SUCCESS"; - $manager.setControlLoopResult("SUCCESS"); - break; - case FAILURE: - $operationWrapper.operation.outcome = "FAILURE"; - $manager.setControlLoopResult("FAILURE"); - break; - } - if ($operationWrapper.operation.outcome.equals("SUCCESS")) { - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_SUCCESS; - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } else if ($operationWrapper.operation.outcome.equals("PROCESSING")) { - retract($response); - } else { - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } - -end - -/* -* -* This rule is used to clean up APPC response -* -*/ -rule "${policyName}.APPC.RESPONSE.CLEANUP" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $response : Response($id : getCommonHeader().RequestID ) - not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) ) - then - System.out.println("rule APPC.RESPONSE.CLEANUP is triggered."); - retract($response); -end - -/* -* -* This rule responds to MSO Response Events -* -*/ -rule "${policyName}.MSO.RESPONSE" - when - $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) - $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) - $manager : EventManager( closedLoopControlName == $event.closedLoopControlName ) - $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID ) - $request : MSORequest( requestId == $event.requestID.toString() ) - $response : MSOResponse( request.requestId == $event.requestID.toString() ) - then - System.out.println("rule MSO.RESPONSE is triggered."); - // - // Construct notification - // - VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event); - notification.from = "policy"; - notification.policyName = drools.getRule().getName(); - notification.policyScope = "${policyScope}"; - notification.policyVersion = "${policyVersion}"; - notification.message = $operationWrapper.operation.toMessage(); - $operationWrapper.operation.message = $operationWrapper.operation.toMessage(); - // - // The operation can either be succeeded or failed - // - if($response.request.requestStatus.requestState.equals("Completed")) { - $operationWrapper.operation.outcome = "SUCCESS"; - $manager.setControlLoopResult("SUCCESS"); - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_SUCCESS; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } else { - $operationWrapper.operation.outcome = "FAILURE"; - $manager.setControlLoopResult("FAILURE"); - notification.history.add($operationWrapper.operation); - notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - notification.notification = ControlLoopNotificationType.FINAL_FAILURE; - // - // Let interested parties know - // - try { - System.out.println(Serialization.gsonPretty.toJson(notification)); - PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); - } catch (Exception e) { - System.out.println("Can't deliver notification: " + notification); - e.printStackTrace(); - } - // - // We are going to retract these objects from memory - // - System.out.println("Retracting everything"); - retract($operationWrapper); - retract($request); - retract($response); - retract($event); - retract($manager); - } - -end |