Drawing Board RECREATE will use a new route that retrieves a template

Use endpoint "templateTopology" instead of "bulkForRetry"

Issue-ID: VID-724

Change-Id: Ic92971e29d1f78768aeb82158ce424ff31bfbbb0
Signed-off-by: Alexey Sandler <alexey.sandler@intl.att.com>
diff --git a/vid-webpack-master/cypress/integration/iFrames/instantiation-templates.e2e.ts b/vid-webpack-master/cypress/integration/iFrames/instantiation-templates.e2e.ts
index be029a1..e01ccfd 100644
--- a/vid-webpack-master/cypress/integration/iFrames/instantiation-templates.e2e.ts
+++ b/vid-webpack-master/cypress/integration/iFrames/instantiation-templates.e2e.ts
@@ -21,7 +21,7 @@
       const templateUuid = "46390edd-7100-46b2-9f18-419bd24fb60b";
 
       const drawingBoardAction = `RECREATE`;
-      const templateTopologyEndpoint = "bulkForRetry"; // will be: "templateTopology"
+      const templateTopologyEndpoint = "templateTopology";
 
       // Given...
 
diff --git a/vid-webpack-master/src/app/drawingBoard/drawingBoard.routing.ts b/vid-webpack-master/src/app/drawingBoard/drawingBoard.routing.ts
index 6564746..aebbdee 100644
--- a/vid-webpack-master/src/app/drawingBoard/drawingBoard.routing.ts
+++ b/vid-webpack-master/src/app/drawingBoard/drawingBoard.routing.ts
@@ -4,6 +4,7 @@
 import {ViewEditResolver} from "../shared/resolvers/viewEdit/viewEdit.resolver";
 import {DrawingBoardGuard} from "./guards/servicePlanningGuard/drawingBoardGuard";
 import {RetryResolver} from "../shared/resolvers/retry/retry.resolver";
