X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-store-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fstore%2Fimpl%2FMbeParser.java;fp=opendaylight%2Fconfig%2Fyang-store-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fstore%2Fimpl%2FMbeParser.java;h=fc895eb51dcb25ba4e381ef3cac7d7b7d49d0479;hb=9fb64948564e252018f9b1e13e7cea2c92f991aa;hp=0000000000000000000000000000000000000000;hpb=1742b3894614be478c333a1043ced8ef1bc5dc84;p=controller.git diff --git a/opendaylight/config/yang-store-impl/src/main/java/org/opendaylight/controller/config/yang/store/impl/MbeParser.java b/opendaylight/config/yang-store-impl/src/main/java/org/opendaylight/controller/config/yang/store/impl/MbeParser.java new file mode 100644 index 0000000000..fc895eb51d --- /dev/null +++ b/opendaylight/config/yang-store-impl/src/main/java/org/opendaylight/controller/config/yang/store/impl/MbeParser.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.config.yang.store.impl; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.io.IOUtils; +import org.opendaylight.controller.config.yang.store.api.YangStoreException; +import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot; +import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry; +import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator; +import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry; +import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper; +import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + +public class MbeParser { + + public YangStoreSnapshot parseYangFiles( + Collection allInput) + throws YangStoreException { + YangParserImpl parser = new YangParserImpl(); + + List bufferedInputStreams = new ArrayList<>(); + for (InputStream is : allInput) { + String content; + try { + content = IOUtils.toString(is); + } catch (IOException e) { + throw new YangStoreException("Can not get yang as String from " + + is, e); + } + InputStream buf = new ByteArrayInputStream(content.getBytes()); + bufferedInputStreams.add(buf); + } + + Map allYangModules = parser + .parseYangModelsFromStreamsMapped(bufferedInputStreams); + + SchemaContext resolveSchemaContext = parser.resolveSchemaContext(Sets + .newHashSet(allYangModules.values())); + + // JMX generator + + Map namespaceToPackageMapping = Maps.newHashMap(); + PackageTranslator packageTranslator = new PackageTranslator( + namespaceToPackageMapping); + + Map qNamesToSIEs = new HashMap<>(); + + // create SIE structure qNamesToSIEs + for (Module module : resolveSchemaContext.getModules()) { + String packageName = packageTranslator.getPackageName(module); + Map namesToSIEntries = ServiceInterfaceEntry + .create(module, packageName); + + for (Entry sieEntry : namesToSIEntries + .entrySet()) { + + // merge value into qNamesToSIEs + if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) { + qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue()); + } else { + throw new IllegalStateException( + "Cannot add two SIE with same qname " + + sieEntry.getValue()); + } + } + } + + Map> retVal = Maps.newHashMap(); + Map> modulesMap = new HashMap<>(); + + for (Entry moduleEntry : allYangModules.entrySet()) { + String packageName = packageTranslator.getPackageName(moduleEntry + .getValue()); + TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper( + new TypeProviderImpl(resolveSchemaContext)); + String yangAsString; + try { + moduleEntry.getKey().reset(); + yangAsString = IOUtils.toString(moduleEntry.getKey()); + } catch (IOException e) { + throw new IllegalStateException(e); + } + modulesMap.put(moduleEntry.getValue().getName(), + Maps.immutableEntry(moduleEntry.getValue(), yangAsString)); + Map namesToMBEs = ModuleMXBeanEntry + .create(moduleEntry.getValue(), qNamesToSIEs, resolveSchemaContext, typeProviderWrapper, + packageName); + retVal.put(moduleEntry.getValue().getNamespace().toString(), + namesToMBEs); + } + + return new YangStoreSnapshotImpl(retVal, modulesMap); + } + + public Map parseYangFilesToString( + Collection allYangs) { + YangParserImpl parser = new YangParserImpl(); + + Map allYangModules = parser + .parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs)); + Map retVal = new HashMap<>(); + + for (Entry entry : allYangModules.entrySet()) { + try { + retVal.put(entry.getValue(), IOUtils.toString(entry.getKey())); + } catch (IOException e) { + throw new IllegalStateException( + "Can not create string from yang file."); + } + } + return retVal; + } + +}