From a6d09fbe1046057b72247f97ac72a521949409ce Mon Sep 17 00:00:00 2001 From: xg353y <xg353y@intl.att.com> Date: Thu, 28 Mar 2019 10:05:25 +0100 Subject: Rework the submit operation Rework the logic to create/delete ms config policies. Issue-ID: CLAMP-303 Change-Id: I317e262ab68280a7518a6e31e82e5fcb0a7f94ed Signed-off-by: xg353y <xg353y@intl.att.com> --- .../clamp/clds/client/DcaeDispatcherServices.java | 4 +- .../clamp/clds/client/DcaeInventoryServices.java | 2 +- .../sdc/controller/installer/BlueprintParser.java | 27 +++- .../sdc/controller/installer/MicroService.java | 22 ++- .../org/onap/clamp/loop/CsarInstallerImpl.java | 4 +- src/main/java/org/onap/clamp/loop/Loop.java | 2 +- .../org/onap/clamp/operation/LoopOperation.java | 164 ++++++++++++++++++++- .../org/onap/clamp/policy/PolicyOperation.java | 131 ++++++++++++++++ .../policy/microservice/MicroServicePolicy.java | 30 +++- .../microservice/MicroservicePolicyService.java | 4 +- .../org/onap/clamp/util/HttpConnectionManager.java | 36 ++++- .../resources/designer/scripts/CldsModelService.js | 2 +- src/main/resources/application-noaaf.properties | 3 + src/main/resources/application.properties | 3 + .../resources/clds/camel/rest/clamp-api-v2.xml | 18 +++ 15 files changed, 426 insertions(+), 26 deletions(-) create mode 100644 src/main/java/org/onap/clamp/policy/PolicyOperation.java (limited to 'src/main') diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java index e2a4c2f48..0f4659591 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java @@ -99,7 +99,7 @@ public class DcaeDispatcherServices { Date startTime = new Date(); LoggingUtils.setTargetContext("DCAE", "getOperationStatus"); try { - String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(statusUrl, "GET", null, null, "DCAE"); + String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(statusUrl, "GET", null, null, "DCAE", null, null); JSONObject jsonObj = parseResponse(responseStr); String operationType = (String) jsonObj.get("operationType"); String status = (String) jsonObj.get(DCAE_STATUS_FIELD); @@ -190,7 +190,7 @@ public class DcaeDispatcherServices { String nodeAttr) throws IOException, ParseException { Date startTime = new Date(); try { - String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(url, requestMethod, payload, contentType, "DCAE"); + String responseStr = dcaeHttpConnectionManager.doGeneralHttpQuery(url, requestMethod, payload, contentType, "DCAE", null, null); JSONObject jsonObj = parseResponse(responseStr); JSONObject linksObj = (JSONObject) jsonObj.get(node); String statusUrl = (String) linksObj.get(nodeAttr); diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java index 63fdc6187..d8bfeeaec 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -207,7 +207,7 @@ public class DcaeInventoryServices { } for (int i = 0; i < retryLimit; i++) { metricsLogger.info("Attempt n°" + i + " to contact DCAE inventory"); - String response = httpConnectionManager.doGeneralHttpQuery(fullUrl, "GET", null, null, "DCAE"); + String response = httpConnectionManager.doGeneralHttpQuery(fullUrl, "GET", null, null, "DCAE", null, null); int totalCount = getTotalCountFromDcaeInventoryResponse(response); metricsLogger.info("getDcaeInformation complete: totalCount returned=" + totalCount); if (totalCount > 0) { diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java index c8de4c589..7447fbae1 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/BlueprintParser.java @@ -27,6 +27,7 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import java.util.Collections; import java.util.HashSet; @@ -51,6 +52,8 @@ public class BlueprintParser { private static final String TYPE = "type"; private static final String PROPERTIES = "properties"; private static final String NAME = "name"; + private static final String POLICYID = "policy_id"; + private static final String POLICY_TYPEID = "policy_type_id"; private static final String RELATIONSHIPS = "relationships"; private static final String CLAMP_NODE_RELATIONSHIPS_GETS_INPUT_FROM = "clamp_node.relationships.gets_input_from"; private static final String TARGET = "target"; @@ -85,7 +88,7 @@ public class BlueprintParser { } } String msName = theBiggestMicroServiceKey.toLowerCase().contains(HOLMES_PREFIX) ? HOLMES : TCA; - return Collections.singletonList(new MicroService(msName, "", "")); + return Collections.singletonList(new MicroService(msName, "", "", "", "")); } String getName(Entry<String, JsonElement> entry) { @@ -114,10 +117,30 @@ public class BlueprintParser { return ""; } + String getModelType(Entry<String, JsonElement> entry) { + JsonObject ob = entry.getValue().getAsJsonObject(); + if (ob.has(PROPERTIES)) { + JsonObject properties = ob.get(PROPERTIES).getAsJsonObject(); + if (properties.has(POLICYID)) { + JsonObject policyIdObj = properties.get(POLICYID).getAsJsonObject(); + if (policyIdObj.has(POLICY_TYPEID)) { + return policyIdObj.get(POLICY_TYPEID).getAsString(); + } + } + } + return ""; + } + + String getBlueprintName(Entry<String, JsonElement> entry) { + return entry.getKey(); + } + MicroService getNodeRepresentation(Entry<String, JsonElement> entry) { String name = getName(entry); String getInputFrom = getInput(entry); - return new MicroService(name, getInputFrom, ""); + String modelType = getModelType(entry); + String blueprintName = getBlueprintName(entry); + return new MicroService(name, modelType, getInputFrom, "", blueprintName); } private String getTarget(JsonObject elementObject) { diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java index 198bf0ede..a785f41ef 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/MicroService.java @@ -27,27 +27,39 @@ import java.util.Objects; public class MicroService { private final String name; + private final String modelType; + private final String blueprintName; private final String inputFrom; private String mappedNameJpa; - public MicroService(String name, String inputFrom, String mappedNameJpa) { + public MicroService(String name, String modelType, String inputFrom, String mappedNameJpa, String blueprintName) { this.name = name; this.inputFrom = inputFrom; this.mappedNameJpa = mappedNameJpa; + this.modelType = modelType; + this.blueprintName = blueprintName; } public String getName() { return name; } + public String getModelType() { + return modelType; + } + public String getInputFrom() { return inputFrom; } + public String getBlueprintName() { + return blueprintName; + } + @Override public String toString() { - return "MicroService{" + "name='" + name + '\'' + ", inputFrom='" + inputFrom + '\'' + ", mappedNameJpa='" - + mappedNameJpa + '\'' + '}'; + return "MicroService{" + "name='" + name + '\'' + ", modelType='" + modelType + '\'' + ", inputFrom='" + inputFrom + '\'' + ", mappedNameJpa='" + + mappedNameJpa + '\'' + ", blueprintName='" + blueprintName + '\'' + '}'; } public String getMappedNameJpa() { @@ -67,11 +79,11 @@ public class MicroService { return false; } MicroService that = (MicroService) o; - return name.equals(that.name) && inputFrom.equals(that.inputFrom) && mappedNameJpa.equals(that.mappedNameJpa); + return name.equals(that.name) && modelType.equals(that.modelType) && inputFrom.equals(that.inputFrom) && mappedNameJpa.equals(that.mappedNameJpa) && blueprintName.equals(that.blueprintName); } @Override public int hashCode() { - return Objects.hash(name, inputFrom, mappedNameJpa); + return Objects.hash(name, modelType, inputFrom, mappedNameJpa, blueprintName); } } diff --git a/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java index fed2c65b7..05d5c480c 100644 --- a/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java +++ b/src/main/java/org/onap/clamp/loop/CsarInstallerImpl.java @@ -163,8 +163,8 @@ public class CsarInstallerImpl implements CsarInstaller { Policy.generatePolicyName(microService.getName(), csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(), blueprintArtifact.getResourceAttached().getResourceInstanceName(), - blueprintArtifact.getBlueprintArtifactName()), - csar.getPolicyModelYaml().orElse(""), false, new HashSet<>(Arrays.asList(newLoop))); + blueprintArtifact.getBlueprintArtifactName()), microService.getModelType(), + csar.getPolicyModelYaml().orElse(""), false, new HashSet<>(Arrays.asList(newLoop)), microService.getBlueprintName()); newSet.add(microServicePolicy); microService.setMappedNameJpa(microServicePolicy.getName()); diff --git a/src/main/java/org/onap/clamp/loop/Loop.java b/src/main/java/org/onap/clamp/loop/Loop.java index 6dc73e987..cc04ce5d2 100644 --- a/src/main/java/org/onap/clamp/loop/Loop.java +++ b/src/main/java/org/onap/clamp/loop/Loop.java @@ -183,7 +183,7 @@ public class Loop implements Serializable { this.operationalPolicies = operationalPolicies; } - Set<MicroServicePolicy> getMicroServicePolicies() { + public Set<MicroServicePolicy> getMicroServicePolicies() { return microServicePolicies; } diff --git a/src/main/java/org/onap/clamp/operation/LoopOperation.java b/src/main/java/org/onap/clamp/operation/LoopOperation.java index af615af11..bdb4b3f0f 100644 --- a/src/main/java/org/onap/clamp/operation/LoopOperation.java +++ b/src/main/java/org/onap/clamp/operation/LoopOperation.java @@ -31,6 +31,7 @@ import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.io.IOException; import java.lang.reflect.Array; import java.util.Collection; import java.util.Date; @@ -40,12 +41,15 @@ import javax.servlet.http.HttpServletRequest; import org.apache.camel.Exchange; import org.onap.clamp.clds.client.DcaeDispatcherServices; +import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.util.LoggingUtils; import org.onap.clamp.clds.util.ONAPLogConstants; import org.onap.clamp.exception.OperationException; import org.onap.clamp.loop.Loop; import org.onap.clamp.loop.LoopService; import org.onap.clamp.loop.LoopState; +import org.onap.clamp.policy.PolicyOperation; +import org.onap.clamp.util.HttpConnectionManager; import org.slf4j.event.Level; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -63,14 +67,17 @@ public class LoopOperation { private final DcaeDispatcherServices dcaeDispatcherServices; private final LoopService loopService; private LoggingUtils util = new LoggingUtils(logger); + private PolicyOperation policyOp; @Autowired private HttpServletRequest request; @Autowired - public LoopOperation(LoopService loopService, DcaeDispatcherServices dcaeDispatcherServices) { + public LoopOperation(LoopService loopService, DcaeDispatcherServices dcaeDispatcherServices, + ClampProperties refProp, HttpConnectionManager httpConnectionManager, PolicyOperation policyOp) { this.loopService = loopService; this.dcaeDispatcherServices = dcaeDispatcherServices; + this.policyOp = policyOp; } /** @@ -209,4 +216,159 @@ public class LoopOperation { // otherwise take it as a string return new JsonPrimitive(String.valueOf(o)); } + + /** + * Submit the Ms policies. + * + * @param loopName the loop name + * @return the updated loop + * @throws IOException IO exception + * @throws Exceptions during the operation + */ + public Loop submitMsPolicies (String loopName) throws OperationException, IOException { + util.entering(request, "LoopOperation: delete microservice policies"); + Date startTime = new Date(); + Loop loop = loopService.getLoop(loopName); + + if (loop == null) { + String msg = "Submit MS policies exception: Not able to find closed loop:" + loopName; + util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // verify the current closed loop state + if (loop.getLastComputedState() != LoopState.SUBMITTED && loop.getLastComputedState() != LoopState.DESIGN) { + String msg = "Submit MS policies exception: This closed loop is in state:" + loop.getLastComputedState() + + ". It could be deleted only when it is in SUBMITTED state."; + util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // Establish the api call to Policy to create the ms services + policyOp.createMsPolicy(loop.getMicroServicePolicies()); + + // audit log + LoggingUtils.setTimeContext(startTime, new Date()); + auditLogger.info("Deletion of MS policies completed"); + util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); + return loop; + } + + + /** + * Delete the Ms policies. + * + * @param loopName the loop name + * @return the updated loop + * @throws IOException IO exception + * @throws Exceptions during the operation + */ + public Loop deleteMsPolicies (Exchange camelExchange, String loopName) throws OperationException, IOException { + util.entering(request, "LoopOperation: delete microservice policies"); + Date startTime = new Date(); + Loop loop = loopService.getLoop(loopName); + + if (loop == null) { + String msg = "Delete MS policies exception: Not able to find closed loop:" + loopName; + util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // verify the current closed loop state + if (loop.getLastComputedState() != LoopState.SUBMITTED) { + String msg = "Delete MS policies exception: This closed loop is in state:" + loop.getLastComputedState() + + ". It could be deleted only when it is in SUBMITTED state."; + util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // Establish the api call to Policy to create the ms services + policyOp.deleteMsPolicy(loop.getMicroServicePolicies()); + + // audit log + LoggingUtils.setTimeContext(startTime, new Date()); + auditLogger.info("Deletion of MS policies completed"); + util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); + return loop; + } + + /** + * Delete the operational policy. + * + * @param loopName the loop name + * @return the updated loop + * @throws Exceptions during the operation + */ + public Loop deleteOpPolicy (Exchange camelExchange, String loopName) throws OperationException { + util.entering(request, "LoopOperation: delete guard policy"); + Date startTime = new Date(); + Loop loop = loopService.getLoop(loopName); + + if (loop == null) { + String msg = "Delete guard policy exception: Not able to find closed loop:" + loopName; + util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // verify the current closed loop state + if (loop.getLastComputedState() != LoopState.SUBMITTED) { + String msg = "Delete MS policies exception: This closed loop is in state:" + loop.getLastComputedState() + + ". It could be deleted only when it is in SUBMITTED state."; + util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // Establish the api call to Policy to delete operational policy + //client.deleteOpPolicy(); + + // audit log + LoggingUtils.setTimeContext(startTime, new Date()); + auditLogger.info("Deletion of Guard policy completed"); + util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); + return loop; + } + + /** + * Delete the Guard policy. + * + * @param loopName the loop name + * @return the updated loop + * @throws Exceptions during the operation + */ + public Loop deleteGuardPolicy (Exchange camelExchange, String loopName) throws OperationException { + util.entering(request, "LoopOperation: delete operational policy"); + Date startTime = new Date(); + Loop loop = loopService.getLoop(loopName); + + if (loop == null) { + String msg = "Delete operational policy exception: Not able to find closed loop:" + loopName; + util.exiting(HttpStatus.INTERNAL_SERVER_ERROR.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // verify the current closed loop state + if (loop.getLastComputedState() != LoopState.SUBMITTED) { + String msg = "Delete MS policies exception: This closed loop is in state:" + loop.getLastComputedState() + + ". It could be deleted only when it is in SUBMITTED state."; + util.exiting(HttpStatus.CONFLICT.toString(), msg, Level.INFO, + ONAPLogConstants.ResponseStatus.ERROR); + throw new OperationException(msg); + } + + // Establish the api call to Policy to delete Guard policy + //client.deleteOpPolicy(); + + // audit log + LoggingUtils.setTimeContext(startTime, new Date()); + auditLogger.info("Deletion of operational policy completed"); + util.exiting(HttpStatus.OK.toString(), "Successful", Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); + return loop; + } } diff --git a/src/main/java/org/onap/clamp/policy/PolicyOperation.java b/src/main/java/org/onap/clamp/policy/PolicyOperation.java new file mode 100644 index 000000000..592338c4d --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/PolicyOperation.java @@ -0,0 +1,131 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 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.clamp.policy; + +import java.io.IOException; +import java.util.Set; + +import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.policy.microservice.MicroServicePolicy; +import org.onap.clamp.util.HttpConnectionManager; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +@Component +public class PolicyOperation { + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyOperation.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + public static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type"; + public static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name"; + public static final String POLICY_BASENAME_PREFIX_PROPERTY_NAME = "policy.base.policyNamePrefix"; + public static final String POLICY_OP_NAME_PREFIX_PROPERTY_NAME = "policy.op.policyNamePrefix"; + public static final String POLICY_MS_NAME_PREFIX_PROPERTY_NAME = "policy.ms.policyNamePrefix"; + public static final String POLICY_OP_TYPE_PROPERTY_NAME = "policy.op.type"; + public static final String POLICY_GUARD_SUFFIX = "_Guard"; + public static final String POLICY_URL_PROPERTY_NAME = "clamp.config.policy.url"; + public static final String POLICY_URL_SUFFIX = "/versions/1.0.0/policies"; + public static final String POLICY_USER_NAME = "clamp.config.policy.userName"; + public static final String POLICY_PASSWORD = "clamp.config.policy.password"; + + public static final String TOSCA_DEF_VERSION = "tosca_definitions_version"; + public static final String TOSCA_DEF_VERSION_VALUE = "tosca_simple_yaml_1_0_0"; + public static final String TEMPLATE = "topology_template"; + public static final String POLICIES = "policies"; + public static final String MS_TYPE = "type"; + public static final String MS_VERSION = "version"; + public static final String MS_VERSION_VALUE = "1.0.0"; + public static final String MS_METADATA = "metadata"; + public static final String MS_POLICY_ID = "policy_id"; + public static final String MS_PROPERTIES = "properties"; + public static final String MS_policy = "tca_policy"; + + private final ClampProperties refProp; + private final HttpConnectionManager httpConnectionManager; + + @Autowired + public PolicyOperation(ClampProperties refProp, HttpConnectionManager httpConnectionManager) { + this.refProp = refProp; + this.httpConnectionManager = httpConnectionManager; + } + + public void createMsPolicy(Set<MicroServicePolicy> policyList) throws IOException { + // Get policy first? if exist delete??? + // push pdp group + for (MicroServicePolicy msPolicy:policyList) { + JsonObject payload = createMsPolicyPayload(msPolicy); + String policyType = msPolicy.getModelType(); + String url = refProp.getStringValue(POLICY_URL_PROPERTY_NAME) + policyType + POLICY_URL_SUFFIX; + String userName = refProp.getStringValue(POLICY_USER_NAME); + String encodedPass = refProp.getStringValue(POLICY_PASSWORD); + httpConnectionManager.doGeneralHttpQuery(url, "POST", payload.toString(), "application/json", "POLICY", userName, encodedPass); + } + } + + public void deleteMsPolicy(Set<MicroServicePolicy> policyList) throws IOException { + for (MicroServicePolicy msPolicy:policyList) { + String policyType = msPolicy.getModelType(); + String url = refProp.getStringValue(POLICY_URL_PROPERTY_NAME) + policyType + POLICY_URL_SUFFIX + "/" + msPolicy.getName(); + String userName = refProp.getStringValue(POLICY_USER_NAME); + String encodedPass = refProp.getStringValue(POLICY_PASSWORD); + httpConnectionManager.doGeneralHttpQuery(url, "POST", null, null, "POLICY", userName, encodedPass); + } + } + + private JsonObject createMsPolicyPayload(MicroServicePolicy microService) { + JsonObject policyConfig = new JsonObject(); + policyConfig.add(MS_policy, microService.getProperties()); + + JsonObject properties = new JsonObject(); + properties.add(MS_policy, policyConfig); + + JsonObject msPolicy = new JsonObject(); + msPolicy.addProperty(MS_TYPE, microService.getModelType()); + msPolicy.addProperty(MS_VERSION, MS_VERSION_VALUE); + JsonObject metaData = new JsonObject(); + metaData.addProperty(MS_POLICY_ID, microService.getName()); + msPolicy.add(MS_METADATA, metaData); + msPolicy.add(MS_PROPERTIES, properties); + + JsonObject msPolicyWithName = new JsonObject(); + msPolicyWithName.add(microService.getName(), msPolicy); + + JsonArray policyArray = new JsonArray(); + policyArray.add(msPolicyWithName); + + JsonObject template = new JsonObject(); + template.add(POLICIES, policyArray); + + JsonObject configPolicy = new JsonObject(); + configPolicy.addProperty(TOSCA_DEF_VERSION, TOSCA_DEF_VERSION_VALUE); + configPolicy.add(TEMPLATE, template); + + return configPolicy; + } + +} diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index 3962a3d4e..c2c60c9c0 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -59,6 +59,14 @@ public class MicroServicePolicy implements Serializable, Policy { @Column(nullable = false, name = "name", unique = true) private String name; + @Expose + @Column(nullable = false, name = "model_type") + private String modelType; + + @Expose + @Column(nullable = false, name = "blueprint_name") + private String blueprintName; + @Expose @Type(type = "json") @Column(columnDefinition = "json", name = "properties") @@ -86,34 +94,42 @@ public class MicroServicePolicy implements Serializable, Policy { /** * The constructor. * @param name The name of the MicroService + * @param type The model type of the MicroService + * @param blueprintName The name in the blueprint * @param policyTosca The policy Tosca of the MicroService * @param shared The flag indicate whether the MicroService is shared * @param usedByLoops The list of loops that uses this MicroService */ - public MicroServicePolicy(String name, String policyTosca, Boolean shared, Set<Loop> usedByLoops) { + public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared, Set<Loop> usedByLoops, String blueprintName) { this.name = name; + this.modelType = modelType; this.policyTosca = policyTosca; this.shared = shared; this.jsonRepresentation = JsonUtils.GSON_JPA_MODEL .fromJson(new ToscaYamlToJsonConvertor(null).parseToscaYaml(policyTosca), JsonObject.class); this.usedByLoops = usedByLoops; + this.blueprintName = blueprintName; } /** * The constructor. * @param name The name of the MicroService + * @param type The model type of the MicroService + * @param blueprintName The name in the blueprint * @param policyTosca The policy Tosca of the MicroService * @param shared The flag indicate whether the MicroService is shared * @param jsonRepresentation The UI representation in json format * @param usedByLoops The list of loops that uses this MicroService */ - public MicroServicePolicy(String name, String policyTosca, Boolean shared, JsonObject jsonRepresentation, - Set<Loop> usedByLoops) { + public MicroServicePolicy(String name, String modelType, String policyTosca, Boolean shared, JsonObject jsonRepresentation, + Set<Loop> usedByLoops, String blueprintName) { this.name = name; + this.modelType = modelType; this.policyTosca = policyTosca; this.shared = shared; this.usedByLoops = usedByLoops; this.jsonRepresentation = jsonRepresentation; + this.blueprintName = blueprintName; } @Override @@ -121,6 +137,14 @@ public class MicroServicePolicy implements Serializable, Policy { return name; } + public String getModelType() { + return modelType; + } + + public String getBlueprintName() { + return blueprintName; + } + public JsonObject getProperties() { return properties; } diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java b/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java index f473d160f..f95ad3b4e 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroservicePolicyService.java @@ -66,8 +66,8 @@ public class MicroservicePolicyService implements PolicyService<MicroServicePoli @Transactional public MicroServicePolicy getAndUpdateMicroServicePolicy(Loop loop, MicroServicePolicy policy) { return repository.findById(policy.getName()).map(p -> updateMicroservicePolicyProperties(p, policy, loop)) - .orElse(new MicroServicePolicy(policy.getName(), policy.getPolicyTosca(), policy.getShared(), - policy.getJsonRepresentation(), Sets.newHashSet(loop))); + .orElse(new MicroServicePolicy(policy.getName(), policy.getModelType(), policy.getPolicyTosca(), policy.getShared(), + policy.getJsonRepresentation(), Sets.newHashSet(loop), policy.getBlueprintName())); } private MicroServicePolicy updateMicroservicePolicyProperties(MicroServicePolicy oldPolicy, diff --git a/src/main/java/org/onap/clamp/util/HttpConnectionManager.java b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java index 9443301b1..4e97f29b7 100644 --- a/src/main/java/org/onap/clamp/util/HttpConnectionManager.java +++ b/src/main/java/org/onap/clamp/util/HttpConnectionManager.java @@ -27,17 +27,22 @@ package org.onap.clamp.util; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import sun.misc.BASE64Encoder; + import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.security.GeneralSecurityException; import javax.net.ssl.HttpsURLConnection; import javax.ws.rs.BadRequestException; +import org.apache.commons.codec.DecoderException; import org.apache.commons.io.IOUtils; +import org.onap.clamp.clds.util.CryptoUtils; import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.stereotype.Component; @@ -50,13 +55,15 @@ public class HttpConnectionManager { protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); private static final String REQUEST_FAILED_LOG = "Request Failed - response payload="; - private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType, String target) throws IOException { + private String doHttpsQuery(URL url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { LoggingUtils utils = new LoggingUtils(logger); logger.info("Using HTTPS URL:" + url.toString()); HttpsURLConnection secureConnection = (HttpsURLConnection) url.openConnection(); secureConnection = utils.invokeHttps(secureConnection, target, requestMethod); secureConnection.setRequestMethod(requestMethod); - secureConnection.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); + if (userName != null && password != null) { + secureConnection.setRequestProperty("Authorization", "Basic " + generateBasicAuth(userName, password)); + } if (payload != null && contentType != null) { secureConnection.setRequestProperty("Content-Type", contentType); secureConnection.setDoOutput(true); @@ -84,12 +91,15 @@ public class HttpConnectionManager { } } - private String doHttpQuery(URL url, String requestMethod, String payload, String contentType, String target) throws IOException { + private String doHttpQuery(URL url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { LoggingUtils utils = new LoggingUtils(logger); logger.info("Using HTTP URL:" + url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection = utils.invoke(connection, target, requestMethod); connection.setRequestMethod(requestMethod); + if (userName != null && password != null) { + connection.setRequestProperty("Authorization", "Basic " + generateBasicAuth(userName, password)); + } if (payload != null && contentType != null) { connection.setRequestProperty("Content-Type", contentType); connection.setDoOutput(true); @@ -134,13 +144,27 @@ public class HttpConnectionManager { * @throws IOException * In case of issue with the streams */ - public String doGeneralHttpQuery(String url, String requestMethod, String payload, String contentType, String target) + public String doGeneralHttpQuery(String url, String requestMethod, String payload, String contentType, String target, String userName, String password) throws IOException { URL urlObj = new URL(url); if (url.contains("https://")) { // Support for HTTPS - return doHttpsQuery(urlObj, requestMethod, payload, contentType, target); + return doHttpsQuery(urlObj, requestMethod, payload, contentType, target, userName, password); } else { // Support for HTTP - return doHttpQuery(urlObj, requestMethod, payload, contentType, target); + return doHttpQuery(urlObj, requestMethod, payload, contentType, target, userName, password); + } + } + + private String generateBasicAuth(String userName, String encodedPassword) { + String password = ""; + try { + password = CryptoUtils.decrypt(encodedPassword); + } catch (GeneralSecurityException e) { + logger.error("Unable to decrypt the password", e); + } catch (DecoderException e) { + logger.error("Exception caught when decoding the HEX String Key for encryption", e); } + BASE64Encoder enc = new sun.misc.BASE64Encoder(); + String userpassword = userName + ":" + password; + return enc.encode( userpassword.getBytes() ); } } diff --git a/src/main/resources/META-INF/resources/designer/scripts/CldsModelService.js b/src/main/resources/META-INF/resources/designer/scripts/CldsModelService.js index a71e6caa6..90cdc0d78 100644 --- a/src/main/resources/META-INF/resources/designer/scripts/CldsModelService.js +++ b/src/main/resources/META-INF/resources/designer/scripts/CldsModelService.js @@ -96,7 +96,7 @@ app var def = $q.defer(); var sets = []; var svcAction = uiAction.toLowerCase(); - var svcUrl = "/restservices/clds/v2/loop/" + "action/" + svcAction + "/" + modelName; + var svcUrl = "/restservices/clds/v2/loop/" + svcAction + "/" + modelName; $http.put(svcUrl).success( function(data) { diff --git a/src/main/resources/application-noaaf.properties b/src/main/resources/application-noaaf.properties index 632856e92..f54cbe096 100644 --- a/src/main/resources/application-noaaf.properties +++ b/src/main/resources/application-noaaf.properties @@ -135,6 +135,9 @@ clamp.config.dcae.deployment.template=classpath:/clds/templates/dcae-deployment- # # # Configuration Settings for Policy Engine Components +clamp.config.policy.url=http://localhost:8085/ +clamp.config.policy.userName=test +clamp.config.policy.password=test clamp.config.policy.pdpUrl1=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123 clamp.config.policy.pdpUrl2=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123 clamp.config.policy.papUrl=http://policy.api.simpledemo.onap.org:8081/pap/ , testpap, alpha123 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 91c02ef74..5b47d32de 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -150,6 +150,9 @@ clamp.config.dcae.deployment.template=classpath:/clds/templates/dcae-deployment- # # # Configuration Settings for Policy Engine Components +clamp.config.policy.url=http://policy.api.simpledemo.onap.org:8081/pdp/ +clamp.config.policy.userName=test +clamp.config.policy.password=test clamp.config.policy.pdpUrl1=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123 clamp.config.policy.pdpUrl2=http://policy.api.simpledemo.onap.org:8081/pdp/ , testpdp, alpha123 clamp.config.policy.papUrl=http://policy.api.simpledemo.onap.org:8081/pap/ , testpap, alpha123 diff --git a/src/main/resources/clds/camel/rest/clamp-api-v2.xml b/src/main/resources/clds/camel/rest/clamp-api-v2.xml index c5828b284..f339d5d70 100644 --- a/src/main/resources/clds/camel/rest/clamp-api-v2.xml +++ b/src/main/resources/clds/camel/rest/clamp-api-v2.xml @@ -71,6 +71,24 @@ <to uri="bean:org.onap.clamp.operation.LoopOperation?method=unDeployLoop(${header.loopName})" /> </route> </put> + <put uri="/v2/loop/submit/{loopName}"> + <route> + <to uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" /> + <to uri="bean:org.onap.clamp.operation.LoopOperation?method=submitMsPolicies(${header.loopName})" /> + <!-- <to uri="bean:org.onap.clamp.operation.LoopOperation?method=submitOpPolicy(${header.loopName})" />--> + <!--<to uri="bean:org.onap.clamp.operation.LoopOperation?method=submitGuardPolicy(${header.loopName})" /> --> + </route> + </put> + <put uri="/v2/loop/delete/{loopName}" + outType="org.onap.clamp.loop.Loop" + produces="application/json"> + <route> + <to uri="bean:org.onap.clamp.authorization.AuthorizationController?method=authorize(*,'cl','','update')" /> + <to uri="bean:org.onap.clamp.operation.LoopOperation?method=deleteMsPolicies(${header.loopName})" /> + <to uri="bean:org.onap.clamp.operation.LoopOperation?method=deleteOpPolicy(${header.loopName})" /> + <to uri="bean:org.onap.clamp.operation.LoopOperation?method=deleteGuardPolicy(${header.loopName})" /> + </route> + </put> </rest> </rests> -- cgit 1.2.3-korg