diff options
author | ramverma <ram.krishna.verma@ericsson.com> | 2018-07-19 18:45:16 +0100 |
---|---|---|
committer | ramverma <ram.krishna.verma@ericsson.com> | 2018-07-19 21:37:59 +0100 |
commit | 3d02543fc00a46da2da8f682f71b538ca2fd36e5 (patch) | |
tree | f31f40279c7d00505b10bf5f7000712ea2e0c879 /examples/myfirstpolicy/src/main/java | |
parent | ab64a570730f3645939a33e06487c2477f1314de (diff) |
Adding examples module to apex-pdp
Adding examples (MyFirstPolicy, AADM, Adaptive, PCVS) to apex-pdp
so that anyone can try out running few sample policies
and understand how to create and run policies in apex-pdp.
Change-Id: I0dff6d54ef94d8b5bdb63eabcb09e9f64d76fd0c
Issue-ID: POLICY-861
Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'examples/myfirstpolicy/src/main/java')
3 files changed, 175 insertions, 0 deletions
diff --git a/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/MFPDomainModelFactory.java b/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/MFPDomainModelFactory.java new file mode 100644 index 000000000..9b3f6cc8a --- /dev/null +++ b/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/MFPDomainModelFactory.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.examples.myfirstpolicy.model; + +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.utilities.ResourceUtils; + +/** + * A factory for creating MFPDomainModel objects. + * + * @author John Keeney (john.keeney@ericsson.com) + */ +public class MFPDomainModelFactory { + + private static final String MFP1PATH = "examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.json"; + private static final String MFP1_ALT_PATH = "examples/models/MyFirstPolicy/1/MyFirstPolicyModel_0.0.1.alt.json"; + private static final String MFP2PATH = "examples/models/MyFirstPolicy/2/MyFirstPolicyModel_0.0.1.json"; + + /** + * Gets the MyFirstPolicy#1 policy model. + * + * @return the MyFirstPolicy#1 policy model + */ + public AxPolicyModel getMFP1PolicyModel() { + java.util.TimeZone.getTimeZone("gmt"); + try { + final ApexModelReader<AxPolicyModel> reader = new ApexModelReader<>(AxPolicyModel.class); + return reader.read(ResourceUtils.getResourceAsString(MFPDomainModelFactory.MFP1PATH)); + } catch (final Exception e) { + throw new ApexRuntimeException("Failed to build MyFirstPolicy from path: " + MFPDomainModelFactory.MFP1PATH, + e); + } + } + + /** + * Gets the MyFirstPolicy#1 policy model, with alternate JavaScript task logic. + * + * @return the MyFirstPolicy#1 policy model + */ + public AxPolicyModel getMFP1AltPolicyModel() { + java.util.TimeZone.getTimeZone("gmt"); + try { + final ApexModelReader<AxPolicyModel> reader = new ApexModelReader<>(AxPolicyModel.class); + return reader.read(ResourceUtils.getResourceAsString(MFPDomainModelFactory.MFP1_ALT_PATH)); + } catch (final Exception e) { + throw new ApexRuntimeException( + "Failed to build MyFirstPolicy_ALT from path: " + MFPDomainModelFactory.MFP1_ALT_PATH, e); + } + } + + /** + * Gets the MyFirstPolicy#1 policy model. + * + * @return the MyFirstPolicy#1 policy model + */ + public AxPolicyModel getMFP2PolicyModel() { + try { + final ApexModelReader<AxPolicyModel> reader = new ApexModelReader<>(AxPolicyModel.class); + return reader.read(ResourceUtils.getResourceAsString(MFPDomainModelFactory.MFP2PATH)); + } catch (final Exception e) { + throw new ApexRuntimeException("Failed to build MyFirstPolicy from path: " + MFPDomainModelFactory.MFP2PATH, + e); + } + } + +} diff --git a/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/MFPDomainModelSaver.java b/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/MFPDomainModelSaver.java new file mode 100644 index 000000000..7343f747d --- /dev/null +++ b/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/MFPDomainModelSaver.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.examples.myfirstpolicy.model; + +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelSaver; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +/** + * The Class MFPDomainModelSaver. + * + * @author John Keeney (john.keeney@ericsson.com) + */ +public final class MFPDomainModelSaver { + + /** Private constructor to prevent instantiation. */ + private MFPDomainModelSaver() {} + + /** + * Write the MyFirstPolicy model to args[0]. + * + * @param args uses <code>arg[0]</code> for directory information + * @throws ApexException the apex exception + */ + public static void main(final String[] args) throws ApexException { + if (args.length != 1) { + System.err.println("usage: " + MFPDomainModelSaver.class.getCanonicalName() + " modelDirectory"); + return; + } + + // Save Java model + AxPolicyModel mfpPolicyModel = new MFPDomainModelFactory().getMFP1PolicyModel(); + ApexModelSaver<AxPolicyModel> mfpModelSaver = + new ApexModelSaver<>(AxPolicyModel.class, mfpPolicyModel, args[0] + "/1/"); + mfpModelSaver.apexModelWriteJSON(); + mfpModelSaver.apexModelWriteXML(); + + mfpPolicyModel = new MFPDomainModelFactory().getMFP2PolicyModel(); + mfpModelSaver = new ApexModelSaver<>(AxPolicyModel.class, mfpPolicyModel, args[0] + "/2/"); + mfpModelSaver.apexModelWriteJSON(); + mfpModelSaver.apexModelWriteXML(); + + } +} diff --git a/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/package-info.java b/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/package-info.java new file mode 100644 index 000000000..ed03da4ca --- /dev/null +++ b/examples/myfirstpolicy/src/main/java/org/onap/policy/apex/examples/myfirstpolicy/model/package-info.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Contains the model for My-First-Policy. + * + * @author John Keeney (john.keeney@ericsson.com) + */ + +package org.onap.policy.apex.examples.myfirstpolicy.model; |