Adding reading service-id from AA&I 07/13007/2
authorRitu Sood <ritu.sood@intel.com>
Sun, 17 Sep 2017 22:34:16 +0000 (15:34 -0700)
committerRitu Sood <ritu.sood@intel.com>
Sun, 17 Sep 2017 22:47:45 +0000 (15:47 -0700)
For VFC adding getting service-id from AA&I.

Issue-Id: POLICY-57
Change-Id: I740c7749bfb2d395851cbd5eb3e486bf4eb9b560
Signed-off-by: Ritu Sood <ritu.sood@intel.com>
controlloop/common/actors/actor.vfc/pom.xml
controlloop/common/actors/actor.vfc/src/main/java/org/onap/policy/controlloop/actor/vfc/VFCActorServiceProvider.java

index de6f058..c52b6ae 100644 (file)
                        <version>1.1.0-SNAPSHOT</version>
                        <scope>provided</scope>
                </dependency>
+               <dependency>
+                        <groupId>org.onap.policy.drools-applications</groupId>
+                        <artifactId>aai</artifactId>
+                        <version>1.1.0-SNAPSHOT</version>
+                        <scope>provided</scope>
+                </dependency>
+
        </dependencies>
 </project>
index bd06ef1..49b93e8 100644 (file)
@@ -5,9 +5,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.
 package org.onap.policy.controlloop.actor.vfc;
 
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
-import org.onap.policy.controlloop.ControlLoopOperation;
 import org.onap.policy.controlloop.VirtualControlLoopEvent;
-import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
-import org.onap.policy.controlloop.policy.Policy;
-import org.onap.policy.vfc.VFCHealActionVmInfo;
-import org.onap.policy.vfc.VFCHealAdditionalParams;
-import org.onap.policy.vfc.VFCHealRequest;
 import org.onap.policy.vfc.VFCRequest;
+import org.onap.policy.vfc.VFCHealRequest;
+import org.onap.policy.vfc.VFCHealAdditionalParams;
+import org.onap.policy.vfc.VFCHealActionVmInfo;
+import org.onap.policy.controlloop.ControlLoopOperation;
+import org.onap.policy.controlloop.policy.Policy;
 
+import org.onap.policy.controlloop.actorServiceProvider.spi.Actor;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import org.onap.policy.aai.AAIManager;
+import org.onap.policy.aai.AAIGETVnfResponse;
+
+import java.util.UUID;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class VFCActorServiceProvider implements Actor {
 
+    private static final Logger logger = LoggerFactory.getLogger(VFCActorServiceProvider.class);
     private static final ImmutableList<String> recipes = ImmutableList.of("Restart");
     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
-            .put("Restart", ImmutableList.of("VM"))
-            .build();
+            .put("Restart", ImmutableList.of("VM")).build();
 
     @Override
     public String actor() {
@@ -61,12 +69,14 @@ public class VFCActorServiceProvider implements Actor {
         return Collections.emptyList();
     }
 
-    public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) {
+    public static VFCRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
+                                              Policy policy) {
 
         // Construct an VFC request
         VFCRequest request = new VFCRequest();
         // TODO: Verify service-instance-id is part of onset event
-        request.nsInstanceId = onset.AAI.get("service-instance.service-instance-id");
+        request.nsInstanceId = getAAIServiceInstance(onset); // onset.AAI.get("service-instance.service-instance-id");
+
         request.healRequest = new VFCHealRequest();
         request.healRequest.vnfInstanceId = onset.AAI.get("generic-vnf.vnf-id");
         request.healRequest.cause = operation.message;
@@ -82,11 +92,41 @@ public class VFCActorServiceProvider implements Actor {
                 request.healRequest.additionalParams.actionInfo.vmname = onset.AAI.get("vserver.vserver-name");
                 break;
             default:
-                //TODO: default
+                // TODO: default
                 break;
         }
         return request;
     }
 
-
+    private static String getAAIServiceInstance(VirtualControlLoopEvent event) {
+        AAIGETVnfResponse response = null;
+        UUID requestID = event.requestID;
+        String serviceInstance = event.AAI.get("service-instance.service-instance-id");
+        String vnfName = event.AAI.get("generic-vnf.vnf-name");
+        String vnfID = event.AAI.get("generic-vnf.vnf-id");
+
+        String urlBase = "http://localhost:6666";
+        String username = "testUser";
+        String password = "testPass";
+        if (serviceInstance == null) {
+            try {
+                AAIManager manager = new AAIManager();
+                if (vnfName != null) {
+                    String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=";
+                    response = manager.getQueryByVnfName(url, username, password, requestID, vnfName);
+                    serviceInstance = response.serviceId;
+                } else if (vnfID != null) {
+                    String url = urlBase + "/aai/v11/network/generic-vnfs/generic-vnf/";
+                    response = manager.getQueryByVnfID(url, username, password, requestID, vnfID);
+                    serviceInstance = response.serviceId;
+                } else {
+                    logger.error("getAAIServiceInstance failed");
+
+                }
+            } catch (Exception e) {
+                logger.error("getAAIServiceInstance exception: ", e);
+            }
+        }
+        return serviceInstance;
+    }
 }