+import {RecreateResolver} from "../shared/resolvers/recreate/recreate.resolver";
 
 export const DrawingBoardRoutes: Route[] = [
   {
@@ -31,7 +32,7 @@
         component: ServicePlanningComponent,
         resolve: {
           flags: FlagsResolve,
-          viewEditResolver: RetryResolver
+          viewEditResolver: RecreateResolver
         }
       },
       {
diff --git a/vid-webpack-master/src/app/shared/resolvers/recreate/recreate.resolver.spec.ts b/vid-webpack-master/src/app/shared/resolvers/recreate/recreate.resolver.spec.ts
new file mode 100644
index 0000000..1a3a1ab
--- /dev/null
+++ b/vid-webpack-master/src/app/shared/resolvers/recreate/recreate.resolver.spec.ts
@@ -0,0 +1,66 @@
+import {RecreateResolver} from "./recreate.resolver";
+import {getTestBed, TestBed} from '@angular/core/testing';
+import {NgRedux} from "@angular-redux/store";
+import {InstantiationTemplatesService} from "../../services/templateService/instantiationTemplates.service";
+import {AaiService} from "../../services/aaiService/aai.service";
+import {mock} from "ts-mockito";
+import {ServiceInstance} from "../../models/serviceInstance";
+import {HttpClientTestingModule} from "@angular/common/http/testing";
+import {FeatureFlagsService} from "../../services/featureFlag/feature-flags.service";
+import {convertToParamMap} from "@angular/router";
+import {of} from 'rxjs/observable/of'
+
+class MockAppStore<T> {
+  getState() {}
+}
+
+describe('Recreate resolver', () => {
+
+  let injector;
+  let recreateResolver: RecreateResolver;
+  let aaiService: AaiService;
+  let instantiationTemplatesService: InstantiationTemplatesService;
+
+  beforeAll(done => (async () => {
+    TestBed.configureTestingModule({
+      imports: [HttpClientTestingModule],
+      providers: [
+        FeatureFlagsService,
+        InstantiationTemplatesService,
+        RecreateResolver,
+        AaiService,
+        {provide: NgRedux, useClass: MockAppStore},
+      ]
+    });
+    await TestBed.compileComponents();
+
+    injector = getTestBed();
+    recreateResolver = injector.get(RecreateResolver);
+    aaiService = injector.get(AaiService);
+    instantiationTemplatesService = injector.get(InstantiationTemplatesService);
+  })().then(done).catch(done.fail));
+
+  test("when resolve() invoked -> then getServiceModelById and retrieveAndStoreInstantiationTemplateTopology are called", done => {
+    jest.spyOn(aaiService, 'getServiceModelById')
+    .mockReturnValue(of({}));
+    jest.spyOn(instantiationTemplatesService, 'retrieveAndStoreInstantiationTemplateTopology')
+    .mockReturnValue(of(mock(ServiceInstance)));
+
+    recreateResolver.resolve(<any>{
+      queryParamMap:
+        convertToParamMap({
+          serviceModelId: "someServiceModelId",
+          jobId: "someJobId",
+        })
+    })
+
+    .subscribe(() => {
+      expect(aaiService.getServiceModelById)
+      .toHaveBeenCalledWith("someServiceModelId");
+      expect(instantiationTemplatesService.retrieveAndStoreInstantiationTemplateTopology)
+      .toHaveBeenCalledWith("someJobId", "someServiceModelId");
+      done();
+    });
+  })
+
+});
diff --git a/vid-webpack-master/src/app/shared/resolvers/recreate/recreate.resolver.ts b/vid-webpack-master/src/app/shared/resolvers/recreate/recreate.resolver.ts
new file mode 100644
index 0000000..b7e952a
--- /dev/null
+++ b/vid-webpack-master/src/app/shared/resolvers/recreate/recreate.resolver.ts
@@ -0,0 +1,29 @@
+import {Injectable} from "@angular/core";
+import {NgRedux} from "@angular-redux/store";
+
+import {ActivatedRouteSnapshot, Resolve} from "@angular/router";
+import {Observable} from "rxjs";
+import {AppState} from "../../store/reducers";
+import {InstantiationTemplatesService} from "../../services/templateService/instantiationTemplates.service";
+import {forkJoin} from "rxjs/observable/forkJoin";
+import {AaiService} from "../../services/aaiService/aai.service";
+import {ServiceInstance} from "../../models/serviceInstance";
+
+@Injectable()
+export class RecreateResolver implements Resolve<Observable<[any, ServiceInstance]>> {
+  constructor(private _templateService: InstantiationTemplatesService,
+              private _aaiService: AaiService,
+              private _store: NgRedux<AppState>) {
+  }
+
+  resolve(route: ActivatedRouteSnapshot): Observable<[any, ServiceInstance]> {
+    const serviceModelId: string = route.queryParamMap.get("serviceModelId");
+    const jobId: string = route.queryParamMap.get("jobId");
+
+    let serviceModelApi = this._aaiService.getServiceModelById(serviceModelId);
+    let instantiationTemplateApi = this._templateService.retrieveAndStoreInstantiationTemplateTopology(jobId, serviceModelId);
+
+    return forkJoin([serviceModelApi, instantiationTemplateApi])
+  }
+
+}
diff --git a/vid-webpack-master/src/app/shared/services/templateService/instantiationTemplates.service.spec.ts b/vid-webpack-master/src/app/shared/services/templateService/instantiationTemplates.service.spec.ts
new file mode 100644
index 0000000..80b113b
--- /dev/null
+++ b/vid-webpack-master/src/app/shared/services/templateService/instantiationTemplates.service.spec.ts
@@ -0,0 +1,51 @@
+import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing";
+import {InstantiationTemplatesService} from "./instantiationTemplates.service";
+import {mock} from "ts-mockito";
+import {NgRedux} from "@angular-redux/store";
+import {getTestBed, TestBed} from "@angular/core/testing";
+import {ServiceInstance} from "../../models/serviceInstance";
+import {Constants} from "../../utils/constants";
+
+class MockAppStore<T> {
+  dispatch() {}
+  getState() {}
+}
+
+describe("TemplateService", ()=>{
+  let injector;
+  let httpMock: HttpTestingController;
+  let templateService: InstantiationTemplatesService;
+
+  beforeAll( done => (async () => {
+    TestBed.configureTestingModule({
+      imports: [HttpClientTestingModule],
+      providers: [
+        InstantiationTemplatesService,
+        {provide: NgRedux, useClass: MockAppStore},
+      ]
+    });
+    await TestBed.compileComponents();
+
+    injector = getTestBed();
+    httpMock = injector.get(HttpTestingController);
+    templateService = injector.get(InstantiationTemplatesService);
+  })().then(done).catch(done.fail));
+
+  describe ('#retrieveInstantiationTemplateTopology tests', () => {
+    test('when called -> retrieve template from backend', done => {
+      const mockedTemplate = mock(ServiceInstance);
+      const jobId: string = "some-random-job-id";
+
+      templateService.retrieveInstantiationTemplateTopology(jobId)
+        .subscribe((result: ServiceInstance) => {
+          expect(Object.is(result, mockedTemplate)).toBe(true);
+          done();
+        });
+
+      httpMock
+        .expectOne(`${Constants.Path.INSTANTIATION_TEMPLATE_TOPOLOGY}/${jobId}`)
+        .flush(mockedTemplate);
+    })
+  })
+});
+
diff --git a/vid-webpack-master/src/app/shared/services/templateService/instantiationTemplates.service.ts b/vid-webpack-master/src/app/shared/services/templateService/instantiationTemplates.service.ts
new file mode 100644
index 0000000..21cdc92
--- /dev/null
+++ b/vid-webpack-master/src/app/shared/services/templateService/instantiationTemplates.service.ts
@@ -0,0 +1,26 @@
+import {Injectable} from "@angular/core";
+import {HttpClient} from "@angular/common/http";
+import {NgRedux} from "@angular-redux/store";
+import {AppState} from "../../store/reducers";
+import {Observable} from "rxjs";
+import {ServiceInstance} from "../../models/serviceInstance";
+import {Constants} from "../../utils/constants";
+import {createServiceInstance} from "../../storeUtil/utils/service/service.actions";
+
+@Injectable()
+export class InstantiationTemplatesService {
+  constructor(private http: HttpClient, private store: NgRedux<AppState>) {
+  }
+
+  retrieveInstantiationTemplateTopology(jobId: string): Observable<ServiceInstance> {
+    let pathQuery: string = `${Constants.Path.INSTANTIATION_TEMPLATE_TOPOLOGY}/${jobId}`;
+    return this.http.get<ServiceInstance>(pathQuery)
+  }
+
+  public retrieveAndStoreInstantiationTemplateTopology(jobId: string, serviceModelId: string): Observable<ServiceInstance> {
+    return this.retrieveInstantiationTemplateTopology(jobId).do((instantiationTemplate: ServiceInstance) => {
+      this.store.dispatch(createServiceInstance(instantiationTemplate, serviceModelId));
+    });
+  };
+
+}
diff --git a/vid-webpack-master/src/app/shared/shared.module.ts b/vid-webpack-master/src/app/shared/shared.module.ts
index b12ac43..a8b45c9 100644
--- a/vid-webpack-master/src/app/shared/shared.module.ts
+++ b/vid-webpack-master/src/app/shared/shared.module.ts
@@ -75,6 +75,8 @@
 import {ModelInformationService} from "./components/model-information/model-information.service";
 import {MultiselectFormControlService} from "./components/formControls/component/multiselect/multiselect.formControl.service";
 import {SearchFilterPipe} from "./pipes/searchFilter/search-filter.pipe";
+import {RecreateResolver} from "./resolvers/recreate/recreate.resolver";
+import {InstantiationTemplatesService} from "./services/templateService/instantiationTemplates.service";
 
 
 @NgModule({
@@ -178,6 +180,8 @@
     FlagsResolve,
     ViewEditResolver,
     RetryResolver,
+    RecreateResolver,
+    InstantiationTemplatesService,
     ServiceControlGenerator,
     ServicePopupService,
     VnfControlGenerator,
diff --git a/vid-webpack-master/src/app/shared/utils/constants.ts b/vid-webpack-master/src/app/shared/utils/constants.ts
index 6172320..4f11cd1 100644
--- a/vid-webpack-master/src/app/shared/utils/constants.ts
+++ b/vid-webpack-master/src/app/shared/utils/constants.ts
@@ -94,6 +94,7 @@
     public static SERVICES_JOB_INFO_PATH = '../../asyncInstantiation';
     public static SERVICE_MODEL_ID = 'serviceModelId';
     public static SERVICES_RETRY_TOPOLOGY = '../../asyncInstantiation/bulkForRetry';
+    public static INSTANTIATION_TEMPLATE_TOPOLOGY = '../../asyncInstantiation/templateTopology';
     public static CONFIGURATION_PATH = '../../get_property/{name}/defaultvalue';
     public static SERVICES_JOB_AUDIT_PATH = '/auditStatus';
     public static SERVICES_PROBE_PATH = "../../probe";